NavigationManager extends Manager<NavigationManager.Model> { public Navigator navigate(Object sender) { return new Navigator(sender, this); }  @Override Class<Model> modelType(); Navigator navigate(Object sender);  }
@Test public void should_skip_interim_location_on_back_navigation() throws Exception { BackListener backListener = mock(BackListener.class); eventBusC.register(backListener); prepareLocationHistory(); reset(backListener); navigationManager.navigate(this).to(locId1, new Forwarder().setInterim(true)); navigationManager.navigate(this).to(locId2, new Forwarder().setInterim(true)); navigationManager.navigate(this).back(); ArgumentCaptor<NavigationManager.Event.OnLocationBack> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationBack.class); verify(backListener).onEvent(event.capture()); assertEquals(event.getValue().getLastValue().getLocationId(), locId2.getName()); assertEquals(event.getValue().getCurrentValue().getLocationId(), locId4.getName()); NavLocation currentLoc = navigationManager.getModel().getCurrentLocation(); assertEquals(currentLoc.getLocationId(), locId4.getName()); Assert.assertTrue(event.getValue().isFastRewind()); }
@Test public void should_post_app_exit_event_on_the_last_back_of_fast_back_navigation() { class AppExitListener { public void onEvent(NavigationManager.Event.OnAppExit event) { } } ArgumentCaptor<NavigationManager.Event.OnAppExit> event = ArgumentCaptor.forClass(NavigationManager.Event.OnAppExit.class); AppExitListener exitListener = mock(AppExitListener.class); eventBusC.register(exitListener); prepareLocationHistory(); reset(exitListener); navigationManager.navigate(this).back(null); verify(exitListener, times(0)).onEvent(event.capture()); navigateBackByFragment(); verify(exitListener, times(1)).onEvent(event.capture()); Assert.assertTrue(this == event.getValue().getSender()); }
@Test public void should_not_raise_navigate_back_event_when_navigate_to_first_location_from_the_first_location() throws Exception { BackListener backListener = mock(BackListener.class); eventBusC.register(backListener); navigationManager.navigate(this).to(locId1); Navigator navigator = navigationManager.navigate(this); navigator.back(null); ArgumentCaptor<NavigationManager.Event.OnLocationBack> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationBack.class); verify(backListener, times(0)).onEvent(event.capture()); }
@Test public void should_not_raise_navigate_back_event_when_navigate_to_unknown_location() throws Exception { BackListener backListener = mock(BackListener.class); eventBusC.register(backListener); prepareLocationHistory(); navigationManager.navigate(this).back(Controller.class); ArgumentCaptor<NavigationManager.Event.OnLocationBack> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationBack.class); verify(backListener, times(0)).onEvent(event.capture()); }
@Test public void should_not_raise_navigate_back_event_when_fast_back_navigate_from_null_location() throws Exception { BackListener backListener = mock(BackListener.class); eventBusC.register(backListener); navigationManager.navigate(this).back(Controller1.class); ArgumentCaptor<NavigationManager.Event.OnLocationBack> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationBack.class); verify(backListener, times(0)).onEvent(event.capture()); }
@Test public void should_be_able_to_log_navigation_history_with_trace_enabled() throws Exception { Logger logger = mock(Logger.class); when(logger.isTraceEnabled()).thenReturn(true); navigationManager.dumpHistoryOnLocationChange = true; navigationManager.logger = logger; navigationManager.navigate(this).to(Controller1.class, new Forwarder().clearTo(Controller.class)); verify(logger, atLeast(1)).trace(anyString()); reset(logger); navigationManager.navigate(this).to(Controller1.class); verify(logger, atLeast(1)).trace(anyString()); reset(logger); navigateBackByFragment(); verify(logger, atLeast(1)).trace(anyString()); reset(logger); navigationManager.navigate(this).to(Controller1.class); navigationManager.navigate(this).back(null); verify(logger, atLeast(1)).trace(anyString()); }
@Test public void should_not_log_navigation_history_without_trace_eanbled() throws Exception { Logger logger = mock(Logger.class); when(logger.isTraceEnabled()).thenReturn(false); navigationManager.dumpHistoryOnLocationChange = true; navigationManager.logger = logger; navigationManager.navigate(this).to(Controller1.class, new Forwarder().clearTo(Controller.class)); verify(logger, atLeast(0)).trace(anyString()); reset(logger); navigationManager.navigate(this).to(Controller1.class); verify(logger, atLeast(0)).trace(anyString()); reset(logger); navigateBackByFragment(); verify(logger, atLeast(0)).trace(anyString()); reset(logger); navigationManager.navigate(this).to(Controller1.class); navigationManager.navigate(this).back(null); verify(logger, atLeast(0)).trace(anyString()); }
@Test public void should_retain_prepared_instance_until_navigation_settled() throws Exception { final long fiveMinutes = 60 * 5; Object module = new Object() { @Provides @Slower2 TimerController timerSlowerX2() { return new TimerController() { { try { onCreated(); } catch (Exception e) { } } @Override public void setInitialValue(long value) { super.setInitialValue(value * 2); } }; } @Provides @Slower3 TimerController timerSlowerX3() { return new TimerController() { { try { onCreated(); } catch (Exception e) { } } @Override public void setInitialValue(long value) { super.setInitialValue(value * 3); } }; } }; Mvc.graph().getRootComponent().register(module); Annotation slower2Qualifier = SlowXHolder.class.getAnnotation(Slower2.class); Annotation slower3Qualifier = SlowXHolder.class.getAnnotation(Slower3.class); Mvc.graph().use(TimerController.class, slower2Qualifier, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { Assert.assertEquals(0, instance.getInitialValue()); } }); Navigator navigator = navigationManager.navigate(this).with(TimerController.class, slower2Qualifier, new Preparer<TimerController>() { @Override public void prepare(TimerController instance) { instance.setInitialValue(fiveMinutes); } }); navigator.to(TimerController.class); Mvc.graph().use(TimerController.class, slower2Qualifier, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { Assert.assertEquals(fiveMinutes * 2, instance.getInitialValue()); } }); navigator.destroy(); Mvc.graph().use(TimerController.class, slower2Qualifier, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { Assert.assertEquals(0, instance.getInitialValue()); } }); Mvc.graph().use(TimerController.class, slower3Qualifier, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { Assert.assertEquals(0, instance.getInitialValue()); } }); navigator = navigationManager.navigate(this).with(TimerController.class, slower3Qualifier, new Preparer<TimerController>() { @Override public void prepare(TimerController instance) { instance.setInitialValue(fiveMinutes); } }); navigator.to(TimerController.class); Mvc.graph().use(TimerController.class, slower3Qualifier, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { Assert.assertEquals(fiveMinutes * 3, instance.getInitialValue()); } }); navigator.destroy(); Mvc.graph().use(TimerController.class, slower3Qualifier, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { Assert.assertEquals(0, instance.getInitialValue()); } }); Mvc.graph().getRootComponent().unregister(module); }
@Test public void should_retain_prepared_instance_until_navigation_settled_without_qualifier() throws Exception { final long fiveMinutes = 60 * 5; Navigator navigator = navigationManager.navigate(this).with(TimerController.class, new Preparer<TimerController>() { @Override public void prepare(TimerController instance) { instance.setInitialValue(fiveMinutes); } }); navigator.to(TimerController.class); Mvc.graph().use(TimerController.class, null, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { Assert.assertEquals(fiveMinutes, instance.getInitialValue()); } }); navigator.destroy(); Mvc.graph().use(TimerController.class, new Consumer<TimerController>() { @Override public void consume(TimerController instance) { Assert.assertEquals(0, instance.getInitialValue()); } }); }
@Test public void should_invoke_singular_argument_with_method_of_navigator_correct() throws Exception { Navigator navigator = navigationManager.navigate(this); Navigator spiedNavigator = spy(navigator); verify(spiedNavigator, times(0)).with(eq(NavigationManager.class), isNull(Annotation.class), isNull(Preparer.class)); spiedNavigator.with(NavigationManager.class); verify(spiedNavigator).with(eq(NavigationManager.class), isNull(Annotation.class), isNull(Preparer.class)); }
@Test public void should_return_correct_sender_by_navigator() throws Exception { Navigator navigator = navigationManager.navigate(this); Assert.assertTrue(this == navigator.getSender()); }
@Test public void should_invoke_on_settled_when_navigation_is_done() throws Exception { Navigator.OnSettled onSettled = mock(Navigator.OnSettled.class); Navigator navigator = navigationManager.navigate(this).with(TimerController.class) .onSettled(onSettled); navigator.to(TimerController.class); verify(onSettled, times(0)).run(); navigator.destroy(); verify(onSettled, times(1)).run(); }
@Test public void interim_forwarder_should_instruct_navigation_not_push_location_to_back_stack(){ ForwardListener forwardListener = mock(ForwardListener.class); eventBusC.register(forwardListener); Navigator navigator = navigationManager.navigate(this); Forwarder forwarder = new Forwarder(); Assert.assertFalse(forwarder.isInterim()); forwarder.setInterim(true); Assert.assertTrue(forwarder.isInterim()); navigator.to(TimerController.class, forwarder); ArgumentCaptor<NavigationManager.Event.OnLocationForward> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationForward.class); verify(forwardListener).onEvent(event.capture()); assertEquals(event.getValue().getCurrentValue().getLocationId(), TimerController.class.getName()); assertTrue(event.getValue().getCurrentValue().isInterim()); Assert.assertTrue(this == event.getValue().getSender()); Assert.assertTrue(navigator == event.getValue().getNavigator()); }
@Test public void should_not_fail_when_logger_is_set_trace() { navigationManager.logger = mock(Logger.class); when(navigationManager.logger.isTraceEnabled()).thenReturn(true); prepareLocationHistory(); navigationManager.navigate(this).back(); }
@Test(expected = MvcGraphException.class) public void should_throw_MvcGraphException_when_mvcGraph_with_method_encounters_PokeException() { navigationManager.navigate(this).with(X.class); }
@Test public void should_throw_MvcGraphException_when_mvcGraph_destroy_method_encounters_PokeException() throws ProvideException, ProviderConflictException, ProviderMissingException { navigationManager.logger = mock(Logger.class); Navigator navigator = navigationManager.navigate(this); Mvc.graph().getRootComponent().register(new Object(){ @Provides public X x() { return new X_1(); } }); navigator.with(X.class); Mvc.graph().getRootComponent().unregister(X.class, null); navigator.destroy(); verify(navigationManager.logger).warn(anyString(), anyString()); }
@Test public void shouldClearNavigationHistoryUpToSpecified() throws Exception { ForwardListener forwardListener = prepareLocationHistory(); navigationManager.navigate(this).to(locId2); navigationManager.navigate(this).to(locId4); navigationManager.navigate(this).to(locId1); reset(forwardListener); navigationManager.navigate(this).to(locId5, new Forwarder().clearTo(locId3)); ArgumentCaptor<NavigationManager.Event.OnLocationForward> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationForward.class); verify(forwardListener).onEvent(event.capture()); assertEquals(event.getValue().getLastValue().getLocationId(), locId1.getName()); assertEquals(event.getValue().getCurrentValue().getLocationId(), locId5.getName()); assertEquals(event.getValue().getLocationWhereHistoryClearedUpTo().getLocationId(), locId3.getName()); assertEquals(event.getValue().isClearHistory(), true); NavigationManager.Model model = navigationManager.getModel(); NavLocation curLoc = model.getCurrentLocation(); assertEquals(curLoc.getLocationId(), locId5.getName()); curLoc = curLoc.getPreviousLocation(); assertEquals(curLoc.getLocationId(), locId3.getName()); curLoc = curLoc.getPreviousLocation(); assertEquals(curLoc.getLocationId(), locId2.getName()); curLoc = curLoc.getPreviousLocation(); assertEquals(curLoc.getLocationId(), locId1.getName()); curLoc = curLoc.getPreviousLocation(); assertEquals(curLoc, null); NavLocation currentLoc = navigationManager.getModel().getCurrentLocation(); assertEquals(currentLoc.getLocationId(), locId5.getName()); assertEquals(currentLoc.getPreviousLocation().getLocationId(), locId3.getName()); assertEquals(currentLoc.getPreviousLocation().getPreviousLocation().getLocationId(), locId2.getName()); assertEquals(currentLoc.getPreviousLocation().getPreviousLocation().getPreviousLocation().getLocationId(), locId1.getName()); assertEquals(currentLoc.getPreviousLocation().getPreviousLocation().getPreviousLocation().getPreviousLocation(), null); }
@Test public void shouldClearAllNavigationHistory() throws Exception { ForwardListener forwardListener = prepareLocationHistory(); reset(forwardListener); navigationManager.navigate(this).to(locId5, new Forwarder().clearAll()); ArgumentCaptor<NavigationManager.Event.OnLocationForward> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationForward.class); verify(forwardListener).onEvent(event.capture()); assertEquals(event.getValue().getLastValue().getLocationId(), locId4.getName()); assertEquals(event.getValue().getCurrentValue().getLocationId(), locId5.getName()); assertEquals(event.getValue().getLocationWhereHistoryClearedUpTo(), null); assertEquals(event.getValue().isClearHistory(), true); NavLocation currentLoc = navigationManager.getModel().getCurrentLocation(); assertEquals(currentLoc.getLocationId(), locId5.getName()); assertEquals(currentLoc.getPreviousLocation(), null); }
@Test public void shouldBeAbleToNavigateBackToGivenLocation() throws Exception { BackListener backListener = mock(BackListener.class); eventBusC.register(backListener); prepareLocationHistory(); reset(backListener); navigationManager.navigate(this).back(locId2); ArgumentCaptor<NavigationManager.Event.OnLocationBack> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationBack.class); verify(backListener).onEvent(event.capture()); assertEquals(event.getValue().getLastValue().getLocationId(), locId4.getName()); assertEquals(event.getValue().getCurrentValue().getLocationId(), locId2.getName()); NavLocation currentLoc = navigationManager.getModel().getCurrentLocation(); assertEquals(currentLoc.getLocationId(), locId2.getName()); assertEquals(currentLoc.getPreviousLocation().getLocationId(), locId1.getName()); assertEquals(currentLoc.getPreviousLocation().getPreviousLocation(), null); Assert.assertTrue(event.getValue().isFastRewind()); }
@Test public void shouldBeAbleToNavigateBackToFirstLocation() throws Exception { BackListener backListener = mock(BackListener.class); eventBusC.register(backListener); prepareLocationHistory(); reset(backListener); Navigator navigator = navigationManager.navigate(this); navigator.back(null); ArgumentCaptor<NavigationManager.Event.OnLocationBack> event = ArgumentCaptor.forClass(NavigationManager.Event.OnLocationBack.class); verify(backListener).onEvent(event.capture()); Assert.assertTrue(this == event.getValue().getSender()); Assert.assertTrue(navigator == event.getValue().getNavigator()); assertEquals(event.getValue().getLastValue().getLocationId(), locId4.getName()); assertEquals(event.getValue().getCurrentValue().getLocationId(), locId1.getName()); NavLocation currentLoc = navigationManager.getModel().getCurrentLocation(); assertEquals(currentLoc.getLocationId(), locId1.getName()); assertEquals(currentLoc.getPreviousLocation(), null); Assert.assertTrue(event.getValue().isFastRewind()); }