2 * Copyright (C) 2019 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 package com.android.settings.gestures;
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;
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;
36 import static com.google.common.truth.Truth.assertThat;
38 import static junit.framework.Assert.assertEquals;
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;
47 import android.content.Context;
48 import android.content.om.IOverlayManager;
49 import android.content.om.OverlayInfo;
50 import android.provider.SearchIndexableResource;
52 import com.android.internal.R;
53 import com.android.settings.testutils.shadow.SettingsShadowResources;
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;
64 import java.util.List;
66 @RunWith(RobolectricTestRunner.class)
67 @Config(shadows = SettingsShadowResources.class)
68 public class SystemNavigationGestureSettingsTest {
70 private Context mContext;
71 private SystemNavigationGestureSettings mSettings;
74 private IOverlayManager mOverlayManager;
76 private OverlayInfo mOverlayInfoEnabled;
78 private OverlayInfo mOverlayInfoDisabled;
81 public void setUp() throws Exception {
82 MockitoAnnotations.initMocks(this);
84 mContext = RuntimeEnvironment.application;
85 mSettings = new SystemNavigationGestureSettings();
87 when(mOverlayInfoDisabled.isEnabled()).thenReturn(false);
88 when(mOverlayInfoEnabled.isEnabled()).thenReturn(true);
89 when(mOverlayManager.getOverlayInfo(any(), anyInt())).thenReturn(mOverlayInfoDisabled);
93 public void testSearchIndexProvider_shouldIndexResource() {
94 final List<SearchIndexableResource> indexRes =
95 SystemNavigationGestureSettings.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
96 RuntimeEnvironment.application, true /* enabled */);
98 assertThat(indexRes).isNotNull();
99 assertThat(indexRes.get(0).xmlResId).isEqualTo(mSettings.getPreferenceScreenResId());
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));
108 SettingsShadowResources.overrideResource(
109 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_3BUTTON);
110 assertEquals(KEY_SYSTEM_NAV_3BUTTONS, mSettings.getCurrentSystemNavigationMode(mContext));
112 SettingsShadowResources.overrideResource(
113 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_2BUTTON);
114 assertEquals(KEY_SYSTEM_NAV_2BUTTONS, mSettings.getCurrentSystemNavigationMode(mContext));
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);
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);
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);
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);
143 mSettings.setCurrentSystemNavigationMode(mContext, mOverlayManager,
144 KEY_SYSTEM_NAV_2BUTTONS);
145 verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
146 NAV_BAR_MODE_2BUTTON_OVERLAY, USER_CURRENT);
148 mSettings.setCurrentSystemNavigationMode(mContext, mOverlayManager,
149 KEY_SYSTEM_NAV_3BUTTONS);
150 verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
151 NAV_BAR_MODE_3BUTTON_OVERLAY, USER_CURRENT);
155 public void testSetCurrentSystemNavigationMode_backSensitivityValuePersists() throws Exception {
156 SettingsShadowResources.overrideResource(
157 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_3BUTTON);
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);
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);
173 public void testGetBackSensitivity_default() {
174 assertEquals(BACK_GESTURE_INSET_DEFAULT_OVERLAY,
175 mSettings.getBackSensitivity(mContext, mOverlayManager));
179 public void testBackSensitivitySetterAndGetter_currentNavModeNotGestural() throws Exception {
180 SettingsShadowResources.overrideResource(
181 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_3BUTTON);
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));
188 verify(mOverlayManager, never()).setEnabledExclusiveInCategory(any(), anyInt());
192 public void testBackSensitivitySetterAndGetter_currentNavModeIsGestural() throws Exception {
193 SettingsShadowResources.overrideResource(
194 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_GESTURAL);
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));
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));
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);