Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dp/ccdm/bodysize annotation #7179

Merged
merged 4 commits into from
Dec 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Set;

import com.vaadin.flow.component.page.Viewport;
import com.vaadin.flow.component.page.BodySize;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -48,7 +49,7 @@
*
* @since 3.0
*/
@HandlesTypes({ VaadinAppShell.class, Meta.class, Meta.Container.class, PWA.class, Viewport.class})
@HandlesTypes({ VaadinAppShell.class, Meta.class, Meta.Container.class, PWA.class, Viewport.class, BodySize.class})
@WebListener
public class VaadinAppShellInitializer implements ServletContainerInitializer,
Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.vaadin.flow.component.page.Meta;
import com.vaadin.flow.component.page.VaadinAppShell;
import com.vaadin.flow.component.page.Viewport;
import com.vaadin.flow.component.page.BodySize;
import com.vaadin.flow.server.InvalidApplicationConfigurationException;
import com.vaadin.flow.server.VaadinContext;

Expand All @@ -53,6 +54,9 @@ public class VaadinAppShellRegistry implements Serializable {
private static final String ERROR_MULTIPLE_VIEWPORT =
"%nViewport is not a repeatable annotation type.%n";

private static final String ERROR_MULTIPLE_BODYSIZE =
"%nBodySize is not a repeatable annotation type.%n";

private Class<? extends VaadinAppShell> shell;

/**
Expand Down Expand Up @@ -194,6 +198,19 @@ public void modifyIndexHtmlResponse(Document document) {
}
metaViewportElement.attr("content", getAnnotations(Viewport.class).get(0).value());
}

if(getAnnotations(BodySize.class).size() > 1) {
throw new InvalidApplicationConfigurationException(
VaadinAppShellRegistry.ERROR_MULTIPLE_BODYSIZE);
} else if(!getAnnotations(BodySize.class).isEmpty()) {
String strBodySizeHeight = "height:" + getAnnotations(BodySize.class).get(0).height();
String strBodySizeWidth = "width:" + getAnnotations(BodySize.class).get(0).width();
Element elemStyle = new Element("style");
elemStyle.attr("type", "text/css");
String strContent = "body,#outlet{" + strBodySizeHeight + ";" + strBodySizeWidth + ";" + "}";
elemStyle.append(strContent);
document.head().appendChild(elemStyle);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import com.vaadin.flow.server.VaadinServletRequest;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.server.frontend.FrontendUtils;
import com.vaadin.flow.server.startup.VaadinAppShellInitializerTest.MyAppShellWithViewportAndMultipleMeta;
import com.vaadin.flow.server.startup.VaadinAppShellInitializerTest.MyAppShellWithMultipleAnnotations;
import com.vaadin.flow.server.startup.VaadinAppShellRegistry;
import com.vaadin.flow.server.startup.VaadinAppShellRegistry.VaadinAppShellRegistryWrapper;
import com.vaadin.tests.util.MockDeploymentConfiguration;
Expand Down Expand Up @@ -351,7 +351,7 @@ public void should_not_add_metaElements_when_not_appShellPresent() throws Except
public void should_add_metaAndPwaElements_when_appShellPresent() throws Exception {
// Set class in context and do not call initializer
VaadinAppShellRegistry registry = new VaadinAppShellRegistry();
registry.setShell(MyAppShellWithViewportAndMultipleMeta.class);
registry.setShell(MyAppShellWithMultipleAnnotations.class);
Mockito.when(mocks.getServletContext()
.getAttribute(VaadinAppShellRegistryWrapper.class.getName()))
.thenReturn(new VaadinAppShellRegistryWrapper(registry));
Expand All @@ -377,6 +377,11 @@ public void should_add_metaAndPwaElements_when_appShellPresent() throws Exceptio
assertEquals("#ffffff", elements.get(5).attr("content"));
assertEquals("apple-mobile-web-app-status-bar-style", elements.get(6).attr("name"));
assertEquals("#ffffff", elements.get(6).attr("content"));

Elements elementsStyle = document.head().getElementsByTag("style");
assertEquals(2, elementsStyle.size());
assertEquals("text/css", elementsStyle.get(1).attr("type"));
assertEquals("body,#outlet{height:50vh;width:50vw;}", elementsStyle.get(1).childNode(0).toString());
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.vaadin.flow.component.page.Viewport;
import com.vaadin.flow.component.page.BodySize;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.After;
Expand Down Expand Up @@ -41,13 +41,15 @@ public static class MyAppShellWithoutMeta extends VaadinAppShell {
@Meta(name = "lorem", content = "ipsum")
@PWA(name = "my-pwa", shortName = "pwa")
@Viewport(Viewport.DEVICE_DIMENSIONS)
public static class MyAppShellWithViewportAndMultipleMeta extends VaadinAppShell {
@BodySize(height = "50vh", width = "50vw")
public static class MyAppShellWithMultipleAnnotations extends VaadinAppShell {
}

@Meta(name = "offending-foo", content = "bar")
@Meta(name = "offending-lorem", content = "ipsum")
@PWA(name = "offending-my-pwa", shortName = "pwa")
@Viewport(Viewport.DEVICE_DIMENSIONS)
@BodySize(height = "50vh", width = "50vw")
public static class OffendingClass {
}

Expand All @@ -70,9 +72,9 @@ public void setup() throws Exception {
servletContext = Mockito.mock(ServletContext.class);
Mockito.when(servletContext.getAttribute(Mockito.anyString())).then(invocationOnMock -> attributeMap.get(invocationOnMock.getArguments()[0].toString()));
Mockito.doAnswer(invocationOnMock -> attributeMap.put(
invocationOnMock.getArguments()[0].toString(),
invocationOnMock.getArguments()[1]
)).when(servletContext).setAttribute(Mockito.anyString(), Mockito.any());
invocationOnMock.getArguments()[0].toString(),
invocationOnMock.getArguments()[1]
)).when(servletContext).setAttribute(Mockito.anyString(), Mockito.any());

ServletRegistration registration = Mockito
.mock(ServletRegistration.class);
Expand Down Expand Up @@ -100,20 +102,24 @@ public void teardown() throws Exception {
}

@Test
public void should_haveMetas_when_annotatedAppShell() throws Exception {
classes.add(MyAppShellWithViewportAndMultipleMeta.class);
public void should_haveMetasAndBodySize_when_annotatedAppShell() throws Exception {
classes.add(MyAppShellWithMultipleAnnotations.class);

initializer.onStartup(classes, servletContext);
VaadinAppShellRegistry.getInstance(context).modifyIndexHtmlResponse(document);

VaadinAppShellRegistry.getInstance(context)
.modifyIndexHtmlResponse(document);

List<Element> elements = document.head().children();
assertEquals(3, elements.size());
assertEquals(4, elements.size());
assertEquals("foo", elements.get(0).attr("name"));
assertEquals("bar", elements.get(0).attr("content"));
assertEquals("lorem", elements.get(1).attr("name"));
assertEquals("ipsum", elements.get(1).attr("content"));
assertEquals("viewport", elements.get(2).attr("name"));
assertEquals(Viewport.DEVICE_DIMENSIONS, elements.get(2).attr("content"));
assertEquals("text/css", elements.get(3).attr("type"));
assertEquals("body,#outlet{height:50vh;width:50vw;}", elements.get(3).childNode(0).toString());
}

@Test
Expand All @@ -131,21 +137,23 @@ public void should_reuseContextAppShell_when_creatingNewInstance()

// Set class in context and do not call initializer
VaadinAppShellRegistry registry = new VaadinAppShellRegistry();
registry.setShell(MyAppShellWithViewportAndMultipleMeta.class);
registry.setShell(MyAppShellWithMultipleAnnotations.class);
context.setAttribute(new VaadinAppShellRegistryWrapper(registry));

VaadinAppShellRegistry.getInstance(context)
.modifyIndexHtmlResponse(document);

List<Element> elements = document.head().children();

assertEquals(3, elements.size());
assertEquals(4, elements.size());
assertEquals("foo", elements.get(0).attr("name"));
assertEquals("bar", elements.get(0).attr("content"));
assertEquals("lorem", elements.get(1).attr("name"));
assertEquals("ipsum", elements.get(1).attr("content"));
assertEquals("viewport", elements.get(2).attr("name"));
assertEquals(Viewport.DEVICE_DIMENSIONS, elements.get(2).attr("content"));
assertEquals("text/css", elements.get(3).attr("type"));
assertEquals("body,#outlet{height:50vh;width:50vw;}", elements.get(3).childNode(0).toString());
}

@Test
Expand All @@ -154,8 +162,7 @@ public void should_throw_when_offendingClass() throws Exception {
exception.expectMessage(
containsString("Found app shell configuration annotations in non"));
exception.expectMessage(
containsString("- @Meta, @PWA, @Viewport from"));

containsString("- @Meta, @PWA, @Viewport, @BodySize from"));
classes.add(MyAppShellWithoutMeta.class);
classes.add(OffendingClass.class);
initializer.onStartup(classes, servletContext);
Expand All @@ -180,7 +187,7 @@ public void should_throw_when_multipleAppShell() throws Exception {
exception.expectMessage(containsString("Unable to find a single class"));

classes.add(MyAppShellWithoutMeta.class);
classes.add(MyAppShellWithViewportAndMultipleMeta.class);
classes.add(MyAppShellWithMultipleAnnotations.class);
initializer.onStartup(classes, servletContext);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.vaadin.flow.ccdmtest;

import com.vaadin.flow.component.page.BodySize;
import com.vaadin.flow.component.page.Meta;
import com.vaadin.flow.component.page.VaadinAppShell;
import com.vaadin.flow.component.page.Viewport;
Expand All @@ -8,5 +9,6 @@
@Meta(name = "foo", content = "bar")
@PWA(name = "My App", shortName = "app")
@Viewport(Viewport.DEVICE_DIMENSIONS)
@BodySize(height = "50vh", width = "50vw")
public class AppShell extends VaadinAppShell {
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public void serviceInit(ServiceInitEvent event) {
getBaseUrl(indexHtmlResponse) + "/image/my_app.png");
indexHtmlResponse.getDocument().head().appendChild(meta);
});

Element styleElem = new Element("style");
meta.attr("type", "text/css");
styleElem.text("body,#outlet{height:50vh;width:50vw;}");
event.addIndexHtmlRequestListener(
response -> response.getDocument().head().appendChild(styleElem));
}

private static String getBaseUrl(IndexHtmlResponse indexHtmlResponse) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.vaadin.flow.ccdmtest;

import java.util.Optional;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
Expand Down Expand Up @@ -94,6 +93,21 @@ public void indexHtmlRequestListener_openRootURL_shouldDynamicMetaContent() {
viewportMeta.get().getAttribute("content"));
}

public void indexHtmlRequestListener_openRootURL_shouldDynamicBodySizeContent() {
openTestUrl("/");
waitForElementPresent(By.cssSelector("style"));
Optional<WebElement> styleElement = findElements(By.tagName("style"))
.stream().filter(webElement -> webElement.getAttribute("type")
.equals("text/css"))
.findFirst();
Assert.assertTrue("The response should have style element",
styleElement.isPresent());
Assert.assertEquals(
"style element should have correct content",
"body,#outlet{height:50vh;width:50vw;}",
styleElement.get().getText());
}

@Test
public void indexHtmlRequestHandler_openRandomRoute_shouldResponseIndexHtml() {
openTestUrl("/abc");
Expand Down