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.KEY_SYSTEM_NAV_2BUTTONS;
28 import static com.android.settings.gestures.SystemNavigationGestureSettings.KEY_SYSTEM_NAV_3BUTTONS;
29 import static com.android.settings.gestures.SystemNavigationGestureSettings.KEY_SYSTEM_NAV_GESTURAL;
30 
31 import static com.google.common.truth.Truth.assertThat;
32 
33 import static junit.framework.Assert.assertEquals;
34 
35 import static org.mockito.ArgumentMatchers.any;
36 import static org.mockito.ArgumentMatchers.anyInt;
37 import static org.mockito.Mockito.spy;
38 import static org.mockito.Mockito.times;
39 import static org.mockito.Mockito.verify;
40 import static org.mockito.Mockito.when;
41 
42 import android.content.Context;
43 import android.content.om.IOverlayManager;
44 import android.content.om.OverlayInfo;
45 import android.content.pm.PackageInfo;
46 import android.content.pm.PackageManager;
47 import android.content.pm.PackageManager.NameNotFoundException;
48 import android.provider.SearchIndexableResource;
49 
50 import com.android.internal.R;
51 import com.android.settings.testutils.shadow.SettingsShadowResources;
52 import com.android.settingslib.search.SearchIndexableRaw;
53 
54 import org.junit.Before;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.Mock;
58 import org.mockito.MockitoAnnotations;
59 import org.robolectric.RobolectricTestRunner;
60 import org.robolectric.RuntimeEnvironment;
61 import org.robolectric.annotation.Config;
62 
63 import java.util.List;
64 
65 @RunWith(RobolectricTestRunner.class)
66 @Config(shadows = SettingsShadowResources.class)
67 public class SystemNavigationGestureSettingsTest {
68 
69     private Context mContext;
70     private SystemNavigationGestureSettings mSettings;
71 
72     @Mock
73     private IOverlayManager mOverlayManager;
74     @Mock
75     private PackageManager mPackageManager;
76     @Mock
77     private OverlayInfo mOverlayInfoEnabled;
78     @Mock
79     private OverlayInfo mOverlayInfoDisabled;
80 
81     @Before
setUp()82     public void setUp() throws Exception {
83         MockitoAnnotations.initMocks(this);
84 
85         mContext = spy(RuntimeEnvironment.application);
86         mSettings = new SystemNavigationGestureSettings();
87 
88         when(mOverlayInfoDisabled.isEnabled()).thenReturn(false);
89         when(mOverlayInfoEnabled.isEnabled()).thenReturn(true);
90         when(mOverlayManager.getOverlayInfo(any(), anyInt())).thenReturn(mOverlayInfoDisabled);
91         when(mContext.getPackageManager()).thenReturn(mPackageManager);
92     }
93 
94     @Test
searchIndexProvider_shouldIndexResource()95     public void searchIndexProvider_shouldIndexResource() {
96         final List<SearchIndexableResource> indexRes =
97                 SystemNavigationGestureSettings.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
98                         RuntimeEnvironment.application, true /* enabled */);
99 
100         assertThat(indexRes).isNotNull();
101         assertThat(indexRes.get(0).xmlResId).isEqualTo(mSettings.getPreferenceScreenResId());
102     }
103 
104     @Test
searchIndexProvider_gesturePackageExist_shouldBeIndexed()105     public void searchIndexProvider_gesturePackageExist_shouldBeIndexed()
106             throws NameNotFoundException {
107         PackageInfo info = new PackageInfo();
108         when(mPackageManager.getPackageInfo(NAV_BAR_MODE_GESTURAL_OVERLAY, 0))
109                 .thenReturn(info);
110 
111         final List<SearchIndexableRaw> indexRaws =
112                 SystemNavigationGestureSettings.SEARCH_INDEX_DATA_PROVIDER
113                         .getRawDataToIndex(mContext, true /* enabled */);
114 
115         assertThat(indexRaws).isNotEmpty();
116     }
117 
118     @Test
searchIndexProvider_noNavigationPackageExist_shouldReturnEmpty()119     public void searchIndexProvider_noNavigationPackageExist_shouldReturnEmpty() {
120         final List<SearchIndexableRaw> indexRaws =
121                 SystemNavigationGestureSettings.SEARCH_INDEX_DATA_PROVIDER
122                         .getRawDataToIndex(mContext, true /* enabled */);
123 
124         assertThat(indexRaws).isEmpty();
125     }
126 
127     @Test
testGetCurrentSystemNavigationMode()128     public void testGetCurrentSystemNavigationMode() {
129         SettingsShadowResources.overrideResource(
130                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_GESTURAL);
131         assertEquals(KEY_SYSTEM_NAV_GESTURAL, mSettings.getCurrentSystemNavigationMode(mContext));
132 
133         SettingsShadowResources.overrideResource(
134                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_3BUTTON);
135         assertEquals(KEY_SYSTEM_NAV_3BUTTONS, mSettings.getCurrentSystemNavigationMode(mContext));
136 
137         SettingsShadowResources.overrideResource(
138                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_2BUTTON);
139         assertEquals(KEY_SYSTEM_NAV_2BUTTONS, mSettings.getCurrentSystemNavigationMode(mContext));
140     }
141 
142     @Test
testSetCurrentSystemNavigationMode()143     public void testSetCurrentSystemNavigationMode() throws Exception {
144         mSettings.setCurrentSystemNavigationMode(mOverlayManager, KEY_SYSTEM_NAV_GESTURAL);
145         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
146                 NAV_BAR_MODE_GESTURAL_OVERLAY, USER_CURRENT);
147 
148         mSettings.setCurrentSystemNavigationMode(mOverlayManager, KEY_SYSTEM_NAV_2BUTTONS);
149         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
150                 NAV_BAR_MODE_2BUTTON_OVERLAY, USER_CURRENT);
151 
152         mSettings.setCurrentSystemNavigationMode(mOverlayManager, KEY_SYSTEM_NAV_3BUTTONS);
153         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
154                 NAV_BAR_MODE_3BUTTON_OVERLAY, USER_CURRENT);
155     }
156 }
157