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.testutils.ImageTestUtils.drawableToBitmap;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 
29 import android.content.ContentResolver;
30 import android.content.Context;
31 import android.graphics.drawable.Drawable;
32 import android.provider.Settings;
33 
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 
49 /** Tests for {@link AccessibilityButtonPreviewPreferenceController}. */
50 @RunWith(RobolectricTestRunner.class)
51 public class AccessibilityButtonPreviewPreferenceControllerTest {
52 
53     @Rule
54     public MockitoRule mocks = MockitoJUnit.rule();
55 
56     @Spy
57     private final Context mContext = ApplicationProvider.getApplicationContext();
58     @Mock
59     private ContentResolver mContentResolver;
60     private AccessibilityButtonPreviewPreferenceController mController;
61 
62     @Before
setUp()63     public void setUp() {
64         when(mContext.getContentResolver()).thenReturn(mContentResolver);
65         mController = new AccessibilityButtonPreviewPreferenceController(mContext, "test_key");
66         mController.mIllustrationPreference = new IllustrationPreference(mContext);
67     }
68 
69     @Test
onChange_a11yBtnModeNavigationBar_getNavigationBarDrawable()70     public void onChange_a11yBtnModeNavigationBar_getNavigationBarDrawable() {
71         Settings.Secure.putInt(mContentResolver, Settings.Secure.ACCESSIBILITY_BUTTON_MODE,
72                 ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR);
73 
74         mController.mContentObserver.onChange(false);
75 
76         final Drawable navigationBarDrawable = mContext.getDrawable(
77                 R.drawable.accessibility_button_navigation);
78         assertThat(drawableToBitmap(mController.mIllustrationPreference.getImageDrawable()).sameAs(
79                 drawableToBitmap(navigationBarDrawable))).isTrue();
80     }
81 
82     @Test
onChange_updatePreviewPreferenceWithConfig_expectedPreviewDrawable()83     public void onChange_updatePreviewPreferenceWithConfig_expectedPreviewDrawable() {
84         Settings.Secure.putInt(mContentResolver,
85                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU);
86         Settings.Secure.putInt(mContentResolver,
87                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_SIZE, /* small size */ 0);
88         Settings.Secure.putFloat(mContentResolver,
89                 Settings.Secure.ACCESSIBILITY_FLOATING_MENU_OPACITY, 0.1f);
90 
91         mController.mContentObserver.onChange(false);
92 
93         final Drawable smallFloatingMenuWithTenOpacityDrawable =
94                 AccessibilityLayerDrawable.createLayerDrawable(mContext,
95                         R.drawable.accessibility_button_preview_small_floating_menu, 10);
96         assertThat(
97                 mController.mIllustrationPreference.getImageDrawable().getConstantState())
98                 .isEqualTo(smallFloatingMenuWithTenOpacityDrawable.getConstantState());
99     }
100 
101     @Test
onResume_registerSpecificContentObserver()102     public void onResume_registerSpecificContentObserver() {
103         mController.onResume();
104 
105         verify(mContentResolver).registerContentObserver(
106                 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_BUTTON_MODE), false,
107                 mController.mContentObserver);
108         verify(mContentResolver).registerContentObserver(
109                 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_FLOATING_MENU_SIZE), false,
110                 mController.mContentObserver);
111         verify(mContentResolver).registerContentObserver(
112                 Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_FLOATING_MENU_OPACITY),
113                 false,
114                 mController.mContentObserver);
115     }
116 
117     @Test
onPause_unregisterContentObserver()118     public void onPause_unregisterContentObserver() {
119         mController.onPause();
120 
121         verify(mContentResolver).unregisterContentObserver(mController.mContentObserver);
122     }
123 }
124