Simulating a click on a menu item

2,656 views
Skip to first unread message

Felix H. Dahlke

unread,
Oct 18, 2011, 1:25:59 AM10/18/11
to Robolectric
Hi,

I want to simulate a click on a menu item and verify that the expected
dialog appears. The latter part is simple, but I cannot seem to get
the menu item click working.

The following code does find the correct menu item, but clicking on
the item doesn't seem to have any effect, my dialog is not showing up.

TestMenu mainMenu = new TestMenu(activity);
new MenuInflater(activity).inflate(R.menu.main, mainMenu);
TestMenuItem aboutItem = mainMenu.findMenuItem("@string/about");
assertThat("About menu item should exist", aboutItem, notNullValue());
aboutItem.click();

There is also a ShadowMenuInflater, but using that yields the same
behaviour:

TestMenu mainMenu = new TestMenu(activity);
shadowOf(new MenuInflater(activity)).inflate(R.menu.main, mainMenu);
TestMenuItem aboutItem = mainMenu.findMenuItem("@string/about");
assertThat("About menu item should exist", aboutItem, notNullValue());
aboutItem.click();

How can I simulate a click on a menu item?

Lowell Kirsh

unread,
Oct 18, 2011, 1:28:56 AM10/18/11
to robol...@googlegroups.com
Try using Robolectric.clickOn(aboutItem)
--
http://lowellkirsh.com

Tyler Schultz

unread,
Oct 18, 2011, 10:13:00 AM10/18/11
to robol...@googlegroups.com
There is no mechanism built into the menu items for what you're looking for. 

You will need to invoke Activity#onMenuItemSelected or one of its cousins yourself.

Take a look at ShadowMenuInflater and its supporting classes.  You may be able to improve it in a way to support clicks the way you wish it to.

--Tyler


On Mon, Oct 17, 2011 at 10:25 PM, Felix H. Dahlke <f...@ubercode.de> wrote:

Felix H. Dahlke

unread,
Oct 19, 2011, 11:24:46 PM10/19/11
to Robolectric
On Oct 18, 7:28 am, Lowell Kirsh <lowe...@gmail.com> wrote:
> Try using Robolectric.clickOn(aboutItem)

Doesn't seem to work with TestMenuItem :(

Felix H. Dahlke

unread,
Oct 19, 2011, 11:27:23 PM10/19/11
to Robolectric
On Oct 18, 4:13 pm, Tyler Schultz <tylerschu...@gmail.com> wrote:
> There is no mechanism built into the menu items for what you're looking
> for.
>
> You will need to invoke Activity#onMenuItemSelected or one of its cousins
> yourself.

Thanks, that does work. But when I call getBaseContext() in the
onMenuItemSelected() method, it returns null. Ideas?

And just out of interest: There is TestMenuItem.click(), and from it's
implementation I thought it was supposed to invoke
onMenuItemSelected(). Is
this a bug or was TestMenuItem.click() intended for testing something
else?

> Take a look at ShadowMenuInflater and its supporting classes.  You may be
> able to improve it in a way to support clicks the way you wish it to.

Whether or not I use ShadowMenuInflater doesn't make any difference at
all in my case, although I don't quite understand why.

Tyler Schultz

unread,
Oct 19, 2011, 11:54:27 PM10/19/11
to robol...@googlegroups.com
If you use set your own click listeners on the menu items yourself, then you can use the performClick functions and invoke your set click listeners. This is how many people who contributed to Robolectric implement their menus. Take for instance the way this activity is written: 

https://github.com/pivotal/RobolectricSample/blob/master/src/main/java/com/pivotallabs/tracker/RecentActivityActivity.java

This activity does not implement onOptionsItemSelected(MenuItem), instead it assigns click listeners in onPrepareOptionsMenu(Menu).  This is probably the assumption that Lowell made when he suggested you use Robolectric#clickOn(View). (SUP LOWELL!!!) 

As it is implemented now, Robolectric does not try to be Android and call back into your Activity when you click on an inflated menu.   Likewise Robolectric does not automatically try to invoke onCreate, onResume, etc. For better or worse, that's up to you to implement. As it's implemented right now, the inflated menu presumes nothing about the menu that inflated it. Personally, I find the disconnect of the onPrepareOptionsMenu and onOptionsItemSelected to be a bit awkward.   

--Tyler

Felix H. Dahlke

unread,
Oct 20, 2011, 11:42:14 PM10/20/11
to robol...@googlegroups.com
I see, but it would be really nice if onOptionsItemSelected was
supported, because that's how menus are explained in the Android
documentation. I'll see if I can get a pull request together for this.

Any idea why getBaseContext() returns null in methods invoked by
opOptionsItemSelected?

signature.asc

Felix H. Dahlke

unread,
Oct 19, 2011, 12:08:39 AM10/19/11
to robol...@googlegroups.com
signature.asc

Felix H. Dahlke

unread,
Oct 19, 2011, 1:31:17 AM10/19/11
to robol...@googlegroups.com
On 10/18/2011 04:13 PM, Tyler Schultz wrote:
> There is no mechanism built into the menu items for what you're looking
> for.
>
> You will need to invoke Activity#onMenuItemSelected or one of its
> cousins yourself.

Thanks, that does work. But when I call getBaseContext() in the


onMenuItemSelected() method, it returns null. Ideas?

And just out of interest: There is TestMenuItem.click(), and from it's

implementation I thought it was supposed to do invoke


onMenuItemSelected(). Is this a bug or was TestMenuItem.click() intended
for testing something else?

> Take a look at ShadowMenuInflater and its supporting classes. You may


> be able to improve it in a way to support clicks the way you wish it to.

Whether or not I use ShadowMenuInflater doesn't make any difference at

signature.asc
Reply all
Reply to author
Forward
0 new messages