Search in sources :

Example 31 with BootstrapContext

use of com.vaadin.flow.server.BootstrapHandler.BootstrapContext in project flow by vaadin.

the class BootstrapHandlerTest method initUI.

private void initUI(UI ui, VaadinRequest request, Set<Class<? extends Component>> navigationTargets) throws InvalidRouteConfigurationException {
    service.getRouteRegistry().setNavigationTargets(navigationTargets);
    this.request = request;
    service.init();
    ui.doInit(request, 0);
    context = new BootstrapContext(request, null, session, ui);
}
Also used : BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext)

Example 32 with BootstrapContext

use of com.vaadin.flow.server.BootstrapHandler.BootstrapContext in project flow by vaadin.

the class BootstrapHandlerTest method bootstrapPage_configJsonPatternIsReplacedBeforeInitialUidl.

@Test
public void bootstrapPage_configJsonPatternIsReplacedBeforeInitialUidl() {
    TestUI anotherUI = new TestUI();
    initUI(testUI);
    SystemMessages messages = Mockito.mock(SystemMessages.class);
    Mockito.when(service.getSystemMessages(Matchers.any(Locale.class), Matchers.any(VaadinRequest.class))).thenReturn(messages);
    Mockito.when(messages.isSessionExpiredNotificationEnabled()).thenReturn(true);
    Mockito.when(session.getSession()).thenReturn(Mockito.mock(WrappedSession.class));
    String url = "http://{{CONFIG_JSON}}/file";
    Mockito.when(messages.getSessionExpiredURL()).thenReturn(url);
    anotherUI.getInternals().setSession(session);
    VaadinRequest vaadinRequest = createVaadinRequest();
    anotherUI.doInit(vaadinRequest, 0);
    BootstrapContext bootstrapContext = new BootstrapContext(vaadinRequest, null, session, anotherUI);
    Document page = BootstrapHandler.getBootstrapPage(bootstrapContext);
    Element head = page.head();
    Assert.assertTrue(head.outerHtml().contains(url));
}
Also used : Locale(java.util.Locale) TargetElement(com.vaadin.flow.component.page.TargetElement) Element(org.jsoup.nodes.Element) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 33 with BootstrapContext

use of com.vaadin.flow.server.BootstrapHandler.BootstrapContext in project flow by vaadin.

the class BootstrapHandlerTest method theme_contents_are_appended_to_head.

// 3197
@Test
public void theme_contents_are_appended_to_head() throws InvalidRouteConfigurationException {
    initUI(testUI, createVaadinRequest(), Collections.singleton(MyThemeTest.class));
    Document page = BootstrapHandler.getBootstrapPage(new BootstrapContext(request, null, session, testUI));
    Elements allElements = page.head().getAllElements();
    Assert.assertTrue("Custom style should have been added to head.", allElements.stream().map(Object::toString).anyMatch(element -> element.equals("<link rel=\"import\" href=\"./frontend/bower_components/vaadin-lumo-styles/color.html\">")));
    allElements = page.body().getAllElements();
    // Note element 0 is the full head element.
    assertStringEquals("Custom style should have been added to head.", "<custom-style><style include=\"lumo-typography\"></style></custom-style>", allElements.get(2).toString());
    Assert.assertTrue("Style should have been wrapped in custom style", page.body().toString().contains("<custom-style><style include=\"lumo-typography\"></style></custom-style>"));
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) Inline(com.vaadin.flow.component.page.Inline) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) TargetElement(com.vaadin.flow.component.page.TargetElement) Registration(com.vaadin.flow.shared.Registration) PageTitle(com.vaadin.flow.router.PageTitle) Router(com.vaadin.flow.router.Router) Route(com.vaadin.flow.router.Route) RouteAlias(com.vaadin.flow.router.RouteAlias) Theme(com.vaadin.flow.theme.Theme) Assert.assertThat(org.junit.Assert.assertThat) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) After(org.junit.After) UI(com.vaadin.flow.component.UI) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) BodySize(com.vaadin.flow.component.page.BodySize) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Elements(org.jsoup.select.Elements) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Matchers(org.mockito.Matchers) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Before(org.junit.Before) Text(com.vaadin.flow.component.Text) RouterLayout(com.vaadin.flow.router.RouterLayout) Html(com.vaadin.flow.component.Html) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) InlineTemplate(com.vaadin.flow.template.angular.InlineTemplate) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Mockito(org.mockito.Mockito) Assert(org.junit.Assert) Collections(java.util.Collections) Viewport(com.vaadin.flow.component.page.Viewport) ParentLayout(com.vaadin.flow.router.ParentLayout) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Example 34 with BootstrapContext

use of com.vaadin.flow.server.BootstrapHandler.BootstrapContext in project flow by vaadin.

the class BootstrapHandlerTest method useDependencyFilters_removeDependenciesAndAddNewOnes.

@Test
public void useDependencyFilters_removeDependenciesAndAddNewOnes() throws ServiceException {
    List<DependencyFilter> filters = new ArrayList<>(5);
    filters.add((list, context) -> {
        // remove everything
        list.clear();
        return list;
    });
    filters.add((list, context) -> {
        list.add(new Dependency(Dependency.Type.HTML_IMPORT, "imported-by-filter.html", LoadMode.EAGER));
        return list;
    });
    filters.add((list, context) -> {
        list.add(new Dependency(Dependency.Type.JAVASCRIPT, "imported-by-filter.js", LoadMode.EAGER));
        list.add(new Dependency(Dependency.Type.JAVASCRIPT, "imported-by-filter2.js", LoadMode.EAGER));
        return list;
    });
    filters.add((list, context) -> {
        // removes the imported-by-filter2.js
        list.remove(2);
        return list;
    });
    filters.add((list, context) -> {
        list.add(new Dependency(Dependency.Type.STYLESHEET, "imported-by-filter.css", LoadMode.EAGER));
        return list;
    });
    Mockito.when(service.createInstantiator()).thenReturn(new MockInstantiator(event -> filters.forEach(event::addDependencyFilter)));
    initUI(testUI);
    BootstrapContext bootstrapContext = new BootstrapContext(request, null, session, testUI);
    Document page = BootstrapHandler.getBootstrapPage(bootstrapContext);
    Elements scripts = page.head().getElementsByTag("script");
    boolean found = scripts.stream().anyMatch(element -> element.attr("src").equals("./frontend/imported-by-filter.js"));
    Assert.assertTrue("imported-by-filter.js should be in the head of the page", found);
    found = scripts.stream().anyMatch(element -> element.attr("src").equals("./frontend/imported-by-filter2.js"));
    Assert.assertFalse("imported-by-filter2.js shouldn't be in the head of the page", found);
    found = scripts.stream().anyMatch(element -> element.attr("src").equals("./eager.js"));
    Assert.assertFalse("eager.js shouldn't be in the head of the page", found);
    Elements links = page.head().getElementsByTag("link");
    found = links.stream().anyMatch(element -> element.attr("href").equals("./frontend/imported-by-filter.css"));
    Assert.assertTrue("imported-by-filter.css should be in the head of the page", found);
    found = links.stream().anyMatch(element -> element.attr("href").equals("./frontend/imported-by-filter.html"));
    Assert.assertTrue("imported-by-filter.html should be in the head of the page", found);
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) Inline(com.vaadin.flow.component.page.Inline) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) TargetElement(com.vaadin.flow.component.page.TargetElement) Registration(com.vaadin.flow.shared.Registration) PageTitle(com.vaadin.flow.router.PageTitle) Router(com.vaadin.flow.router.Router) Route(com.vaadin.flow.router.Route) RouteAlias(com.vaadin.flow.router.RouteAlias) Theme(com.vaadin.flow.theme.Theme) Assert.assertThat(org.junit.Assert.assertThat) Locale(java.util.Locale) Element(org.jsoup.nodes.Element) After(org.junit.After) UI(com.vaadin.flow.component.UI) Set(java.util.Set) StandardCharsets(java.nio.charset.StandardCharsets) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) Assert.assertFalse(org.junit.Assert.assertFalse) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) BodySize(com.vaadin.flow.component.page.BodySize) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Elements(org.jsoup.select.Elements) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) Matchers(org.mockito.Matchers) TestRouteRegistry(com.vaadin.flow.router.TestRouteRegistry) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) AbstractTheme(com.vaadin.flow.theme.AbstractTheme) Before(org.junit.Before) Text(com.vaadin.flow.component.Text) RouterLayout(com.vaadin.flow.router.RouterLayout) Html(com.vaadin.flow.component.Html) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) InlineTemplate(com.vaadin.flow.template.angular.InlineTemplate) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Mockito(org.mockito.Mockito) Assert(org.junit.Assert) Collections(java.util.Collections) Viewport(com.vaadin.flow.component.page.Viewport) ParentLayout(com.vaadin.flow.router.ParentLayout) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Dependency(com.vaadin.flow.shared.ui.Dependency) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Example 35 with BootstrapContext

use of com.vaadin.flow.server.BootstrapHandler.BootstrapContext in project flow by vaadin.

the class BootstrapHandlerTest method use_inline_to_prepend_files_to_body.

// 3010
@Test
public void use_inline_to_prepend_files_to_body() throws InvalidRouteConfigurationException {
    initUI(testUI, createVaadinRequest(), Collections.singleton(PrependInlineAnnotationsBodyTarget.class));
    Document page = BootstrapHandler.getBootstrapPage(new BootstrapContext(request, null, session, testUI));
    Elements allElements = page.body().getAllElements();
    // Note element 0 is the full head element.
    assertStringEquals("File javascript should have been prepended to body element", "<script type=\"text/javascript\">window.messages = window.messages || [];\n" + "window.messages.push(\"inline.js\");</script>", allElements.get(1).toString());
    assertStringEquals("File html should have been prepended to body element", "<script type=\"text/javascript\">\n" + "    // document.body might not yet be accessible, so just leave a message\n" + "    window.messages = window.messages || [];\n" + "    window.messages.push(\"inline.html\");\n" + "</script>", allElements.get(2).toString());
    assertStringEquals("File css should have been prepended to body element", "<style type=\"text/css\">/* inline.css */\n" + "\n" + "#preloadedDiv {\n" + "    color: rgba(255, 255, 0, 1);\n" + "}\n" + "\n" + "#inlineCssTestDiv {\n" + "    color: rgba(255, 255, 0, 1);\n" + "}</style>", allElements.get(3).toString());
}
Also used : BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) Test(org.junit.Test)

Aggregations

BootstrapContext (com.vaadin.flow.server.BootstrapHandler.BootstrapContext)36 Test (org.junit.Test)32 Document (org.jsoup.nodes.Document)31 Elements (org.jsoup.select.Elements)21 TargetElement (com.vaadin.flow.component.page.TargetElement)13 Element (org.jsoup.nodes.Element)13 UI (com.vaadin.flow.component.UI)9 Registration (com.vaadin.flow.shared.Registration)9 Component (com.vaadin.flow.component.Component)8 Html (com.vaadin.flow.component.Html)8 Tag (com.vaadin.flow.component.Tag)8 Text (com.vaadin.flow.component.Text)8 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)8 JavaScript (com.vaadin.flow.component.dependency.JavaScript)8 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)8 BodySize (com.vaadin.flow.component.page.BodySize)8 Inline (com.vaadin.flow.component.page.Inline)8 Viewport (com.vaadin.flow.component.page.Viewport)8 PageTitle (com.vaadin.flow.router.PageTitle)8 ParentLayout (com.vaadin.flow.router.ParentLayout)8