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