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.preference.Preference;
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import com.android.settings.R;
32 import com.android.settings.testutils.shadow.SettingsShadowResources;
33 import com.android.settingslib.emergencynumber.EmergencyNumberUtils;
34 
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RobolectricTestRunner;
42 import org.robolectric.annotation.Config;
43 
44 
45 @RunWith(RobolectricTestRunner.class)
46 @Config(shadows = SettingsShadowResources.class)
47 public class EmergencyGestureNumberOverridePreferenceControllerTest {
48     private static final String PREF_KEY = "test";
49 
50     @Mock
51     private EmergencyNumberUtils mEmergencyNumberUtils;
52     private Context mContext;
53     private EmergencyGestureNumberOverridePreferenceController mController;
54 
55     @Before
setUp()56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58         mContext = ApplicationProvider.getApplicationContext();
59         mController = new EmergencyGestureNumberOverridePreferenceController(mContext, PREF_KEY);
60     }
61 
62     @After
tearDown()63     public void tearDown() {
64         SettingsShadowResources.reset();
65     }
66 
67     @Test
getAvailabilityStatus_configIsTrue_shouldReturnAvailable()68     public void getAvailabilityStatus_configIsTrue_shouldReturnAvailable() {
69         SettingsShadowResources.overrideResource(
70                 R.bool.config_show_emergency_gesture_settings,
71                 Boolean.TRUE);
72 
73         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
74     }
75 
76     @Test
getAvailabilityStatus_configIsFalse_shouldReturnUnsupported()77     public void getAvailabilityStatus_configIsFalse_shouldReturnUnsupported() {
78         SettingsShadowResources.overrideResource(
79                 R.bool.config_show_emergency_gesture_settings,
80                 Boolean.FALSE);
81 
82         assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
83     }
84 
85     @Test
updateState_shouldLoadNumberFromSettings()86     public void updateState_shouldLoadNumberFromSettings() {
87         final Preference preference = new Preference(mContext);
88         mController.mEmergencyNumberUtils = mEmergencyNumberUtils;
89         when(mEmergencyNumberUtils.getPoliceNumber()).thenReturn("123");
90 
91         mController.updateState(preference);
92 
93         assertThat(preference.getSummary().toString()).isEqualTo(
94                 mContext.getString(R.string.emergency_gesture_call_for_help_summary, "123"));
95     }
96 
97 }
98