StackController extends ParentController<StackLayout> { public void push(ViewController child, CommandListener listener) { final ViewController toRemove = stack.peek(); if (size() > 0) backButtonHelper.addToPushedChild(child); child.setParentController(this); stack.push(child.getId(), child); Options resolvedOptions = resolveCurrentOptions(presenter.getDefaultOptions()); addChildToStack(child, child.getView(), resolvedOptions); if (toRemove != null) { if (resolvedOptions.animations.push.enabled.isTrueOrUndefined()) { if (resolvedOptions.animations.push.waitForRender.isTrue()) { child.getView().setAlpha(0); child.setOnAppearedListener(() -> animator.push(child.getView(), resolvedOptions.animations.push, resolvedOptions.transitions, toRemove.getElements(), child.getElements(), () -> { getView().removeView(toRemove.getView()); listener.onSuccess(child.getId()); })); } else { animator.push(child.getView(), resolvedOptions.animations.push, () -> { if (!toRemove.equals(peek())) { getView().removeView(toRemove.getView()); } listener.onSuccess(child.getId()); }); } } else { getView().removeView(toRemove.getView()); listener.onSuccess(child.getId()); } } else { listener.onSuccess(child.getId()); } } StackController(Activity activity, List<ViewController> children, ChildControllersRegistry childRegistry, TopBarBackgroundViewController topBarBackgroundViewController, TopBarController topBarController, NavigationAnimator animator, String id, Options initialOptions, BackButtonHelper backButtonHelper, StackPresenter stackPresenter, Presenter presenter); @Override void setDefaultOptions(Options defaultOptions); @Override void onAttachToParent(); @Override void mergeOptions(Options options); @Override void applyChildOptions(Options options, Component child); @Override void mergeChildOptions(Options options, ViewController childController, Component child); @Override void destroy(); @Override void clearOptions(); @Override void onChildDestroyed(Component child); void push(ViewController child, CommandListener listener); void setRoot(ViewController child, CommandListener listener); void pop(Options mergeOptions, CommandListener listener); void popTo(ViewController viewController, Options mergeOptions, CommandListener listener); void popToRoot(Options mergeOptions, CommandListener listener); int size(); boolean isEmpty(); @Override boolean handleBack(CommandListener listener); @Override void sendOnNavigationButtonPressed(String buttonId); @NonNull @Override Collection<ViewController> getChildControllers(); @Override void setupTopTabsWithViewPager(ViewPager viewPager); @Override void clearTopTabs(); @RestrictTo(RestrictTo.Scope.TESTS) TopBar getTopBar(); @RestrictTo(RestrictTo.Scope.TESTS) StackLayout getStackLayout();  }
@Test public void push_assignsRefToSelfOnPushedController() { assertThat(child1.getParentController()).isNull(); uut.push(child1, new CommandListenerAdapter()); assertThat(child1.getParentController()).isEqualTo(uut); StackController anotherNavController = createStack("another"); anotherNavController.ensureViewIsCreated(); anotherNavController.push(child2, new CommandListenerAdapter()); assertThat(child2.getParentController()).isEqualTo(anotherNavController); }
@Test public void findControllerById_ReturnsSelfOrChildrenById() { assertThat(uut.findController("123")).isNull(); assertThat(uut.findController(uut.getId())).isEqualTo(uut); uut.push(child1, new CommandListenerAdapter()); assertThat(uut.findController(child1.getId())).isEqualTo(child1); }
@Test public void findControllerById_Deeply() { StackController stack = createStack("another"); stack.ensureViewIsCreated(); stack.push(child2, new CommandListenerAdapter()); uut.push(stack, new CommandListenerAdapter()); assertThat(uut.findController(child2.getId())).isEqualTo(child2); }
@Test public void stackCanBePushed() { ViewUtils.removeFromParent(uut.getView()); StackController parent = createStack("someStack"); parent.ensureViewIsCreated(); parent.push(uut, new CommandListenerAdapter()); uut.onViewAppeared(); assertThat(parent.getView().getChildAt(0)).isEqualTo(uut.getView()); }
@Test public void push() { assertThat(uut.isEmpty()).isTrue(); CommandListenerAdapter listener = spy(new CommandListenerAdapter()); uut.push(child1, listener); assertContainsOnlyId(child1.getId()); verify(listener, times(1)).onSuccess(child1.getId()); }
@Test public void push_backButtonIsNotAddedIfScreenContainsLeftButton() { disablePushAnimation(child1, child2); uut.push(child1, new CommandListenerAdapter()); child2.options.topBar.buttons.left = new ArrayList<>(Collections.singleton(TitleBarHelper.iconButton("someButton", "icon.png"))); uut.push(child2, new CommandListenerAdapter()); assertThat(topBarController.getView().getTitleBar().getNavigationIcon()).isNotNull(); verify(topBarController.getView(), times(0)).setBackButton(any()); }
@Test public void push_backButtonAddedBeforeChildViewIsCreated() { disablePopAnimation(child1, child2); uut.push(child1, new CommandListenerAdapter()); uut.push(child2, new CommandListenerAdapter()); InOrder inOrder = inOrder(backButtonHelper, child2); inOrder.verify(backButtonHelper).addToPushedChild(child2); inOrder.verify(child2).setParentController(uut); inOrder.verify(child2).getView(); }
@Test public void push_waitForRender() { disablePushAnimation(child1); uut.push(child1, new CommandListenerAdapter()); child2.options.animations.push.waitForRender = new Bool(true); uut.push(child2, new CommandListenerAdapter()); verify(child2).setOnAppearedListener(any()); verify(animator, times(0)).push(eq(child1.getView()), eq(child1.options.animations.push), any()); }
@Test public void push_addsToViewTree() { assertNotChildOf(uut.getView(), child1.getView()); uut.push(child1, new CommandListenerAdapter()); assertIsChild(uut.getView(), child1.getView()); }
@Test public void push_removesPreviousFromTree() { assertNotChildOf(uut.getView(), child1.getView()); uut.push(child1, new CommandListenerAdapter()); assertIsChild(uut.getView(), child1.getView()); uut.push(child2, new CommandListenerAdapter() { @Override public void onSuccess(String childId) { assertThat((View) uut.getView().findViewById(child1.getView().getId())).isNull(); assertThat((View) uut.getView().findViewById(child2.getView().getId())).isNotNull(); } }); }