views:

95

answers:

2

Hello,

I need to cover Menu functionality by unit tests, however I'm struggling to obtain Menu object.

Following test case fails (mMenu is null):

public void testMenu() {
    sendKeys(KeyEvent.KEYCODE_MENU);
    mMenu = (Menu) mActivity.findViewById(com.###.###.R.id.main_menu);
    assertNotNull(mMenu);
}

Please advice.

Thank you.

+1  A: 

What exactly are you trying to test? That menu items do the correct action?

You can call Activity.openOptionsMenu() to open the menu and get a reference to the menu by overriding one of the onMenu methods. At that point you can use Menu.performIdentifierAction to select menu items.

BrennaSoft
Yes, I need to check that all menu and sub-menu items are doing correct actions.sendKeys(KeyEvent.KEYCODE_MENU) works correctly and I can see Menu on emulator (I can't see it if I call Activity.openOptionsMenu()).Do you mean I need to override onMenuOpened in target app and save Menu reference inside of activity?Maybe there is a way to not touch target application?
cement
A: 

If you want to do UI, system or function tests I would recommend you to use Robotium. Then you can just use sendKey(Solo.MENU) and then click on the menu items by using clickOnText() or clickOnView(). When you have done that you can assert right behaviour. Just asserting that it should not be null is not enough. You should check Robotium out, its way more appropriate to use when testing things like this.

Renas