Search in sources :

Example 26 with Dependency

use of com.vaadin.flow.shared.ui.Dependency in project flow by vaadin.

the class UidlWriter method populateDependencies.

private static void populateDependencies(JsonObject response, VaadinSession session, DependencyList dependencyList) {
    Collection<Dependency> pendingSendToClient = dependencyList.getPendingSendToClient();
    FilterContext context = new FilterContext(session);
    for (DependencyFilter filter : session.getService().getDependencyFilters()) {
        pendingSendToClient = filter.filter(new ArrayList<>(pendingSendToClient), context);
    }
    if (!pendingSendToClient.isEmpty()) {
        groupDependenciesByLoadMode(pendingSendToClient).forEach((loadMode, dependencies) -> response.put(loadMode.name(), dependencies));
    }
    dependencyList.clearPendingSendToClient();
}
Also used : ArrayList(java.util.ArrayList) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency) FilterContext(com.vaadin.flow.server.DependencyFilter.FilterContext)

Example 27 with Dependency

use of com.vaadin.flow.shared.ui.Dependency 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 28 with Dependency

use of com.vaadin.flow.shared.ui.Dependency in project flow by vaadin.

the class UidlWriterTest method parentViewDependenciesAreAddedFirst.

@Test
public void parentViewDependenciesAreAddedFirst() {
    UI ui = initializeUIForDependenciesTest(new UI());
    UidlWriter uidlWriter = new UidlWriter();
    ui.add(new BaseClass());
    JsonObject response = uidlWriter.createUidl(ui, false);
    assertFalse("Did not expect to have lazy dependencies in uidl", response.hasKey(LoadMode.LAZY.name()));
    assertFalse("Did not expect to have inline dependencies in uidl", response.hasKey(LoadMode.INLINE.name()));
    assertTrue("Expected to have eager dependencies in uidl", response.hasKey(LoadMode.EAGER.name()));
    JsonArray eagerDependencies = response.getArray(LoadMode.EAGER.name());
    assertEquals("Expected to have exactly 3 eager dependencies in uidl, actual: %d", eagerDependencies.length(), 3);
    List<Class<?>> expectedClassOrder = Arrays.asList(SuperParentClass.class, ParentClass.class, BaseClass.class);
    for (int i = 0; i < expectedClassOrder.size(); i++) {
        Class<?> expectedClass = expectedClassOrder.get(i);
        HtmlImport htmlImport = expectedClass.getAnnotation(HtmlImport.class);
        JsonValue actualDependency = eagerDependencies.get(i);
        JsonObject expectedDependency = new Dependency(Dependency.Type.HTML_IMPORT, htmlImport.value(), htmlImport.loadMode()).toJson();
        assertTrue(String.format("Unexpected dependency. Expected: '%s', actual: '%s', class: '%s'", expectedDependency, actualDependency, expectedClass), expectedDependency.jsEquals(actualDependency));
    }
}
Also used : JsonArray(elemental.json.JsonArray) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) UI(com.vaadin.flow.component.UI) JsonValue(elemental.json.JsonValue) JsonObject(elemental.json.JsonObject) Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 29 with Dependency

use of com.vaadin.flow.shared.ui.Dependency in project flow by vaadin.

the class BundleDependencyFilter method filter.

@Override
public List<Dependency> filter(List<Dependency> dependencies, FilterContext filterContext) {
    List<Dependency> dependenciesWithBundles = new ArrayList<>(dependencies.size());
    Set<String> bundleUrlsToInclude = new HashSet<>();
    for (Dependency dependency : dependencies) {
        Set<String> bundleUrls = importContainedInBundles.get(clearFlowProtocols(dependency.getUrl()));
        if (bundleUrls != null) {
            if (bundleUrls.size() > 1) {
                getLogger().warn("Dependency '{}' is contained in multiple fragments: '{}', this may lead to performance degradation", dependency, bundleUrls);
            }
            bundleUrlsToInclude.addAll(bundleUrls);
        } else {
            dependenciesWithBundles.add(dependency);
        }
    }
    if (!bundleUrlsToInclude.isEmpty()) {
        dependenciesWithBundles.add(0, createBundleDependency(MAIN_BUNDLE_URL));
        bundleUrlsToInclude.stream().filter(bundleUrl -> !MAIN_BUNDLE_URL.equals(bundleUrl)).map(BundleDependencyFilter::createBundleDependency).forEach(dependenciesWithBundles::add);
    }
    return dependenciesWithBundles;
}
Also used : ArrayList(java.util.ArrayList) Dependency(com.vaadin.flow.shared.ui.Dependency) HashSet(java.util.HashSet)

Example 30 with Dependency

use of com.vaadin.flow.shared.ui.Dependency in project flow by vaadin.

the class DefaultTemplateParserTest method defaultParser_useDependencyFilters_returnBundle.

@Test
public void defaultParser_useDependencyFilters_returnBundle() {
    AtomicInteger calls = new AtomicInteger();
    DependencyFilter filter = (list, context) -> {
        list.clear();
        list.add(new Dependency(Type.HTML_IMPORT, "/bundle.html", LoadMode.EAGER));
        calls.incrementAndGet();
        return list;
    };
    Mockito.when(service.getDependencyFilters()).thenReturn(Collections.singletonList(filter));
    TemplateParser parser = DefaultTemplateParser.getInstance();
    Element element = parser.getTemplateContent(ImportsInspectTemplate.class, "foo").getTemplateElement();
    Assert.assertTrue(element.getElementById("foo") != null);
    element = parser.getTemplateContent(RootImportsInspectTemplate.class, "foo").getTemplateElement();
    Assert.assertTrue(element.getElementById("foo") != null);
    element = parser.getTemplateContent(OtherImportsInspectTemplate.class, "bar").getTemplateElement();
    Assert.assertTrue(element.getElementById("bar") != null);
    Assert.assertEquals("The DependencyFilter should be called exactly 3 times", 3, calls.get());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) TemplateData(com.vaadin.flow.component.polymertemplate.TemplateParser.TemplateData) WrappedHttpSession(com.vaadin.flow.server.WrappedHttpSession) ServletException(javax.servlet.ServletException) Mock(org.mockito.Mock) CurrentInstance(com.vaadin.flow.internal.CurrentInstance) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) Comment(org.jsoup.nodes.Comment) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) Type(com.vaadin.flow.shared.ui.Dependency.Type) Assert.assertThat(org.junit.Assert.assertThat) MockitoAnnotations(org.mockito.MockitoAnnotations) Tag(com.vaadin.flow.component.Tag) ByteArrayInputStream(java.io.ByteArrayInputStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(org.jsoup.nodes.Element) After(org.junit.After) DependencyFilter(com.vaadin.flow.server.DependencyFilter) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Before(org.junit.Before) HttpSession(javax.servlet.http.HttpSession) VaadinSession(com.vaadin.flow.server.VaadinSession) Matchers.empty(org.hamcrest.Matchers.empty) VaadinServlet(com.vaadin.flow.server.VaadinServlet) Test(org.junit.Test) VaadinRequest(com.vaadin.flow.server.VaadinRequest) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) NotThreadSafe(net.jcip.annotations.NotThreadSafe) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Node(org.jsoup.nodes.Node) Mockito(org.mockito.Mockito) List(java.util.List) Stream(java.util.stream.Stream) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) ServletContext(javax.servlet.ServletContext) ModelClass(com.vaadin.flow.component.polymertemplate.PolymerTemplateTest.ModelClass) ServiceException(com.vaadin.flow.server.ServiceException) Assert(org.junit.Assert) Collections(java.util.Collections) VaadinServletService(com.vaadin.flow.server.VaadinServletService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(org.jsoup.nodes.Element) DependencyFilter(com.vaadin.flow.server.DependencyFilter) Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Aggregations

Dependency (com.vaadin.flow.shared.ui.Dependency)30 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)8 LoadMode (com.vaadin.flow.shared.ui.LoadMode)7 List (java.util.List)7 HtmlImport (com.vaadin.flow.component.dependency.HtmlImport)6 JsonObject (elemental.json.JsonObject)6 DependencyFilter (com.vaadin.flow.server.DependencyFilter)5 VaadinUriResolver (com.vaadin.flow.shared.VaadinUriResolver)5 StandardCharsets (java.nio.charset.StandardCharsets)5 Collections (java.util.Collections)5 Element (org.jsoup.nodes.Element)5 Tag (com.vaadin.flow.component.Tag)4 UI (com.vaadin.flow.component.UI)4 VaadinService (com.vaadin.flow.server.VaadinService)3 VaadinServlet (com.vaadin.flow.server.VaadinServlet)3 VaadinSession (com.vaadin.flow.server.VaadinSession)3 Type (com.vaadin.flow.shared.ui.Dependency.Type)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3