1 /*
2  * Copyright (C) 2021 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_OVERLAY;
20 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_3BUTTON_OVERLAY;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.robolectric.Shadows.shadowOf;
25 
26 import android.content.Context;
27 import android.content.pm.PackageInfo;
28 
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.robolectric.RobolectricTestRunner;
34 import org.robolectric.shadows.ShadowPackageManager;
35 
36 @RunWith(RobolectricTestRunner.class)
37 public class ButtonNavigationSettingsFragmentTest {
38 
39     @Test
getNonIndexableKeys_twoAndThreeButtonNavigationNotAvailable_allKeysNonIndexable()40     public void getNonIndexableKeys_twoAndThreeButtonNavigationNotAvailable_allKeysNonIndexable() {
41         assertThat(ButtonNavigationSettingsFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
42                 ApplicationProvider.getApplicationContext())).isNotEmpty();
43     }
44 
45     @Test
getNonIndexableKeys_twoButtonNavigationAvailable_allKeysExceptAnimIndexable()46     public void getNonIndexableKeys_twoButtonNavigationAvailable_allKeysExceptAnimIndexable() {
47         addPackageToPackageManager(ApplicationProvider.getApplicationContext(),
48                 NAV_BAR_MODE_2BUTTON_OVERLAY);
49         assertThat(ButtonNavigationSettingsFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
50                 ApplicationProvider.getApplicationContext())).containsExactly(
51                 "gesture_power_menu_video");
52     }
53 
54     @Test
getNonIndexableKeys_threeButtonNavigationAvailable_allKeysExceptAnimIndexable()55     public void getNonIndexableKeys_threeButtonNavigationAvailable_allKeysExceptAnimIndexable() {
56         addPackageToPackageManager(ApplicationProvider.getApplicationContext(),
57                 NAV_BAR_MODE_3BUTTON_OVERLAY);
58         assertThat(ButtonNavigationSettingsFragment.SEARCH_INDEX_DATA_PROVIDER.getNonIndexableKeys(
59                 ApplicationProvider.getApplicationContext())).containsExactly(
60                 "gesture_power_menu_video");
61     }
62 
addPackageToPackageManager(Context context, String pkg)63     private static void addPackageToPackageManager(Context context, String pkg) {
64         ShadowPackageManager shadowPm = shadowOf(context.getPackageManager());
65         PackageInfo pi = new PackageInfo();
66         pi.packageName = pkg;
67         shadowPm.installPackage(pi);
68     }
69 
70 }
71