Search in sources :

Example 1 with LoadMode

use of com.vaadin.flow.shared.ui.LoadMode 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)

Example 2 with LoadMode

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

the class GwtDependencyLoaderTest method createDependenciesMap.

private JsMap<LoadMode, JsonArray> createDependenciesMap(JsonObject... dependencies) {
    JsMap<LoadMode, JsonArray> result = JsCollections.map();
    for (int i = 0; i < dependencies.length; i++) {
        JsonObject dependency = dependencies[i];
        LoadMode loadMode = LoadMode.valueOf(dependency.getString(Dependency.KEY_LOAD_MODE));
        JsonArray jsonArray = Json.createArray();
        jsonArray.set(0, dependency);
        JsonArray oldResult = result.get(loadMode);
        if (oldResult == null) {
            result.set(loadMode, jsonArray);
        } else {
            mergeArrays(oldResult, jsonArray);
        }
    }
    return result;
}
Also used : JsonArray(elemental.json.JsonArray) LoadMode(com.vaadin.flow.shared.ui.LoadMode) JsonObject(elemental.json.JsonObject)

Example 3 with LoadMode

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

the class BootstrapHandler method setupDocumentHead.

private static List<Element> setupDocumentHead(Element head, BootstrapContext context) {
    setupMetaAndTitle(head, context);
    setupCss(head, context);
    JsonObject initialUIDL = getInitialUidl(context.getUI());
    Map<LoadMode, JsonArray> dependenciesToProcessOnServer = popDependenciesToProcessOnServer(initialUIDL);
    setupFrameworkLibraries(head, initialUIDL, context);
    return applyUserDependencies(head, context, dependenciesToProcessOnServer);
}
Also used : JsonArray(elemental.json.JsonArray) LoadMode(com.vaadin.flow.shared.ui.LoadMode) JsonObject(elemental.json.JsonObject)

Example 4 with LoadMode

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

the class UidlWriterTest method checkAllTypesOfDependencies_npmMode.

@Test
public void checkAllTypesOfDependencies_npmMode() throws Exception {
    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 an eager dependency", eagerDependencies, hasSize(1));
    assertThat("Eager dependencies should not have inline contents", eagerDependencies.stream().filter(json -> json.hasKey(Dependency.KEY_CONTENTS)).collect(Collectors.toList()), hasSize(0));
    JsonObject eagerDependency = eagerDependencies.get(0);
    assertEquals("eager.css", eagerDependency.getString(Dependency.KEY_URL));
    assertEquals(Dependency.Type.STYLESHEET, Dependency.Type.valueOf(eagerDependency.getString(Dependency.KEY_TYPE)));
    List<JsonObject> lazyDependencies = dependenciesMap.get(LoadMode.LAZY);
    JsonObject lazyDependency = lazyDependencies.get(0);
    assertEquals("lazy.css", lazyDependency.getString(Dependency.KEY_URL));
    assertEquals(Dependency.Type.STYLESHEET, Dependency.Type.valueOf(lazyDependency.getString(Dependency.KEY_TYPE)));
    List<JsonObject> inlineDependencies = dependenciesMap.get(LoadMode.INLINE);
    assertInlineDependencies(inlineDependencies);
}
Also used : VaadinServletContext(com.vaadin.flow.server.VaadinServletContext) Component(com.vaadin.flow.component.Component) JavaScript(com.vaadin.flow.component.dependency.JavaScript) PendingJavaScriptInvocation(com.vaadin.flow.component.internal.PendingJavaScriptInvocation) Json(elemental.json.Json) JsonArray(elemental.json.JsonArray) Route(com.vaadin.flow.router.Route) Map(java.util.Map) After(org.junit.After) Element(com.vaadin.flow.dom.Element) Is.is(org.hamcrest.core.Is.is) Lookup(com.vaadin.flow.di.Lookup) UI(com.vaadin.flow.component.UI) VaadinSession(com.vaadin.flow.server.VaadinSession) RoutePathProvider(com.vaadin.flow.router.RoutePathProvider) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation) BootstrapHandlerTest(com.vaadin.flow.server.BootstrapHandlerTest) Collectors(java.util.stream.Collectors) NotThreadSafe(net.jcip.annotations.NotThreadSafe) List(java.util.List) Stream(java.util.stream.Stream) Assert.assertFalse(org.junit.Assert.assertFalse) LoadMode(com.vaadin.flow.shared.ui.LoadMode) ApplicationConstants(com.vaadin.flow.shared.ApplicationConstants) Mockito.mock(org.mockito.Mockito.mock) Dependency(com.vaadin.flow.shared.ui.Dependency) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) Tag(com.vaadin.flow.component.Tag) VaadinServletRequest(com.vaadin.flow.server.VaadinServletRequest) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ElementFactory(com.vaadin.flow.dom.ElementFactory) RouterLayout(com.vaadin.flow.router.RouterLayout) StyleSheet(com.vaadin.flow.component.dependency.StyleSheet) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) MockServletServiceSessionSetup(com.vaadin.flow.server.MockServletServiceSessionSetup) Mockito.when(org.mockito.Mockito.when) Mockito(org.mockito.Mockito) JsonUtils(com.vaadin.flow.internal.JsonUtils) RoutePathProviderImpl(com.vaadin.flow.server.MockVaadinContext.RoutePathProviderImpl) RouteConfiguration(com.vaadin.flow.router.RouteConfiguration) JsonObject(elemental.json.JsonObject) Collections(java.util.Collections) ParentLayout(com.vaadin.flow.router.ParentLayout) 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) BootstrapHandlerTest(com.vaadin.flow.server.BootstrapHandlerTest) Test(org.junit.Test)

Example 5 with LoadMode

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

the class DependencyLoaderTest method createDependenciesMap.

private JsMap<LoadMode, JsonArray> createDependenciesMap(JsonObject... dependencies) {
    JsMap<LoadMode, JsonArray> result = JsCollections.map();
    for (int i = 0; i < dependencies.length; i++) {
        JsonObject dependency = dependencies[i];
        LoadMode loadMode = LoadMode.valueOf(dependency.getString(Dependency.KEY_LOAD_MODE));
        JsonArray jsonArray = Json.createArray();
        jsonArray.set(0, dependency);
        JsonArray oldResult = result.get(loadMode);
        if (oldResult == null) {
            result.set(loadMode, jsonArray);
        } else {
            mergeArrays(oldResult, jsonArray);
        }
    }
    return result;
}
Also used : JsonArray(elemental.json.JsonArray) LoadMode(com.vaadin.flow.shared.ui.LoadMode) JsonObject(elemental.json.JsonObject)

Aggregations

LoadMode (com.vaadin.flow.shared.ui.LoadMode)7 JsonArray (elemental.json.JsonArray)6 JsonObject (elemental.json.JsonObject)5 Dependency (com.vaadin.flow.shared.ui.Dependency)3 Component (com.vaadin.flow.component.Component)2 Tag (com.vaadin.flow.component.Tag)2 UI (com.vaadin.flow.component.UI)2 JavaScript (com.vaadin.flow.component.dependency.JavaScript)2 StyleSheet (com.vaadin.flow.component.dependency.StyleSheet)2 JavaScriptInvocation (com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)2 Element (com.vaadin.flow.dom.Element)2 ElementFactory (com.vaadin.flow.dom.ElementFactory)2 JsonUtils (com.vaadin.flow.internal.JsonUtils)2 VaadinServletRequest (com.vaadin.flow.server.VaadinServletRequest)2 VaadinSession (com.vaadin.flow.server.VaadinSession)2 ApplicationConstants (com.vaadin.flow.shared.ApplicationConstants)2 Json (elemental.json.Json)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2