如何发送触摸到ActivityInstrumentationTestCase2测试时,修复INJECT_EVENT许可例外 [英] How to fix INJECT_EVENT permission exception when sending touches to an ActivityInstrumentationTestCase2 test

查看:177
本文介绍了如何发送触摸到ActivityInstrumentationTestCase2测试时,修复INJECT_EVENT许可例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然有显示,这样的事情应该工作的例子很多,下面code失败。
这code生活在一个与真实的项目相关的测试项目。

 公共类MyTest的扩展ActivityInstrumentationTestCase2< MyActivity> {    公共MyTest的(字符串名称)
    {
        超(com.mypackage.activities,MyActivity.class);
        的setName(名);
    }    公共无效testTap()抛出的Throwable
    {
        //根据通过的JavaDoc需要MotionEvent.obtain
        长时间的停机= SystemClock.uptimeMillis();
        长EVENTTIME = SystemClock.uptimeMillis();        仪器I = getInstrumentation();        //设置的信息需要我们向下和向上的活动创造一个水龙头
        MotionEvent downEvent = MotionEvent.obtain(停机时间,EVENTTIME,MotionEvent.ACTION_DOWN,300,20,0);
        MotionEvent upEvent = MotionEvent.obtain(停机时间,EVENTTIME,MotionEvent.ACTION_UP,300,20,0);        //发送下/上点击事件
        i.sendPointerSync(downEvent);
        i.sendPointerSync(upEvent);        //延时看到的结果
        Thread.currentThread()睡眠(3000)。
    }}

这将引发
    java.lang.SecurityException异常:注射到另一个应用程序需要INJECT_EVENTS权限
在i.sendPointerSync()调用。
我也曾尝试view.onTouchEvent(事件)和view.dispatchTouchEvent(事件)都没有成功。

我能想到的唯一的事情是,如果被测试,其中这是在项目工作现场的例子。这似乎是不好的,因为该建议是测试分离到不同的项目,并能够从生成服务器的东西,如运行它们:

  ADB -e外壳上午仪器-w com.mypackage.activities.test / android.test.InstrumentationTestRunner


解决方案

这可能意味着你的主要项目,测试项目或模拟器版本不同步。

Although there are many examples showing that something like this should work, the following code fails. This code lives in a test project that is associated with the real project.

public class MyTest extends ActivityInstrumentationTestCase2<MyActivity> {

    public MyTest(String name)
    {
        super("com.mypackage.activities", MyActivity.class);
        setName(name);
    }

    public void testTap() throws Throwable
    {
        //Required by MotionEvent.obtain according to JavaDocs
        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis();

        Instrumentation i = getInstrumentation();

        //Setup the info needed for our down and up events to create a tap
        MotionEvent downEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, 300, 20, 0);
        MotionEvent upEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, 300, 20, 0);

        //Send the down/up tap event
        i.sendPointerSync(downEvent);
        i.sendPointerSync(upEvent);

        //Delay to see the results
        Thread.currentThread().sleep(3000);
    }

}

This throws a java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission on the i.sendPointerSync() calls. I have also tried view.onTouchEvent(event) and view.dispatchTouchEvent(event) without success.

The only thing I can think of is if the examples where this is working live in the project being tested. This seems bad because the recommendation is to separate tests to a different project and be able to run them from a build server with something like:

adb -e shell am instrument -w com.mypackage.activities.test/android.test.InstrumentationTestRunner

解决方案

This probably means that your main project, your test project or your emulator versions are out of sync.

这篇关于如何发送触摸到ActivityInstrumentationTestCase2测试时,修复INJECT_EVENT许可例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆