From ea9b2539adc9c544c97a8124de4aa860b8d52243 Mon Sep 17 00:00:00 2001 From: Nicolas Peltier Date: Fri, 4 Sep 2020 18:00:08 +0200 Subject: [PATCH] GH-24 make StyleService responsive --- .../java/com/adobe/dx/style/StyleWorker.java | 11 +- .../com/adobe/dx/style/internal/Border.java | 29 ++-- .../com/adobe/dx/style/internal/Shadow.java | 34 +++-- .../dx/style/internal/StyleServiceImpl.java | 67 ++++++--- .../java/com/adobe/dx/utils/RequestUtil.java | 65 +++++++++ .../internal/ResponsivePropertiesTest.java | 4 +- .../internal/AbstractStyleWorkerTest.java | 42 ++++++ .../adobe/dx/style/internal/BorderTest.java | 13 +- .../adobe/dx/style/internal/ShadowTest.java | 13 +- .../style/internal/StyleServiceImplTest.java | 133 +++++++++++++----- 10 files changed, 313 insertions(+), 98 deletions(-) create mode 100644 bundles/core/src/main/java/com/adobe/dx/utils/RequestUtil.java create mode 100644 bundles/core/src/test/java/com/adobe/dx/style/internal/AbstractStyleWorkerTest.java diff --git a/bundles/core/src/main/java/com/adobe/dx/style/StyleWorker.java b/bundles/core/src/main/java/com/adobe/dx/style/StyleWorker.java index dd7a6be7..3d1f2487 100644 --- a/bundles/core/src/main/java/com/adobe/dx/style/StyleWorker.java +++ b/bundles/core/src/main/java/com/adobe/dx/style/StyleWorker.java @@ -15,8 +15,9 @@ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ package com.adobe.dx.style; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ValueMap; +import com.adobe.dx.responsive.Breakpoint; + +import org.apache.sling.api.SlingHttpServletRequest; import org.jetbrains.annotations.Nullable; /** @@ -32,10 +33,10 @@ public interface StyleWorker { /** * Generates a declaration specific to that generator, for an upper rule * - * @param resource current component resource, - * @param dxPolicy policy, could be obtained from above resource, but in the signature for practical reason, + * @param breakpoint breakpoint if declaration should be specific to one, null otherwise + * @param request current request * @return single or several declarations split by ';', or null if not necessary or able to generate some */ - @Nullable String getDeclaration(Resource resource, ValueMap dxPolicy); + @Nullable String getDeclaration(@Nullable Breakpoint breakpoint, SlingHttpServletRequest request); } diff --git a/bundles/core/src/main/java/com/adobe/dx/style/internal/Border.java b/bundles/core/src/main/java/com/adobe/dx/style/internal/Border.java index 05eeaba9..38a084ad 100644 --- a/bundles/core/src/main/java/com/adobe/dx/style/internal/Border.java +++ b/bundles/core/src/main/java/com/adobe/dx/style/internal/Border.java @@ -20,14 +20,16 @@ import static com.adobe.dx.style.Constants.PX; import static com.adobe.dx.style.Constants.PX_SPACE; import static com.adobe.dx.style.Constants.SPACE; +import static com.adobe.dx.utils.RequestUtil.getPolicy; +import com.adobe.dx.responsive.Breakpoint; import com.adobe.dx.style.StyleWorker; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; -import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.ValueMap; import org.jetbrains.annotations.Nullable; import org.osgi.service.component.annotations.Component; @@ -68,18 +70,21 @@ public String getKey() { } @Override - public @Nullable String getDeclaration(Resource resource, ValueMap dxPolicy) { - List declarations = null; - String border = buildBorder(dxPolicy); - String radius = buildRadius(dxPolicy); - if (border != null) { - if (radius != null) { - return border + DEL_SPACE + radius; - } else { - return border; + public @Nullable String getDeclaration(Breakpoint breakpoint, SlingHttpServletRequest request) { + if (breakpoint == null) { + //we only do border for all + ValueMap dxPolicy = getPolicy(request); + String border = buildBorder(dxPolicy); + String radius = buildRadius(dxPolicy); + if (border != null) { + if (radius != null) { + return border + DEL_SPACE + radius; + } else { + return border; + } + } else if (radius != null) { + return radius; } - } else if (radius != null) { - return radius; } return null; } diff --git a/bundles/core/src/main/java/com/adobe/dx/style/internal/Shadow.java b/bundles/core/src/main/java/com/adobe/dx/style/internal/Shadow.java index 640d584d..0d8be9e3 100644 --- a/bundles/core/src/main/java/com/adobe/dx/style/internal/Shadow.java +++ b/bundles/core/src/main/java/com/adobe/dx/style/internal/Shadow.java @@ -16,10 +16,12 @@ package com.adobe.dx.style.internal; import static com.adobe.dx.style.Constants.PX_SPACE; +import static com.adobe.dx.utils.RequestUtil.getPolicy; +import com.adobe.dx.responsive.Breakpoint; import com.adobe.dx.style.StyleWorker; -import org.apache.sling.api.resource.Resource; +import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.ValueMap; import org.jetbrains.annotations.Nullable; import org.osgi.service.component.annotations.Component; @@ -45,20 +47,24 @@ public String getKey() { } @Override - public @Nullable String getDeclaration(Resource resource, ValueMap dxPolicy) { - String color = dxPolicy.get(PN_COLOR, String.class); - if (color != null) { - StringBuilder sb = new StringBuilder(); - sb.append(RULE) - .append(dxPolicy.get(PN_OFFSETX, 0L)).append(PX_SPACE) - .append(dxPolicy.get(PN_OFFSETY, 0L)).append(PX_SPACE) - .append(dxPolicy.get(PN_BLUR, 0L)).append(PX_SPACE) - .append(dxPolicy.get(PN_SPREAD, 0L)).append(PX_SPACE) - .append(color); - if (dxPolicy.containsKey(PN_INSET)) { - sb.append(INSET_SUFFIX); + public @Nullable String getDeclaration(Breakpoint breakpoint, SlingHttpServletRequest request) { + if (breakpoint == null) { + //we only do border for all + ValueMap dxPolicy = getPolicy(request); + String color = dxPolicy.get(PN_COLOR, String.class); + if (color != null) { + StringBuilder sb = new StringBuilder(); + sb.append(RULE) + .append(dxPolicy.get(PN_OFFSETX, 0L)).append(PX_SPACE) + .append(dxPolicy.get(PN_OFFSETY, 0L)).append(PX_SPACE) + .append(dxPolicy.get(PN_BLUR, 0L)).append(PX_SPACE) + .append(dxPolicy.get(PN_SPREAD, 0L)).append(PX_SPACE) + .append(color); + if (dxPolicy.containsKey(PN_INSET)) { + sb.append(INSET_SUFFIX); + } + return sb.toString(); } - return sb.toString(); } return null; } diff --git a/bundles/core/src/main/java/com/adobe/dx/style/internal/StyleServiceImpl.java b/bundles/core/src/main/java/com/adobe/dx/style/internal/StyleServiceImpl.java index 16a43869..ea526ba8 100644 --- a/bundles/core/src/main/java/com/adobe/dx/style/internal/StyleServiceImpl.java +++ b/bundles/core/src/main/java/com/adobe/dx/style/internal/StyleServiceImpl.java @@ -17,11 +17,14 @@ package com.adobe.dx.style.internal; import static com.adobe.dx.style.Constants.DECLARATION_DELIMITER; +import static com.day.cq.wcm.commons.Constants.EMPTY_STRING_ARRAY; import static org.apache.commons.lang3.StringUtils.EMPTY; import com.adobe.dx.bindings.internal.DxBindingsValueProvider; +import com.adobe.dx.responsive.Breakpoint; import com.adobe.dx.style.StyleWorker; import com.adobe.dx.style.StyleService; +import com.adobe.dx.utils.RequestUtil; import java.util.ArrayList; import java.util.HashMap; @@ -30,10 +33,11 @@ import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; +import org.apache.poi.hssf.record.PageBreakRecord; import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ValueMap; import org.apache.sling.api.scripting.SlingBindings; +import org.jetbrains.annotations.NotNull; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.ConfigurationPolicy; import org.osgi.service.component.annotations.Reference; @@ -47,6 +51,7 @@ public class StyleServiceImpl implements StyleService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); private static final String FORMAT_ID = "#%s {%s}"; + private static final String FORMAT_BP = "\n%s {\n%s\n}"; private static final String SLASH = "/"; private static final String TYPE_PREFIX = "/apps/"; private static final String STYLEWORKERS_SUFFIX = "/styleWorkers"; @@ -59,33 +64,49 @@ public class StyleServiceImpl implements StyleService { Map workerMap = MapUtils.EMPTY_MAP; + String getStylePerBreakpoint(String id, Breakpoint breakpoint, String[] keys, SlingHttpServletRequest request) { + List declarations = null; + for (String workerKey : keys) { + if (workerMap.containsKey(workerKey)) { + logger.debug("found {} worker", workerKey); + String declaration = workerMap.get(workerKey).getDeclaration(breakpoint, request); + if (StringUtils.isNotBlank(declaration)) { + logger.debug("generated {}", declaration); + if (declarations == null) { + declarations = new ArrayList<>(); + } + declarations.add(declaration); + } + } + } + if (declarations != null && !declarations.isEmpty()) { + String concat = String.join(DECLARATION_DELIMITER, declarations); + return StringUtils.isNotBlank(id) ? String.format(FORMAT_ID, id, concat) : concat; + } + return EMPTY; + } + @Override public String getLocalStyle(String id, SlingHttpServletRequest request) { - List declarations = null; Resource resource = request.getResource(); String[] keys = getWorkerKeys(resource); - if (keys != null) { - SlingBindings bindings = (SlingBindings)request.getAttribute(SlingBindings.class.getName()); - ValueMap dxPolicy = bindings != null ? (ValueMap)bindings.get(DxBindingsValueProvider.POLICY_KEY) : ValueMap.EMPTY; - for (String workerKey : keys) { - StyleWorker worker = workerMap.get(workerKey); - if (worker != null) { - String declaration = worker.getDeclaration(resource, dxPolicy); - if (StringUtils.isNotBlank(declaration)) { - logger.debug("generated {} from {}", declaration, request); - if (declarations == null) { - declarations = new ArrayList<>(); - } - declarations.add(declaration); + if (keys.length > 0) { + StringBuilder style = new StringBuilder(); + String defaultStyle = getStylePerBreakpoint(id, null, keys, request); + if (StringUtils.isNotBlank(defaultStyle)) { + style.append(defaultStyle); + } + Breakpoint[] breakpoints = RequestUtil.getBreakpoints(request); + if (breakpoints != null) { + for (Breakpoint breakpoint : breakpoints) { + String bpStyle = getStylePerBreakpoint(id, breakpoint, keys, request); + if (StringUtils.isNotBlank(bpStyle)) { + style.append(String.format(FORMAT_BP, breakpoint.mediaQuery(), bpStyle)); } - } else { - logger.debug("{} was required resource type {}, but no associated worker is registered", workerKey, - resource.getResourceType()); } } - if (declarations != null && !declarations.isEmpty()) { - String concat = String.join(DECLARATION_DELIMITER, declarations); - return StringUtils.isNotBlank(id) ? String.format(FORMAT_ID, id, concat) : concat; + if (style.length() > 0) { + return style.toString(); } } return EMPTY; @@ -94,14 +115,14 @@ public String getLocalStyle(String id, SlingHttpServletRequest request) { /** * returns ordered list of workers for that given resource (or null) */ - String[] getWorkerKeys(Resource resource) { + @NotNull String[] getWorkerKeys(Resource resource) { String type = resource.getResourceType(); String typePath = (type.startsWith(SLASH) ? type : TYPE_PREFIX + type) + STYLEWORKERS_SUFFIX; Resource keys = resource.getResourceResolver().getResource(typePath); if (keys != null) { return keys.adaptTo(String[].class); } - return null; + return EMPTY_STRING_ARRAY; } void refreshWorkers() { diff --git a/bundles/core/src/main/java/com/adobe/dx/utils/RequestUtil.java b/bundles/core/src/main/java/com/adobe/dx/utils/RequestUtil.java new file mode 100644 index 00000000..8e7e5564 --- /dev/null +++ b/bundles/core/src/main/java/com/adobe/dx/utils/RequestUtil.java @@ -0,0 +1,65 @@ +/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + ~ Copyright 2020 Adobe + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ +package com.adobe.dx.utils; + +import com.adobe.dx.bindings.internal.DxBindingsValueProvider; +import com.adobe.dx.responsive.Breakpoint; + +import java.util.Map; + +import org.apache.sling.api.SlingHttpServletRequest; +import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.api.scripting.SlingBindings; + +/** + * Utility class for fetching utility objects from request + */ +public class RequestUtil { + private RequestUtil() { + } + + /** + * @param request current request + * @return sling bindings + */ + public static final SlingBindings getBindings(SlingHttpServletRequest request) { + return (SlingBindings)request.getAttribute(SlingBindings.class.getName()); + } + + /** + * @param request current request + * @return current DX policy + */ + public static final ValueMap getPolicy(SlingHttpServletRequest request) { + return (ValueMap)getBindings(request).get(DxBindingsValueProvider.POLICY_KEY); + } + + /** + * @param request current request + * @return current set of breakpoints + */ + public static final Breakpoint[] getBreakpoints(SlingHttpServletRequest request) { + return (Breakpoint[]) getBindings(request).get(DxBindingsValueProvider.BP_KEY); + } + + /** + * @param request current request + * @return current set of responsive properties + */ + public static final Map getResponsiveProperties(SlingHttpServletRequest request) { + return (Map) getBindings(request).get(DxBindingsValueProvider.RESP_PROPS_KEY); + } +} diff --git a/bundles/core/src/test/java/com/adobe/dx/responsive/internal/ResponsivePropertiesTest.java b/bundles/core/src/test/java/com/adobe/dx/responsive/internal/ResponsivePropertiesTest.java index 6f5f20d6..a09acd89 100644 --- a/bundles/core/src/test/java/com/adobe/dx/responsive/internal/ResponsivePropertiesTest.java +++ b/bundles/core/src/test/java/com/adobe/dx/responsive/internal/ResponsivePropertiesTest.java @@ -44,8 +44,8 @@ public static ResponsiveConfiguration initResponsiveConfiguration(AemContext con context.build().resource(CONF_ROOT + "/sling:configs/" + ResponsiveConfiguration.class.getName() + "/breakpoints") .siblingsMode() .resource("1","propertySuffix", "Mobile", "key", "mobile") - .resource("2", "propertySuffix", "Tablet", "key", "tablet") - .resource("3", "propertySuffix", "Desktop", "key", "desktop"); + .resource("2", "propertySuffix", "Tablet", "key", "tablet", "mediaQuery", "@media screen and (min-width: 600px)") + .resource("3", "propertySuffix", "Desktop", "key", "desktop", "mediaQuery", "@media screen and (min-width: 1200px)"); MockContextAwareConfig.registerAnnotationClasses(context, ResponsiveConfiguration.class); MockContextAwareConfig.registerAnnotationClasses(context, Breakpoint.class); context.create().resource(CONTENT_ROOT, "sling:configRef", CONF_ROOT); diff --git a/bundles/core/src/test/java/com/adobe/dx/style/internal/AbstractStyleWorkerTest.java b/bundles/core/src/test/java/com/adobe/dx/style/internal/AbstractStyleWorkerTest.java new file mode 100644 index 00000000..3a6d2256 --- /dev/null +++ b/bundles/core/src/test/java/com/adobe/dx/style/internal/AbstractStyleWorkerTest.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * + * ADOBE CONFIDENTIAL + * __________________ + * + * Copyright 2019 Adobe + * All Rights Reserved. + * + * NOTICE: All information contained herein is, and remains + * the property of Adobe and its suppliers, if any. The intellectual + * and technical concepts contained herein are proprietary to Adobe + * and its suppliers and are protected by all applicable intellectual + * property laws, including trade secret and copyright laws. + * Dissemination of this information or reproduction of this material + * is strictly forbidden unless prior written permission is obtained + * from Adobe. + ******************************************************************************/ + +package com.adobe.dx.style.internal; + +import com.adobe.dx.bindings.internal.DxBindingsValueProvider; +import com.adobe.dx.responsive.Breakpoint; +import com.adobe.dx.style.StyleWorker; +import com.adobe.dx.testing.AbstractTest; + +import org.apache.sling.api.scripting.SlingBindings; + +public abstract class AbstractStyleWorkerTest extends AbstractTest { + + abstract StyleWorker getWorker(); + + String getDeclaration() { + return getDeclaration(null); + } + + String getDeclaration(Breakpoint breakpoint) { + + SlingBindings bindings = (SlingBindings)context.request().getAttribute(SlingBindings.class.getName()); + bindings.put(DxBindingsValueProvider.POLICY_KEY, getVM(CONTENT_ROOT)); + return getWorker().getDeclaration(breakpoint, context.request()); + } +} diff --git a/bundles/core/src/test/java/com/adobe/dx/style/internal/BorderTest.java b/bundles/core/src/test/java/com/adobe/dx/style/internal/BorderTest.java index 98144d82..1e52730e 100644 --- a/bundles/core/src/test/java/com/adobe/dx/style/internal/BorderTest.java +++ b/bundles/core/src/test/java/com/adobe/dx/style/internal/BorderTest.java @@ -18,11 +18,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import com.adobe.dx.testing.AbstractTest; +import com.adobe.dx.style.StyleWorker; import org.junit.jupiter.api.Test; -public class BorderTest extends AbstractTest { +public class BorderTest extends AbstractStyleWorkerTest { @Test void lockKey() { @@ -30,13 +30,13 @@ void lockKey() { } private void assertBorderEquals(String expected) { - assertEquals(expected, new Border().getDeclaration(context.currentResource(), getVM(CONTENT_ROOT))); + assertEquals(expected, getDeclaration()); } @Test void getNoBorder() { context.build().resource(CONTENT_ROOT, "foo", "bar"); - assertNull(new Border().getDeclaration(context.currentResource(), getVM(CONTENT_ROOT))); + assertNull(getDeclaration(null)); } @Test @@ -82,4 +82,9 @@ void getSomeBordersAndRadiuses() { "borderRadius", "each", "borderRadiusBottomLeft", 4, "borderRadiusTopRight", 3); assertBorderEquals("border-top: dotted 4px red; border-bottom: dotted 4px red; border-radius: 0px 3px 0px 4px"); } + + @Override + StyleWorker getWorker() { + return new Border(); + } } diff --git a/bundles/core/src/test/java/com/adobe/dx/style/internal/ShadowTest.java b/bundles/core/src/test/java/com/adobe/dx/style/internal/ShadowTest.java index 5610af5d..cfb51690 100644 --- a/bundles/core/src/test/java/com/adobe/dx/style/internal/ShadowTest.java +++ b/bundles/core/src/test/java/com/adobe/dx/style/internal/ShadowTest.java @@ -17,11 +17,16 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import com.adobe.dx.testing.AbstractTest; +import com.adobe.dx.style.StyleWorker; import org.junit.jupiter.api.Test; -class ShadowTest extends AbstractTest { +class ShadowTest extends AbstractStyleWorkerTest { + + @Override + StyleWorker getWorker() { + return new Shadow(); + } @Test void lockKey() { @@ -35,13 +40,13 @@ void getShadow() { "shadowOffsetY", 10L, "shadowBlur", 11L, "shadowSpread", 12L); - assertEquals("box-shadow: 9px 10px 11px 12px blue", new Shadow().getDeclaration(context.currentResource(), getVM(CONTENT_ROOT))); + assertEquals("box-shadow: 9px 10px 11px 12px blue", getDeclaration()); } @Test void testInset() { context.build().resource(CONTENT_ROOT, "shadowColor", "red", "shadowInset", true); - assertEquals("box-shadow: 0px 0px 0px 0px red inset", new Shadow().getDeclaration(context.currentResource(), getVM(CONTENT_ROOT))); + assertEquals("box-shadow: 0px 0px 0px 0px red inset", getDeclaration()); } } \ No newline at end of file diff --git a/bundles/core/src/test/java/com/adobe/dx/style/internal/StyleServiceImplTest.java b/bundles/core/src/test/java/com/adobe/dx/style/internal/StyleServiceImplTest.java index 684e79da..69661b4c 100644 --- a/bundles/core/src/test/java/com/adobe/dx/style/internal/StyleServiceImplTest.java +++ b/bundles/core/src/test/java/com/adobe/dx/style/internal/StyleServiceImplTest.java @@ -18,54 +18,119 @@ import static org.junit.jupiter.api.Assertions.*; import com.adobe.dx.bindings.internal.DxBindingsValueProvider; +import com.adobe.dx.responsive.Breakpoint; +import com.adobe.dx.responsive.ResponsiveConfiguration; +import com.adobe.dx.responsive.internal.ResponsiveProperties; +import com.adobe.dx.responsive.internal.ResponsivePropertiesTest; import com.adobe.dx.style.StyleWorker; import com.adobe.dx.testing.AbstractTest; +import com.adobe.dx.utils.RequestUtil; + +import java.util.Map; import org.apache.commons.lang3.StringUtils; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ValueMap; +import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.scripting.SlingBindings; import org.jetbrains.annotations.Nullable; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; class StyleServiceImplTest extends AbstractTest { - @Test - void getLocalStyle() { - final String[] array = new String[] {"worker1", "worker2"}; - context.build().resource("/apps/foo/bar", "styleWorkers", array); - context.build().resource(CONTENT_ROOT, "sling:resourceType", "foo/bar", "color", "blue", "fontsize", "13px"); - context.currentResource(CONTENT_ROOT); - ((SlingBindings)context.request().getAttribute(SlingBindings.class.getName())).put(DxBindingsValueProvider.POLICY_KEY, context.currentResource().getValueMap()); - StyleServiceImpl service = new StyleServiceImpl(); - assertTrue(StringUtils.isBlank(service.getLocalStyle(null, context.request()))); - service.bindWorker(new StyleWorker() { - @Override - public String getKey() { - return "worker1"; + private static String getFromRequest(String cssKey, String bpKey, String prop, SlingHttpServletRequest request) { + String value = null; + Map resprops = RequestUtil.getResponsiveProperties(request); + if (resprops != null) { + if (resprops.get(prop) != null) { + Map map = (Map)resprops.get(prop); + if (map != null) { + value = map.get(bpKey) != null ? map.get(bpKey).toString() : null; + } } + } + if (StringUtils.isNotBlank(value)) { + return cssKey + ": " + value; + } + return null; + } - @Override - public @Nullable String getDeclaration(Resource resource, ValueMap dxPolicy) { - return "color: " + dxPolicy.get("color", String.class); - } - }); - StyleWorker worker2 = new StyleWorker() { - @Override - public String getKey() { - return "worker2"; + StyleWorker worker1 = new StyleWorker() { + @Override + public String getKey() { + return "worker1"; + } + + @Override + public @Nullable String getDeclaration(Breakpoint breakpoint, SlingHttpServletRequest request) { + if (breakpoint == null) { + return "color: " + RequestUtil.getPolicy(request).get("color", String.class); + } else { + return getFromRequest("min-height", breakpoint.key(), "minheight", request); } + } + }; + + StyleWorker worker2 = new StyleWorker() { + @Override + public String getKey() { + return "worker2"; + } - @Override - public @Nullable String getDeclaration(Resource resource, ValueMap dxPolicy) { - return "font-size: " + dxPolicy.get("fontsize", String.class); + @Override + public @Nullable String getDeclaration(Breakpoint breakpoint,SlingHttpServletRequest request) { + if (breakpoint == null) { + return "font-size: " + RequestUtil.getPolicy(request).get("fontsize", String.class); + } else { + return getFromRequest("min-width", breakpoint.key(), "minwidth", request); } - }; + } + }; + StyleServiceImpl service; + + @BeforeEach + void setup() { + ResponsiveConfiguration conf = ResponsivePropertiesTest.initResponsiveConfiguration(context); + String someComp = CONTENT_ROOT + "/comp"; + final String[] array = new String[] {"worker1", "worker2"}; + context.build().resource("/apps/foo/bar", "styleWorkers", array); + context.build().resource(someComp, "sling:resourceType", "foo/bar", + "color", "blue", + "minheightTablet", "200px", + "fontsize", "13px", + "minwidthDesktop", "90%"); + context.currentResource(someComp); + ((SlingBindings)context.request().getAttribute(SlingBindings.class.getName())).put(DxBindingsValueProvider.POLICY_KEY, context.currentResource().getValueMap()); + RequestUtil.getBindings(context.request()).put(DxBindingsValueProvider.BP_KEY, + conf.breakpoints()); + RequestUtil.getBindings(context.request()).put(DxBindingsValueProvider.RESP_PROPS_KEY, + new ResponsiveProperties(conf, context.currentResource().getValueMap())); + service = new StyleServiceImpl(); + } + + @Test + void getLocalStyle() { + assertTrue(StringUtils.isBlank(service.getLocalStyle(null, context.request()))); + service.bindWorker(worker1); service.bindWorker(worker2); - assertEquals("color: blue;font-size: 13px", service.getLocalStyle(null, context.request())); - assertEquals("#this-is-my-block {color: blue;font-size: 13px}", service.getLocalStyle("this-is-my-block", context.request())); + assertEquals("color: blue;font-size: 13px\n" + + "@media screen and (min-width: 600px) {\n" + + "min-height: 200px\n" + + "}\n" + + "@media screen and (min-width: 1200px) {\n" + + "min-width: 90%\n" + + "}", service.getLocalStyle(null, context.request())); + assertEquals("#this-is-my-block {color: blue;font-size: 13px}\n" + + "@media screen and (min-width: 600px) {\n" + + "#this-is-my-block {min-height: 200px}\n" + + "}\n" + + "@media screen and (min-width: 1200px) {\n" + + "#this-is-my-block {min-width: 90%}\n" + + "}", service.getLocalStyle("this-is-my-block", context.request())); service.unbindWorker(worker2); - assertEquals("color: blue", service.getLocalStyle(null, context.request())); + assertEquals("color: blue\n" + + "@media screen and (min-width: 600px) {\n" + + "min-height: 200px\n" + + "}", service.getLocalStyle(null, context.request())); } @Test @@ -79,8 +144,8 @@ void getWorkerKeysFullPath() { @Test void getWorkerKeysNothing() { - context.build().resource("/apps/foo/bar", "blah", "blah"); - context.build().resource(CONTENT_ROOT, "sling:resourceType", "foo/bar"); - assertNull(new StyleServiceImpl().getWorkerKeys(context.currentResource(CONTENT_ROOT))); + context.build().resource("/apps/check/this", "blah", "blah"); + context.build().resource(CONTENT_ROOT, "sling:resourceType", "check/this"); + assertEquals(0, new StyleServiceImpl().getWorkerKeys(context.currentResource(CONTENT_ROOT)).length); } } \ No newline at end of file