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);
}
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;
}
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);
}
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);
}
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;
}
Aggregations