Search in sources :

Example 1 with JavaScript

use of com.vaadin.flow.component.dependency.JavaScript in project flow by vaadin.

the class BootstrapHandlerDependenciesTest method assertJavaScriptElementInlined.

private void assertJavaScriptElementInlined(Element head, String expectedContents) {
    List<Element> scriptsWithoutExpectedContents = head.getElementsByTag("script").stream().filter(element -> element.toString().contains(expectedContents)).collect(Collectors.toList());
    assertThat("Expected to have only one inlined js element with contents = " + expectedContents, scriptsWithoutExpectedContents.size(), is(1));
    Element inlinedElement = scriptsWithoutExpectedContents.get(0);
    assertThat("The element should have correct js type attribute", inlinedElement.attr("type"), is("text/javascript"));
    assertThat("Inlined js element should not have defer attribute", inlinedElement.attr("defer"), is(""));
    assertThat("Inlined js element should not have src attribute", inlinedElement.attr("src"), is(""));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) JavaScript(com.vaadin.flow.component.dependency.JavaScript) BootstrapContext(com.vaadin.flow.server.BootstrapHandler.BootstrapContext) CoreMatchers.not(org.hamcrest.CoreMatchers.not) CoreMatchers.either(org.hamcrest.CoreMatchers.either) Router(com.vaadin.flow.router.Router) HttpServletRequest(javax.servlet.http.HttpServletRequest) CoreMatchers.both(org.hamcrest.CoreMatchers.both) Element(org.jsoup.nodes.Element) After(org.junit.After) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Lookup(com.vaadin.flow.di.Lookup) UI(com.vaadin.flow.component.UI) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Before(org.junit.Before) Properties(java.util.Properties) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) TestVaadinServletService(com.vaadin.flow.server.MockServletServiceSessionSetup.TestVaadinServletService) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ResourceProvider(com.vaadin.flow.di.ResourceProvider) Collectors(java.util.stream.Collectors) TestVaadinServlet(com.vaadin.flow.server.MockServletServiceSessionSetup.TestVaadinServlet) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) NotThreadSafe(net.jcip.annotations.NotThreadSafe) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) Document(org.jsoup.nodes.Document) LoadMode(com.vaadin.flow.shared.ui.LoadMode) Optional(java.util.Optional) Elements(org.jsoup.select.Elements) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) Mockito.mock(org.mockito.Mockito.mock) Element(org.jsoup.nodes.Element)

Example 2 with JavaScript

use of com.vaadin.flow.component.dependency.JavaScript in project flow by vaadin.

the class UIInternals method addFallbackDependencies.

private void addFallbackDependencies(DependencyInfo dependency) {
    if (isFallbackChunkLoaded) {
        return;
    }
    VaadinContext context = ui.getSession().getService().getContext();
    FallbackChunk chunk = context.getAttribute(FallbackChunk.class);
    if (chunk == null) {
        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Fallback chunk is not available, skipping fallback dependencies load");
        }
        return;
    }
    Set<String> modules = chunk.getModules();
    Set<CssImportData> cssImportsData = chunk.getCssImports();
    if (modules.isEmpty() && cssImportsData.isEmpty()) {
        getLogger().debug("Fallback chunk is empty, skipping fallback dependencies load");
        return;
    }
    List<CssImport> cssImports = dependency.getCssImports();
    List<JavaScript> javaScripts = dependency.getJavaScripts();
    List<JsModule> jsModules = dependency.getJsModules();
    if (jsModules.stream().map(JsModule::value).anyMatch(modules::contains)) {
        loadFallbackChunk();
        return;
    }
    if (javaScripts.stream().map(JavaScript::value).anyMatch(modules::contains)) {
        loadFallbackChunk();
        return;
    }
    if (cssImports.stream().map(this::buildData).anyMatch(cssImportsData::contains)) {
        loadFallbackChunk();
        return;
    }
}
Also used : JsModule(com.vaadin.flow.component.dependency.JsModule) CssImport(com.vaadin.flow.component.dependency.CssImport) VaadinContext(com.vaadin.flow.server.VaadinContext) FallbackChunk(com.vaadin.flow.server.frontend.FallbackChunk) JavaScript(com.vaadin.flow.component.dependency.JavaScript) CssImportData(com.vaadin.flow.server.frontend.FallbackChunk.CssImportData)

Aggregations

JavaScript (com.vaadin.flow.component.dependency.JavaScript)2 UI (com.vaadin.flow.component.UI)1 CssImport (com.vaadin.flow.component.dependency.CssImport)1 JsModule (com.vaadin.flow.component.dependency.JsModule)1 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)1 Lookup (com.vaadin.flow.di.Lookup)1 ResourceProvider (com.vaadin.flow.di.ResourceProvider)1 Router (com.vaadin.flow.router.Router)1 BootstrapContext (com.vaadin.flow.server.BootstrapHandler.BootstrapContext)1 TestVaadinServlet (com.vaadin.flow.server.MockServletServiceSessionSetup.TestVaadinServlet)1 TestVaadinServletService (com.vaadin.flow.server.MockServletServiceSessionSetup.TestVaadinServletService)1 VaadinContext (com.vaadin.flow.server.VaadinContext)1 FallbackChunk (com.vaadin.flow.server.frontend.FallbackChunk)1 CssImportData (com.vaadin.flow.server.frontend.FallbackChunk.CssImportData)1 ApplicationConstants (com.vaadin.flow.shared.ApplicationConstants)1 LoadMode (com.vaadin.flow.shared.ui.LoadMode)1 InputStream (java.io.InputStream)1 List (java.util.List)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1