SessionRouteRegistry extends AbstractRouteRegistry { @Override public Registration addRoutesChangeListener( RoutesChangedListener listener) { final Registration parentRegistration = getParentRegistry() .addRoutesChangeListener(event -> { ConfiguredRoutes configuration = getConfiguration(); List<RouteBaseData<?>> addedVisible = event.getAddedRoutes() .stream() .filter(routeData -> !configuration .hasTemplate(routeData.getTemplate())) .collect(Collectors.toList()); List<RouteBaseData<?>> removedVisible = event .getRemovedRoutes().stream() .filter(routeData -> !configuration .hasTemplate(routeData.getTemplate())) .collect(Collectors.toList()); if (!(addedVisible.isEmpty() && removedVisible.isEmpty())) { fireEvent(new RoutesChangedEvent(event.getSource(), addedVisible, removedVisible)); } }); final Registration registration = super.addRoutesChangeListener( listener); return () -> { registration.remove(); parentRegistration.remove(); }; } SessionRouteRegistry(VaadinSession session); static RouteRegistry getSessionRegistry(VaadinSession session); @Override List<RouteData> getRegisteredRoutes(); @Override Registration addRoutesChangeListener(
            RoutesChangedListener listener); @Override NavigationRouteTarget getNavigationRouteTarget(String url); @Override RouteTarget getRouteTarget(Class<? extends Component> target,
            RouteParameters parameters); @Override Optional<Class<? extends Component>> getNavigationTarget(
            String url); @Override Optional<Class<? extends Component>> getNavigationTarget(
            String url, List<String> segments); @Override Optional<String> getTargetUrl(
            Class<? extends Component> navigationTarget); @Override Optional<String> getTargetUrl(
            Class<? extends Component> navigationTarget,
            RouteParameters parameters); @Override Optional<String> getTemplate(
            Class<? extends Component> navigationTarget); @Override @Deprecated List<Class<? extends RouterLayout>> getRouteLayouts(String path,
            Class<? extends Component> navigationTarget);  }
@Test public void routeWithAliases_eventShowsCorrectlyAsRemoved() { SessionRouteRegistry sessionRegistry = getRegistry(session); List<RouteBaseData> added = new ArrayList<>(); List<RouteBaseData> removed = new ArrayList<>(); sessionRegistry.addRoutesChangeListener(event -> { added.clear(); removed.clear(); added.addAll(event.getAddedRoutes()); removed.addAll(event.getRemovedRoutes()); }); sessionRegistry.update(() -> { sessionRegistry.setRoute("main", Secondary.class, Collections.emptyList()); sessionRegistry.setRoute("Alias1", Secondary.class, Collections.emptyList()); sessionRegistry.setRoute("Alias2", Secondary.class, Collections.emptyList()); }); Assert.assertEquals( "Main route and aliases should all be seen as added.", 3, added.size()); Assert.assertTrue("No routes should have been removed", removed.isEmpty()); sessionRegistry.removeRoute("Alias2"); Assert.assertTrue("No routes should have been added", added.isEmpty()); Assert.assertEquals( "Removing the alias route should be seen in the event", 1, removed.size()); }
@Test public void maskedPathsInParent_eventContainsOnlyChangesVisibleForSession() { registry.setRoute("main", MyRoute.class, Collections.emptyList()); SessionRouteRegistry sessionRegistry = getRegistry(session); List<RoutesChangedEvent> events = new ArrayList<>(); sessionRegistry.update(() -> { sessionRegistry.setRoute("main", Secondary.class, Collections.emptyList()); sessionRegistry.setRoute("Alias1", Secondary.class, Collections.emptyList()); sessionRegistry.setRoute("Alias2", Secondary.class, Collections.emptyList()); }); sessionRegistry.addRoutesChangeListener(events::add); registry.removeRoute(MyRoute.class); Assert.assertTrue("No event for masked path should have been received.", events.isEmpty()); registry.setRoute("main", MyRoute.class, Collections.emptyList()); Assert.assertTrue("No event for masked path should have been received.", events.isEmpty()); registry.setRoute("home", Secondary.class, Collections.emptyList()); Assert.assertEquals( "Addition of non masked path should have fired an event.", 1, events.size()); Assert.assertEquals("Source should have been ApplicationRouteRegistry", registry, events.get(0).getSource()); Assert.assertEquals("One route should have been added", 1, events.get(0).getAddedRoutes().size()); Assert.assertEquals("No routes should have been removed", 0, events.get(0).getRemovedRoutes().size()); }
@Test public void removeListener_noEventsAreGottenForAnyRegistry() { SessionRouteRegistry sessionRegistry = getRegistry(session); List<RoutesChangedEvent> events = new ArrayList<>(); Registration registration = sessionRegistry .addRoutesChangeListener(events::add); registry.setRoute("main", MyRoute.class, Collections.emptyList()); sessionRegistry.update(() -> { sessionRegistry.setRoute("main", Secondary.class, Collections.emptyList()); sessionRegistry.setRoute("Alias1", Secondary.class, Collections.emptyList()); sessionRegistry.setRoute("Alias2", Secondary.class, Collections.emptyList()); }); Assert.assertEquals( "One event for both registries should have been fired.", 2, events.size()); registration.remove(); sessionRegistry.removeRoute("main"); Assert.assertEquals( "No new event should have been received for session scope", 2, events.size()); registry.removeRoute("main"); Assert.assertEquals( "No new event should have been received for application scope", 2, events.size()); }