blob: 6386c4cd6331daade7e99573f837e4fca21bffb0 [file] [log] [blame]
Chad Brubaker90f391f2018-10-19 10:26:19 -07001/*
2 * Copyright (C) 2018 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
17package com.android.systemui.qs.tiles;
18
Jason Monkc0e0e2b2018-12-26 16:47:41 -050019import static org.mockito.Mockito.mock;
Chad Brubaker90f391f2018-10-19 10:26:19 -070020import static org.mockito.Mockito.never;
21import static org.mockito.Mockito.verify;
22import static org.mockito.Mockito.when;
23
24import android.hardware.SensorPrivacyManager;
Chad Brubaker90f391f2018-10-19 10:26:19 -070025import android.testing.AndroidTestingRunner;
26import android.testing.TestableLooper;
27
Brett Chabot84151d92019-02-27 15:37:59 -080028import androidx.test.filters.SmallTest;
29
Chad Brubaker90f391f2018-10-19 10:26:19 -070030import com.android.systemui.Dependency;
31import com.android.systemui.SysuiTestCase;
Jason Monkc0e0e2b2018-12-26 16:47:41 -050032import com.android.systemui.plugins.ActivityStarter;
Chad Brubaker90f391f2018-10-19 10:26:19 -070033import com.android.systemui.qs.QSTileHost;
34import com.android.systemui.statusbar.policy.KeyguardMonitor;
35
36import org.junit.Before;
37import org.junit.Test;
38import org.junit.runner.RunWith;
39import org.mockito.Mock;
40import org.mockito.MockitoAnnotations;
41
42@RunWith(AndroidTestingRunner.class)
43@TestableLooper.RunWithLooper
44@SmallTest
45public class SensorPrivacyTileTest extends SysuiTestCase {
46
47 @Mock
48 private KeyguardMonitor mKeyguard;
49 @Mock
50 private QSTileHost mHost;
51 @Mock
52 SensorPrivacyManager mSensorPrivacyManager;
53
54 private TestableLooper mTestableLooper;
55
56 private SensorPrivacyTile mSensorPrivacyTile;
57
58 @Before
59 public void setUp() throws Exception {
60 MockitoAnnotations.initMocks(this);
61
62 mTestableLooper = TestableLooper.get(this);
63 mDependency.injectTestDependency(Dependency.BG_LOOPER, mTestableLooper.getLooper());
64 mKeyguard = mDependency.injectMockDependency(KeyguardMonitor.class);
65
66 mSensorPrivacyManager = mDependency.injectMockDependency(SensorPrivacyManager.class);
67
68 when(mHost.getContext()).thenReturn(mContext);
69
Jason Monkc0e0e2b2018-12-26 16:47:41 -050070 mSensorPrivacyTile = new SensorPrivacyTile(mHost, mSensorPrivacyManager, mKeyguard,
71 mock(ActivityStarter.class));
Chad Brubaker90f391f2018-10-19 10:26:19 -070072 }
73
74 @Test
75 public void testSensorPrivacyListenerAdded_handleListeningTrue() {
76 // To prevent access to privacy related features from apps with WRITE_SECURE_SETTINGS the
77 // sensor privacy state is not stored in Settings; to receive notification apps must add
78 // themselves as a listener with the SensorPrivacyManager. This test verifies when
79 // setListening is called with a value of true the tile adds itself as a listener.
80 mSensorPrivacyTile.handleSetListening(true);
81 mTestableLooper.processAllMessages();
82 verify(mSensorPrivacyManager).addSensorPrivacyListener(mSensorPrivacyTile);
83 }
84
85 @Test
86 public void testSensorPrivacyListenerRemoved_handleListeningFalse() {
87 // Similar to the test above verifies that the tile removes itself as a listener when
88 // setListening is called with a value of false.
89 mSensorPrivacyTile.handleSetListening(false);
90 mTestableLooper.processAllMessages();
91 verify(mSensorPrivacyManager).removeSensorPrivacyListener((mSensorPrivacyTile));
92 }
93
94 @Test
95 public void testSensorPrivacyEnabled_handleClick() {
96 // Verifies when the SensorPrivacy tile is clicked it invokes the SensorPrivacyManager to
97 // set sensor privacy.
98 mSensorPrivacyTile.getState().value = false;
99 mSensorPrivacyTile.handleClick();
100 mTestableLooper.processAllMessages();
101 verify(mSensorPrivacyManager).setSensorPrivacy(true);
102
103 mSensorPrivacyTile.getState().value = true;
104 mSensorPrivacyTile.handleClick();
105 mTestableLooper.processAllMessages();
106 verify(mSensorPrivacyManager).setSensorPrivacy(false);
107 }
108
109 @Test
110 public void testSensorPrivacyNotDisabled_keyguard() {
111 // Verifies when the device is locked that sensor privacy cannot be disabled
112 when(mKeyguard.isSecure()).thenReturn(true);
113 when(mKeyguard.isShowing()).thenReturn(true);
114 mSensorPrivacyTile.getState().value = true;
115 mSensorPrivacyTile.handleClick();
116 mTestableLooper.processAllMessages();
117 verify(mSensorPrivacyManager, never()).setSensorPrivacy(false);
118 }
119}