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.accessibility;
18 
19 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON;
20 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.when;
25 
26 import android.content.Context;
27 import android.content.res.Resources;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 import androidx.test.core.app.ApplicationProvider;
32 
33 import com.android.settings.R;
34 import com.android.settingslib.search.SearchIndexableRaw;
35 
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.Spy;
42 import org.mockito.junit.MockitoJUnit;
43 import org.mockito.junit.MockitoRule;
44 import org.robolectric.RobolectricTestRunner;
45 
46 import java.util.ArrayList;
47 import java.util.List;
48 
49 /** Tests for {@link AccessibilityButtonPreferenceController}. */
50 @RunWith(RobolectricTestRunner.class)
51 public class AccessibilityButtonPreferenceControllerTest {
52 
53     @Rule
54     public final MockitoRule mockito = MockitoJUnit.rule();
55     @Spy
56     private final Context mContext = ApplicationProvider.getApplicationContext();
57     @Spy
58     private final Resources mResources = mContext.getResources();
59     @Mock
60     private PreferenceScreen mScreen;
61     private Preference mPreference;
62     private AccessibilityButtonPreferenceController mController;
63 
64     @Before
setUp()65     public void setUp() {
66         mController = new AccessibilityButtonPreferenceController(mContext, "test_key");
67         mPreference = new Preference(mContext);
68         mPreference.setKey("test_key");
69 
70         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
71         when(mContext.getResources()).thenReturn(mResources);
72     }
73 
74     @Test
displayPreference_navigationGestureEnabled_setCorrectTitle()75     public void displayPreference_navigationGestureEnabled_setCorrectTitle() {
76         when(mResources.getInteger(com.android.internal.R.integer.config_navBarInteractionMode))
77                 .thenReturn(NAV_BAR_MODE_GESTURAL);
78 
79         mController.displayPreference(mScreen);
80 
81         assertThat(mPreference.getTitle()).isEqualTo(
82                 mContext.getText(R.string.accessibility_button_gesture_title));
83     }
84 
85     @Test
displayPreference_navigationGestureDisabled_setCorrectTitle()86     public void displayPreference_navigationGestureDisabled_setCorrectTitle() {
87         when(mResources.getInteger(com.android.internal.R.integer.config_navBarInteractionMode))
88                 .thenReturn(NAV_BAR_MODE_2BUTTON);
89 
90         mController.displayPreference(mScreen);
91 
92         assertThat(mPreference.getTitle()).isEqualTo(
93                 mContext.getText(R.string.accessibility_button_title));
94     }
95 
96     @Test
updateDynamicRawDataToIndex_navigationGestureEnabled_setCorrectIndex()97     public void updateDynamicRawDataToIndex_navigationGestureEnabled_setCorrectIndex() {
98         when(mResources.getInteger(com.android.internal.R.integer.config_navBarInteractionMode))
99                 .thenReturn(NAV_BAR_MODE_GESTURAL);
100         List<SearchIndexableRaw> rawDataList = new ArrayList<>();
101 
102         mController.updateDynamicRawDataToIndex(rawDataList);
103 
104         assertThat(rawDataList).hasSize(1);
105         SearchIndexableRaw raw = rawDataList.get(0);
106         assertThat(raw.title).isEqualTo(
107                 mResources.getString(R.string.accessibility_button_gesture_title));
108         assertThat(raw.screenTitle).isEqualTo(
109                 mResources.getString(R.string.accessibility_shortcuts_settings_title));
110     }
111 
112     @Test
updateDynamicRawDataToIndex_navigationGestureDisabled_setCorrectIndex()113     public void updateDynamicRawDataToIndex_navigationGestureDisabled_setCorrectIndex() {
114         when(mResources.getInteger(com.android.internal.R.integer.config_navBarInteractionMode))
115                 .thenReturn(NAV_BAR_MODE_2BUTTON);
116         List<SearchIndexableRaw> rawDataList = new ArrayList<>();
117 
118         mController.updateDynamicRawDataToIndex(rawDataList);
119 
120         assertThat(rawDataList).hasSize(1);
121         SearchIndexableRaw raw = rawDataList.get(0);
122         assertThat(raw.title).isEqualTo(
123                 mResources.getString(R.string.accessibility_button_title));
124         assertThat(raw.screenTitle).isEqualTo(
125                 mResources.getString(R.string.accessibility_shortcuts_settings_title));
126     }
127 }
128