按键按下和按键按下事件之间的测试

发布于 2024-11-28 20:58:01 字数 1397 浏览 2 评论 0原文

是否可以在应用程序的按键按下和按键按下事件处理程序之间运行测试?我开发 2D 街机游戏,gameView 内的 gameThread 处理按键。我编写了 JUnit Android 测试项目。 我读过有关 this.sendKeys() 的内容,但似乎该方法发送键向上,然后向下发送信号。

细节: 我将同时使用 - touch &非触摸交互。但现在我正在测试

非触摸。所以我写道:

“protected void setUp() throws Exception {

    super.setUp();
    setActivityInitialTouchMode(false);
    _activity = (GameTemplate) getActivity();
    <...>

}”

作为测试初始化​​。

我编写了测试以确保 keyUp 事件处理良好:

`public void testReturnState() {

    this.sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
    _instrumentation.waitForIdleSync();
assertTrue(_logic.getSerp().state().equals(SerpentState.SERP_MOVES_FORWARD));

}`

它工作正常,但我编写了另一个测试:

`public void testKeysSimple() {

    this.sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
    assertTrue(_logic.getSerp().state().equals(SerpentState.SERP_MOVES_LEFT));

}`

它失败了,我认为它是由于以下两个原因之一而发生的:

1)程序还没有处理keyDown事件;

2)程序已经处理了 keyDown 和 keyUp 事件

我是测试新手,也许所有的事情都可以用另一种方式更容易地完成。请帮我。

我尝试在测试中添加重复:

public void testKeysUpDown() {  
    int t = TSet.skips;
    this.sendRepeatedKeys(TSet.keyRepeats,KeyEvent.KEYCODE_DPAD_LEFT);
    while(t>0 && !_logic.getSerp().state().equals(SerpentState.SERP_MOVES_LEFT)) t--;
    assertTrue(t>0);
}

但它也失败了。

Is there a possibility to run test between application's key down and key up event handlers? I develope 2D arcade game, gameThread inside gameView handles key press. I wrote JUnit Android Testing Project.
I have read about this.sendKeys(), but it seems, that this method sends key up, then down signals.

DETAILS:
I'll be using both - touch & non-touch interaction. But now I'm testing

non-touch. So I wrote:

`protected void setUp() throws Exception {

    super.setUp();
    setActivityInitialTouchMode(false);
    _activity = (GameTemplate) getActivity();
    <...>

}`

as test initialize.

I wrote test to ensure, that keyUp event processed well:

`public void testReturnState() {

    this.sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
    _instrumentation.waitForIdleSync();
assertTrue(_logic.getSerp().state().equals(SerpentState.SERP_MOVES_FORWARD));

}`

It works ok, but I wrote another test:

`public void testKeysSimple() {

    this.sendKeys(KeyEvent.KEYCODE_DPAD_LEFT);
    assertTrue(_logic.getSerp().state().equals(SerpentState.SERP_MOVES_LEFT));

}`

It fails, I think it happens because of one of two reasons:

1) program hasn't handled keyDown event yet;

2) program has handled both keyDown and keyUp events

I'm new to testing, maybe all could be done much easier in another way. Please, help me.

I tried to add repeats in test:

public void testKeysUpDown() {  
    int t = TSet.skips;
    this.sendRepeatedKeys(TSet.keyRepeats,KeyEvent.KEYCODE_DPAD_LEFT);
    while(t>0 && !_logic.getSerp().state().equals(SerpentState.SERP_MOVES_LEFT)) t--;
    assertTrue(t>0);
}

but it also fails.

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

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

发布评论

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

评论(2

水溶 2024-12-05 20:58:01

您需要 Instrumentation#sendKeySync完全控制发送的事件。

You need Instrumentation#sendKeySync to have full control over the event sent.

美人骨 2024-12-05 20:58:01

如果您使用基于触摸的交互,则可以使用 MotionEvent.ACTION_MOVE。这是当用户触摸屏幕并拖动而没有抬起手指时的情况。

代码片段

public boolean onTouch(View v, MotionEvent rawEvent) {

    switch (rawEvent.getAction() & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_DOWN:
        // First touch on the screen
        break;

        case MotionEvent.ACTION_POINTER_DOWN:
        // Multi-touch on the screen
        break;

        case MotionEvent.ACTION_UP:
        // Lift from the screen
        break;

        case MotionEvent.ACTION_POINTER_UP:
        // Multi-Lift from the screen
        break;

        case MotionEvent.ACTION_MOVE:
        // Drag on the screen
        break;
    }

}

Shash

If you are using touch based interaction, then you can use MotionEvent.ACTION_MOVE. This is when user has touched the screen and dragging without lifting the finger up.

Code Snippet

public boolean onTouch(View v, MotionEvent rawEvent) {

    switch (rawEvent.getAction() & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_DOWN:
        // First touch on the screen
        break;

        case MotionEvent.ACTION_POINTER_DOWN:
        // Multi-touch on the screen
        break;

        case MotionEvent.ACTION_UP:
        // Lift from the screen
        break;

        case MotionEvent.ACTION_POINTER_UP:
        // Multi-Lift from the screen
        break;

        case MotionEvent.ACTION_MOVE:
        // Drag on the screen
        break;
    }

}

Shash

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