1 /* 2 * Copyright (C) 2021 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.emergency; 18 19 20 import static com.google.common.truth.Truth.assertThat; 21 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 26 import androidx.test.core.app.ApplicationProvider; 27 28 import com.android.settings.R; 29 import com.android.settings.testutils.shadow.SettingsShadowResources; 30 import com.android.settingslib.emergencynumber.EmergencyNumberUtils; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RobolectricTestRunner; 39 import org.robolectric.annotation.Config; 40 41 @RunWith(RobolectricTestRunner.class) 42 @Config(shadows = SettingsShadowResources.class) 43 public class EmergencyGestureSoundPreferenceControllerTest { 44 @Mock 45 private EmergencyNumberUtils mEmergencyNumberUtils; 46 private Context mContext; 47 private EmergencyGestureSoundPreferenceController mController; 48 49 @Before setUp()50 public void setUp() { 51 MockitoAnnotations.initMocks(this); 52 mContext = ApplicationProvider.getApplicationContext(); 53 mController = new EmergencyGestureSoundPreferenceController(mContext, "test_key"); 54 mController.mEmergencyNumberUtils = mEmergencyNumberUtils; 55 } 56 57 @After tearDown()58 public void tearDown() { 59 SettingsShadowResources.reset(); 60 } 61 62 @Test isAvailable_configIsTrue_shouldReturnTrue()63 public void isAvailable_configIsTrue_shouldReturnTrue() { 64 SettingsShadowResources.overrideResource( 65 R.bool.config_show_emergency_gesture_settings, 66 Boolean.TRUE); 67 68 assertThat(mController.isAvailable()).isTrue(); 69 } 70 71 @Test isAvailable_configIsTrue_shouldReturnFalse()72 public void isAvailable_configIsTrue_shouldReturnFalse() { 73 SettingsShadowResources.overrideResource( 74 R.bool.config_show_emergency_gesture_settings, 75 Boolean.FALSE); 76 77 assertThat(mController.isAvailable()).isFalse(); 78 } 79 80 @Test isChecked_configIsSet_shouldReturnTrue()81 public void isChecked_configIsSet_shouldReturnTrue() { 82 // Set the setting to be enabled. 83 when(mEmergencyNumberUtils.getEmergencyGestureSoundEnabled()).thenReturn(true); 84 85 assertThat(mController.isChecked()).isTrue(); 86 } 87 88 @Test isChecked_configIsSetToFalse_shouldReturnFalse()89 public void isChecked_configIsSetToFalse_shouldReturnFalse() { 90 // Set the setting to be disabled. 91 when(mEmergencyNumberUtils.getEmergencyGestureSoundEnabled()).thenReturn(false); 92 93 assertThat(mController.isChecked()).isFalse(); 94 } 95 96 @Test isSliceable_returnsFalse()97 public void isSliceable_returnsFalse() { 98 assertThat(mController.isSliceable()).isFalse(); 99 } 100 101 } 102