Test menu items

2019-07-26 08:43发布

问题:

I found the following test (slightly modified) as an answer here. But I get an error that says that I need android.permission.INJECT_EVENT. I have added it both to the application-project and the test-project but the test still says it needs it.

Have I missunderstood how the test should be written?

public void testMenuItemAddDrink(){
        Start activity = getActivity();
        ActivityMonitor am = getInstrumentation().addMonitor(Start.class.getName(), null, false);

        getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
        getInstrumentation().invokeMenuActionSync(activity, se.javalia.myDrinks.R.id.menu_addDrink, 0);

        Activity addDrink = getInstrumentation().waitForMonitorWithTimeout(am, 1000);
        assertEquals(true, getInstrumentation().checkMonitorHit(am, 1));
        addDrink.finish();
    }

Thanks in advance Roland

回答1:

You can remove this line:

getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);

it's not necessary because you are using invokeMenuActionSync() instead.