Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [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.server.appops; |
| 18 | |
| 19 | import android.Manifest; |
| 20 | import android.app.AppOpsManager; |
| 21 | import android.app.AppOpsManager.OnOpNotedListener; |
| 22 | import android.content.Context; |
| 23 | import android.os.Process; |
| 24 | import androidx.test.InstrumentationRegistry; |
| 25 | import androidx.test.runner.AndroidJUnit4; |
| 26 | import androidx.test.filters.SmallTest; |
| 27 | import org.junit.Test; |
| 28 | import org.junit.runner.RunWith; |
| 29 | import org.mockito.InOrder; |
| 30 | |
| 31 | |
| 32 | import static org.junit.Assert.fail; |
| 33 | import static org.mockito.Mockito.eq; |
| 34 | import static org.mockito.Mockito.inOrder; |
| 35 | import static org.mockito.Mockito.mock; |
| 36 | import static org.mockito.Mockito.timeout; |
| 37 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 38 | |
| 39 | /** |
| 40 | * Tests watching noted ops. |
| 41 | */ |
| 42 | @SmallTest |
| 43 | @RunWith(AndroidJUnit4.class) |
| 44 | public class AppOpsNotedWatcherTest { |
| 45 | |
| 46 | private static final long NOTIFICATION_TIMEOUT_MILLIS = 5000; |
| 47 | |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 48 | @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 Kozynski | 0b4592c | 2018-12-20 12:58:45 -0500 | [diff] [blame] | 55 | appOpsManager.startWatchingNoted(new int[]{AppOpsManager.OP_FINE_LOCATION, |
| 56 | AppOpsManager.OP_CAMERA}, listener); |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 57 | |
| 58 | // Note some ops |
Fabian Kozynski | 0b4592c | 2018-12-20 12:58:45 -0500 | [diff] [blame] | 59 | appOpsManager.noteOp(AppOpsManager.OP_FINE_LOCATION, Process.myUid(), |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 60 | getContext().getPackageName()); |
Fabian Kozynski | 0b4592c | 2018-12-20 12:58:45 -0500 | [diff] [blame] | 61 | appOpsManager.noteOp(AppOpsManager.OP_CAMERA, Process.myUid(), |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 62 | 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 Kozynski | 0b4592c | 2018-12-20 12:58:45 -0500 | [diff] [blame] | 67 | .times(1)).onOpNoted(eq(AppOpsManager.OP_FINE_LOCATION), |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 68 | eq(Process.myUid()), eq(getContext().getPackageName()), |
| 69 | eq(AppOpsManager.MODE_ALLOWED)); |
| 70 | inOrder.verify(listener, timeout(NOTIFICATION_TIMEOUT_MILLIS) |
Fabian Kozynski | 0b4592c | 2018-12-20 12:58:45 -0500 | [diff] [blame] | 71 | .times(1)).onOpNoted(eq(AppOpsManager.OP_CAMERA), |
Svet Ganov | b3d2ae2 | 2018-12-17 22:06:15 -0800 | [diff] [blame] | 72 | 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 | } |