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 
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.Spy;
41 import org.mockito.junit.MockitoJUnit;
42 import org.mockito.junit.MockitoRule;
43 import org.robolectric.RobolectricTestRunner;
44 
45 /** Tests for {@link AccessibilityButtonPreferenceController}. */
46 @RunWith(RobolectricTestRunner.class)
47 public class AccessibilityButtonPreferenceControllerTest {
48 
49     @Rule
50     public final MockitoRule mockito = MockitoJUnit.rule();
51     @Spy
52     private final Context mContext = ApplicationProvider.getApplicationContext();
53     @Spy
54     private final Resources mResources = mContext.getResources();
55     @Mock
56     private PreferenceScreen mScreen;
57     private Preference mPreference;
58     private AccessibilityButtonPreferenceController mController;
59 
60     @Before
setUp()61     public void setUp() {
62         mController = new AccessibilityButtonPreferenceController(mContext, "test_key");
63         mPreference = new Preference(mContext);
64         mPreference.setKey("test_key");
65 
66         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
67         when(mContext.getResources()).thenReturn(mResources);
68     }
69 
70     @Test
displayPreference_navigationGestureEnabled_setCorrectTitle()71     public void displayPreference_navigationGestureEnabled_setCorrectTitle() {
72         when(mResources.getInteger(com.android.internal.R.integer.config_navBarInteractionMode))
73                 .thenReturn(NAV_BAR_MODE_GESTURAL);
74 
75         mController.displayPreference(mScreen);
76 
77         assertThat(mPreference.getTitle()).isEqualTo(
78                 mContext.getText(R.string.accessibility_button_gesture_title));
79     }
80 
81     @Test
displayPreference_navigationGestureDisabled_setCorrectTitle()82     public void displayPreference_navigationGestureDisabled_setCorrectTitle() {
83         when(mResources.getInteger(com.android.internal.R.integer.config_navBarInteractionMode))
84                 .thenReturn(NAV_BAR_MODE_2BUTTON);
85 
86         mController.displayPreference(mScreen);
87 
88         assertThat(mPreference.getTitle()).isEqualTo(
89                 mContext.getText(R.string.accessibility_button_title));
90     }
91 }
92