RapidFtrActivity extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { if (getContext().isLoggedIn()) { getMenuInflater().inflate(R.menu.options_menu, menu); setMenu(menu); toggleChangePassword(menu); toggleSync(menu); setContextToSyncTask(); } return getContext().isLoggedIn(); }  RapidFtrApplication getContext(); void searchChildrenTabListener(View view); void createEnquiryTabListener(View view); void viewAllEnquiryTabListener(View view); void registerChildTabListener(View view); void viewAllChildrenListener(View view); void addResultListener(int requestCode, ResultListener listener); @Override void onActivityResult(int requestCode, int resultCode, Intent data); @Override boolean onCreateOptionsMenu(Menu menu); @Override boolean onOptionsItemSelected(MenuItem item); void getServerAndSync(); void hideEnquiriesTabIfRapidReg(); static final String LOGOUT_INTENT_FILTER; }
@Test public void shouldNotRenderMenuWhenUserIsNotLoggedIn() throws IOException { RapidFtrApplication instance = RapidFtrApplication.getApplicationInstance(); instance.setCurrentUser(null); RapidFtrActivity loginActivity = new LoginActivity_(); Menu menu = mock(Menu.class); doReturn(mock(MenuItem.class)).when(menu).getItem(anyInt()); boolean showMenu = loginActivity.onCreateOptionsMenu(menu); assertThat(showMenu, is(false)); }
@Test public void shouldRenderMenuWhenUserIsLoggedIn() throws IOException { RapidFtrApplication instance = RapidFtrApplication.getApplicationInstance(); instance.setCurrentUser(createUser()); RapidFtrActivity loginActivity = new LoginActivity_(); Menu menu = mock(Menu.class); doReturn(mock(MenuItem.class)).when(menu).getItem(anyInt()); doReturn(mock(MenuItem.class)).when(menu).findItem(anyInt()); boolean showMenu = loginActivity.onCreateOptionsMenu(menu); assertThat(showMenu, is(true)); }
@Test public void shouldSetTheMenuBasedOnAsynTask() throws IOException { RapidFtrApplication instance = RapidFtrApplication.getApplicationInstance(); SynchronisationAsyncTask mockAsyncTask = mock(SynchronisationAsyncTask.class); instance.setSyncTask(mockAsyncTask); instance.setCurrentUser(createUser()); Menu mockMenu = mock(Menu.class); MenuItem syncAllMenuItem = mock(MenuItem.class); MenuItem cancelSyncAllMenuItem = mock(MenuItem.class); MenuItem changePasswordMenuItem = mock(MenuItem.class); doReturn(syncAllMenuItem).when(mockMenu).getItem(0); doReturn(cancelSyncAllMenuItem).when(mockMenu).getItem(1); doReturn(changePasswordMenuItem).when(mockMenu).findItem(R.id.change_password); RapidFtrActivity mainActivity = new ViewAllChildrenActivity(); mainActivity.onCreateOptionsMenu(mockMenu); verify(syncAllMenuItem).setVisible(false); verify(cancelSyncAllMenuItem).setVisible(true); instance.setSyncTask(null); mainActivity.onCreateOptionsMenu(mockMenu); verify(syncAllMenuItem).setVisible(true); verify(cancelSyncAllMenuItem).setVisible(false); }
@Test public void shouldSetCurrentContextWhileCreatingMenu() throws IOException { RapidFtrApplication instance = RapidFtrApplication.getApplicationInstance(); SynchronisationAsyncTask mockSyncAll = mock(SynchronisationAsyncTask.class); instance.setSyncTask(mockSyncAll); instance.setCurrentUser(createUser()); RapidFtrActivity mainActivity = new ViewAllChildrenActivity(); Menu mockMenu = mock(Menu.class); when(mockMenu.getItem(anyInt())).thenReturn(mock(MenuItem.class)); when(mockMenu.findItem(anyInt())).thenReturn(mock(MenuItem.class)); mainActivity.onCreateOptionsMenu(mockMenu); verify(mockSyncAll).setContext(mainActivity); }