如何能在动作条一个搜索查看测试? [英] How can a SearchView in the ActionBar be tested?

查看:138
本文介绍了如何能在动作条一个搜索查看测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何例子或如何使用Android测试,以取得在菜单进行测试搜索查看JavaDoc中提供帮助。有没有办法做到这一点?

I can't find any examples or help in the JavaDocs on how to use an Android test to get at the SearchView in the Menu for testing. Is there a way to do it?

我愿做一做 .setQuery(富,真)或等同于我的应用程序测试搜索。那怎么可以呢?

I would like to do do a .setQuery("Foo", true) or equivalent to test search in my app. How can that be done?

推荐答案

根据这一文件

注:虽然你的崩溃动作视图是可选的,我们建议
  你总是崩溃你的动作视图如果它包含搜索查看。
  另外要注意,有些设备提供一个专门的搜索按钮,
  你应该扩大搜索行动视图如果用户presses的
  搜索按钮。简单地覆盖您的活动的的onkeyup()回调
  法,监听KEY code_SEARCH事件,然后调用
  expandActionView()。

Note: Although collapsing your action view is optional, we recommend that you always collapse your action view if it includes SearchView. Also be aware that some devices provide a dedicated SEARCH button and you should expand your search action view if the user presses the SEARCH button. Simply override your activity's onKeyUp() callback method, listen for the KEYCODE_SEARCH event, then call expandActionView().

如果您已经扩展为重点code_SEARCH事件检索算法观点,那么测试应该通过仪器注入事件是可能的。

If you have expanded the serach view for the KEYCODE_SEARCH event,then testing should be possible by using the instrumentation to inject events.

public void testSearch() { 
   Instrumentation instrumentation = getInstrumentation(); 
   instrumentation.sendKeyDownUpSync(KeyEvent.KEYCODE_SEARCH); 
   instrumentation.sendCharacterSync(KeyEvent.KEYCODE_F); 
   instrumentation.sendCharacterSync(KeyEvent.KEYCODE_O); 
   instrumentation.sendCharacterSync(KeyEvent.KEYCODE_O);
   //Assert here for whatever you want. 
}

请确保您有权限注入你的清单。

Make sure you have the inject permission in your manifest.

这篇关于如何能在动作条一个搜索查看测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆