use of com.vaadin.flow.router.Location in project flow by vaadin.
the class RouterConfigurationTest method testRoutesCopied.
@Test
public void testRoutesCopied() throws Exception {
RouterConfiguration original = createConfiguration();
original.setRoute("foo/bar", createNoopHandler());
RouterConfiguration copy = new RouterConfiguration(original, false);
original.removeRoute("foo/bar");
Assert.assertNotNull("Updating the original should not affect the copy", copy.resolveRoute(new Location("foo/bar")));
}
use of com.vaadin.flow.router.Location in project flow by vaadin.
the class RouterTest method testChangeLocation.
@Test
public void testChangeLocation() {
UI ui = new RouterTestUI();
Router router = new Router();
TestResolver resolver = new TestResolver();
router.reconfigure(c -> c.setResolver(resolver));
VaadinRequest request = requestWithPathInfo(null);
router.initializeUI(ui, request);
Assert.assertEquals(Arrays.asList(""), resolver.resolvedLocation.get().getSegments());
resolver.resolvedLocation.set(null);
resolver.handledEvent.set(null);
ui.getPage().getHistory().getHistoryStateChangeHandler().onHistoryStateChange(new HistoryStateChangeEvent(ui.getPage().getHistory(), null, new Location("foo"), NavigationTrigger.HISTORY));
Assert.assertEquals(Arrays.asList("foo"), resolver.resolvedLocation.get().getSegments());
}
use of com.vaadin.flow.router.Location in project flow by vaadin.
the class RouterTest method testResolverError_noCurrentResponse.
@Test
public void testResolverError_noCurrentResponse() {
UI ui = new RouterTestUI();
Router router = new Router();
router.reconfigure(c -> c.setResolver(event -> Optional.empty()));
router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC);
Assert.assertTrue(ui.getElement().getTextRecursively().contains("404"));
}
use of com.vaadin.flow.router.Location in project flow by vaadin.
the class RouterTest method testNavigateToEmptyLocation_triggersDefaultErrorView.
@Test
public void testNavigateToEmptyLocation_triggersDefaultErrorView() {
UI ui = new RouterTestUI();
Router router = new Router();
router.reconfigure(c -> {
});
router.navigate(ui, new Location(""), NavigationTrigger.PROGRAMMATIC);
Assert.assertEquals(new DefaultErrorView().getText(), ui.getElement().getTextRecursively());
}
use of com.vaadin.flow.router.Location in project flow by vaadin.
the class ViewRenderer method extractRoutePlaceholders.
private static Map<String, String> extractRoutePlaceholders(Location destination, RouteLocation routeDefinition) {
assert destination != null;
assert routeDefinition != null;
Map<String, String> routePlaceholders = new HashMap<>();
routeDefinition.visitSegments(new RouteSegmentVisitor() {
Optional<Location> maybeCurrentDestination = Optional.of(destination);
private String getNextDestinationSegment() {
Location currentDestination = maybeCurrentDestination.get();
/*
* This might return an empty optional, but in that case there
* shouldn't be any more calls to any accept method, as long as
* the current destination actually matches the route
* definition.
*/
maybeCurrentDestination = currentDestination.getSubLocation();
return currentDestination.getFirstSegment();
}
@Override
public void acceptWildcard() {
// This is always the last step, so not need to advance the
// destination
routePlaceholders.put("*", maybeCurrentDestination.get().getPath());
}
@Override
public void acceptSegment(String segmentName) {
// Just advance the current destination
getNextDestinationSegment();
}
@Override
public void acceptPlaceholder(String placeholderName) {
String placeholderValue = getNextDestinationSegment();
assert !routePlaceholders.containsKey(placeholderName);
routePlaceholders.put(placeholderName, placeholderValue);
}
});
return routePlaceholders;
}
Aggregations