RapidFtrActivity extends Activity { @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.change_password: if (this.getClass() == RegisterChildActivity.class || this.getClass() == EditChildActivity.class) { saveAlertListener(ChangePasswordActivity.class); } else { startActivity(new Intent(this, ChangePasswordActivity.class)); } return true; case R.id.synchronize_all: if (isNullOrEmpty(getCurrentUser().getServerUrl())) { getServerAndSync(); } else { synchronise(); } return true; case R.id.cancel_synchronize_all: getContext().cleanSyncTask(); return true; case R.id.logout: if (this.getClass() == RegisterChildActivity.class || this.getClass() == EditChildActivity.class) { saveAlertListenerForLogout(); } else { inject(LogOutService.class).attemptLogOut(this); } return true; case R.id.info: startActivity(new Intent(this, InfoActivity.class)); return true; } return false; }  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 shouldFinishActivityOnLogout() throws IOException { RapidFtrApplication instance = RapidFtrApplication.getApplicationInstance(); instance.setCurrentUser(createUser()); RapidFtrActivity loginActivity = new LoginActivity_(); MenuItem menuItem = mock(MenuItem.class); given(menuItem.getItemId()).willReturn(R.id.logout); loginActivity.onOptionsItemSelected(menuItem); assertThat(loginActivity.isFinishing(), is(true)); }
@Test public void shouldCancelTheAsyncTaskIfCancelSynMenuIsClicked() { RapidFtrApplication instance = RapidFtrApplication.getApplicationInstance(); SynchronisationAsyncTask mockAsyncTask = mock(SynchronisationAsyncTask.class); instance.setSyncTask(mockAsyncTask); RapidFtrActivity mainActivity = new ViewAllChildrenActivity(); MenuItem cancelSynAll = mock(MenuItem.class); doReturn(R.id.cancel_synchronize_all).when(cancelSynAll).getItemId(); mainActivity.onOptionsItemSelected(cancelSynAll); verify(mockAsyncTask).cancel(false); }
@Test public void shouldNotThrowExceptionIfAsyncTaskIsNull() { RapidFtrApplication instance = RapidFtrApplication.getApplicationInstance(); instance.setSyncTask(null); RapidFtrActivity mainActivity = new ViewAllChildrenActivity(); MenuItem cancelSynAll = mock(MenuItem.class); doReturn(R.id.cancel_synchronize_all).when(cancelSynAll).getItemId(); mainActivity.onOptionsItemSelected(cancelSynAll); }
@Test public void shouldPromptUserWhenAttemptingToLogOutWhileSyncIsActive() { RapidFtrApplication instance = RapidFtrApplication.getApplicationInstance(); SynchronisationAsyncTask mockAsyncTask = mock(SynchronisationAsyncTask.class); instance.setSyncTask(mockAsyncTask); RapidFtrActivity mainActivity = new ViewAllChildrenActivity(); MenuItem cancelSynAll = mock(MenuItem.class); doReturn(R.id.cancel_synchronize_all).when(cancelSynAll).getItemId(); mainActivity.onOptionsItemSelected(cancelSynAll); verify(mockAsyncTask).cancel(false); }