frameworks/base/services/tests/servicestests/src/com/android/server/appop/AppOpsActiveWatcherTest.java
@Test
public void a1() {
final CountDownLatch count = new CountDownLatch(1);
final OnOpActiveChangedListener listener =
(op, uid, packageName, active) -> count.countDown();
// Start watching active ops
final AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class);
appOpsManager.startWatchingActive(new String[]{AppOpsManager.OPSTR_CAMERA,
AppOpsManager.OPSTR_RECORD_AUDIO}, getContext().getMainExecutor(), listener);
// Start the op
appOpsManager.startOp(AppOpsManager.OP_CAMERA);
try {
assertTrue(count.await(2, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Test
public void a2() {
final AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class);
appOpsManager.startOp(AppOpsManager.OP_CAMERA,Process.myUid(), getContext().getOpPackageName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// Verify that the op is active
assertThat(appOpsManager.isOperationActive(AppOpsManager.OP_CAMERA,
Process.myUid(), getContext().getPackageName())).isTrue();
assertThat(appOpsManager.isOperationActive(AppOpsManager.OP_CAMERA,
Process.myUid(), getContext().getPackageName()+"123")).isFalse();
}
@Test
public void a3() {
final CountDownLatch count = new CountDownLatch(2);
final OnOpActiveChangedListener listener =
(op, uid, packageName, active) -> {
count.countDown() ;
};
// Start watching active ops
final AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class);
appOpsManager.startWatchingActive(new String[]{AppOpsManager.OPSTR_CAMERA,
AppOpsManager.OPSTR_RECORD_AUDIO}, getContext().getMainExecutor(), listener);
// Start the op
appOpsManager.startOp(AppOpsManager.OP_CAMERA);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
appOpsManager.finishOp(AppOpsManager.OP_CAMERA);
try {
assertTrue(count.await(1, TimeUnit.SECONDS));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@Test
public void a4() {
final OnOpActiveChangedListener listener =
(op, uid, packageName, active) -> {
};
// Start watching active ops
final AppOpsManager appOpsManager = getContext().getSystemService(AppOpsManager.class);
appOpsManager.startWatchingActive(new String[]{AppOpsManager.OPSTR_CAMERA,
AppOpsManager.OPSTR_RECORD_AUDIO}, getContext().getMainExecutor(), listener);
// Start the op
appOpsManager.startOp(AppOpsManager.OP_CAMERA);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
appOpsManager.finishOp(AppOpsManager.OP_CAMERA);
// Verify that the op is active
assertThat(appOpsManager.isOperationActive(OP_CAMERA,
Process.myUid(), getContext().getPackageName())).isFalse();
}