mercredi 28 janvier 2015

Using ActivityInstrumentationTestCase2 to select a menu item?

I am trying to write test cases for my main activity. All my main activity shows right now is a list view, and I was able to write tests to start the activity and make sure the list view was created by doing this:



public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity>{
private MainActivity mainActivity;
private ListView mListView;

public MainActivityTest(){
super("MainActivityTest", MainActivity.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();
mainActivity = this.getActivity();
mListView = (ListView) mainActivity.findViewById(R.id.prescriptionListView);
}

public void testPreconditions(){
assertNotNull(mListView);
}
}


What I would like to do next is open the options menu and select an item. When that item is selected, it should create an intent and start a new activity. However, I can't figure out how to reference the menu. I have tried a few things:



public void testOptionItem(){
// Not sure how to use this
mainActivity.getMenuInflater();

// Not sure if this actually opens the menu
// app closes to quickly, but even if it does I don't know how to select item
mainActivity.openOptionsMenu();

// I get a null reference exception on the menu item.
mainActivity.onOptionsItemSelected((MenuItem) mainActivity.findViewById(R.id.action_addPrescription));
}


How can I obtain a reference to the menu and select an item?


Aucun commentaire:

Enregistrer un commentaire