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