• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.app.contextualsearch.ContextualSearchManager.FEATURE_CONTEXTUAL_SEARCH;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.spy;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.content.pm.PackageManager;
29 import android.provider.Settings;
30 
31 import androidx.test.core.app.ApplicationProvider;
32 
33 import com.android.settings.testutils.shadow.ShadowDeviceConfig;
34 
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.robolectric.RobolectricTestRunner;
39 import org.robolectric.annotation.Config;
40 
41 @RunWith(RobolectricTestRunner.class)
42 @Config(shadows = ShadowDeviceConfig.class)
43 public class NavigationSettingsContextualSearchControllerTest {
44 
45     private static final String KEY_PRESS_HOLD_FOR_SEARCH = "search_gesture_press_hold";
46 
47     private NavigationSettingsContextualSearchController mController;
48     private Context mContext;
49     private PackageManager mPackageManager;
50 
51     @Before
setUp()52     public void setUp() {
53         mContext = spy(ApplicationProvider.getApplicationContext());
54         mPackageManager = mock(PackageManager.class);
55         when(mContext.getPackageManager()).thenReturn(mPackageManager);
56         mController = new NavigationSettingsContextualSearchController(
57                 mContext, KEY_PRESS_HOLD_FOR_SEARCH);
58     }
59 
60     @Test
isAvailable_hasContextualSearchSystemFeature_shouldReturnTrue()61     public void isAvailable_hasContextualSearchSystemFeature_shouldReturnTrue() {
62         when(mPackageManager.hasSystemFeature(FEATURE_CONTEXTUAL_SEARCH)).thenReturn(true);
63         assertThat(mController.isAvailable()).isTrue();
64     }
65 
66     @Test
isAvailable_doesNotHaveContextualSearchSystemFeature_shouldReturnFalse()67     public void isAvailable_doesNotHaveContextualSearchSystemFeature_shouldReturnFalse() {
68         when(mPackageManager.hasSystemFeature(FEATURE_CONTEXTUAL_SEARCH)).thenReturn(false);
69         assertThat(mController.isAvailable()).isFalse();
70     }
71 
72     @Test
isChecked_noDefault_true()73     public void isChecked_noDefault_true() {
74         assertThat(mController.isChecked()).isTrue();
75     }
76 
77     @Test
isChecked_valueFalse_shouldReturnFalse()78     public void isChecked_valueFalse_shouldReturnFalse() {
79         Settings.Secure.putInt(mContext.getContentResolver(),
80                 Settings.Secure.SEARCH_ALL_ENTRYPOINTS_ENABLED, 0);
81         assertThat(mController.isChecked()).isFalse();
82     }
83 
84     @Test
isChecked_valueTrue_shouldReturnTrue()85     public void isChecked_valueTrue_shouldReturnTrue() {
86         Settings.Secure.putInt(mContext.getContentResolver(),
87                 Settings.Secure.SEARCH_ALL_ENTRYPOINTS_ENABLED, 1);
88         assertThat(mController.isChecked()).isTrue();
89     }
90 
91     @Test
onPreferenceChange_preferenceChecked_valueTrue()92     public void onPreferenceChange_preferenceChecked_valueTrue() {
93         mController.onPreferenceChange(null, true);
94         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
95                 Settings.Secure.SEARCH_ALL_ENTRYPOINTS_ENABLED, -1)).isEqualTo(1);
96     }
97 
98     @Test
onPreferenceChange_preferenceUnchecked_valueFalse()99     public void onPreferenceChange_preferenceUnchecked_valueFalse() {
100         mController.onPreferenceChange(null, false);
101         assertThat(Settings.Secure.getInt(mContext.getContentResolver(),
102                 Settings.Secure.SEARCH_ALL_ENTRYPOINTS_ENABLED, -1)).isEqualTo(0);
103     }
104 }
105