Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Inflate menu before calling method injection#1962

Merged
WonderCsabo merged 2 commits into
androidannotations:developandroidannotations/androidannotations:developfrom
dodgex:1961_inflate_menu_before_calling_method_injectionCopy head branch name to clipboard
Mar 18, 2017
Merged

Inflate menu before calling method injection#1962
WonderCsabo merged 2 commits into
androidannotations:developandroidannotations/androidannotations:developfrom
dodgex:1961_inflate_menu_before_calling_method_injectionCopy head branch name to clipboard

Conversation

@dodgex

@dodgex dodgex commented Mar 5, 2017

Copy link
Copy Markdown
Member

Fix issue reported in #1961

@dodgex dodgex requested a review from WonderCsabo March 5, 2017 09:46
@dodgex

dodgex commented Mar 6, 2017

Copy link
Copy Markdown
Member Author

I just realized that it should be enough to move InjectMenuHandler below OptionsMenuHandler or OptionsMenuItemHandler (see CorePlugin.java#L177)

@WonderCsabo what do you think should we prefer?

@WonderCsabo

Copy link
Copy Markdown
Member

@dodgex i think this fix is better, because this way we do not rely on the order of handlers.

@WonderCsabo WonderCsabo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dodgex can you add a test case?

@dodgex

dodgex commented Mar 14, 2017

Copy link
Copy Markdown
Member Author

Not sure how to test this as it seems that robolectrict does not support menu inflation?

@WonderCsabo

Copy link
Copy Markdown
Member

@dodgex maybe you could create a spy of the Activity?

TestActivity activity = Robolectric.buildActivity ...
TestActivity activitySpy = Mockito.spy(activity);

Mockito.when(activity.getMenuInflater()).thenReturn(...); // create a mock menu inflater, and stub its inflate() method which alters the `Menu` object in some way you can assert on it 

activity.onCreateOptionsMenu(menu);
assertThat(menu).is...

@dodgex

dodgex commented Mar 16, 2017

Copy link
Copy Markdown
Member Author

added a test similar to what you suggested.

@dodgex

dodgex commented Mar 17, 2017

Copy link
Copy Markdown
Member Author

Yay.... to get the spy working i needed the generated class to be non-final...

AbstractActivityTest.activityShouldBeFinal:33 expected:<[tru]e> but was:<[fals]e>

but there is a test that checks if a generated class is final...

@WonderCsabo

WonderCsabo commented Mar 17, 2017 via email

Copy link
Copy Markdown
Member

@dodgex

dodgex commented Mar 17, 2017

Copy link
Copy Markdown
Member Author

Build should go green now.

@WonderCsabo WonderCsabo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not modify the existing tests, add new one(s). A test should only assert one thing.

final InjectMenuActivity.MockMenuInflater menuInflater = Mockito.mock(InjectMenuActivity.MockMenuInflater.class);
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
menuInflater.menuInflated = true;

@WonderCsabo WonderCsabo Mar 18, 2017

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, i realised you can just you Mockito verification, since you already using mocking.

Mockito.verify(menuInflater).inflate(R.menu.my_menu, menu);

This way you can get rid of the inner class and the boolean field as well.

Another thing: most of the times, we static import methods like mock and verify.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We then would need to call verify() in the InjectMenuActivity. after the call to injectMenuActivity.onCreateOptionsMenu(menu); the inflater is awlays called. the interesting part is that it has to be called BEFORE a method with @InjectMenu is called.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So do you have another Idea to change this or should we keep it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this one.

@dodgex

dodgex commented Mar 18, 2017

Copy link
Copy Markdown
Member Author

I reverted the changes to the existing tests and added a new test case for this

assertThat(injectMenuActivity.menuIsInflated).isFalse();
injectMenuActivity.onCreateOptionsMenu(menu);
assertThat(injectMenuActivity.menuIsInflated).isTrue();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this assert is not needed, it is already checked in other tests.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

@WonderCsabo WonderCsabo merged commit ed48fc0 into androidannotations:develop Mar 18, 2017
@WonderCsabo WonderCsabo added this to the 4.3 milestone Mar 18, 2017
@dodgex dodgex deleted the 1961_inflate_menu_before_calling_method_injection branch March 18, 2017 10:40
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.