1 /* 2 * Copyright (C) 2016 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.System.NOTIFICATION_LIGHT_PULSE; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.verify; 26 import static org.mockito.Mockito.when; 27 28 import android.content.Context; 29 import android.content.res.Resources; 30 import android.provider.Settings; 31 32 import androidx.preference.Preference; 33 import androidx.preference.PreferenceScreen; 34 import androidx.preference.TwoStatePreference; 35 36 import com.android.settings.core.BasePreferenceController; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 import org.junit.runner.RunWith; 41 import org.mockito.Answers; 42 import org.mockito.Mock; 43 import org.mockito.MockitoAnnotations; 44 import org.robolectric.RobolectricTestRunner; 45 import org.robolectric.RuntimeEnvironment; 46 47 @RunWith(RobolectricTestRunner.class) 48 public class PulseNotificationPreferenceControllerTest { 49 50 private Context mContext; 51 @Mock 52 private Resources mResources; 53 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 54 private PreferenceScreen mScreen; 55 56 private PulseNotificationPreferenceController mController; 57 private Preference mPreference; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 mContext = spy(RuntimeEnvironment.application); 63 when(mContext.getResources()).thenReturn(mResources); 64 65 mController = new PulseNotificationPreferenceController(mContext, "testkey"); 66 mPreference = new Preference(mContext); 67 mPreference.setKey(mController.getPreferenceKey()); 68 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 69 } 70 71 @Test display_configIsTrue_shouldDisplay()72 public void display_configIsTrue_shouldDisplay() { 73 when(mContext.getResources(). 74 getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) 75 .thenReturn(true); 76 77 mController.displayPreference(mScreen); 78 79 assertThat(mPreference.isVisible()).isTrue(); 80 } 81 82 @Test display_configIsFalse_shouldNotDisplay()83 public void display_configIsFalse_shouldNotDisplay() { 84 when(mContext.getResources(). 85 getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) 86 .thenReturn(false); 87 88 mController.displayPreference(mScreen); 89 90 assertThat(mPreference.isVisible()).isFalse(); 91 } 92 93 @Test updateState_preferenceSetCheckedWhenSettingIsOn()94 public void updateState_preferenceSetCheckedWhenSettingIsOn() { 95 final TwoStatePreference preference = mock(TwoStatePreference.class); 96 final Context context = RuntimeEnvironment.application; 97 Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 1); 98 99 mController = new PulseNotificationPreferenceController(context, "testkey"); 100 mController.updateState(preference); 101 102 verify(preference).setChecked(true); 103 } 104 105 @Test updateState_preferenceSetUncheckedWhenSettingIsOff()106 public void updateState_preferenceSetUncheckedWhenSettingIsOff() { 107 final TwoStatePreference preference = mock(TwoStatePreference.class); 108 final Context context = RuntimeEnvironment.application; 109 Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 0); 110 111 mController = new PulseNotificationPreferenceController(context, "testkey"); 112 mController.updateState(preference); 113 114 verify(preference).setChecked(false); 115 } 116 117 @Test isAvailable_configTrue_shouldReturnTrue()118 public void isAvailable_configTrue_shouldReturnTrue() { 119 when(mContext.getResources(). 120 getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)).thenReturn( 121 true); 122 123 assertThat(mController.isAvailable()).isTrue(); 124 assertThat(mController.getAvailabilityStatus()).isEqualTo( 125 BasePreferenceController.AVAILABLE); 126 } 127 128 @Test isAvailable_configFalse_shouldReturnFalse()129 public void isAvailable_configFalse_shouldReturnFalse() { 130 when(mContext.getResources(). 131 getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)).thenReturn( 132 false); 133 134 assertThat(mController.isAvailable()).isFalse(); 135 assertThat(mController.getAvailabilityStatus()).isEqualTo( 136 BasePreferenceController.UNSUPPORTED_ON_DEVICE); 137 } 138 139 @Test isChecked_configOn_shouldReturnTrue()140 public void isChecked_configOn_shouldReturnTrue() { 141 Settings.System.putInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 1); 142 143 assertThat(mController.isChecked()).isTrue(); 144 } 145 146 @Test isChecked_configOff_shouldReturnFalse()147 public void isChecked_configOff_shouldReturnFalse() { 148 Settings.System.putInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 0); 149 150 assertThat(mController.isChecked()).isFalse(); 151 } 152 153 @Test testSetChecked_configIsSet_shouldReturnTrue()154 public void testSetChecked_configIsSet_shouldReturnTrue() { 155 mController.setChecked(true); 156 157 assertThat(mController.isChecked()).isTrue(); 158 assertThat( 159 Settings.System.getInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 0)) 160 .isEqualTo(1); 161 } 162 163 @Test testSetChecked_configIsNotSet_shouldReturnFalse()164 public void testSetChecked_configIsNotSet_shouldReturnFalse() { 165 mController.setChecked(false); 166 167 assertThat(mController.isChecked()).isFalse(); 168 assertThat( 169 Settings.System.getInt(mContext.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 1)) 170 .isEqualTo(0); 171 } 172 } 173