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());
}
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());
}
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());
}
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();
}
}
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;
}
Aggregations