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