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 com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.content.Context; 24 import android.os.SystemProperties; 25 import android.support.v14.preference.SwitchPreference; 26 import android.support.v7.preference.PreferenceScreen; 27 28 import com.android.settings.testutils.SettingsRobolectricTestRunner; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Answers; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.shadows.ShadowSystemProperties; 37 38 @RunWith(SettingsRobolectricTestRunner.class) 39 public class BootSoundPreferenceControllerTest { 40 41 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 42 private Context mContext; 43 @Mock 44 private PreferenceScreen mScreen; 45 @Mock 46 private SwitchPreference mPreference; 47 48 private BootSoundPreferenceController mController; 49 50 @Before setUp()51 public void setUp() { 52 MockitoAnnotations.initMocks(this); 53 when(mContext.getResources().getBoolean(com.android.settings.R.bool.has_boot_sounds)) 54 .thenReturn(true); 55 mController = new BootSoundPreferenceController(mContext); 56 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 57 when(mPreference.getKey()).thenReturn(mController.getPreferenceKey()); 58 } 59 60 @Test isAvailable_hasBootSounds_shouldReturnTrue()61 public void isAvailable_hasBootSounds_shouldReturnTrue() { 62 when(mContext.getResources().getBoolean( 63 com.android.settings.R.bool.has_boot_sounds)).thenReturn(true); 64 65 assertThat(mController.isAvailable()).isTrue(); 66 } 67 68 @Test isAvailable_noBootSounds_shouldReturnFale()69 public void isAvailable_noBootSounds_shouldReturnFale() { 70 when(mContext.getResources().getBoolean( 71 com.android.settings.R.bool.has_boot_sounds)).thenReturn(false); 72 73 assertThat(mController.isAvailable()).isFalse(); 74 } 75 76 @Test displayPreference_bootSoundEnabled_shouldCheckedPreference()77 public void displayPreference_bootSoundEnabled_shouldCheckedPreference() { 78 ShadowSystemProperties.native_set(BootSoundPreferenceController.PROPERTY_BOOT_SOUNDS, "1"); 79 80 mController.displayPreference(mScreen); 81 82 verify(mPreference).setChecked(true); 83 } 84 85 @Test displayPreference_bootSoundDisabled_shouldUncheckedPreference()86 public void displayPreference_bootSoundDisabled_shouldUncheckedPreference() { 87 ShadowSystemProperties.native_set(BootSoundPreferenceController.PROPERTY_BOOT_SOUNDS, "0"); 88 89 mController.displayPreference(mScreen); 90 91 verify(mPreference).setChecked(false); 92 } 93 94 @Test handlePreferenceTreeClick_preferenceChecked_shouldEnableBootSound()95 public void handlePreferenceTreeClick_preferenceChecked_shouldEnableBootSound() { 96 when(mPreference.isChecked()).thenReturn(true); 97 98 mController.handlePreferenceTreeClick(mPreference); 99 100 assertThat(SystemProperties.get( 101 BootSoundPreferenceController.PROPERTY_BOOT_SOUNDS, null)).isEqualTo("1"); 102 } 103 104 @Test handlePreferenceTreeClick_preferenceUnchecked_shouldDisableBootSound()105 public void handlePreferenceTreeClick_preferenceUnchecked_shouldDisableBootSound() { 106 when(mPreference.isChecked()).thenReturn(false); 107 108 mController.handlePreferenceTreeClick(mPreference); 109 110 assertThat(SystemProperties.get( 111 BootSoundPreferenceController.PROPERTY_BOOT_SOUNDS, null)).isEqualTo("0"); 112 } 113 } 114