Chad Brubaker | 90f391f | 2018-10-19 10:26:19 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.qs.tiles; |
| 18 | |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 19 | import static org.mockito.Mockito.mock; |
Chad Brubaker | 90f391f | 2018-10-19 10:26:19 -0700 | [diff] [blame] | 20 | import static org.mockito.Mockito.never; |
| 21 | import static org.mockito.Mockito.verify; |
| 22 | import static org.mockito.Mockito.when; |
| 23 | |
| 24 | import android.hardware.SensorPrivacyManager; |
Chad Brubaker | 90f391f | 2018-10-19 10:26:19 -0700 | [diff] [blame] | 25 | import android.testing.AndroidTestingRunner; |
| 26 | import android.testing.TestableLooper; |
| 27 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 28 | import androidx.test.filters.SmallTest; |
| 29 | |
Chad Brubaker | 90f391f | 2018-10-19 10:26:19 -0700 | [diff] [blame] | 30 | import com.android.systemui.Dependency; |
| 31 | import com.android.systemui.SysuiTestCase; |
Jason Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 32 | import com.android.systemui.plugins.ActivityStarter; |
Chad Brubaker | 90f391f | 2018-10-19 10:26:19 -0700 | [diff] [blame] | 33 | import com.android.systemui.qs.QSTileHost; |
| 34 | import com.android.systemui.statusbar.policy.KeyguardMonitor; |
| 35 | |
| 36 | import org.junit.Before; |
| 37 | import org.junit.Test; |
| 38 | import org.junit.runner.RunWith; |
| 39 | import org.mockito.Mock; |
| 40 | import org.mockito.MockitoAnnotations; |
| 41 | |
| 42 | @RunWith(AndroidTestingRunner.class) |
| 43 | @TestableLooper.RunWithLooper |
| 44 | @SmallTest |
| 45 | public 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 Monk | c0e0e2b | 2018-12-26 16:47:41 -0500 | [diff] [blame] | 70 | mSensorPrivacyTile = new SensorPrivacyTile(mHost, mSensorPrivacyManager, mKeyguard, |
| 71 | mock(ActivityStarter.class)); |
Chad Brubaker | 90f391f | 2018-10-19 10:26:19 -0700 | [diff] [blame] | 72 | } |
| 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 | } |