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.systemui.accessibility;
18 
19 
20 import static com.google.common.truth.Truth.assertThat;
21 
22 import static org.mockito.ArgumentMatchers.anyInt;
23 import static org.mockito.Mockito.never;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.app.ActivityManager;
28 import android.provider.Settings;
29 import android.testing.AndroidTestingRunner;
30 
31 import androidx.test.filters.SmallTest;
32 
33 import com.android.systemui.SysuiTestCase;
34 import com.android.systemui.settings.UserTracker;
35 
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnit;
42 import org.mockito.junit.MockitoRule;
43 
44 @RunWith(AndroidTestingRunner.class)
45 @SmallTest
46 public class AccessibilityButtonModeObserverTest extends SysuiTestCase {
47     private static final int MY_USER_ID = ActivityManager.getCurrentUser();
48 
49     @Rule
50     public MockitoRule mockito = MockitoJUnit.rule();
51 
52     @Mock
53     private UserTracker mUserTracker;
54     @Mock
55     private AccessibilityButtonModeObserver.ModeChangedListener mListener;
56 
57     private AccessibilityButtonModeObserver mAccessibilityButtonModeObserver;
58 
59     private static final int TEST_A11Y_BTN_MODE_VALUE =
60             Settings.Secure.ACCESSIBILITY_BUTTON_MODE_FLOATING_MENU;
61 
62     @Before
setUp()63     public void setUp() {
64         when(mUserTracker.getUserId()).thenReturn(MY_USER_ID);
65         Settings.Secure.putIntForUser(mContext.getContentResolver(),
66                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE,
67                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE_NAVIGATION_BAR, MY_USER_ID);
68         mAccessibilityButtonModeObserver = new AccessibilityButtonModeObserver(mContext,
69                 mUserTracker);
70     }
71 
72     @Test
onChange_haveListener_invokeCallback()73     public void onChange_haveListener_invokeCallback() {
74         mAccessibilityButtonModeObserver.addListener(mListener);
75         Settings.Secure.putIntForUser(mContext.getContentResolver(),
76                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, TEST_A11Y_BTN_MODE_VALUE,
77                 MY_USER_ID);
78 
79         mAccessibilityButtonModeObserver.mContentObserver.onChange(false);
80 
81         verify(mListener).onAccessibilityButtonModeChanged(TEST_A11Y_BTN_MODE_VALUE);
82     }
83 
84     @Test
onChange_noListener_noInvokeCallback()85     public void onChange_noListener_noInvokeCallback() {
86         mAccessibilityButtonModeObserver.addListener(mListener);
87         mAccessibilityButtonModeObserver.removeListener(mListener);
88         Settings.Secure.putIntForUser(mContext.getContentResolver(),
89                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, TEST_A11Y_BTN_MODE_VALUE,
90                 MY_USER_ID);
91 
92         mAccessibilityButtonModeObserver.mContentObserver.onChange(false);
93 
94         verify(mListener, never()).onAccessibilityButtonModeChanged(anyInt());
95     }
96 
97     @Test
getCurrentAccessibilityButtonMode_expectedValue()98     public void getCurrentAccessibilityButtonMode_expectedValue() {
99         Settings.Secure.putIntForUser(mContext.getContentResolver(),
100                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, TEST_A11Y_BTN_MODE_VALUE,
101                 MY_USER_ID);
102 
103         final int actualValue =
104                 mAccessibilityButtonModeObserver.getCurrentAccessibilityButtonMode();
105 
106         assertThat(actualValue).isEqualTo(TEST_A11Y_BTN_MODE_VALUE);
107     }
108 }
109