Search in sources :

Example 1 with Router

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);
}
Also used : UI(com.vaadin.flow.component.UI) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Router(com.vaadin.flow.router.Router) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) Before(org.junit.Before)

Example 2 with Router

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);
}
Also used : Inline(com.vaadin.flow.component.page.Inline) TargetElement(com.vaadin.flow.component.page.TargetElement) Json(elemental.json.Json) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) RouterUtil(com.vaadin.flow.router.internal.RouterUtil) Dependency(com.vaadin.flow.shared.ui.Dependency) Router(com.vaadin.flow.router.Router) Route(com.vaadin.flow.router.Route) Theme(com.vaadin.flow.theme.Theme) Charset(java.nio.charset.Charset) Map(java.util.Map) Location(com.vaadin.flow.router.Location) UI(com.vaadin.flow.component.UI) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) NavigationTrigger(com.vaadin.flow.router.NavigationTrigger) QueryParameters(com.vaadin.flow.router.QueryParameters) RouterLayout(com.vaadin.flow.router.RouterLayout) EnumMap(java.util.EnumMap) HasElement(com.vaadin.flow.component.HasElement) ReflectTools(com.vaadin.flow.internal.ReflectTools) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) List(java.util.List) Stream(java.util.stream.Stream) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent) LoadMode(com.vaadin.flow.shared.ui.LoadMode) BodySize(com.vaadin.flow.component.page.BodySize) Optional(java.util.Optional) JsonObject(elemental.json.JsonObject) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) Viewport(com.vaadin.flow.component.page.Viewport) NavigationEvent(com.vaadin.flow.router.NavigationEvent) ParentLayout(com.vaadin.flow.router.ParentLayout) InputStream(java.io.InputStream) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent) NavigationEvent(com.vaadin.flow.router.NavigationEvent) Router(com.vaadin.flow.router.Router) UI(com.vaadin.flow.component.UI) HasElement(com.vaadin.flow.component.HasElement) Location(com.vaadin.flow.router.Location) AfterNavigationEvent(com.vaadin.flow.router.AfterNavigationEvent)

Example 3 with Router

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));
}
Also used : Router(com.vaadin.flow.router.Router) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) Test(org.junit.Test)

Example 4 with Router

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;
}
Also used : StreamRequestHandler(com.vaadin.flow.server.communication.StreamRequestHandler) UidlRequestHandler(com.vaadin.flow.server.communication.UidlRequestHandler) SessionRequestHandler(com.vaadin.flow.server.communication.SessionRequestHandler) Router(com.vaadin.flow.router.Router) RouterConfigurator(com.vaadin.flow.router.legacy.RouterConfigurator) DeploymentConfiguration(com.vaadin.flow.function.DeploymentConfiguration)

Example 5 with Router

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);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) Router(com.vaadin.flow.router.Router) Text(com.vaadin.flow.component.Text) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) MockUI(com.vaadin.tests.util.MockUI) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) MockInstantiator(com.vaadin.flow.server.MockInstantiator) Component(com.vaadin.flow.component.Component) Location(com.vaadin.flow.router.Location) NavigationStateRenderer(com.vaadin.flow.router.internal.NavigationStateRenderer) Test(org.junit.Test)

Aggregations

Router (com.vaadin.flow.router.Router)5 TestRouteRegistry (com.vaadin.flow.router.TestRouteRegistry)3 UI (com.vaadin.flow.component.UI)2 Location (com.vaadin.flow.router.Location)2 NavigationEvent (com.vaadin.flow.router.NavigationEvent)2 Component (com.vaadin.flow.component.Component)1 HasElement (com.vaadin.flow.component.HasElement)1 Text (com.vaadin.flow.component.Text)1 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)1 BodySize (com.vaadin.flow.component.page.BodySize)1 Inline (com.vaadin.flow.component.page.Inline)1 TargetElement (com.vaadin.flow.component.page.TargetElement)1 Viewport (com.vaadin.flow.component.page.Viewport)1 DeploymentConfiguration (com.vaadin.flow.function.DeploymentConfiguration)1 ReflectTools (com.vaadin.flow.internal.ReflectTools)1 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)1 NavigationTrigger (com.vaadin.flow.router.NavigationTrigger)1 ParentLayout (com.vaadin.flow.router.ParentLayout)1 QueryParameters (com.vaadin.flow.router.QueryParameters)1 Route (com.vaadin.flow.router.Route)1