use of com.vaadin.flow.router.Router in project flow by vaadin.
the class BootstrapHandlerTest method setup.
@Before
public void setup() {
TestRouteRegistry routeRegistry = new TestRouteRegistry();
BootstrapHandler.clientEngineFile = "foobar";
testUI = new TestUI();
deploymentConfiguration = new MockDeploymentConfiguration("test/");
service = Mockito.spy(new MockVaadinServletService(deploymentConfiguration));
Mockito.when(service.getRouteRegistry()).thenReturn(routeRegistry);
Mockito.when(service.getRouter()).thenReturn(new Router(routeRegistry) {
@Override
public void initializeUI(UI ui, VaadinRequest initRequest) {
// injected
if (routeRegistry.hasNavigationTargets()) {
super.initializeUI(ui, initRequest);
}
}
});
session = Mockito.spy(new MockVaadinSession(service));
session.lock();
session.setConfiguration(deploymentConfiguration);
testUI.getInternals().setSession(session);
browser = Mockito.mock(WebBrowser.class);
Mockito.when(browser.isEs6Supported()).thenReturn(false);
Mockito.when(session.getBrowser()).thenReturn(browser);
}
use of com.vaadin.flow.router.Router in project flow by vaadin.
the class BootstrapUtils method createInitialPageSettingsObject.
private static InitialPageSettings createInitialPageSettingsObject(BootstrapHandler.BootstrapContext context) {
UI ui = context.getUI();
VaadinRequest request = context.getRequest();
WebBrowser browser = context.getSession().getBrowser();
String pathInfo = request.getPathInfo();
if (pathInfo == null) {
pathInfo = "";
} else {
assert pathInfo.startsWith("/");
pathInfo = pathInfo.substring(1);
}
Optional<Router> router = ui.getRouter();
NavigationEvent navigationEvent = new NavigationEvent(router.isPresent() ? router.get() : null, new Location(pathInfo, QueryParameters.full(request.getParameterMap())), ui, NavigationTrigger.PAGE_LOAD);
List<HasElement> components = ui.getChildren().map(component -> (HasElement) component).collect(Collectors.toList());
AfterNavigationEvent afterNavigationEvent = new AfterNavigationEvent(RouterUtil.createEvent(navigationEvent, components));
return new InitialPageSettings(request, ui, afterNavigationEvent, browser);
}
use of com.vaadin.flow.router.Router in project flow by vaadin.
the class LocationObserverTest method navigation_and_locale_change_should_fire_locale_change_observer.
@Test
public void navigation_and_locale_change_should_fire_locale_change_observer() throws InvalidRouteConfigurationException {
router = new Router(new TestRouteRegistry());
ui = new RouterTestUI(router);
router.getRegistry().setNavigationTargets(Collections.singleton(Translations.class));
ui.navigate("");
Assert.assertEquals("Expected event amount was wrong", 1, eventCollector.size());
Assert.assertEquals("Received locale change event for locale: " + Locale.getDefault().getDisplayName(), eventCollector.get(0));
ui.setLocale(Locale.CANADA);
Assert.assertEquals("Expected event amount was wrong", 2, eventCollector.size());
Assert.assertEquals("Received locale change event for locale: " + Locale.CANADA.getDisplayName(), eventCollector.get(1));
}
use of com.vaadin.flow.router.Router in project flow by vaadin.
the class VaadinService method init.
/**
* Initializes this service. The service should be initialized before it is
* used.
*
* @since 7.1
* @throws ServiceException
* if a problem occurs when creating the service
*/
public void init() throws ServiceException {
instantiator = createInstantiator();
List<RequestHandler> handlers = createRequestHandlers();
ServiceInitEvent event = new ServiceInitEvent(this);
instantiator.getServiceInitListeners().forEach(listener -> listener.serviceInit(event));
event.getAddedRequestHandlers().forEach(handlers::add);
Collections.reverse(handlers);
requestHandlers = Collections.unmodifiableCollection(handlers);
dependencyFilters = instantiator.getDependencyFilters(event.getAddedDependencyFilters()).collect(Collectors.toList());
bootstrapListeners = instantiator.getBootstrapListeners(event.getAddedBootstrapListeners()).collect(Collectors.toList());
DeploymentConfiguration deploymentConf = getDeploymentConfiguration();
if (deploymentConf.isUsingNewRouting()) {
router = new Router(getRouteRegistry());
} else {
router = new com.vaadin.flow.router.legacy.Router();
String routerConfiguratorClassName = deploymentConf.getRouterConfiguratorClassName();
if (routerConfiguratorClassName != null && !RouterConfigurator.class.getName().equals(routerConfiguratorClassName)) {
// Configure router if we have a non-default configurator type
configureRouter(routerConfiguratorClassName);
}
}
initialized = true;
}
use of com.vaadin.flow.router.Router in project flow by vaadin.
the class NavigationStateRendererTest method instantiatorUse.
@Test
public void instantiatorUse() throws ServiceException {
MockVaadinServletService service = new MockVaadinServletService();
service.init(new MockInstantiator() {
@Override
public <T extends HasElement> T createRouteTarget(Class<T> routeTargetType, NavigationEvent event) {
Assert.assertEquals(Component.class, routeTargetType);
return (T) new Text("foo");
}
});
MockUI ui = new MockUI(new MockVaadinSession(service));
NavigationEvent event = new NavigationEvent(new Router(new TestRouteRegistry()), new Location(""), ui, NavigationTrigger.PAGE_LOAD);
NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(ChildConfiguration.class));
Component routeTarget = renderer.getRouteTarget(Component.class, event);
Assert.assertEquals(Text.class, routeTarget.getClass());
UI.setCurrent(null);
}
Aggregations