在质量保证的过程中,人无疑是最重要的,没有什么比一次性写出高质量代码就有效的了。但事实是即使有良好的架构设计,辅之于单元测试和代码评审等一些有效实践,仍然有些BUG成为漏网之鱼,更何况很多团队这些工作做得并到位。单一工具和方法很难包医百病,但各种方法和工具综合起来使用的效果就大不一样了。
前段时间一位同事开发了一个GUI自动测试工具,我们把它用于BUG重现和压力测试中,取得了不错的效果。这里介绍一下DirectFB里面事件录制和重放的方法:
获得键盘设备:
dfb_input_enumerate_devices ((InputDeviceCallback)device_callback,
&context->keyboard_device, DICAPS_KEYS);
&context->keyboard_device, DICAPS_KEYS);
获得鼠标或触摸屏设备:
dfb_input_enumerate_devices ((InputDeviceCallback)device_callback,
&context->mouse_device, DICAPS_AXES | DICAPS_BUTTONS);
&context->mouse_device, DICAPS_AXES | DICAPS_BUTTONS);
向设备注册事件监听函数:
dfb_input_attach (context->mouse_device,
input_device_listener, context, &context->mouse_reaction);
dfb_input_attach (context->keyboard_device,
input_device_listener, context, &context->keyboard_reaction);
input_device_listener, context, &context->mouse_reaction);
dfb_input_attach (context->keyboard_device,
input_device_listener, context, &context->keyboard_reaction);
事件监听函数:
static ReactionResult input_device_listener (const void *msg_data, void *ctx)
...{
DFBContext *context = (DFBContext*)ctx;
DFBInputEvent *event = (DFBInputEvent*)msg_data;
event->locks = 0;
event->flags &= ~DIEF_LOCKS;
if (fwrite (msg_data, sizeof (DFBInputEvent), 1, context->file) != 1)
...{
printf ("[%s]: fwrite Error errno = %d ", __func__, errno);
g_main_loop_quit (context->loop);
}
fflush(context->file);
return RS_OK;
}
...{
DFBContext *context = (DFBContext*)ctx;
DFBInputEvent *event = (DFBInputEvent*)msg_data;
event->locks = 0;
event->flags &= ~DIEF_LOCKS;
if (fwrite (msg_data, sizeof (DFBInputEvent), 1, context->file) != 1)
...{
printf ("[%s]: fwrite Error errno = %d ", __func__, errno);
g_main_loop_quit (context->loop);
}
fflush(context->file);
return RS_OK;
}
事件重放函数:
static gboolean replay_one_event (gpointer user_data)
...{
DFBContext *context = (DFBContext*)user_data;
off_t cur = 0;
DFBInputEvent event = context->event;
if(event.type == DIET_KEYPRESS || event.type == DIET_KEYRELEASE)
...{
dfb_input_dispatch (context->keyboard_device, &event);
}
else
...{
dfb_input_dispatch (context->mouse_device, &event);
}
if (fread (&context->event, sizeof (DFBInputEvent), 1, context->file) == 1)
...{
guint ms = (context->event.timestamp.tv_sec - event.timestamp.tv_sec) * 1000
+ (context->event.timestamp.tv_usec - event.timestamp.tv_usec)/1000;
g_timeout_add (ms, replay_one_event, user_data);
}
else
...{
g_main_loop_quit (context->loop);
}
return FALSE;
}
...{
DFBContext *context = (DFBContext*)user_data;
off_t cur = 0;
DFBInputEvent event = context->event;
if(event.type == DIET_KEYPRESS || event.type == DIET_KEYRELEASE)
...{
dfb_input_dispatch (context->keyboard_device, &event);
}
else
...{
dfb_input_dispatch (context->mouse_device, &event);
}
if (fread (&context->event, sizeof (DFBInputEvent), 1, context->file) == 1)
...{
guint ms = (context->event.timestamp.tv_sec - event.timestamp.tv_sec) * 1000
+ (context->event.timestamp.tv_usec - event.timestamp.tv_usec)/1000;
g_timeout_add (ms, replay_one_event, user_data);
}
else
...{
g_main_loop_quit (context->loop);
}
return FALSE;
}
注意:DirectFB中的笔点事件是以相对坐标方式表示的,所以要保证重放时光标在同样的初始位置。如果DirectFB以多进程的方式运行,这个程序可以是一个独立的进程,否则要放到应用程序的进程中才行。
文章来源于领测软件测试网 https://www.ltesting.net/