Reputation: 3995
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?
I would like to do do a .setQuery("Foo", true)
or equivalent to test search in my app. How can that be done?
Upvotes: 1
Views: 915
Reputation: 1271
According to this documentation
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().
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.
Upvotes: 2
Reputation: 9061
I think you can use monkeyrunner to do that : http://developer.android.com/tools/help/monkeyrunner_concepts.html
Upvotes: 1