2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.systemui.statusbar;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
23 import android.app.AppOpsManager;
24 import android.os.Handler;
25 import android.os.Looper;
26 import android.support.test.filters.SmallTest;
27 import android.testing.AndroidTestingRunner;
28 import android.testing.TestableLooper;
30 import com.android.systemui.ForegroundServiceController;
31 import com.android.systemui.SysuiTestCase;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
40 @RunWith(AndroidTestingRunner.class)
41 @TestableLooper.RunWithLooper
42 public class AppOpsListenerTest extends SysuiTestCase {
43 private static final String TEST_PACKAGE_NAME = "test";
44 private static final int TEST_UID = 0;
46 @Mock private NotificationPresenter mPresenter;
47 @Mock private AppOpsManager mAppOpsManager;
50 @Mock private NotificationEntryManager mEntryManager;
51 @Mock private ForegroundServiceController mFsc;
53 private AppOpsListener mListener;
57 MockitoAnnotations.initMocks(this);
58 mDependency.injectTestDependency(NotificationEntryManager.class, mEntryManager);
59 mDependency.injectTestDependency(ForegroundServiceController.class, mFsc);
60 getContext().addMockSystemService(AppOpsManager.class, mAppOpsManager);
61 when(mPresenter.getHandler()).thenReturn(Handler.createAsync(Looper.myLooper()));
63 mListener = new AppOpsListener(mContext);
67 public void testOnlyListenForFewOps() {
68 mListener.setUpWithPresenter(mPresenter, mEntryManager);
70 verify(mAppOpsManager, times(1)).startWatchingActive(AppOpsListener.OPS, mListener);
74 public void testStopListening() {
76 verify(mAppOpsManager, times(1)).stopWatchingActive(mListener);
80 public void testInformEntryMgrOnAppOpsChange() {
81 mListener.setUpWithPresenter(mPresenter, mEntryManager);
82 mListener.onOpActiveChanged(
83 AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true);
84 TestableLooper.get(this).processAllMessages();
85 verify(mEntryManager, times(1)).updateNotificationsForAppOp(
86 AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true);
90 public void testInformFscOnAppOpsChange() {
91 mListener.setUpWithPresenter(mPresenter, mEntryManager);
92 mListener.onOpActiveChanged(
93 AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true);
94 TestableLooper.get(this).processAllMessages();
95 verify(mFsc, times(1)).onAppOpChanged(
96 AppOpsManager.OP_RECORD_AUDIO, TEST_UID, TEST_PACKAGE_NAME, true);