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.notification; 18 19 import static android.provider.Settings.Secure.NOTIFICATION_BADGING; 20 21 import static com.android.settings.notification.BadgingNotificationPreferenceController.OFF; 22 import static com.android.settings.notification.BadgingNotificationPreferenceController.ON; 23 24 import static com.google.common.truth.Truth.assertThat; 25 import static org.mockito.Mockito.mock; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.content.ContentResolver; 30 import android.content.Context; 31 import android.provider.Settings; 32 import android.support.v7.preference.Preference; 33 import android.support.v7.preference.PreferenceScreen; 34 import android.support.v7.preference.TwoStatePreference; 35 36 import com.android.settings.search.InlinePayload; 37 import com.android.settings.search.InlineSwitchPayload; 38 import com.android.settings.testutils.shadow.ShadowSecureSettings; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.Answers; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.RuntimeEnvironment; 48 import org.robolectric.annotation.Config; 49 50 @RunWith(RobolectricTestRunner.class) 51 public class BadgingNotificationPreferenceControllerTest { 52 53 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 54 private Context mContext; 55 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 56 private PreferenceScreen mScreen; 57 58 private BadgingNotificationPreferenceController mController; 59 private Preference mPreference; 60 61 private static final String KEY_NOTIFICATION_BADGING = "notification_badging"; 62 63 @Before setUp()64 public void setUp() { 65 MockitoAnnotations.initMocks(this); 66 mController = new BadgingNotificationPreferenceController(mContext, 67 KEY_NOTIFICATION_BADGING); 68 mPreference = new Preference(RuntimeEnvironment.application); 69 mPreference.setKey(mController.getPreferenceKey()); 70 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 71 } 72 73 @Test display_configIsTrue_shouldDisplay()74 public void display_configIsTrue_shouldDisplay() { 75 when(mContext.getResources(). 76 getBoolean(com.android.internal.R.bool.config_notificationBadging)) 77 .thenReturn(true); 78 mController.displayPreference(mScreen); 79 80 assertThat(mPreference.isVisible()).isTrue(); 81 } 82 83 @Test display_configIsFalse_shouldNotDisplay()84 public void display_configIsFalse_shouldNotDisplay() { 85 when(mContext.getResources(). 86 getBoolean(com.android.internal.R.bool.config_notificationBadging)) 87 .thenReturn(false); 88 89 mController.displayPreference(mScreen); 90 91 assertThat(mPreference.isVisible()).isFalse(); 92 } 93 94 @Test updateState_preferenceSetCheckedWhenSettingIsOn()95 public void updateState_preferenceSetCheckedWhenSettingIsOn() { 96 final TwoStatePreference preference = mock(TwoStatePreference.class); 97 final Context context = RuntimeEnvironment.application; 98 Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, ON); 99 100 mController = new BadgingNotificationPreferenceController(context, 101 KEY_NOTIFICATION_BADGING); 102 mController.updateState(preference); 103 104 verify(preference).setChecked(true); 105 } 106 107 @Test updateState_preferenceSetUncheckedWhenSettingIsOff()108 public void updateState_preferenceSetUncheckedWhenSettingIsOff() { 109 final TwoStatePreference preference = mock(TwoStatePreference.class); 110 final Context context = RuntimeEnvironment.application; 111 Settings.Secure.putInt(context.getContentResolver(), NOTIFICATION_BADGING, OFF); 112 113 mController = new BadgingNotificationPreferenceController(context, 114 KEY_NOTIFICATION_BADGING); 115 mController.updateState(preference); 116 117 verify(preference).setChecked(false); 118 } 119 120 @Test testPreferenceController_ProperResultPayloadType()121 public void testPreferenceController_ProperResultPayloadType() { 122 assertThat(mController.getResultPayload()).isInstanceOf(InlineSwitchPayload.class); 123 } 124 125 @Test 126 @Config(shadows = ShadowSecureSettings.class) testSetValue_updatesCorrectly()127 public void testSetValue_updatesCorrectly() { 128 final int newValue = 0; 129 ContentResolver resolver = mContext.getContentResolver(); 130 Settings.Secure.putInt(resolver, Settings.Secure.NOTIFICATION_BADGING, ON); 131 132 ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue); 133 final int updatedValue = 134 Settings.Secure.getInt(resolver, Settings.Secure.NOTIFICATION_BADGING, ON); 135 136 assertThat(updatedValue).isEqualTo(newValue); 137 } 138 139 @Test 140 @Config(shadows = ShadowSecureSettings.class) testGetValue_correctValueReturned()141 public void testGetValue_correctValueReturned() { 142 final int currentValue = 1; 143 final ContentResolver resolver = mContext.getContentResolver(); 144 Settings.Secure.putInt(resolver, Settings.Secure.NOTIFICATION_BADGING, currentValue); 145 146 final int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext); 147 148 assertThat(newValue).isEqualTo(currentValue); 149 } 150 151 @Test isChecked_settingIsOff_shouldReturnFalse()152 public void isChecked_settingIsOff_shouldReturnFalse() { 153 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, OFF); 154 155 assertThat(mController.isChecked()).isFalse(); 156 } 157 158 @Test isChecked_settingIsOn_shouldReturnTrue()159 public void isChecked_settingIsOn_shouldReturnTrue() { 160 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, ON); 161 162 assertThat(mController.isChecked()).isTrue(); 163 } 164 165 @Test setChecked_setFalse_disablesSetting()166 public void setChecked_setFalse_disablesSetting() { 167 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, ON); 168 169 mController.setChecked(false); 170 int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(), 171 NOTIFICATION_BADGING, -1); 172 173 assertThat(updatedValue).isEqualTo(OFF); 174 } 175 176 @Test setChecked_setTrue_enablesSetting()177 public void setChecked_setTrue_enablesSetting() { 178 Settings.Secure.putInt(mContext.getContentResolver(), NOTIFICATION_BADGING, OFF); 179 180 mController.setChecked(true); 181 int updatedValue = Settings.Secure.getInt(mContext.getContentResolver(), 182 NOTIFICATION_BADGING, -1); 183 184 assertThat(updatedValue).isEqualTo(ON); 185 } 186 187 @Test isSliceableCorrectKey_returnsTrue()188 public void isSliceableCorrectKey_returnsTrue() { 189 final BadgingNotificationPreferenceController controller = 190 new BadgingNotificationPreferenceController(mContext, 191 "notification_badging"); 192 assertThat(controller.isSliceable()).isTrue(); 193 } 194 195 @Test isSliceableIncorrectKey_returnsFalse()196 public void isSliceableIncorrectKey_returnsFalse() { 197 final BadgingNotificationPreferenceController controller = 198 new BadgingNotificationPreferenceController(mContext, "bad_key"); 199 assertThat(controller.isSliceable()).isFalse(); 200 } 201 } 202