OSDN Git Service

Show a dialog with a slider to set the back gesture's sensitivity
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / gestures / SystemNavigationGestureSettingsTest.java
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.os.UserHandle.USER_CURRENT;
20 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON;
21 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON_OVERLAY;
22 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON;
23 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON_OVERLAY;
24 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
25 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL_OVERLAY;
26
27 import static com.android.settings.gestures.SystemNavigationGestureSettings.BACK_GESTURE_INSET_DEFAULT_OVERLAY;
28 import static com.android.settings.gestures.SystemNavigationGestureSettings.BACK_GESTURE_INSET_OVERLAYS;
29 import static com.android.settings.gestures.SystemNavigationGestureSettings.KEY_SYSTEM_NAV_2BUTTONS;
30 import static com.android.settings.gestures.SystemNavigationGestureSettings.KEY_SYSTEM_NAV_3BUTTONS;
31 import static com.android.settings.gestures.SystemNavigationGestureSettings.KEY_SYSTEM_NAV_GESTURAL;
32 import static com.android.settings.gestures.SystemNavigationGestureSettings.NAV_BAR_MODE_GESTURAL_OVERLAY_EXTRA_WIDE_BACK;
33 import static com.android.settings.gestures.SystemNavigationGestureSettings.NAV_BAR_MODE_GESTURAL_OVERLAY_NARROW_BACK;
34 import static com.android.settings.gestures.SystemNavigationGestureSettings.NAV_BAR_MODE_GESTURAL_OVERLAY_WIDE_BACK;
35
36 import static com.google.common.truth.Truth.assertThat;
37
38 import static junit.framework.Assert.assertEquals;
39
40 import static org.mockito.ArgumentMatchers.any;
41 import static org.mockito.ArgumentMatchers.anyInt;
42 import static org.mockito.Mockito.never;
43 import static org.mockito.Mockito.times;
44 import static org.mockito.Mockito.verify;
45 import static org.mockito.Mockito.when;
46
47 import android.content.Context;
48 import android.content.om.IOverlayManager;
49 import android.content.om.OverlayInfo;
50 import android.provider.SearchIndexableResource;
51
52 import com.android.internal.R;
53 import com.android.settings.testutils.shadow.SettingsShadowResources;
54
55 import org.junit.Before;
56 import org.junit.Test;
57 import org.junit.runner.RunWith;
58 import org.mockito.Mock;
59 import org.mockito.MockitoAnnotations;
60 import org.robolectric.RobolectricTestRunner;
61 import org.robolectric.RuntimeEnvironment;
62 import org.robolectric.annotation.Config;
63
64 import java.util.List;
65
66 @RunWith(RobolectricTestRunner.class)
67 @Config(shadows = SettingsShadowResources.class)
68 public class SystemNavigationGestureSettingsTest {
69
70     private Context mContext;
71     private SystemNavigationGestureSettings mSettings;
72
73     @Mock
74     private IOverlayManager mOverlayManager;
75     @Mock
76     private OverlayInfo mOverlayInfoEnabled;
77     @Mock
78     private OverlayInfo mOverlayInfoDisabled;
79
80     @Before
81     public void setUp() throws Exception {
82         MockitoAnnotations.initMocks(this);
83
84         mContext = RuntimeEnvironment.application;
85         mSettings = new SystemNavigationGestureSettings();
86
87         when(mOverlayInfoDisabled.isEnabled()).thenReturn(false);
88         when(mOverlayInfoEnabled.isEnabled()).thenReturn(true);
89         when(mOverlayManager.getOverlayInfo(any(), anyInt())).thenReturn(mOverlayInfoDisabled);
90     }
91
92     @Test
93     public void testSearchIndexProvider_shouldIndexResource() {
94         final List<SearchIndexableResource> indexRes =
95                 SystemNavigationGestureSettings.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
96                         RuntimeEnvironment.application, true /* enabled */);
97
98         assertThat(indexRes).isNotNull();
99         assertThat(indexRes.get(0).xmlResId).isEqualTo(mSettings.getPreferenceScreenResId());
100     }
101
102     @Test
103     public void testGetCurrentSystemNavigationMode() {
104         SettingsShadowResources.overrideResource(
105                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_GESTURAL);
106         assertEquals(KEY_SYSTEM_NAV_GESTURAL, mSettings.getCurrentSystemNavigationMode(mContext));
107
108         SettingsShadowResources.overrideResource(
109                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_3BUTTON);
110         assertEquals(KEY_SYSTEM_NAV_3BUTTONS, mSettings.getCurrentSystemNavigationMode(mContext));
111
112         SettingsShadowResources.overrideResource(
113                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_2BUTTON);
114         assertEquals(KEY_SYSTEM_NAV_2BUTTONS, mSettings.getCurrentSystemNavigationMode(mContext));
115     }
116
117     @Test
118     public void testSetCurrentSystemNavigationMode() throws Exception {
119         mSettings.setBackSensitivity(mContext, mOverlayManager, 0);
120         mSettings.setCurrentSystemNavigationMode(mContext, mOverlayManager,
121                 KEY_SYSTEM_NAV_GESTURAL);
122         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
123                 NAV_BAR_MODE_GESTURAL_OVERLAY_NARROW_BACK, USER_CURRENT);
124
125         mSettings.setBackSensitivity(mContext, mOverlayManager, 1);
126         mSettings.setCurrentSystemNavigationMode(mContext, mOverlayManager,
127                 KEY_SYSTEM_NAV_GESTURAL);
128         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
129                 NAV_BAR_MODE_GESTURAL_OVERLAY, USER_CURRENT);
130
131         mSettings.setBackSensitivity(mContext, mOverlayManager, 2);
132         mSettings.setCurrentSystemNavigationMode(mContext, mOverlayManager,
133                 KEY_SYSTEM_NAV_GESTURAL);
134         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
135                 NAV_BAR_MODE_GESTURAL_OVERLAY_WIDE_BACK, USER_CURRENT);
136
137         mSettings.setBackSensitivity(mContext, mOverlayManager, 3);
138         mSettings.setCurrentSystemNavigationMode(mContext, mOverlayManager,
139                 KEY_SYSTEM_NAV_GESTURAL);
140         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
141                 NAV_BAR_MODE_GESTURAL_OVERLAY_EXTRA_WIDE_BACK, USER_CURRENT);
142
143         mSettings.setCurrentSystemNavigationMode(mContext, mOverlayManager,
144                 KEY_SYSTEM_NAV_2BUTTONS);
145         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
146                 NAV_BAR_MODE_2BUTTON_OVERLAY, USER_CURRENT);
147
148         mSettings.setCurrentSystemNavigationMode(mContext, mOverlayManager,
149                 KEY_SYSTEM_NAV_3BUTTONS);
150         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
151                 NAV_BAR_MODE_3BUTTON_OVERLAY, USER_CURRENT);
152     }
153
154     @Test
155     public void testSetCurrentSystemNavigationMode_backSensitivityValuePersists() throws Exception {
156         SettingsShadowResources.overrideResource(
157                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_3BUTTON);
158
159         mSettings.setBackSensitivity(mContext, mOverlayManager, 2);
160         mSettings.setCurrentSystemNavigationMode(mContext, mOverlayManager,
161                 KEY_SYSTEM_NAV_3BUTTONS);
162         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
163                 NAV_BAR_MODE_3BUTTON_OVERLAY, USER_CURRENT);
164
165         // Return to Gesture navigation, without setting the sensitivity value.
166         mSettings.setCurrentSystemNavigationMode(mContext, mOverlayManager,
167                 KEY_SYSTEM_NAV_GESTURAL);
168         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
169                 NAV_BAR_MODE_GESTURAL_OVERLAY_WIDE_BACK, USER_CURRENT);
170     }
171
172     @Test
173     public void testGetBackSensitivity_default() {
174         assertEquals(BACK_GESTURE_INSET_DEFAULT_OVERLAY,
175                 mSettings.getBackSensitivity(mContext, mOverlayManager));
176     }
177
178     @Test
179     public void testBackSensitivitySetterAndGetter_currentNavModeNotGestural() throws Exception {
180         SettingsShadowResources.overrideResource(
181                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_3BUTTON);
182
183         mSettings.setBackSensitivity(mContext, mOverlayManager, 3);
184         assertEquals(3, mSettings.getBackSensitivity(mContext, mOverlayManager));
185         mSettings.setBackSensitivity(mContext, mOverlayManager, 2);
186         assertEquals(2, mSettings.getBackSensitivity(mContext, mOverlayManager));
187
188         verify(mOverlayManager, never()).setEnabledExclusiveInCategory(any(), anyInt());
189     }
190
191     @Test
192     public void testBackSensitivitySetterAndGetter_currentNavModeIsGestural() throws Exception {
193         SettingsShadowResources.overrideResource(
194                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_GESTURAL);
195
196         when(mOverlayManager.getOverlayInfo(BACK_GESTURE_INSET_OVERLAYS[3], USER_CURRENT))
197                 .thenReturn(mOverlayInfoEnabled);
198         mSettings.setBackSensitivity(mContext, mOverlayManager, 3);
199         assertEquals(3, mSettings.getBackSensitivity(mContext, mOverlayManager));
200
201         when(mOverlayManager.getOverlayInfo(BACK_GESTURE_INSET_OVERLAYS[2], USER_CURRENT))
202                 .thenReturn(mOverlayInfoEnabled);
203         mSettings.setBackSensitivity(mContext, mOverlayManager, 2);
204         assertEquals(2, mSettings.getBackSensitivity(mContext, mOverlayManager));
205
206         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
207                 NAV_BAR_MODE_GESTURAL_OVERLAY_WIDE_BACK, USER_CURRENT);
208         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
209                 NAV_BAR_MODE_GESTURAL_OVERLAY_EXTRA_WIDE_BACK, USER_CURRENT);
210     }
211 }