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