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 
22 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
23 import static com.android.settings.core.BasePreferenceController.DISABLED_DEPENDENT_SETTING;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
29 
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.provider.Settings;
33 
34 import androidx.preference.SwitchPreference;
35 import androidx.test.core.app.ApplicationProvider;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.Spy;
43 import org.mockito.junit.MockitoJUnit;
44 import org.mockito.junit.MockitoRule;
45 import org.robolectric.RobolectricTestRunner;
46 
47 /** Tests for {@link FloatingMenuFadePreferenceController}. */
48 @RunWith(RobolectricTestRunner.class)
49 public class FloatingMenuFadePreferenceControllerTest {
50 
51     @Rule
52     public MockitoRule mocks = MockitoJUnit.rule();
53 
54     private static final int OFF = 0;
55     private static final int ON = 1;
56 
57     @Spy
58     private final Context mContext = ApplicationProvider.getApplicationContext();
59     @Mock
60     private ContentResolver mContentResolver;
61     private final SwitchPreference mSwitchPreference = new SwitchPreference(mContext);
62     private FloatingMenuFadePreferenceController mController;
63 
64     @Before
setUp()65     public void setUp() {
66         when(mContext.getContentResolver()).thenReturn(mContentResolver);
67         mController = new FloatingMenuFadePreferenceController(mContext, "test_key");
68     }
69 
70     @Test
getAvailabilityStatus_a11yBtnModeFloatingMenu_returnAvailable()71     public void getAvailabilityStatus_a11yBtnModeFloatingMenu_returnAvailable() {
72         Settings.Secure.putInt(mContentResolver, Settings.Secure.ACCESSIBILITY_BUTTON_MODE,
73                 ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU);
74 
75         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
76     }
77 
78     @Test
getAvailabilityStatus_a11yBtnModeNavigationBar_returnDisabledDependentSetting()79     public void getAvailabilityStatus_a11yBtnModeNavigationBar_returnDisabledDependentSetting() {
80         Settings.Secure.putInt(mContentResolver, Settings.Secure.ACCESSIBILITY_BUTTON_MODE,
81                 ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR);
82 
83         assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_DEPENDENT_SETTING);
84     }
85 
86     @Test
updateState_keyFloatingMenuFadeDisabled_fadeIsDisabled()87     public void updateState_keyFloatingMenuFadeDisabled_fadeIsDisabled() {
88         Settings.Secure.putInt(mContentResolver,
89                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_FADE_ENABLED, OFF);
90 
91         mController.updateState(mSwitchPreference);
92 
93         assertThat(mSwitchPreference.isChecked()).isFalse();
94     }
95 
96     @Test
onPreferenceChange_floatingMenuFadeEnabled_keyFloatingMenuFadeIsOn()97     public void onPreferenceChange_floatingMenuFadeEnabled_keyFloatingMenuFadeIsOn() {
98         mController.onPreferenceChange(mSwitchPreference, Boolean.TRUE);
99 
100         final int actualValue = Settings.Secure.getInt(mContentResolver,
101                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_FADE_ENABLED, OFF);
102         assertThat(actualValue).isEqualTo(ON);
103     }
104 
105     @Test
onChange_floatingMenuFadeChangeToDisabled_preferenceDisabled()106     public void onChange_floatingMenuFadeChangeToDisabled_preferenceDisabled() {
107         mController.mPreference = mSwitchPreference;
108         Settings.Secure.putInt(mContentResolver,
109                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_FADE_ENABLED, OFF);
110 
111         mController.mContentObserver.onChange(false);
112 
113         assertThat(mController.mPreference.isEnabled()).isFalse();
114     }
115 
116     @Test
onResume_registerSpecificContentObserver()117     public void onResume_registerSpecificContentObserver() {
118         mController.onResume();
119 
120         verify(mContentResolver).registerContentObserver(
121                 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_BUTTON_MODE), false,
122                 mController.mContentObserver);
123     }
124 
125     @Test
onPause_unregisterContentObserver()126     public void onPause_unregisterContentObserver() {
127         mController.onPause();
128 
129         verify(mContentResolver).unregisterContentObserver(mController.mContentObserver);
130     }
131 }
132