Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Conversation

@dodgex
Copy link
Member

@dodgex dodgex commented Mar 5, 2017

Fix issue reported in #1961

@dodgex dodgex requested a review from WonderCsabo March 5, 2017 09:46
@dodgex
Copy link
Member Author

dodgex commented Mar 6, 2017

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
Member

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

Copy link
Member

@WonderCsabo WonderCsabo left a comment

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
Copy link
Member Author

dodgex commented Mar 14, 2017

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

@WonderCsabo
Copy link
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
Copy link
Member Author

dodgex commented Mar 16, 2017

added a test similar to what you suggested.

@dodgex
Copy link
Member Author

dodgex commented Mar 17, 2017

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
Copy link
Member

WonderCsabo commented Mar 17, 2017 via email

@dodgex
Copy link
Member Author

dodgex commented Mar 17, 2017

Build should go green now.

Copy link
Member

@WonderCsabo WonderCsabo left a comment

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;
Copy link
Member

@WonderCsabo WonderCsabo Mar 18, 2017

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
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
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
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
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
Copy link
Member Author

dodgex commented Mar 18, 2017

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
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
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