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 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.app.ActivityManager;
22 import android.content.Context;
23 import android.provider.Settings;
24 import android.testing.AndroidTestingRunner;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import com.android.systemui.SysuiTestCase;
29 import com.android.systemui.settings.UserTracker;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mockito;
35 
36 /** Test for {@link SecureSettingsContentObserver}. */
37 @RunWith(AndroidTestingRunner.class)
38 @SmallTest
39 public class SecureSettingsContentObserverTest extends SysuiTestCase {
40 
41     private FakeSecureSettingsContentObserver mTestObserver;
42 
43     @Before
setUpObserver()44     public void setUpObserver() {
45         UserTracker userTracker = Mockito.mock(UserTracker.class);
46         Mockito.when(userTracker.getUserId()).thenReturn(ActivityManager.getCurrentUser());
47         mTestObserver = new FakeSecureSettingsContentObserver(mContext, userTracker,
48                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE);
49     }
50 
51     @Test(expected = NullPointerException.class)
addNullListener_throwNPE()52     public void addNullListener_throwNPE() {
53         mTestObserver.addListener(null);
54     }
55 
56     @Test(expected = NullPointerException.class)
removeNullListener_throwNPE()57     public void removeNullListener_throwNPE() {
58         mTestObserver.removeListener(null);
59     }
60 
61     @Test
checkValue()62     public void checkValue() {
63         Settings.Secure.putIntForUser(mContext.getContentResolver(),
64                 Settings.Secure.ACCESSIBILITY_BUTTON_MODE, 1, ActivityManager.getCurrentUser());
65 
66         assertThat(mTestObserver.getSettingsValue()).isEqualTo("1");
67     }
68 
69 
70     private static class FakeSecureSettingsContentObserver extends
71             SecureSettingsContentObserver<Object> {
72 
FakeSecureSettingsContentObserver(Context context, UserTracker userTracker, String secureSettingsKey)73         protected FakeSecureSettingsContentObserver(Context context, UserTracker userTracker,
74                 String secureSettingsKey) {
75             super(context, userTracker, secureSettingsKey);
76         }
77 
78         @Override
onValueChanged(Object listener, String value)79         void onValueChanged(Object listener, String value) {
80         }
81     }
82 }
83