AudioPlayerController extends MediaController { @Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { ((Activity) getContext()).finish(); } return super.dispatchKeyEvent(event); } @Inject  AudioPlayerController(Context context); @Override boolean dispatchKeyEvent(KeyEvent event);  }
@Test public void itFinishesActivityWhenKeyCodeBackIsPerformed() { when(event.getKeyCode()).thenReturn(KeyEvent.KEYCODE_BACK); audioPlayerController.dispatchKeyEvent(event); assertThat(activity.isFinishing(), is(true)); }
@Test public void itDoesNotFinishActivityWhenAnotherKeyCodeIsPerformed() { when(event.getKeyCode()).thenReturn(KeyEvent.KEYCODE_UNKNOWN); audioPlayerController.dispatchKeyEvent(event); assertThat(activity.isFinishing(), is(false)); }