Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Android UiAutomation.injectInputEvent注入单击事件_Android_Testing_Ui Automation_Robotium - Fatal编程技术网

如何使用Android UiAutomation.injectInputEvent注入单击事件

如何使用Android UiAutomation.injectInputEvent注入单击事件,android,testing,ui-automation,robotium,Android,Testing,Ui Automation,Robotium,我正在安装设备管理员的应用程序中自动测试流。要在大多数设备上激活设备管理员(假设我没有三星提供的企业API),系统会向用户显示一个弹出窗口,用户必须单击“激活”按钮 我正在使用Android和JUnit来驱动我的测试。在正常的测试案例中,用户只能与被测应用程序和流程交互,而不能与出现的任何系统活动交互 声明允许您通过利用,然后允许一个应用程序与其他应用程序交互 所以-以下是我想做的: public class AbcTests extends ActivityInstrumentationTes

我正在安装设备管理员的应用程序中自动测试流。要在大多数设备上激活设备管理员(假设我没有三星提供的企业API),系统会向用户显示一个弹出窗口,用户必须单击“激活”按钮

我正在使用Android和JUnit来驱动我的测试。在正常的测试案例中,用户只能与被测应用程序和流程交互,而不能与出现的任何系统活动交互

声明允许您通过利用,然后允许一个应用程序与其他应用程序交互

所以-以下是我想做的:

public class AbcTests extends ActivityInstrumentationTestCase2<AbcActivity> {

    private Solo mSolo

    @Override
    public void setUp() {
        mSolo = new Solo(getInstrumentation(), getActivity());

    }

    ...

    public void testAbc(){
    
        final UiAutomation automation = getInstrumentation().getUiAutomation();         
        
        MotionEvent motionDown = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_DOWN,
                100,  100, 0);

        automation.injectInputEvent(motionDown, true)
        MotionEvent motionUp = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), KeyEvent.ACTION_UP,
                100, 100, 0);

        automation.injectInputEvent(motionUp, true)
        motionUp.recycle();
        motionDown.recycle();
     }
    
 }
公共类AbcTests扩展了ActivityInstrumentationTestCase2{
私人独奏
@凌驾
公共作废设置(){
mSolo=newsolo(getInstrumentation(),getActivity());
}
...
公共图书馆{
最终UiAutomation=getInstrumentation().getUiAutomation();
MotionEvent motionDown=MotionEvent.Acquire(SystemClock.uptimeMillis()、SystemClock.uptimeMillis()、KeyEvent.ACTION\u DOWN、,
100,  100, 0);
automation.injectInputEvent(motionDown,true)
MotionEvent motionUp=MotionEvent.Acquire(SystemClock.uptimeMillis()、SystemClock.uptimeMillis()、KeyEvent.ACTION\u-UP、,
100, 100, 0);
automation.injectInputEvent(motionUp,true)
motionUp.recycle();
motionDown.recycle();
}
}
当运行此测试时,系统弹出窗口“激活”设备管理员处于活动状态,我只想单击屏幕。为了回答这个问题,我硬编码了100100作为点击位置,但实际上我会点击屏幕右下角,这样我就可以点击按钮了

我没有在屏幕上看到任何单击事件。有人有这方面的经验吗?我想做的事有其他选择吗?据我所知,很少有工具能做到这一点

谢谢

更新
添加了正确答案的
setSource

最终解决了这个问题。我将MotionEvents与单击按钮时发送的两个事件进行了比较,唯一的区别是源。所以,我在两个motionEvents上设置了源代码,它成功了

....
motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
....
motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);
这是这个方法的完整版本

//=========================================================================
//==                        Utility Methods                             ===
//=========================================================================
/**
 * Helper method injects a click event at a point on the active screen via the UiAutomation object.
 * @param x the x position on the screen to inject the click event
 * @param y the y position on the screen to inject the click event
 * @param automation a UiAutomation object rtreived through the current Instrumentation
 */
static void injectClickEvent(float x, float y, UiAutomation automation){
    //A MotionEvent is a type of InputEvent.  
    //The event time must be the current uptime.
    final long eventTime = SystemClock.uptimeMillis();

    //A typical click event triggered by a user click on the touchscreen creates two MotionEvents,
    //first one with the action KeyEvent.ACTION_DOWN and the 2nd with the action KeyEvent.ACTION_UP
    MotionEvent motionDown = MotionEvent.obtain(eventTime, eventTime, KeyEvent.ACTION_DOWN,
            x,  y, 0); 
    //We must set the source of the MotionEvent or the click doesn't work.
    motionDown.setSource(InputDevice.SOURCE_TOUCHSCREEN);
    automation.injectInputEvent(motionDown, true);
    MotionEvent motionUp = MotionEvent.obtain(eventTime, eventTime, KeyEvent.ACTION_UP,
            x, y, 0);
    motionUp.setSource(InputDevice.SOURCE_TOUCHSCREEN);
    automation.injectInputEvent(motionUp, true);
    //Recycle our events back to the system pool.
    motionUp.recycle();
    motionDown.recycle();
}

我喜欢您的javadoc和Comments,我认为您应该删除您在中编辑的setSource()调用。原始问题应演示问题,答案显示解决方案。事实上,比较这两种方法会导致混淆,因为它们都有效。