Search in sources :

Example 1 with RouteBaseData

use of com.vaadin.flow.router.RouteBaseData in project flow by vaadin.

the class ApplicationRouteRegistryTest method routeChangeListener_blockChangesAreGivenCorrectlyInEvent.

@Test
public void routeChangeListener_blockChangesAreGivenCorrectlyInEvent() {
    getTestedRegistry().setRoute("", MyRoute.class, Collections.emptyList());
    List<RouteBaseData> added = new ArrayList<>();
    List<RouteBaseData> removed = new ArrayList<>();
    getTestedRegistry().addRoutesChangeListener(event -> {
        added.clear();
        removed.clear();
        added.addAll(event.getAddedRoutes());
        removed.addAll(event.getRemovedRoutes());
    });
    getTestedRegistry().update(() -> {
        getTestedRegistry().removeRoute("");
        getTestedRegistry().setRoute("path", Secondary.class, Collections.emptyList());
        getTestedRegistry().setRoute("", MyRoute.class, Collections.singletonList(MainLayout.class));
    });
    Assert.assertFalse("", added.isEmpty());
    Assert.assertEquals("", 2, added.size());
    Assert.assertFalse("", removed.isEmpty());
    for (RouteBaseData data : added) {
        if (data.getUrl().equals("")) {
            Assert.assertEquals("MyRoute should have been added", MyRoute.class, data.getNavigationTarget());
            Assert.assertEquals("MyRoute should have been seen as a update as the parent layouts changed.", MainLayout.class, data.getParentLayout());
        } else {
            Assert.assertEquals("", Secondary.class, data.getNavigationTarget());
        }
    }
    Assert.assertEquals("MyRoute should have been both removed and added", MyRoute.class, removed.get(0).getNavigationTarget());
    Assert.assertEquals("Removed version should not have a parent layout", Collections.emptyList(), removed.get(0).getParentLayouts());
}
Also used : RouteBaseData(com.vaadin.flow.router.RouteBaseData) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 2 with RouteBaseData

use of com.vaadin.flow.router.RouteBaseData in project flow by vaadin.

the class AbstractRouteRegistryTest method routeChangeListener_blockChangesAreGivenCorrectlyInEvent.

@Test
public void routeChangeListener_blockChangesAreGivenCorrectlyInEvent() {
    registry.setRoute("", MyRoute.class, Collections.emptyList());
    List<RouteBaseData> added = new ArrayList<>();
    List<RouteBaseData> removed = new ArrayList<>();
    registry.addRoutesChangeListener(event -> {
        added.clear();
        removed.clear();
        added.addAll(event.getAddedRoutes());
        removed.addAll(event.getRemovedRoutes());
    });
    registry.update(() -> {
        registry.removeRoute("");
        registry.setRoute("path", Secondary.class, Collections.emptyList());
        registry.setRoute("", MyRoute.class, Collections.singletonList(MainLayout.class));
    });
    Assert.assertFalse("", added.isEmpty());
    Assert.assertEquals("", 2, added.size());
    Assert.assertFalse("", removed.isEmpty());
    for (RouteBaseData data : added) {
        if (data.getUrl().equals("")) {
            Assert.assertEquals("MyRoute should have been added", MyRoute.class, data.getNavigationTarget());
            Assert.assertEquals("MyRoute should have been seen as a update as the parent layouts changed.", MainLayout.class, data.getParentLayout());
        } else {
            Assert.assertEquals("", Secondary.class, data.getNavigationTarget());
        }
    }
    Assert.assertEquals("MyRoute should have been both removed and added", MyRoute.class, removed.get(0).getNavigationTarget());
    Assert.assertEquals("Removed version should not have a parent layout", Collections.emptyList(), removed.get(0).getParentLayouts());
}
Also used : RouteBaseData(com.vaadin.flow.router.RouteBaseData) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with RouteBaseData

use of com.vaadin.flow.router.RouteBaseData in project flow by vaadin.

the class SessionRouteRegistryTest method routeChangeListener_blockChangesAreGivenCorrectlyInEvent.

@Test
public void routeChangeListener_blockChangesAreGivenCorrectlyInEvent() {
    SessionRouteRegistry registry = getRegistry(session);
    registry.setRoute("", MyRoute.class, Collections.emptyList());
    List<RouteBaseData> added = new ArrayList<>();
    List<RouteBaseData> removed = new ArrayList<>();
    registry.addRoutesChangeListener(event -> {
        added.clear();
        removed.clear();
        added.addAll(event.getAddedRoutes());
        removed.addAll(event.getRemovedRoutes());
    });
    registry.update(() -> {
        registry.removeRoute("");
        registry.setRoute("path", Secondary.class, Collections.emptyList());
        registry.setRoute("", MyRoute.class, Collections.singletonList(MainLayout.class));
    });
    Assert.assertEquals("Two ne paths should have been added", 2, added.size());
    Assert.assertEquals("One path should have been removed", 1, removed.size());
    for (RouteBaseData data : added) {
        if (data.getUrl().equals("")) {
            Assert.assertEquals("MyRoute should have been added", MyRoute.class, data.getNavigationTarget());
            Assert.assertEquals("MyRoute should have been seen as a update as the parent layouts changed.", MainLayout.class, data.getParentLayout());
        } else {
            Assert.assertEquals("", Secondary.class, data.getNavigationTarget());
        }
    }
    Assert.assertEquals("One MyRoute should have been removed", MyRoute.class, removed.get(0).getNavigationTarget());
    Assert.assertEquals("Removed version should not have a parent layout", Collections.emptyList(), removed.get(0).getParentLayouts());
}
Also used : RouteBaseData(com.vaadin.flow.router.RouteBaseData) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 4 with RouteBaseData

use of com.vaadin.flow.router.RouteBaseData in project flow by vaadin.

the class AbstractRouteRegistry method unlock.

private void unlock() {
    if (configurationLock.getHoldCount() == 1 && editing != null) {
        try {
            ConfiguredRoutes oldConfiguration = configuredRoutes;
            configuredRoutes = new ConfiguredRoutes(editing);
            if (!routesChangedListeners.isEmpty()) {
                List<RouteBaseData<?>> oldRoutes = flattenRoutes(getRegisteredRoutes(oldConfiguration));
                List<RouteBaseData<?>> newRoutes = flattenRoutes(getRegisteredRoutes(configuredRoutes));
                List<RouteBaseData<?>> added = new ArrayList<>();
                List<RouteBaseData<?>> removed = new ArrayList<>();
                oldRoutes.stream().filter(route -> !newRoutes.contains(route)).forEach(removed::add);
                newRoutes.stream().filter(route -> !oldRoutes.contains(route)).forEach(added::add);
                fireEvent(new RoutesChangedEvent(this, added, removed));
            }
        } finally {
            editing = null;
            configurationLock.unlock();
        }
    } else {
        configurationLock.unlock();
    }
}
Also used : RouteAliasData(com.vaadin.flow.router.RouteAliasData) Component(com.vaadin.flow.component.Component) SerializableBiConsumer(com.vaadin.flow.function.SerializableBiConsumer) Registration(com.vaadin.flow.shared.Registration) ArrayList(java.util.ArrayList) Map(java.util.Map) Command(com.vaadin.flow.server.Command) HasErrorParameter(com.vaadin.flow.router.HasErrorParameter) NotFoundException(com.vaadin.flow.router.NotFoundException) RouterLayout(com.vaadin.flow.router.RouterLayout) ReentrantLock(java.util.concurrent.locks.ReentrantLock) RouteData(com.vaadin.flow.router.RouteData) RouteRegistry(com.vaadin.flow.server.RouteRegistry) ReflectTools(com.vaadin.flow.internal.ReflectTools) RoutesChangedListener(com.vaadin.flow.router.RoutesChangedListener) Serializable(java.io.Serializable) InvalidRouteConfigurationException(com.vaadin.flow.server.InvalidRouteConfigurationException) Objects(java.util.Objects) List(java.util.List) Optional(java.util.Optional) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent) RouteBaseData(com.vaadin.flow.router.RouteBaseData) Collections(java.util.Collections) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RouteParameters(com.vaadin.flow.router.RouteParameters) RouteBaseData(com.vaadin.flow.router.RouteBaseData) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RoutesChangedEvent(com.vaadin.flow.router.RoutesChangedEvent)

Example 5 with RouteBaseData

use of com.vaadin.flow.router.RouteBaseData in project flow by vaadin.

the class AbstractRouteRegistry method flattenRoutes.

/**
 * Flatten route data so that all route aliases are also as their own
 * entries in the list. Removes any route aliases as the route is the same
 * even if aliases change.
 *
 * @param routeData
 *            route data to flatten.
 * @return flattened list of routes and aliases
 */
private List<RouteBaseData<?>> flattenRoutes(List<RouteData> routeData) {
    List<RouteBaseData<?>> flatRoutes = new ArrayList<>();
    for (RouteData route : routeData) {
        RouteData nonAliasCollection = new RouteData(route.getParentLayouts(), route.getTemplate(), route.getRouteParameters(), route.getNavigationTarget(), Collections.emptyList());
        flatRoutes.add(nonAliasCollection);
        route.getRouteAliases().forEach(flatRoutes::add);
    }
    return flatRoutes;
}
Also used : RouteBaseData(com.vaadin.flow.router.RouteBaseData) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) RouteData(com.vaadin.flow.router.RouteData)

Aggregations

RouteBaseData (com.vaadin.flow.router.RouteBaseData)6 ArrayList (java.util.ArrayList)6 RouteData (com.vaadin.flow.router.RouteData)3 Test (org.junit.Test)3 Component (com.vaadin.flow.component.Component)2 RouteParameters (com.vaadin.flow.router.RouteParameters)2 RouterLayout (com.vaadin.flow.router.RouterLayout)2 RoutesChangedEvent (com.vaadin.flow.router.RoutesChangedEvent)2 RoutesChangedListener (com.vaadin.flow.router.RoutesChangedListener)2 Registration (com.vaadin.flow.shared.Registration)2 List (java.util.List)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 SerializableBiConsumer (com.vaadin.flow.function.SerializableBiConsumer)1 ReflectTools (com.vaadin.flow.internal.ReflectTools)1 HasErrorParameter (com.vaadin.flow.router.HasErrorParameter)1 NotFoundException (com.vaadin.flow.router.NotFoundException)1 RouteAliasData (com.vaadin.flow.router.RouteAliasData)1 AbstractRouteRegistry (com.vaadin.flow.router.internal.AbstractRouteRegistry)1