ActionBarFragment extends BaseFragment { @Override public void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater) { if (annotationHandler == null) { super.onCreateOptionsMenu(menu, inflater); return; } final ActionBarFragmentAnnotationHandler annotationHandler = (ActionBarFragmentAnnotationHandler) this.annotationHandler; if (annotationHandler.hasOptionsMenu()) { if (annotationHandler.shouldClearOptionsMenu()) { menu.clear(); } final int menuResource = annotationHandler.getOptionsMenuResource(0); if (menuResource == 0) { super.onCreateOptionsMenu(menu, inflater); } else { switch (annotationHandler.getOptionsMenuFlags(0)) { case MenuOptions.IGNORE_SUPER: inflater.inflate(menuResource, menu); break; case MenuOptions.BEFORE_SUPER: inflater.inflate(menuResource, menu); super.onCreateOptionsMenu(menu, inflater); break; case MenuOptions.DEFAULT: default: super.onCreateOptionsMenu(menu, inflater); inflater.inflate(menuResource, menu); break; } } } else { super.onCreateOptionsMenu(menu, inflater); } }  @Override void onCreate(@Nullable final Bundle savedInstanceState); @Override void onCreateOptionsMenu(@NonNull final Menu menu, @NonNull final MenuInflater inflater); @Override void onActivityCreated(@Nullable final Bundle savedInstanceState); void invalidateActionBar();  }
@SuppressWarnings("ResourceType") @Test public void testOnCreateOptionsMenu() { final ActionBarFragment fragment = new TestFragmentWithMenuOptions(); final Menu mockMenu = mock(Menu.class); final MenuInflater mockMenuInflater = mock(MenuInflater.class); fragment.onCreateOptionsMenu(mockMenu, mockMenuInflater); verify(mockMenuInflater).inflate(TestFragmentWithMenuOptions.MENU_RESOURCE, mockMenu); verifyNoMoreInteractions(mockMenuInflater); verifyZeroInteractions(mockMenu); }
@SuppressWarnings("ResourceType") @Test public void testOnCreateOptionsMenuIgnoringSuper() { final ActionBarFragment fragment = new TestFragmentWithMenuOptionsIgnoringSuper(); final Menu mockMenu = mock(Menu.class); final MenuInflater mockMenuInflater = mock(MenuInflater.class); fragment.onCreateOptionsMenu(mockMenu, mockMenuInflater); verify(mockMenuInflater).inflate(TestFragmentWithMenuOptions.MENU_RESOURCE, mockMenu); verifyNoMoreInteractions(mockMenuInflater); verifyZeroInteractions(mockMenu); }
@SuppressWarnings("ResourceType") @Test public void testOnCreateOptionsMenuBeforeSuper() { final ActionBarFragment fragment = new TestFragmentWithMenuOptionsBeforeSuper(); final Menu mockMenu = mock(Menu.class); final MenuInflater mockMenuInflater = mock(MenuInflater.class); fragment.onCreateOptionsMenu(mockMenu, mockMenuInflater); verify(mockMenuInflater).inflate(TestFragmentWithMenuOptions.MENU_RESOURCE, mockMenu); verifyNoMoreInteractions(mockMenuInflater); verifyZeroInteractions(mockMenu); }
@Test public void testOnCreateOptionsMenuWithoutMenuResource() { final ActionBarFragment fragment = new TestFragmentWithMenuOptionsWithoutResource(); final Menu mockMenu = mock(Menu.class); final MenuInflater mockMenuInflater = mock(MenuInflater.class); fragment.onCreateOptionsMenu(mockMenu, mockMenuInflater); verify(mockMenu).clear(); verifyNoMoreInteractions(mockMenu); verifyZeroInteractions(mockMenuInflater); }
@Test public void testOnCreateOptionsMenuForFragmentWithoutMenu() { final ActionBarFragment fragment = new TestFragmentWithoutAnnotation(); final Menu mockMenu = mock(Menu.class); final MenuInflater mockMenuInflater = mock(MenuInflater.class); fragment.onCreateOptionsMenu(mockMenu, mockMenuInflater); verifyZeroInteractions(mockMenu, mockMenuInflater); }
@Test public void testOnCreateOptionsMenuWhenAnnotationsAreDisabled() { FragmentAnnotations.setEnabled(false); final ActionBarFragment fragment = new TestFragment(); final Menu mockMenu = mock(Menu.class); final MenuInflater mockMenuInflater = mock(MenuInflater.class); fragment.onCreateOptionsMenu(mockMenu, mockMenuInflater); verifyZeroInteractions(mockMenu, mockMenuInflater); }