1 /*
2  * Copyright (C) 2019 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.gestures;
18 
19 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON;
20 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON;
21 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
22 
23 import static com.android.settings.gestures.SystemNavigationPreferenceController.PREF_KEY_SYSTEM_NAVIGATION;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.Mockito.when;
28 
29 import android.content.ComponentName;
30 import android.content.Context;
31 import android.content.Intent;
32 import android.content.pm.ApplicationInfo;
33 import android.content.pm.PackageManager;
34 import android.content.pm.ResolveInfo;
35 import android.content.pm.ServiceInfo;
36 import android.text.TextUtils;
37 
38 import com.android.internal.R;
39 import com.android.settings.testutils.shadow.SettingsShadowResources;
40 
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mock;
46 import org.mockito.MockitoAnnotations;
47 import org.robolectric.RobolectricTestRunner;
48 import org.robolectric.RuntimeEnvironment;
49 import org.robolectric.Shadows;
50 import org.robolectric.annotation.Config;
51 import org.robolectric.shadows.ShadowPackageManager;
52 
53 @RunWith(RobolectricTestRunner.class)
54 @Config(shadows = SettingsShadowResources.class)
55 public class SystemNavigationPreferenceControllerTest {
56 
57     private Context mContext;
58     private ShadowPackageManager mPackageManager;
59 
60     @Mock
61     private Context mMockContext;
62     @Mock
63     private PackageManager mMockPackageManager;
64 
65     private SystemNavigationPreferenceController mController;
66 
67     private static final String ACTION_QUICKSTEP = "android.intent.action.QUICKSTEP_SERVICE";
68     private static final String TEST_RECENTS_COMPONENT_NAME = "test.component.name/.testActivity";
69 
70     @Before
setUp()71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73 
74         SettingsShadowResources.overrideResource(R.bool.config_swipe_up_gesture_setting_available,
75                 true);
76 
77         mContext = RuntimeEnvironment.application;
78         mPackageManager = Shadows.shadowOf(mContext.getPackageManager());
79 
80         mController = new SystemNavigationPreferenceController(mContext,
81                 PREF_KEY_SYSTEM_NAVIGATION);
82 
83         when(mMockContext.getPackageManager()).thenReturn(mMockPackageManager);
84         when(mMockContext.getString(com.android.internal.R.string.config_recentsComponentName))
85                 .thenReturn(TEST_RECENTS_COMPONENT_NAME);
86     }
87 
88     @After
tearDown()89     public void tearDown() {
90         SettingsShadowResources.reset();
91     }
92 
93     @Test
testIsGestureAvailable_matchingServiceExists_shouldReturnTrue()94     public void testIsGestureAvailable_matchingServiceExists_shouldReturnTrue() {
95         final ComponentName recentsComponentName = ComponentName.unflattenFromString(
96                 mContext.getString(com.android.internal.R.string.config_recentsComponentName));
97         final Intent quickStepIntent = new Intent(ACTION_QUICKSTEP)
98                 .setPackage(recentsComponentName.getPackageName());
99         final ResolveInfo info = new ResolveInfo();
100         info.serviceInfo = new ServiceInfo();
101         info.resolvePackageName = recentsComponentName.getPackageName();
102         info.serviceInfo.packageName = info.resolvePackageName;
103         info.serviceInfo.name = recentsComponentName.getClassName();
104         info.serviceInfo.applicationInfo = new ApplicationInfo();
105         info.serviceInfo.applicationInfo.flags = ApplicationInfo.FLAG_SYSTEM;
106         mPackageManager.addResolveInfoForIntent(quickStepIntent, info);
107 
108         assertThat(SystemNavigationPreferenceController.isGestureAvailable(mContext)).isTrue();
109     }
110 
111     @Test
testIsGestureAvailable_overlayDisabled_matchingServiceExists_shouldReturnFalse()112     public void testIsGestureAvailable_overlayDisabled_matchingServiceExists_shouldReturnFalse() {
113         SettingsShadowResources.overrideResource(R.bool.config_swipe_up_gesture_setting_available,
114                 false);
115 
116         final ComponentName recentsComponentName = ComponentName.unflattenFromString(
117                 mContext.getString(com.android.internal.R.string.config_recentsComponentName));
118         final Intent quickStepIntent = new Intent(ACTION_QUICKSTEP)
119                 .setPackage(recentsComponentName.getPackageName());
120         mPackageManager.addResolveInfoForIntent(quickStepIntent, new ResolveInfo());
121 
122         assertThat(SystemNavigationPreferenceController.isGestureAvailable(mContext)).isFalse();
123     }
124 
125     @Test
testIsGestureAvailable_noMatchingServiceExists_shouldReturnFalse()126     public void testIsGestureAvailable_noMatchingServiceExists_shouldReturnFalse() {
127         assertThat(SystemNavigationPreferenceController.isGestureAvailable(mContext)).isFalse();
128     }
129 
130     @Test
testIsOverlayPackageAvailable_noOverlayPackage_shouldReturnFalse()131     public void testIsOverlayPackageAvailable_noOverlayPackage_shouldReturnFalse() {
132         assertThat(SystemNavigationPreferenceController.isOverlayPackageAvailable(mContext,
133                 "com.package.fake")).isFalse();
134     }
135 
136     @Test
testIsSwipeUpEnabled()137     public void testIsSwipeUpEnabled() {
138         SettingsShadowResources.overrideResource(R.integer.config_navBarInteractionMode,
139                 NAV_BAR_MODE_2BUTTON);
140         assertThat(SystemNavigationPreferenceController.is2ButtonNavigationEnabled(
141                 mContext)).isTrue();
142 
143         SettingsShadowResources.overrideResource(R.integer.config_navBarInteractionMode,
144                 NAV_BAR_MODE_3BUTTON);
145         assertThat(SystemNavigationPreferenceController.is2ButtonNavigationEnabled(
146                 mContext)).isFalse();
147 
148         SettingsShadowResources.overrideResource(R.integer.config_navBarInteractionMode,
149                 NAV_BAR_MODE_GESTURAL);
150         assertThat(SystemNavigationPreferenceController.is2ButtonNavigationEnabled(
151                 mContext)).isFalse();
152     }
153 
154     @Test
testIsEdgeToEdgeEnabled()155     public void testIsEdgeToEdgeEnabled() {
156         SettingsShadowResources.overrideResource(R.integer.config_navBarInteractionMode,
157                 NAV_BAR_MODE_GESTURAL);
158         assertThat(SystemNavigationPreferenceController.isGestureNavigationEnabled(
159                 mContext)).isTrue();
160 
161         SettingsShadowResources.overrideResource(R.integer.config_navBarInteractionMode,
162                 NAV_BAR_MODE_3BUTTON);
163         assertThat(SystemNavigationPreferenceController.isGestureNavigationEnabled(
164                 mContext)).isFalse();
165 
166         SettingsShadowResources.overrideResource(R.integer.config_navBarInteractionMode,
167                 NAV_BAR_MODE_2BUTTON);
168         assertThat(SystemNavigationPreferenceController.isGestureNavigationEnabled(
169                 mContext)).isFalse();
170     }
171 
172     @Test
testGetSummary()173     public void testGetSummary() {
174         SettingsShadowResources.overrideResource(R.integer.config_navBarInteractionMode,
175                 NAV_BAR_MODE_GESTURAL);
176         assertThat(TextUtils.equals(mController.getSummary(), mContext.getText(
177                 com.android.settings.R.string.edge_to_edge_navigation_title))).isTrue();
178 
179         SettingsShadowResources.overrideResource(R.integer.config_navBarInteractionMode,
180                 NAV_BAR_MODE_3BUTTON);
181         assertThat(TextUtils.equals(mController.getSummary(),
182                 mContext.getText(com.android.settings.R.string.legacy_navigation_title))).isTrue();
183 
184         SettingsShadowResources.overrideResource(R.integer.config_navBarInteractionMode,
185                 NAV_BAR_MODE_2BUTTON);
186         assertThat(TextUtils.equals(mController.getSummary(), mContext.getText(
187                 com.android.settings.R.string.swipe_up_to_switch_apps_title))).isTrue();
188     }
189 }