如何在 Android 中对手势进行单元测试?

发布于 2024-09-28 21:08:16 字数 68 浏览 5 评论 0原文

我想在 Android 中对屏幕上向左/向右滑动的操作进行单元测试,但我找不到任何相关文档。有人可以帮忙吗?还可以做到吗?

I want to unit test a swipe left/right action on the screen in Android but I haven't been able to find any documentation on it. Can anyone lend a hand? Can it even be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一瞬间的火花 2024-10-05 21:08:16

通常,触摸将执行一个操作,您将测试该操作,而不是触摸本身。

Generally the touch will perform an action and you will be testing the action, not the touch itself.

带上头具痛哭 2024-10-05 21:08:16

我发现你的问题试图学习如何对完全相同类型的手势做完全相同的事情。我能够使用以下方法运行非常纯粹的单元测试。换句话说,单元测试 View.OnTouchListener.onTouch(View v, Event event) 函数。

以下是我如何“模拟”ViewEvent 以便运行 onTouch

import android.os.Looper;
import android.test.mock.MockContext;
import android.view.MotionEvent;
import android.view.ViewStub;
import junit.framework.TestCase;

public class SwipeDetectorTest extends TestCase {
    public void testOnTouch() throws Exception {
         class MyMockContext extends MockContext {
            public Looper getMainLooper() {
                return Looper.getMainLooper();
            }
        }
        MyMockContext context = new MyMockContext();
        View v = new ViewStub(context);
        int x = 0; int y = 0;

        // simulated down event
        MotionEvent event = 
            MotionEvent.obtain(1, 0, MotionEvent.ACTION_DOWN, x, y, 0);
        SwipeDetector swipeDetector = new SwipeDetector();
        assertFalse(swipeDetector.onTouch(v, event));

        x = 30; y = 0;
        event = 
           MotionEvent.obtain(1, 1, MotionEvent.ACTION_MOVE, x, y, 0);
        boolean action = swipeDetector.onTouch(v, event);
        assertTrue(action);

        boolean result = swipeDetector.getSwipeHorizontal()
            .equals(SwipeDetector.Action.LeftToRight);
        assertTrue(result);
    }
}

I found your question trying to learn how to do the exact same thing for the exact same type of gesture. I was able to run pretty pure unit tests using the following approach. In other words unit test View.OnTouchListener.onTouch(View v, Event event) function.

Here is how I was able to "mock" the View and Event in order to run onTouch

import android.os.Looper;
import android.test.mock.MockContext;
import android.view.MotionEvent;
import android.view.ViewStub;
import junit.framework.TestCase;

public class SwipeDetectorTest extends TestCase {
    public void testOnTouch() throws Exception {
         class MyMockContext extends MockContext {
            public Looper getMainLooper() {
                return Looper.getMainLooper();
            }
        }
        MyMockContext context = new MyMockContext();
        View v = new ViewStub(context);
        int x = 0; int y = 0;

        // simulated down event
        MotionEvent event = 
            MotionEvent.obtain(1, 0, MotionEvent.ACTION_DOWN, x, y, 0);
        SwipeDetector swipeDetector = new SwipeDetector();
        assertFalse(swipeDetector.onTouch(v, event));

        x = 30; y = 0;
        event = 
           MotionEvent.obtain(1, 1, MotionEvent.ACTION_MOVE, x, y, 0);
        boolean action = swipeDetector.onTouch(v, event);
        assertTrue(action);

        boolean result = swipeDetector.getSwipeHorizontal()
            .equals(SwipeDetector.Action.LeftToRight);
        assertTrue(result);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文