I'm attempting to write unit tests for a very simple app. Here is the menu_main.xml
<menu xmlns:android="http://ift.tt/nIICcg"
xmlns:app="http://ift.tt/GEGVYd"
xmlns:tools="http://ift.tt/LrGmb4" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />
<item android:id="@+id/action_refresh" android:title="@string/action_refresh"
android:orderInCategory="100" app:showAsAction="never" />
App runs fine, both items show up. MainActivity.xml can clearly find them because
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
works just fine.
I have unit tests that descend from ActivityInstrumentationTestCase2, one of which checks the menu_main.xml.
public void testSettingsMenuItemExists() {
Object view = activity.findViewById(R.id.action_settings);
assertNotNull("Settings menu item does not exist", view);
}
R.id.action_settings obviously exists because the code compiles, but when the tests run, they do not find item. They do find elements from activity_main.xml. I've tried getting the action bar via activity.getActionBar(), it is null. I thought maybe the action bar was owned by the application, not the activity, but Applications don't even have getActionBar().
How do I find menu items so I can test them in unit tests?
Aucun commentaire:
Enregistrer un commentaire