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.times;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.when;
40 
41 import android.content.Context;
42 import android.content.om.IOverlayManager;
43 import android.content.om.OverlayInfo;
44 import android.provider.SearchIndexableResource;
45 
46 import com.android.internal.R;
47 import com.android.settings.testutils.shadow.SettingsShadowResources;
48 
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 import org.mockito.Mock;
53 import org.mockito.MockitoAnnotations;
54 import org.robolectric.RobolectricTestRunner;
55 import org.robolectric.RuntimeEnvironment;
56 import org.robolectric.annotation.Config;
57 
58 import java.util.List;
59 
60 @RunWith(RobolectricTestRunner.class)
61 @Config(shadows = SettingsShadowResources.class)
62 public class SystemNavigationGestureSettingsTest {
63 
64     private Context mContext;
65     private SystemNavigationGestureSettings mSettings;
66 
67     @Mock
68     private IOverlayManager mOverlayManager;
69     @Mock
70     private OverlayInfo mOverlayInfoEnabled;
71     @Mock
72     private OverlayInfo mOverlayInfoDisabled;
73 
74     @Before
setUp()75     public void setUp() throws Exception {
76         MockitoAnnotations.initMocks(this);
77 
78         mContext = RuntimeEnvironment.application;
79         mSettings = new SystemNavigationGestureSettings();
80 
81         when(mOverlayInfoDisabled.isEnabled()).thenReturn(false);
82         when(mOverlayInfoEnabled.isEnabled()).thenReturn(true);
83         when(mOverlayManager.getOverlayInfo(any(), anyInt())).thenReturn(mOverlayInfoDisabled);
84     }
85 
86     @Test
testSearchIndexProvider_shouldIndexResource()87     public void testSearchIndexProvider_shouldIndexResource() {
88         final List<SearchIndexableResource> indexRes =
89                 SystemNavigationGestureSettings.SEARCH_INDEX_DATA_PROVIDER.getXmlResourcesToIndex(
90                         RuntimeEnvironment.application, true /* enabled */);
91 
92         assertThat(indexRes).isNotNull();
93         assertThat(indexRes.get(0).xmlResId).isEqualTo(mSettings.getPreferenceScreenResId());
94     }
95 
96     @Test
testGetCurrentSystemNavigationMode()97     public void testGetCurrentSystemNavigationMode() {
98         SettingsShadowResources.overrideResource(
99                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_GESTURAL);
100         assertEquals(KEY_SYSTEM_NAV_GESTURAL, mSettings.getCurrentSystemNavigationMode(mContext));
101 
102         SettingsShadowResources.overrideResource(
103                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_3BUTTON);
104         assertEquals(KEY_SYSTEM_NAV_3BUTTONS, mSettings.getCurrentSystemNavigationMode(mContext));
105 
106         SettingsShadowResources.overrideResource(
107                 R.integer.config_navBarInteractionMode, NAV_BAR_MODE_2BUTTON);
108         assertEquals(KEY_SYSTEM_NAV_2BUTTONS, mSettings.getCurrentSystemNavigationMode(mContext));
109     }
110 
111     @Test
testSetCurrentSystemNavigationMode()112     public void testSetCurrentSystemNavigationMode() throws Exception {
113         mSettings.setCurrentSystemNavigationMode(mOverlayManager, KEY_SYSTEM_NAV_GESTURAL);
114         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
115                 NAV_BAR_MODE_GESTURAL_OVERLAY, USER_CURRENT);
116 
117         mSettings.setCurrentSystemNavigationMode(mOverlayManager, KEY_SYSTEM_NAV_2BUTTONS);
118         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
119                 NAV_BAR_MODE_2BUTTON_OVERLAY, USER_CURRENT);
120 
121         mSettings.setCurrentSystemNavigationMode(mOverlayManager, KEY_SYSTEM_NAV_3BUTTONS);
122         verify(mOverlayManager, times(1)).setEnabledExclusiveInCategory(
123                 NAV_BAR_MODE_3BUTTON_OVERLAY, USER_CURRENT);
124     }
125 }
126