Search in sources :

Example 16 with NavigationEvent

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

the class RouterConfigurationTest method defaultErrorHandlerStatusCode.

@Test
public void defaultErrorHandlerStatusCode() {
    Router router = new Router();
    int statusCode = router.getConfiguration().getErrorHandler().handle(new NavigationEvent(router, new Location(""), new UI(), NavigationTrigger.PROGRAMMATIC));
    Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, statusCode);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) UI(com.vaadin.flow.component.UI) Router(com.vaadin.flow.router.legacy.Router) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 17 with NavigationEvent

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

the class ViewRendererTest method testViewInstantiationCustomization.

@Test
public void testViewInstantiationCustomization() {
    // override default implementation of reusing the views if possible
    ViewRenderer renderer = new TestViewRenderer(TestView.class, ParentView.class) {

        @Override
        protected <T extends View> T getView(Class<T> viewType, NavigationEvent event) {
            // always return a new view
            return ReflectTools.createInstance(viewType);
        }
    };
    renderer.handle(dummyEvent);
    View view1 = dummyEvent.getUI().getInternals().getActiveViewChain().get(0);
    View parentView1 = dummyEvent.getUI().getInternals().getActiveViewChain().get(1);
    renderer.handle(dummyEvent);
    View view2 = dummyEvent.getUI().getInternals().getActiveViewChain().get(0);
    View parentView2 = dummyEvent.getUI().getInternals().getActiveViewChain().get(1);
    Assert.assertNotSame(view1, view2);
    Assert.assertNotSame(parentView1, parentView2);
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) ViewRenderer(com.vaadin.flow.router.legacy.ViewRenderer) HasChildView(com.vaadin.flow.router.legacy.HasChildView) DefaultErrorView(com.vaadin.flow.router.legacy.DefaultErrorView) View(com.vaadin.flow.router.legacy.View) Test(org.junit.Test)

Example 18 with NavigationEvent

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

the class NavigationStateRendererTest method handle_RouterLinkTriggerNullState_IllegalStateException.

@Test
public void handle_RouterLinkTriggerNullState_IllegalStateException() {
    // given a service with instantiator
    MockVaadinServletService service = createMockServiceWithInstantiator();
    service.setRouter(router);
    // given a locked session
    MockVaadinSession session = new AlwaysLockedVaadinSession(service);
    session.setConfiguration(new MockDeploymentConfiguration());
    // given a NavigationStateRenderer mapping to RegularView
    new NavigationStateBuilder(router).withTarget(RegularView.class).build();
    NavigationStateRenderer renderer = new NavigationStateRenderer(navigationStateFromTarget(RegularView.class));
    MockUI ui = new MockUI(session);
    expectedException.expect(IllegalStateException.class);
    renderer.handle(new NavigationEvent(router, new Location("regular"), ui, NavigationTrigger.ROUTER_LINK, null, false));
}
Also used : NavigationStateBuilder(com.vaadin.flow.router.NavigationStateBuilder) MockUI(com.vaadin.tests.util.MockUI) NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) AlwaysLockedVaadinSession(com.vaadin.tests.util.AlwaysLockedVaadinSession) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Location(com.vaadin.flow.router.Location) Test(org.junit.Test)

Example 19 with NavigationEvent

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

the class NavigationStateRendererTest method createMockServiceWithInstantiator.

private MockVaadinServletService createMockServiceWithInstantiator() {
    MockVaadinServletService service = new MockVaadinServletService();
    service.init(new MockInstantiator() {

        @Override
        public <T extends HasElement> T createRouteTarget(Class<T> routeTargetType, NavigationEvent event) {
            try {
                return routeTargetType.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    });
    return service;
}
Also used : NavigationEvent(com.vaadin.flow.router.NavigationEvent) MockVaadinServletService(com.vaadin.flow.server.MockVaadinServletService) MockInstantiator(com.vaadin.flow.server.MockInstantiator)

Example 20 with NavigationEvent

use of com.vaadin.flow.router.NavigationEvent 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) Test(org.junit.Test)

Aggregations

NavigationEvent (com.vaadin.flow.router.NavigationEvent)25 Location (com.vaadin.flow.router.Location)19 Test (org.junit.Test)15 Router (com.vaadin.flow.router.Router)11 UI (com.vaadin.flow.component.UI)10 MockVaadinServletService (com.vaadin.flow.server.MockVaadinServletService)10 MockUI (com.vaadin.tests.util.MockUI)10 MockVaadinSession (com.vaadin.flow.server.MockVaadinSession)9 AlwaysLockedVaadinSession (com.vaadin.tests.util.AlwaysLockedVaadinSession)8 ExtendedClientDetails (com.vaadin.flow.component.page.ExtendedClientDetails)7 TestRouteRegistry (com.vaadin.flow.router.TestRouteRegistry)7 MockDeploymentConfiguration (com.vaadin.tests.util.MockDeploymentConfiguration)7 Component (com.vaadin.flow.component.Component)6 ErrorNavigationEvent (com.vaadin.flow.router.ErrorNavigationEvent)6 HasElement (com.vaadin.flow.component.HasElement)5 AfterNavigationEvent (com.vaadin.flow.router.AfterNavigationEvent)5 NavigationState (com.vaadin.flow.router.NavigationState)5 NavigationStateBuilder (com.vaadin.flow.router.NavigationStateBuilder)5 Page (com.vaadin.flow.component.page.Page)4 Element (com.vaadin.flow.dom.Element)4