NavigationDrawerActivity extends BaseActivity implements View.OnClickListener { @Override public void onBackPressed() { if (!mHasNavigationDrawer) { super.onBackPressed(); return; } DrawerLayout drawer = mNavigationDrawer.mLayout; if (drawer.isDrawerVisible(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { if (NavigationDrawerEntry.FIND_BAGGER.equals(mSelectedDrawerItem)) { super.onBackPressed(); } else { Intent intent = BaggersListActivity.createLaunchIntent(this, mPrefs.getCity()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK); startActivityWithoutTransition(intent); finish(); } } }  @Override void onBackPressed(); @Override boolean onOptionsItemSelected(MenuItem item); @Override void onClick(View v); @Override void finish(); @Override void setContentView(int layoutResID); @Override void setContentView(View view); @Override void setContentView(View view, ViewGroup.LayoutParams params);  }
@Test public void should_close_drawer_if_visible_on_back_pressed() { when(drawer.isDrawerVisible(GravityCompat.START)).thenReturn(true); activity.onBackPressed(); verify(drawer).closeDrawer(GravityCompat.START); }
@Test public void should_start_baggers_list_activity_if_drawer_is_not_visible_on_back_pressed_and_current_activity_is_not_a_baggers_list_activity() { when(drawer.isDrawerVisible(GravityCompat.START)).thenReturn(false); activity.onBackPressed(); ShadowActivity shadowActivity = Shadows.shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); ShadowIntent shadowIntent = Shadows.shadowOf(startedIntent); Truth.assertThat(shadowIntent.getIntentClass().getName()).isEqualTo(BaggersListActivity.class.getName()); }