Home | History | Annotate | Download | only in statusbar
      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.statusbar;
     18 
     19 import static org.mockito.Mockito.times;
     20 import static org.mockito.Mockito.verify;
     21 import static org.mockito.Mockito.when;
     22 
     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;
     29 
     30 import com.android.systemui.ForegroundServiceController;
     31 import com.android.systemui.SysuiTestCase;
     32 
     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;
     38 
     39 @SmallTest
     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;
     45 
     46     @Mock private NotificationPresenter mPresenter;
     47     @Mock private AppOpsManager mAppOpsManager;
     48 
     49     // Dependency mocks:
     50     @Mock private NotificationEntryManager mEntryManager;
     51     @Mock private ForegroundServiceController mFsc;
     52 
     53     private AppOpsListener mListener;
     54 
     55     @Before
     56     public void setUp() {
     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()));
     62 
     63         mListener = new AppOpsListener(mContext);
     64     }
     65 
     66     @Test
     67     public void testOnlyListenForFewOps() {
     68         mListener.setUpWithPresenter(mPresenter, mEntryManager);
     69 
     70         verify(mAppOpsManager, times(1)).startWatchingActive(AppOpsListener.OPS, mListener);
     71     }
     72 
     73     @Test
     74     public void testStopListening() {
     75         mListener.destroy();
     76         verify(mAppOpsManager, times(1)).stopWatchingActive(mListener);
     77     }
     78 
     79     @Test
     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);
     87     }
     88 
     89     @Test
     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);
     97     }
     98 }
     99