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.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 import static org.robolectric.Shadows.shadowOf;
27 
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.graphics.drawable.Drawable;
31 import android.provider.Settings;
32 
33 import androidx.preference.PreferenceScreen;
34 import androidx.test.core.app.ApplicationProvider;
35 
36 import com.android.settings.R;
37 import com.android.settingslib.widget.IllustrationPreference;
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.Mock;
44 import org.mockito.Spy;
45 import org.mockito.junit.MockitoJUnit;
46 import org.mockito.junit.MockitoRule;
47 import org.robolectric.RobolectricTestRunner;
48 import org.robolectric.shadows.ShadowDrawable;
49 
50 /** Tests for {@link AccessibilityButtonPreviewPreferenceController}. */
51 @RunWith(RobolectricTestRunner.class)
52 public class AccessibilityButtonPreviewPreferenceControllerTest {
53 
54     @Rule
55     public MockitoRule mocks = MockitoJUnit.rule();
56     private static final String PREF_KEY = "test_key";
57     @Spy
58     private final Context mContext = ApplicationProvider.getApplicationContext();
59     @Mock
60     private ContentResolver mContentResolver;
61     @Mock
62     private PreferenceScreen mPreferenceScreen;
63     private AccessibilityButtonPreviewPreferenceController mController;
64 
65     @Before
setUp()66     public void setUp() {
67         when(mContext.getContentResolver()).thenReturn(mContentResolver);
68         mController = new AccessibilityButtonPreviewPreferenceController(mContext, PREF_KEY);
69         mController.mIllustrationPreference = new IllustrationPreference(mContext);
70         when(mPreferenceScreen.findPreference(PREF_KEY))
71                 .thenReturn(mController.mIllustrationPreference);
72     }
73 
74     @Test
onChange_a11yBtnModeNavigationBar_getNavigationBarDrawable()75     public void onChange_a11yBtnModeNavigationBar_getNavigationBarDrawable() {
76         Settings.Secure.putInt(mContentResolver, Settings.Secure.ACCESSIBILITY_BUTTON_MODE,
77                 ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR);
78 
79         mController.mContentObserver.onChange(false);
80 
81         ShadowDrawable drawable = shadowOf(mController.mIllustrationPreference.getImageDrawable());
82         assertThat(drawable.getCreatedFromResId())
83                 .isEqualTo(R.drawable.accessibility_shortcut_type_navbar);
84     }
85 
86     @Test
onChange_updateFloatingMenuSize_expectedPreviewDrawable()87     public void onChange_updateFloatingMenuSize_expectedPreviewDrawable() {
88         Settings.Secure.putInt(mContentResolver,
89                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU);
90         Settings.Secure.putInt(mContentResolver,
91                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_SIZE, /* small size */ 0);
92         mController.displayPreference(mPreferenceScreen);
93         Drawable actualDrawable = mController.mIllustrationPreference.getImageDrawable();
94         ShadowDrawable shadowDrawable = shadowOf(actualDrawable);
95         assertThat(shadowDrawable.getCreatedFromResId())
96                 .isEqualTo(R.drawable.accessibility_shortcut_type_fab_size_small_preview);
97 
98         Settings.Secure.putInt(mContentResolver,
99                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_SIZE, /* large size */ 1);
100         mController.mContentObserver.onChange(false);
101 
102         actualDrawable = mController.mIllustrationPreference.getImageDrawable();
103         shadowDrawable = shadowOf(actualDrawable);
104         assertThat(shadowDrawable.getCreatedFromResId())
105                 .isEqualTo(R.drawable.accessibility_shortcut_type_fab_size_large_preview);
106     }
107 
108     @Test
onResume_registerSpecificContentObserver()109     public void onResume_registerSpecificContentObserver() {
110         mController.onResume();
111 
112         verify(mContentResolver).registerContentObserver(
113                 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_BUTTON_MODE), false,
114                 mController.mContentObserver);
115         verify(mContentResolver).registerContentObserver(
116                 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_FLOATING_MENU_SIZE), false,
117                 mController.mContentObserver);
118         verify(mContentResolver).registerContentObserver(
119                 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_FLOATING_MENU_OPACITY),
120                 false,
121                 mController.mContentObserver);
122     }
123 
124     @Test
onPause_unregisterContentObserver()125     public void onPause_unregisterContentObserver() {
126         mController.onPause();
127 
128         verify(mContentResolver).unregisterContentObserver(mController.mContentObserver);
129     }
130 }
131