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 import static com.google.common.truth.Truth.assertThat; 21 import static org.mockito.Mockito.mock; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.provider.Settings; 27 import android.support.v7.preference.Preference; 28 import android.support.v7.preference.PreferenceScreen; 29 import android.support.v7.preference.TwoStatePreference; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Answers; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.RobolectricTestRunner; 38 import org.robolectric.RuntimeEnvironment; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class PulseNotificationPreferenceControllerTest { 42 43 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 44 private Context mContext; 45 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 46 private PreferenceScreen mScreen; 47 48 private PulseNotificationPreferenceController mController; 49 private Preference mPreference; 50 51 @Before setUp()52 public void setUp() { 53 MockitoAnnotations.initMocks(this); 54 mController = new PulseNotificationPreferenceController(mContext); 55 mPreference = new Preference(RuntimeEnvironment.application); 56 mPreference.setKey(mController.getPreferenceKey()); 57 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 58 } 59 60 @Test display_configIsTrue_shouldDisplay()61 public void display_configIsTrue_shouldDisplay() { 62 when(mContext.getResources(). 63 getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) 64 .thenReturn(true); 65 mController.displayPreference(mScreen); 66 67 assertThat(mPreference.isVisible()).isTrue(); 68 } 69 70 @Test display_configIsFalse_shouldNotDisplay()71 public void display_configIsFalse_shouldNotDisplay() { 72 when(mContext.getResources(). 73 getBoolean(com.android.internal.R.bool.config_intrusiveNotificationLed)) 74 .thenReturn(false); 75 76 mController.displayPreference(mScreen); 77 78 assertThat(mPreference.isVisible()).isFalse(); 79 } 80 81 @Test updateState_preferenceSetCheckedWhenSettingIsOn()82 public void updateState_preferenceSetCheckedWhenSettingIsOn() { 83 final TwoStatePreference preference = mock(TwoStatePreference.class); 84 final Context context = RuntimeEnvironment.application; 85 Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 1); 86 87 mController = new PulseNotificationPreferenceController(context); 88 mController.updateState(preference); 89 90 verify(preference).setChecked(true); 91 } 92 93 @Test updateState_preferenceSetUncheckedWhenSettingIsOff()94 public void updateState_preferenceSetUncheckedWhenSettingIsOff() { 95 final TwoStatePreference preference = mock(TwoStatePreference.class); 96 final Context context = RuntimeEnvironment.application; 97 Settings.System.putInt(context.getContentResolver(), NOTIFICATION_LIGHT_PULSE, 0); 98 99 mController = new PulseNotificationPreferenceController(context); 100 mController.updateState(preference); 101 102 verify(preference).setChecked(false); 103 } 104 } 105