AppOpsActiveWatcherTest

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();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值