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 import static com.android.settings.emergency.EmergencyGestureEntrypointPreferenceController.ACTION_EMERGENCY_GESTURE_SETTINGS;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.pm.ActivityInfo;
28 import android.content.pm.ResolveInfo;
29 
30 import androidx.test.core.app.ApplicationProvider;
31 
32 import com.android.settings.R;
33 import com.android.settings.testutils.shadow.SettingsShadowResources;
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.robolectric.RobolectricTestRunner;
40 import org.robolectric.Shadows;
41 import org.robolectric.annotation.Config;
42 import org.robolectric.shadows.ShadowPackageManager;
43 
44 @RunWith(RobolectricTestRunner.class)
45 @Config(shadows = SettingsShadowResources.class)
46 public class EmergencyGestureEntrypointPreferenceControllerTest {
47 
48     private static final String TEST_PKG_NAME = "test_pkg";
49     private static final String TEST_CLASS_NAME = "name";
50     private static final Intent SETTING_INTENT = new Intent(ACTION_EMERGENCY_GESTURE_SETTINGS)
51             .setPackage(TEST_PKG_NAME);
52 
53     private Context mContext;
54     private ShadowPackageManager mPackageManager;
55     private static final String PREF_KEY = "gesture_emergency_button";
56 
57     @Before
setUp()58     public void setUp() {
59         mContext = ApplicationProvider.getApplicationContext();
60         mPackageManager = Shadows.shadowOf(mContext.getPackageManager());
61 
62     }
63 
64     @After
tearDown()65     public void tearDown() {
66         SettingsShadowResources.reset();
67     }
68 
69     @Test
constructor_hasCustomPackageConfig_shouldSetIntent()70     public void constructor_hasCustomPackageConfig_shouldSetIntent() {
71         SettingsShadowResources.overrideResource(
72                 R.bool.config_show_emergency_gesture_settings,
73                 Boolean.TRUE);
74         SettingsShadowResources.overrideResource(
75                 R.string.emergency_gesture_settings_package,
76                 TEST_PKG_NAME);
77         prepareCustomIntent();
78 
79         EmergencyGestureEntrypointPreferenceController controller =
80                 new EmergencyGestureEntrypointPreferenceController(mContext, PREF_KEY);
81 
82         assertThat(controller.mIntent).isNotNull();
83     }
84 
85     @Test
getAvailabilityStatus_configIsTrue_shouldReturnAvailable()86     public void getAvailabilityStatus_configIsTrue_shouldReturnAvailable() {
87         SettingsShadowResources.overrideResource(
88                 R.bool.config_show_emergency_gesture_settings,
89                 Boolean.TRUE);
90         EmergencyGestureEntrypointPreferenceController controller =
91                 new EmergencyGestureEntrypointPreferenceController(mContext, PREF_KEY);
92 
93         assertThat(controller.getAvailabilityStatus()).isEqualTo(AVAILABLE);
94     }
95 
96     @Test
getAvailabilityStatus_configIsFalse_shouldReturnUnsupported()97     public void getAvailabilityStatus_configIsFalse_shouldReturnUnsupported() {
98         SettingsShadowResources.overrideResource(
99                 R.bool.config_show_emergency_gesture_settings,
100                 Boolean.FALSE);
101         EmergencyGestureEntrypointPreferenceController controller =
102                 new EmergencyGestureEntrypointPreferenceController(mContext, PREF_KEY);
103 
104         assertThat(controller.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
105     }
106 
107     @Test
getAvailabilityStatus_noSuitableIntent_shouldReturnAvailable()108     public void getAvailabilityStatus_noSuitableIntent_shouldReturnAvailable() {
109         SettingsShadowResources.overrideResource(
110                 R.bool.config_show_emergency_gesture_settings,
111                 Boolean.TRUE);
112         // Provide override package name but don't provide resolvable intent
113         SettingsShadowResources.overrideResource(
114                 R.string.emergency_gesture_settings_package,
115                 TEST_PKG_NAME);
116 
117         EmergencyGestureEntrypointPreferenceController controller =
118                 new EmergencyGestureEntrypointPreferenceController(mContext, PREF_KEY);
119 
120         assertThat(controller.getAvailabilityStatus()).isEqualTo(AVAILABLE);
121         assertThat(controller.mIntent).isNull();
122     }
123 
prepareCustomIntent()124     private void prepareCustomIntent() {
125         final ResolveInfo info = new ResolveInfo();
126         info.activityInfo = new ActivityInfo();
127         info.activityInfo.packageName = TEST_PKG_NAME;
128         info.activityInfo.name = TEST_CLASS_NAME;
129 
130         mPackageManager.addResolveInfoForIntent(SETTING_INTENT, info);
131     }
132 }
133