android - 如何使 MotionEvent 缩放?

标签 android testing instrumentation

我有一个工具想用来测试我的应用程序,但我似乎无法正确模拟捏合缩放。这是我当前的代码。其要点是:同时按下手指 1 和手指 2,将它们彼此靠近,然后松开它们。

private void performZoomTest(int numUpdates) {
    long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis();    

    float x1 = 0;
    float y1 = 0;

    Display main = activity.getWindowManager().getDefaultDisplay();

    float x2 = main.getWidth();
    float y2 = main.getHeight();

    float xstep = x2 / (2 * numUpdates);
    float ystep = y2 / (2 * numUpdates);

    int id1 = 0 << MotionEvent.ACTION_POINTER_ID_SHIFT;
    int id2 = 1 << MotionEvent.ACTION_POINTER_ID_SHIFT;

    MotionEvent event = 
        MotionEvent.obtain(downTime, eventTime, 
                   MotionEvent.ACTION_DOWN + id1,
                   x1, y1, 0);
    sendPointerSync(event);
    event = 
        MotionEvent.obtain(downTime, eventTime, 
                   MotionEvent.ACTION_DOWN + id2,
                   x2, y2, 0);
    sendPointerSync(event);

    waitForIdleSync();

    for (int i = 0; i < numUpdates; i++) {
        eventTime = SystemClock.uptimeMillis();

        Log.i("",Integer.toString(i));
        x1 += xstep;
        y1 += ystep;

        x2 -= xstep;
        y2 -= ystep;

        event = 
            MotionEvent.obtain(downTime, eventTime, 
                       MotionEvent.ACTION_MOVE + id1,
                       x1, y1, 0);
        Log.i("id1", Integer.toString(MotionEvent.ACTION_MOVE + id1));
        sendPointerSync(event);
        event = 
            MotionEvent.obtain(downTime, eventTime, 
                       MotionEvent.ACTION_MOVE + id2, 
                       x2, y2, 0);
        Log.i("id1", Integer.toString(MotionEvent.ACTION_MOVE + id2));
        sendPointerSync(event);

        waitForIdleSync();
    } 

    eventTime = SystemClock.uptimeMillis();

    event = 
        MotionEvent.obtain(downTime, eventTime, 
                   MotionEvent.ACTION_UP + id1, 
                   x1, y1, 0);
    sendPointerSync(event);
    event = 
        MotionEvent.obtain(downTime, eventTime, 
                   MotionEvent.ACTION_UP + id2, 
                   x2, y2, 0);
    sendPointerSync(event);

    waitForIdleSync();
}

结果似乎是应用程序将这两个事件视为不同的事件,并且没有将它们注册为单个事件。有更好的方法吗?

最佳答案

看这里:https://stackoverflow.com/questions/522312/best-practices-for-unit-testing-android-apps

我认为使用标准 Android 框架无法进行多点触控单元测试。

关于android - 如何使 MotionEvent 缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6311031/

相关文章:

java - JUnit 测试不是 Spring Controller 中的触发方法

android - Android 操作系统代码库中的 ActiveSync/MS 交换源在哪里

android - SharedPreferences 不在具有自己进程的服务内 apply() 或 commit()

android - 使用 Graphview 在 android 中绘制图形?

android - 如何使用 Jetpack Compose 在文本字段中添加国家代码前缀

testing - 在 wxPython 应用程序的自动化测试中处理模态对话框

javascript - (window.beforeEach || window.setup) 不是函数

c - 有没有可靠的方法来获取 Valgrind 工具中包装函数的调用位置?

android - 获取资源编号 0xffffffff 的名称时没有已知包

iOS 应用程序在启动时崩溃