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.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU;
20 import static android.provider.Settings.Secure.ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR;
21 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_2BUTTON;
22 import static android.view.WindowManagerPolicyConstants.NAV_BAR_MODE_GESTURAL;
23 
24 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
25 import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
26 
27 import static com.google.common.truth.Truth.assertThat;
28 
29 import static org.mockito.Mockito.when;
30 
31 import android.content.ContentResolver;
32 import android.content.Context;
33 import android.content.res.Resources;
34 import android.provider.Settings;
35 
36 import androidx.preference.ListPreference;
37 import androidx.test.core.app.ApplicationProvider;
38 
39 import org.junit.Before;
40 import org.junit.Rule;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Spy;
44 import org.mockito.junit.MockitoJUnit;
45 import org.mockito.junit.MockitoRule;
46 import org.robolectric.RobolectricTestRunner;
47 
48 /** Tests for {@link AccessibilityButtonLocationPreferenceController}. */
49 @RunWith(RobolectricTestRunner.class)
50 public class AccessibilityButtonLocationPreferenceControllerTest {
51 
52     @Rule
53     public final MockitoRule mockito = MockitoJUnit.rule();
54 
55     @Spy
56     private final Context mContext = ApplicationProvider.getApplicationContext();
57     @Spy
58     private final Resources mResources = mContext.getResources();
59     private final ContentResolver mContentResolver = mContext.getContentResolver();
60     private final ListPreference mListPreference = new ListPreference(mContext);
61     private AccessibilityButtonLocationPreferenceController mController;
62 
63 
64     @Before
setUp()65     public void setUp() {
66         mController = new AccessibilityButtonLocationPreferenceController(mContext,
67                 "test_key");
68         when(mContext.getResources()).thenReturn(mResources);
69     }
70 
71     @Test
getAvailabilityStatus_navigationGestureEnabled_returnConditionallyUnavailable()72     public void getAvailabilityStatus_navigationGestureEnabled_returnConditionallyUnavailable() {
73         when(mResources.getInteger(com.android.internal.R.integer.config_navBarInteractionMode))
74                 .thenReturn(NAV_BAR_MODE_GESTURAL);
75 
76         assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
77     }
78 
79     @Test
getAvailabilityStatus_navigationGestureDisabled_returnAvailable()80     public void getAvailabilityStatus_navigationGestureDisabled_returnAvailable() {
81         when(mResources.getInteger(com.android.internal.R.integer.config_navBarInteractionMode))
82                 .thenReturn(NAV_BAR_MODE_2BUTTON);
83 
84         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
85     }
86 
87     @Test
updateState_a11yBtnModeNavigationBar_navigationBarValue()88     public void updateState_a11yBtnModeNavigationBar_navigationBarValue() {
89         Settings.Secure.putInt(mContentResolver, Settings.Secure.ACCESSIBILITY_BUTTON_MODE,
90                 ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR);
91 
92         mController.updateState(mListPreference);
93 
94         final String navigationBarValue = String.valueOf(ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR);
95         assertThat(mListPreference.getValue()).isEqualTo(navigationBarValue);
96     }
97 
98     @Test
onPreferenceChange_a11yBtnModeFloatingMenu_floatingMenuValue()99     public void onPreferenceChange_a11yBtnModeFloatingMenu_floatingMenuValue() {
100         final String floatingMenuValue = String.valueOf(ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU);
101 
102         mController.onPreferenceChange(mListPreference, floatingMenuValue);
103 
104         assertThat(mListPreference.getValue()).isEqualTo(floatingMenuValue);
105     }
106 }
107