From 52e2545fa2523cb3d9230d8c29711cddae26fea8 Mon Sep 17 00:00:00 2001 From: Denis Anisimov Date: Wed, 2 Oct 2019 18:27:23 +0300 Subject: [PATCH] Add server side unit tests. --- .../com/vaadin/flow/shared/ui/Dependency.java | 35 +++++------ .../flow/component/internal/PageTest.java | 14 +++++ .../vaadin/flow/shared/ui/DependencyTest.java | 58 ++++++++++++++----- 3 files changed, 74 insertions(+), 33 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/shared/ui/Dependency.java b/flow-server/src/main/java/com/vaadin/flow/shared/ui/Dependency.java index 0e9853639c8..f55736e3589 100644 --- a/flow-server/src/main/java/com/vaadin/flow/shared/ui/Dependency.java +++ b/flow-server/src/main/java/com/vaadin/flow/shared/ui/Dependency.java @@ -36,7 +36,6 @@ public class Dependency implements Serializable { public static final String KEY_URL = "url"; public static final String KEY_TYPE = "type"; public static final String KEY_LOAD_MODE = "mode"; - public static final String KEY_EXPRESSION = "expression"; public static final String KEY_CONTENTS = "contents"; /** @@ -61,7 +60,6 @@ public static boolean contains(String value) { private final Type type; private final String url; private final LoadMode loadMode; - private final String expression; /** * Creates a new dependency of the given type, to be loaded from the given @@ -87,22 +85,22 @@ public Dependency(Type type, String url, LoadMode loadMode) { if (url == null) { throw new IllegalArgumentException("url cannot be null"); } - assert type != null; + this.type = Objects.requireNonNull(type); - this.type = type; - if (type.equals(Type.JS_MODULE)) { + if (type.equals(Type.JS_MODULE) || type.equals(Type.DYNAMIC_IMPORT)) { this.url = url; } else { this.url = SharedUtil.prefixIfRelative(url, ApplicationConstants.FRONTEND_PROTOCOL_PREFIX); } this.loadMode = loadMode; - this.expression = null; } /** * Creates a new dependency of the given type, to be loaded using JS * expression which is supposed to return a Promise. + *

+ * The created instance dependency mode is {@link LoadMode#LAZY}. * * @param type * the type of the dependency, not {@code null} @@ -110,10 +108,12 @@ public Dependency(Type type, String url, LoadMode loadMode) { * the JS expression to load the dependency, not {@code null} */ public Dependency(Type type, String expression) { - this.type = Objects.requireNonNull(type); - this.expression = Objects.requireNonNull(expression); - this.loadMode = LoadMode.LAZY; - this.url = null; + // It's important that the load mode of the dependency is Lazy because + // any other mode is not sent to the client at all when it's added at + // the initial request: it's processed by the bootstrap handler via + // adding an element into the document head right away (no client side + // processing is involved). + this(type, expression, LoadMode.LAZY); } /** @@ -151,20 +151,15 @@ public LoadMode getLoadMode() { */ public JsonObject toJson() { JsonObject jsonObject = Json.createObject(); - if (url != null) { - jsonObject.put(KEY_URL, url); - } + jsonObject.put(KEY_URL, url); jsonObject.put(KEY_TYPE, type.name()); jsonObject.put(KEY_LOAD_MODE, loadMode.name()); - if (expression != null) { - jsonObject.put(KEY_EXPRESSION, expression); - } return jsonObject; } @Override public int hashCode() { - return Objects.hash(type, url, loadMode, expression); + return Objects.hash(type, url, loadMode); } @Override @@ -177,13 +172,13 @@ public boolean equals(Object o) { } Dependency that = (Dependency) o; return type == that.type && loadMode == that.loadMode - && Objects.equals(url, that.url) - && Objects.equals(expression, that.expression); + && Objects.equals(url, that.url); + } @Override public String toString() { return "Dependency [type=" + type + ", url=" + url + ", loadMode=" - + loadMode + ", expression: " + expression + "]"; + + loadMode + "]"; } } diff --git a/flow-server/src/test/java/com/vaadin/flow/component/internal/PageTest.java b/flow-server/src/test/java/com/vaadin/flow/component/internal/PageTest.java index 146e57055d0..a1089a2fa1a 100644 --- a/flow-server/src/test/java/com/vaadin/flow/component/internal/PageTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/component/internal/PageTest.java @@ -15,6 +15,8 @@ */ package com.vaadin.flow.component.internal; +import java.util.Collection; + import net.jcip.annotations.NotThreadSafe; import org.junit.After; import org.junit.Assert; @@ -23,6 +25,7 @@ import com.vaadin.flow.component.UI; import com.vaadin.flow.component.page.Page; import com.vaadin.flow.component.page.Page.ExecutionCanceler; +import com.vaadin.flow.shared.ui.Dependency; import com.vaadin.tests.util.MockUI; @NotThreadSafe @@ -86,6 +89,17 @@ public void testJavaScriptExecutionTooLateCancel() { Assert.assertFalse(executeJavaScript.cancelExecution()); } + @Test + public void addDynamicImport_dynamicDependencyIsAvaialbleViaGetPendingSendToClient() { + page.addDynamicImport("foo"); + + DependencyList list = ui.getInternals().getDependencyList(); + Collection dependencies = list.getPendingSendToClient(); + Assert.assertEquals(1, dependencies.size()); + Dependency dependency = dependencies.iterator().next(); + Assert.assertEquals("foo", dependency.getUrl()); + } + private long countPendingInvocations() { return ui.getInternals().getPendingJavaScriptInvocations().count(); } diff --git a/flow-server/src/test/java/com/vaadin/flow/shared/ui/DependencyTest.java b/flow-server/src/test/java/com/vaadin/flow/shared/ui/DependencyTest.java index 28e18ae30dd..4633abbb2ee 100644 --- a/flow-server/src/test/java/com/vaadin/flow/shared/ui/DependencyTest.java +++ b/flow-server/src/test/java/com/vaadin/flow/shared/ui/DependencyTest.java @@ -16,16 +16,14 @@ package com.vaadin.flow.shared.ui; -import static org.hamcrest.core.Is.is; -import static org.junit.Assert.assertThat; - +import org.hamcrest.CoreMatchers; import org.junit.Test; -import com.vaadin.flow.shared.ui.Dependency; -import com.vaadin.flow.shared.ui.LoadMode; - import elemental.json.JsonObject; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + /** * @author Vaadin Ltd * @since 1.0. @@ -33,18 +31,52 @@ public class DependencyTest { @Test - public void checkJsonSerialization() { - Dependency dependency = new Dependency(Dependency.Type.HTML_IMPORT, "url", LoadMode.INLINE); + public void checkJsonSerialization_3ArgsCTor() { + Dependency dependency = new Dependency(Dependency.Type.HTML_IMPORT, + "url", LoadMode.INLINE); + assertDependency(dependency); + } + + @Test + public void dynamicDependency_hasLazyMode() { + Dependency dependency = new Dependency(Dependency.Type.DYNAMIC_IMPORT, + "foo"); + + // It's important that the load mode of the dependency is Lazy because + // any other mode is not sent to the client at all when it's added at + // the initial request: it's processed by the bootstrap handler via + // adding an element into the document head right away (no client side + // processing is involved). + assertThat(dependency.getLoadMode(), + CoreMatchers.equalTo(LoadMode.LAZY)); + } + + @Test + public void checkJsonSerialization_2ArgsCTor() { + Dependency dependency = new Dependency(Dependency.Type.DYNAMIC_IMPORT, + "foo"); + + assertDependency(dependency); + + } + + private void assertDependency(Dependency dependency) { JsonObject dependencyJson = dependency.toJson(); assertThat("No contents should be present in json now", dependencyJson.hasKey(Dependency.KEY_CONTENTS), is(false)); - assertThat("Dependency type should match corresponding enum name in pojo", - dependencyJson.getString(Dependency.KEY_TYPE), is(dependency.getType().name())); + assertThat( + "Dependency type should match corresponding enum name in pojo", + dependencyJson.getString(Dependency.KEY_TYPE), + is(dependency.getType().name())); assertThat("Dependency url should match corresponding url in pojo", - dependencyJson.getString(Dependency.KEY_URL), is(dependency.getUrl())); - assertThat("Dependency load mode should match corresponding enum name in pojo", - dependencyJson.getString(Dependency.KEY_LOAD_MODE), is(dependency.getLoadMode().name())); + dependencyJson.getString(Dependency.KEY_URL), + is(dependency.getUrl())); + assertThat( + "Dependency load mode should match corresponding enum name in pojo", + dependencyJson.getString(Dependency.KEY_LOAD_MODE), + is(dependency.getLoadMode().name())); } + }