Search in sources :

Example 6 with Dependency

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

the class DependencyLoaderTest method loadFrontendDependencyWithContext.

@Test
public void loadFrontendDependencyWithContext() {
    String TEST_URL = "frontend://my-component.html";
    ApplicationConfiguration config = new ApplicationConfiguration();
    registry.set(ApplicationConfiguration.class, config);
    config.setFrontendRootUrl("context://es6/");
    config.setContextRootUrl("http://someplace.com/");
    new DependencyLoader(registry).loadDependencies(createDependenciesMap(new Dependency(Dependency.Type.HTML_IMPORT, TEST_URL, LoadMode.EAGER).toJson()));
    assertEquals(Collections.singletonList("http://someplace.com/es6/my-component.html"), mockResourceLoader.loadingHtml);
}
Also used : Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 7 with Dependency

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

the class DependencyLoaderTest method loadFrontendDependency.

@Test
public void loadFrontendDependency() {
    String TEST_URL = "frontend://my-component.html";
    ApplicationConfiguration config = new ApplicationConfiguration();
    registry.set(ApplicationConfiguration.class, config);
    config.setFrontendRootUrl("http://someplace.com/es6/");
    new DependencyLoader(registry).loadDependencies(createDependenciesMap(new Dependency(Dependency.Type.HTML_IMPORT, TEST_URL, LoadMode.EAGER).toJson()));
    assertEquals(Collections.singletonList("http://someplace.com/es6/my-component.html"), mockResourceLoader.loadingHtml);
}
Also used : Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 8 with Dependency

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

the class DependencyLoaderTest method ensureEagerDependenciesLoadedInOrder.

@Test
public void ensureEagerDependenciesLoadedInOrder() {
    String jsUrl1 = "/1.js";
    String jsUrl2 = "/2.js";
    String cssUrl1 = "/1.css";
    String cssUrl2 = "/2.css";
    String htmlUrl1 = "/1.html";
    String htmlUrl2 = "/2.html";
    new DependencyLoader(registry).loadDependencies(createDependenciesMap(new Dependency(Dependency.Type.JAVASCRIPT, jsUrl1, LoadMode.EAGER).toJson(), new Dependency(Dependency.Type.JAVASCRIPT, jsUrl2, LoadMode.EAGER).toJson(), new Dependency(Dependency.Type.STYLESHEET, cssUrl1, LoadMode.EAGER).toJson(), new Dependency(Dependency.Type.STYLESHEET, cssUrl2, LoadMode.EAGER).toJson(), new Dependency(Dependency.Type.HTML_IMPORT, htmlUrl1, LoadMode.EAGER).toJson(), new Dependency(Dependency.Type.HTML_IMPORT, htmlUrl2, LoadMode.EAGER).toJson()));
    assertEquals("jsUrl1 should come before jsUrl2, because it was added earlier", Arrays.asList(jsUrl1, jsUrl2), mockResourceLoader.loadingScripts);
    assertEquals("cssUrl1 should come before cssUrl2, because it was added earlier", Arrays.asList(cssUrl1, cssUrl2), mockResourceLoader.loadingStyles);
    assertEquals("htmlUrl1 should come before htmlUrl2, because it was added earlier", Arrays.asList(htmlUrl1, htmlUrl2), mockResourceLoader.loadingHtml);
}
Also used : Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 9 with Dependency

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

the class TestingServiceInitListener method serviceInit.

@Override
public void serviceInit(ServiceInitEvent event) {
    initCount.incrementAndGet();
    event.addRequestHandler((session, request, response) -> {
        requestCount.incrementAndGet();
        return false;
    });
    event.addDependencyFilter((dependencies, context) -> {
        // used by DependencyFilterUI
        if (dependencies.stream().anyMatch(dependency -> dependency.getUrl().startsWith("replaceme://"))) {
            List<Dependency> newList = new ArrayList<>();
            newList.add(new Dependency(Dependency.Type.HTML_IMPORT, "frontend://com/vaadin/flow/uitest/ui/dependencies/filtered.html", LoadMode.EAGER));
            dependencies.stream().filter(dependency -> !dependency.getUrl().startsWith("replaceme://")).forEach(newList::add);
            dependencies = newList;
        } else // used by BundledTemplateInTemplateWithIdView
        if (dependencies.stream().anyMatch(dependency -> dependency.getUrl().startsWith("bundle://"))) {
            List<Dependency> newList = new ArrayList<>();
            newList.add(new Dependency(Dependency.Type.HTML_IMPORT, "frontend://com/vaadin/flow/uitest/ui/template/BundleIdTemplate.html", LoadMode.EAGER));
            dependencies = newList;
        }
        return dependencies;
    });
}
Also used : ServiceInitEvent(com.vaadin.flow.server.ServiceInitEvent) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LoadMode(com.vaadin.flow.shared.ui.LoadMode) Dependency(com.vaadin.flow.shared.ui.Dependency) VaadinServiceInitListener(com.vaadin.flow.server.VaadinServiceInitListener) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Dependency(com.vaadin.flow.shared.ui.Dependency)

Example 10 with Dependency

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

the class UidlWriterTest method checkAllTypesOfDependencies.

@Test
public void checkAllTypesOfDependencies() {
    UI ui = initializeUIForDependenciesTest(new TestUI());
    UidlWriter uidlWriter = new UidlWriter();
    addInitialComponentDependencies(ui, uidlWriter);
    ui.add(new ComponentWithAllDependencyTypes());
    JsonObject response = uidlWriter.createUidl(ui, false);
    Map<LoadMode, List<JsonObject>> dependenciesMap = Stream.of(LoadMode.values()).map(mode -> response.getArray(mode.name())).flatMap(JsonUtils::<JsonObject>stream).collect(Collectors.toMap(jsonObject -> LoadMode.valueOf(jsonObject.getString(Dependency.KEY_LOAD_MODE)), Collections::singletonList, (list1, list2) -> {
        List<JsonObject> result = new ArrayList<>(list1);
        result.addAll(list2);
        return result;
    }));
    assertThat("Dependencies with all types of load mode should be present in this response", dependenciesMap.size(), is(LoadMode.values().length));
    List<JsonObject> eagerDependencies = dependenciesMap.get(LoadMode.EAGER);
    assertThat("Should have 3 eager dependencies", eagerDependencies, hasSize(3));
    assertThat("Eager dependencies should not have inline contents", eagerDependencies.stream().filter(json -> json.hasKey(Dependency.KEY_CONTENTS)).collect(Collectors.toList()), hasSize(0));
    assertThat("Should have 3 different eager urls", eagerDependencies.stream().map(json -> json.getString(Dependency.KEY_URL)).map(url -> url.substring(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX.length())).collect(Collectors.toList()), containsInAnyOrder("eager.js", "eager.html", "eager.css"));
    assertThat("Should have 3 different eager dependency types", eagerDependencies.stream().map(json -> json.getString(Dependency.KEY_TYPE)).map(Dependency.Type::valueOf).collect(Collectors.toList()), containsInAnyOrder(Dependency.Type.values()));
    List<JsonObject> lazyDependencies = dependenciesMap.get(LoadMode.LAZY);
    assertThat("Should have 3 lazy dependencies", lazyDependencies, hasSize(3));
    assertThat("Lazy dependencies should not have inline contents", lazyDependencies.stream().filter(json -> json.hasKey(Dependency.KEY_CONTENTS)).collect(Collectors.toList()), hasSize(0));
    assertThat("Should have 3 different lazy urls", lazyDependencies.stream().map(json -> json.getString(Dependency.KEY_URL)).map(url -> url.substring(ApplicationConstants.FRONTEND_PROTOCOL_PREFIX.length())).collect(Collectors.toList()), containsInAnyOrder("lazy.js", "lazy.html", "lazy.css"));
    assertThat("Should have 3 different lazy dependency types", lazyDependencies.stream().map(json -> json.getString(Dependency.KEY_TYPE)).map(Dependency.Type::valueOf).collect(Collectors.toList()), containsInAnyOrder(Dependency.Type.values()));
    List<JsonObject> inlineDependencies = dependenciesMap.get(LoadMode.INLINE);
    assertInlineDependencies(inlineDependencies, ApplicationConstants.FRONTEND_PROTOCOL_PREFIX);
}
Also used : Arrays(java.util.Arrays) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) Assert.assertThat(org.junit.Assert.assertThat) JsonValue(elemental.json.JsonValue) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) After(org.junit.After) Element(com.vaadin.flow.dom.Element) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Is.is(org.hamcrest.core.Is.is) UI(com.vaadin.flow.component.UI) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinServlet(com.vaadin.flow.server.VaadinServlet) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) RouterInterface(com.vaadin.flow.router.RouterInterface) Collectors(java.util.stream.Collectors) NotThreadSafe(net.jcip.annotations.NotThreadSafe) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Stream(java.util.stream.Stream) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Assert.assertFalse(org.junit.Assert.assertFalse) VaadinService(com.vaadin.flow.server.VaadinService) LoadMode(com.vaadin.flow.shared.ui.LoadMode) VaadinServletService(com.vaadin.flow.server.VaadinServletService) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) Mockito.mock(org.mockito.Mockito.mock) MockDeploymentConfiguration(com.vaadin.tests.util.MockDeploymentConfiguration) VaadinUriResolverFactory(com.vaadin.flow.server.VaadinUriResolverFactory) HasChildView(com.vaadin.flow.router.legacy.HasChildView) MockVaadinSession(com.vaadin.flow.server.MockVaadinSession) VaadinUriResolver(com.vaadin.flow.shared.VaadinUriResolver) Dependency(com.vaadin.flow.shared.ui.Dependency) Function(java.util.function.Function) Matchers.anyString(org.mockito.Matchers.anyString) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) View(com.vaadin.flow.router.legacy.View) DependencyFilter(com.vaadin.flow.server.DependencyFilter) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ElementFactory(com.vaadin.flow.dom.ElementFactory) VaadinResponse(com.vaadin.flow.server.VaadinResponse) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) HtmlImport(com.vaadin.flow.component.dependency.HtmlImport) Mockito(org.mockito.Mockito) JsonUtils(com.vaadin.flow.internal.JsonUtils) ServletContext(javax.servlet.ServletContext) JsonObject(elemental.json.JsonObject) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) LoadMode(com.vaadin.flow.shared.ui.LoadMode) UI(com.vaadin.flow.component.UI) JsonObject(elemental.json.JsonObject) List(java.util.List) ArrayList(java.util.ArrayList) 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