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