blob: 96f329b9161e6a285a3fa20d4644555fd4356757 [file] [log] [blame]
Svet Ganovb3d2ae22018-12-17 22:06:15 -08001/*
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.server.appops;
18
19import android.Manifest;
20import android.app.AppOpsManager;
21import android.app.AppOpsManager.OnOpNotedListener;
22import android.content.Context;
23import android.os.Process;
24import androidx.test.InstrumentationRegistry;
25import androidx.test.runner.AndroidJUnit4;
26import androidx.test.filters.SmallTest;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29import org.mockito.InOrder;
30
31
32import static org.junit.Assert.fail;
33import static org.mockito.Mockito.eq;
34import static org.mockito.Mockito.inOrder;
35import static org.mockito.Mockito.mock;
36import static org.mockito.Mockito.timeout;
37import static org.mockito.Mockito.verifyNoMoreInteractions;
38
39/**
40 * Tests watching noted ops.
41 */
42@SmallTest
43@RunWith(AndroidJUnit4.class)
44public class AppOpsNotedWatcherTest {
45
46 private static final long NOTIFICATION_TIMEOUT_MILLIS = 5000;
47
Svet Ganovb3d2ae22018-12-17 22:06:15 -080048 @Test
49 public void testWatchNotedOps() {
50 // Create a mock listener
51 final OnOpNotedListener listener = mock(OnOpNotedListener.class);
52
53 // Start watching noted ops
54 final AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class);
Fabian Kozynski0b4592c2018-12-20 12:58:45 -050055 appOpsManager.startWatchingNoted(new int[]{AppOpsManager.OP_FINE_LOCATION,
56 AppOpsManager.OP_CAMERA}, listener);
Svet Ganovb3d2ae22018-12-17 22:06:15 -080057
58 // Note some ops
Fabian Kozynski0b4592c2018-12-20 12:58:45 -050059 appOpsManager.noteOp(AppOpsManager.OP_FINE_LOCATION, Process.myUid(),
Svet Ganovb3d2ae22018-12-17 22:06:15 -080060 getContext().getPackageName());
Fabian Kozynski0b4592c2018-12-20 12:58:45 -050061 appOpsManager.noteOp(AppOpsManager.OP_CAMERA, Process.myUid(),
Svet Ganovb3d2ae22018-12-17 22:06:15 -080062 getContext().getPackageName());
63
64 // Verify that we got called for the ops being noted
65 final InOrder inOrder = inOrder(listener);
66 inOrder.verify(listener, timeout(NOTIFICATION_TIMEOUT_MILLIS)
Fabian Kozynski0b4592c2018-12-20 12:58:45 -050067 .times(1)).onOpNoted(eq(AppOpsManager.OP_FINE_LOCATION),
Svet Ganovb3d2ae22018-12-17 22:06:15 -080068 eq(Process.myUid()), eq(getContext().getPackageName()),
69 eq(AppOpsManager.MODE_ALLOWED));
70 inOrder.verify(listener, timeout(NOTIFICATION_TIMEOUT_MILLIS)
Fabian Kozynski0b4592c2018-12-20 12:58:45 -050071 .times(1)).onOpNoted(eq(AppOpsManager.OP_CAMERA),
Svet Ganovb3d2ae22018-12-17 22:06:15 -080072 eq(Process.myUid()), eq(getContext().getPackageName()),
73 eq(AppOpsManager.MODE_ALLOWED));
74
75 // Stop watching
76 appOpsManager.stopWatchingNoted(listener);
77
78 // This should be the only two callbacks we got
79 verifyNoMoreInteractions(listener);
80 }
81
82 private static Context getContext() {
83 return InstrumentationRegistry.getContext();
84 }
85}