按键按下和按键按下事件之间的测试
是否可以在应用程序的按键按下和按键按下事件处理程序之间运行测试?我开发 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要 Instrumentation#sendKeySync完全控制发送的事件。
You need Instrumentation#sendKeySync to have full control over the event sent.
如果您使用基于触摸的交互,则可以使用 MotionEvent.ACTION_MOVE。这是当用户触摸屏幕并拖动而没有抬起手指时的情况。
代码片段
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
Shash