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);
}
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));
}
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>"));
}
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);
}
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());
}
Aggregations