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_GESTURE;
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 AccessibilityButtonGesturePreferenceController}. */
49 @RunWith(RobolectricTestRunner.class)
50 public class AccessibilityButtonGesturePreferenceControllerTest {
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 AccessibilityButtonGesturePreferenceController mController;
62 
63     @Before
setUp()64     public void setUp() {
65         mController = new AccessibilityButtonGesturePreferenceController(mContext,
66                 "test_key");
67         when(mContext.getResources()).thenReturn(mResources);
68     }
69 
70     @Test
getAvailabilityStatus_navigationGestureEnabled_returnAvailable()71     public void getAvailabilityStatus_navigationGestureEnabled_returnAvailable() {
72         when(mResources.getInteger(com.android.internal.R.integer.config_navBarInteractionMode))
73                 .thenReturn(NAV_BAR_MODE_GESTURAL);
74 
75         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
76     }
77 
78     @Test
getAvailabilityStatus_navigationGestureDisabled_returnConditionallyUnavailable()79     public void getAvailabilityStatus_navigationGestureDisabled_returnConditionallyUnavailable() {
80         when(mResources.getInteger(com.android.internal.R.integer.config_navBarInteractionMode))
81                 .thenReturn(NAV_BAR_MODE_2BUTTON);
82 
83         assertThat(mController.getAvailabilityStatus()).isEqualTo(CONDITIONALLY_UNAVAILABLE);
84     }
85 
86     @Test
updateState_a11yBtnModeGesture_navigationBarValue()87     public void updateState_a11yBtnModeGesture_navigationBarValue() {
88         Settings.Secure.putInt(mContentResolver, Settings.Secure.ACCESSIBILITY_BUTTON_MODE,
89                 ACCESSIBILITY_BUTTON_MODE_GESTURE);
90 
91         mController.updateState(mListPreference);
92 
93         final String gestureValue = String.valueOf(ACCESSIBILITY_BUTTON_MODE_GESTURE);
94         assertThat(mListPreference.getValue()).isEqualTo(gestureValue);
95     }
96 
97     @Test
onPreferenceChange_a11yBtnModeFloatingMenu_floatingMenuValue()98     public void onPreferenceChange_a11yBtnModeFloatingMenu_floatingMenuValue() {
99         final String floatingMenuValue = String.valueOf(ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU);
100 
101         mController.onPreferenceChange(mListPreference, floatingMenuValue);
102 
103         assertThat(mListPreference.getValue()).isEqualTo(floatingMenuValue);
104     }
105 }
106