1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.applications.appinfo; 18 19 import static com.android.settings.SettingsActivity.EXTRA_FRAGMENT_ARG_KEY; 20 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.spy; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.app.Activity; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.content.pm.ApplicationInfo; 31 import android.support.v7.preference.Preference; 32 import android.support.v7.preference.PreferenceScreen; 33 34 import com.android.settings.notification.AppNotificationSettings; 35 import com.android.settings.notification.NotificationBackend; 36 import com.android.settings.testutils.SettingsRobolectricTestRunner; 37 import com.android.settingslib.applications.ApplicationsState; 38 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 import org.robolectric.RuntimeEnvironment; 45 import org.robolectric.util.ReflectionHelpers; 46 47 @RunWith(SettingsRobolectricTestRunner.class) 48 public class AppNotificationPreferenceControllerTest { 49 50 @Mock 51 private AppInfoDashboardFragment mFragment; 52 @Mock 53 private PreferenceScreen mScreen; 54 @Mock 55 private Preference mPreference; 56 57 private Context mContext; 58 private AppNotificationPreferenceController mController; 59 60 @Before 61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 mContext = RuntimeEnvironment.application; 64 mController = spy(new AppNotificationPreferenceController(mContext, "test_key")); 65 mController.setParentFragment(mFragment); 66 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 67 final String key = mController.getPreferenceKey(); 68 when(mPreference.getKey()).thenReturn(key); 69 } 70 71 @Test 72 public void getDetailFragmentClass_shouldReturnAppNotificationSettings() { 73 assertThat(mController.getDetailFragmentClass()).isEqualTo(AppNotificationSettings.class); 74 } 75 76 @Test 77 public void updateState_shouldSetSummary() { 78 final ApplicationsState.AppEntry appEntry = mock(ApplicationsState.AppEntry.class); 79 appEntry.info = new ApplicationInfo(); 80 when(mFragment.getAppEntry()).thenReturn(appEntry); 81 ReflectionHelpers.setField(mController, "mBackend", new NotificationBackend()); 82 mController.displayPreference(mScreen); 83 84 mController.updateState(mPreference); 85 86 verify(mPreference).setSummary(any()); 87 } 88 89 @Test 90 public void getArguments_nullIfChannelIsNull() { 91 assertThat(mController.getArguments()).isNull(); 92 } 93 94 @Test 95 public void getArguments_containsChannelId() { 96 Activity activity = mock(Activity.class); 97 Intent intent = new Intent(); 98 intent.putExtra(EXTRA_FRAGMENT_ARG_KEY, "test"); 99 when(mFragment.getActivity()).thenReturn(activity); 100 when(activity.getIntent()).thenReturn(intent); 101 AppNotificationPreferenceController controller = 102 new AppNotificationPreferenceController(mContext, "test"); 103 controller.setParentFragment(mFragment); 104 105 assertThat(controller.getArguments().containsKey(EXTRA_FRAGMENT_ARG_KEY)).isTrue(); 106 assertThat(controller.getArguments().getString(EXTRA_FRAGMENT_ARG_KEY)).isEqualTo("test"); 107 } 108 109 @Test 110 public void getNotificationSummary_noCrashOnNull() { 111 mController.getNotificationSummary(null, mContext); 112 } 113 114 @Test 115 public void getNotificationSummary_appBlocked() { 116 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 117 appRow.banned = true; 118 appRow.blockedChannelCount = 30; 119 assertThat(mController.getNotificationSummary(appRow, mContext).toString()) 120 .isEqualTo("Off"); 121 } 122 123 @Test 124 public void getNotificationSummary_appNotBlockedAllChannelsBlocked() { 125 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 126 appRow.banned = false; 127 appRow.blockedChannelCount = 30; 128 appRow.channelCount = 30; 129 assertThat(mController.getNotificationSummary(appRow, mContext).toString()) 130 .isEqualTo("Off"); 131 } 132 133 @Test 134 public void getNotificationSummary_appNotBlocked() { 135 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 136 appRow.banned = false; 137 appRow.blockedChannelCount = 30; 138 appRow.channelCount = 60; 139 assertThat(mController.getNotificationSummary( 140 appRow, mContext).toString().contains("30")).isTrue(); 141 assertThat(mController.getNotificationSummary( 142 appRow, mContext).toString().contains("On")).isTrue(); 143 } 144 145 @Test 146 public void getNotificationSummary_channelsNotBlocked() { 147 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 148 appRow.banned = false; 149 appRow.blockedChannelCount = 0; 150 appRow.channelCount = 10; 151 assertThat(mController.getNotificationSummary(appRow, mContext).toString()).isEqualTo("On"); 152 } 153 154 @Test 155 public void getNotificationSummary_noChannels() { 156 NotificationBackend.AppRow appRow = new NotificationBackend.AppRow(); 157 appRow.banned = false; 158 appRow.blockedChannelCount = 0; 159 appRow.channelCount = 0; 160 assertThat(mController.getNotificationSummary(appRow, mContext).toString()).isEqualTo("On"); 161 } 162 } 163