| /* |
| * Copyright (C) 2018 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.systemui.appops; |
| |
| import static junit.framework.TestCase.assertFalse; |
| |
| import static org.junit.Assert.assertEquals; |
| import static org.junit.Assert.assertTrue; |
| import static org.mockito.ArgumentMatchers.any; |
| import static org.mockito.ArgumentMatchers.anyBoolean; |
| import static org.mockito.ArgumentMatchers.anyInt; |
| import static org.mockito.ArgumentMatchers.anyLong; |
| import static org.mockito.ArgumentMatchers.anyString; |
| import static org.mockito.Mockito.never; |
| import static org.mockito.Mockito.times; |
| import static org.mockito.Mockito.verify; |
| |
| import static java.lang.Thread.sleep; |
| |
| import android.app.AppOpsManager; |
| import android.content.pm.PackageManager; |
| import android.os.UserHandle; |
| import android.testing.AndroidTestingRunner; |
| import android.testing.TestableLooper; |
| |
| import androidx.test.filters.SmallTest; |
| |
| import com.android.systemui.DumpController; |
| import com.android.systemui.SysuiTestCase; |
| |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.mockito.Mock; |
| import org.mockito.MockitoAnnotations; |
| |
| import java.util.List; |
| |
| @SmallTest |
| @RunWith(AndroidTestingRunner.class) |
| @TestableLooper.RunWithLooper |
| public class AppOpsControllerTest extends SysuiTestCase { |
| private static final String TEST_PACKAGE_NAME = "test"; |
| private static final int TEST_UID = UserHandle.getUid(0, 0); |
| private static final int TEST_UID_OTHER = UserHandle.getUid(1, 0); |
| |
| @Mock |
| private AppOpsManager mAppOpsManager; |
| @Mock |
| private PackageManager mPackageManager; |
| @Mock |
| private AppOpsController.Callback mCallback; |
| @Mock |
| private AppOpsControllerImpl.H mMockHandler; |
| @Mock |
| private DumpController mDumpController; |
| |
| private AppOpsControllerImpl mController; |
| private TestableLooper mTestableLooper; |
| |
| @Before |
| public void setUp() { |
| MockitoAnnotations.initMocks(this); |
| mTestableLooper = TestableLooper.get(this); |
| |
| getContext().addMockSystemService(AppOpsManager.class, mAppOpsManager); |
| |
| // All permissions of TEST_UID and TEST_UID_OTHER are user sensitive. None of |
| // TEST_UID_NON_USER_SENSITIVE are user sensitive. |
| getContext().setMockPackageManager(mPackageManager); |
| |
| mController = |
| new AppOpsControllerImpl(mContext, mTestableLooper.getLooper(), mDumpController); |
| } |
| |
| @Test |
| public void testOnlyListenForFewOps() { |
| mController.setListening(true); |
| verify(mAppOpsManager, times(1)).startWatchingActive(AppOpsControllerImpl.OPS, mController); |
| } |
| |
| @Test |
| public void testStopListening() { |
| mController.setListening(false); |
| verify(mAppOpsManager, times(1)).stopWatchingActive(mController); |
| } |
| |
| @Test |
| public void addCallback_includedCode() { |
| mController.addCallback( |
| new int[]{AppOpsManager.OP_RECORD_AUDIO, AppOpsManager.OP_FINE_LOCATION}, |
| mCallback); |
| mController.onOpActiveChanged( |
| AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true); |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| mTestableLooper.processAllMessages(); |
| verify(mCallback).onActiveStateChanged(AppOpsManager.OP_RECORD_AUDIO, |
| TEST_UID, TEST_PACKAGE_NAME, true); |
| } |
| |
| @Test |
| public void addCallback_notIncludedCode() { |
| mController.addCallback(new int[]{AppOpsManager.OP_FINE_LOCATION}, mCallback); |
| mController.onOpActiveChanged( |
| AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true); |
| mTestableLooper.processAllMessages(); |
| verify(mCallback, never()).onActiveStateChanged( |
| anyInt(), anyInt(), anyString(), anyBoolean()); |
| } |
| |
| @Test |
| public void removeCallback_sameCode() { |
| mController.addCallback(new int[]{AppOpsManager.OP_RECORD_AUDIO}, mCallback); |
| mController.removeCallback(new int[]{AppOpsManager.OP_RECORD_AUDIO}, mCallback); |
| mController.onOpActiveChanged( |
| AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true); |
| mTestableLooper.processAllMessages(); |
| verify(mCallback, never()).onActiveStateChanged( |
| anyInt(), anyInt(), anyString(), anyBoolean()); |
| } |
| |
| @Test |
| public void addCallback_notSameCode() { |
| mController.addCallback(new int[]{AppOpsManager.OP_RECORD_AUDIO}, mCallback); |
| mController.removeCallback(new int[]{AppOpsManager.OP_CAMERA}, mCallback); |
| mController.onOpActiveChanged( |
| AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true); |
| mTestableLooper.processAllMessages(); |
| verify(mCallback).onActiveStateChanged(AppOpsManager.OP_RECORD_AUDIO, |
| TEST_UID, TEST_PACKAGE_NAME, true); |
| } |
| |
| @Test |
| public void getActiveItems_sameDetails() { |
| mController.onOpActiveChanged(AppOpsManager.OP_RECORD_AUDIO, |
| TEST_UID, TEST_PACKAGE_NAME, true); |
| mController.onOpActiveChanged(AppOpsManager.OP_RECORD_AUDIO, |
| TEST_UID, TEST_PACKAGE_NAME, true); |
| assertEquals(1, mController.getActiveAppOps().size()); |
| } |
| |
| @Test |
| public void getActiveItems_differentDetails() { |
| mController.onOpActiveChanged(AppOpsManager.OP_RECORD_AUDIO, |
| TEST_UID, TEST_PACKAGE_NAME, true); |
| mController.onOpActiveChanged(AppOpsManager.OP_CAMERA, |
| TEST_UID, TEST_PACKAGE_NAME, true); |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, |
| TEST_UID, TEST_PACKAGE_NAME, AppOpsManager.MODE_ALLOWED); |
| assertEquals(3, mController.getActiveAppOps().size()); |
| } |
| |
| @Test |
| public void getActiveItemsForUser() { |
| mController.onOpActiveChanged(AppOpsManager.OP_RECORD_AUDIO, |
| TEST_UID, TEST_PACKAGE_NAME, true); |
| mController.onOpActiveChanged(AppOpsManager.OP_CAMERA, |
| TEST_UID_OTHER, TEST_PACKAGE_NAME, true); |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, |
| TEST_UID, TEST_PACKAGE_NAME, AppOpsManager.MODE_ALLOWED); |
| assertEquals(2, |
| mController.getActiveAppOpsForUser(UserHandle.getUserId(TEST_UID)).size()); |
| assertEquals(1, |
| mController.getActiveAppOpsForUser(UserHandle.getUserId(TEST_UID_OTHER)).size()); |
| } |
| |
| @Test |
| public void opNotedScheduledForRemoval() { |
| mController.setBGHandler(mMockHandler); |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| verify(mMockHandler).scheduleRemoval(any(AppOpItem.class), anyLong()); |
| } |
| |
| @Test |
| public void noItemsAfterStopListening() { |
| mController.setBGHandler(mMockHandler); |
| |
| mController.setListening(true); |
| mController.onOpActiveChanged(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| true); |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| assertFalse(mController.getActiveAppOps().isEmpty()); |
| |
| mController.setListening(false); |
| |
| verify(mMockHandler).removeCallbacksAndMessages(null); |
| assertTrue(mController.getActiveAppOps().isEmpty()); |
| } |
| |
| @Test |
| public void noDoubleUpdateOnOpNoted() { |
| mController.setBGHandler(mMockHandler); |
| |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| |
| // Only one post to notify subscribers |
| verify(mMockHandler, times(1)).post(any()); |
| |
| List<AppOpItem> list = mController.getActiveAppOps(); |
| assertEquals(1, list.size()); |
| } |
| |
| @Test |
| public void onDoubleOPNoted_scheduleTwiceForRemoval() { |
| mController.setBGHandler(mMockHandler); |
| |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| |
| // Only one post to notify subscribers |
| verify(mMockHandler, times(2)).scheduleRemoval(any(), anyLong()); |
| } |
| |
| @Test |
| public void testActiveOpNotRemovedAfterNoted() throws InterruptedException { |
| // Replaces the timeout delay with 5 ms |
| AppOpsControllerImpl.H testHandler = mController.new H(mTestableLooper.getLooper()) { |
| @Override |
| public void scheduleRemoval(AppOpItem item, long timeToRemoval) { |
| super.scheduleRemoval(item, 5L); |
| } |
| }; |
| |
| mController.addCallback(new int[]{AppOpsManager.OP_FINE_LOCATION}, mCallback); |
| mController.setBGHandler(testHandler); |
| |
| mController.onOpActiveChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, true); |
| |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| |
| mTestableLooper.processAllMessages(); |
| List<AppOpItem> list = mController.getActiveAppOps(); |
| verify(mCallback).onActiveStateChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, true); |
| |
| // Duplicates are not removed between active and noted |
| assertEquals(2, list.size()); |
| |
| sleep(10L); |
| |
| mTestableLooper.processAllMessages(); |
| |
| verify(mCallback, never()).onActiveStateChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, false); |
| list = mController.getActiveAppOps(); |
| assertEquals(1, list.size()); |
| } |
| |
| @Test |
| public void testNotedNotRemovedAfterActive() { |
| mController.addCallback(new int[]{AppOpsManager.OP_FINE_LOCATION}, mCallback); |
| |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| |
| mController.onOpActiveChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, true); |
| |
| mTestableLooper.processAllMessages(); |
| List<AppOpItem> list = mController.getActiveAppOps(); |
| verify(mCallback).onActiveStateChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, true); |
| |
| // Duplicates are not removed between active and noted |
| assertEquals(2, list.size()); |
| |
| mController.onOpActiveChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, false); |
| |
| mTestableLooper.processAllMessages(); |
| |
| verify(mCallback, never()).onActiveStateChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, false); |
| list = mController.getActiveAppOps(); |
| assertEquals(1, list.size()); |
| } |
| |
| @Test |
| public void testNotedAndActiveOnlyOneCall() { |
| mController.addCallback(new int[]{AppOpsManager.OP_FINE_LOCATION}, mCallback); |
| |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| |
| mController.onOpActiveChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, true); |
| |
| mTestableLooper.processAllMessages(); |
| verify(mCallback).onActiveStateChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, true); |
| } |
| |
| @Test |
| public void testActiveAndNotedOnlyOneCall() { |
| mController.addCallback(new int[]{AppOpsManager.OP_FINE_LOCATION}, mCallback); |
| |
| mController.onOpActiveChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, true); |
| |
| mController.onOpNoted(AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, |
| AppOpsManager.MODE_ALLOWED); |
| |
| mTestableLooper.processAllMessages(); |
| verify(mCallback).onActiveStateChanged( |
| AppOpsManager.OP_FINE_LOCATION, TEST_UID, TEST_PACKAGE_NAME, true); |
| } |
| } |