Search in sources :

Example 1 with HtmlLibraryManager

use of com.adobe.granite.ui.clientlibs.HtmlLibraryManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ClientLibraryCategoriesDataSourceServletTest method setUp.

@BeforeEach
public void setUp() {
    context.load().json(TEST_BASE + CoreComponentTestContext.TEST_CONTENT_JSON, TEST_APPS_ROOT);
    dataSourceServlet = new ClientLibraryCategoriesDataSourceServlet();
    HtmlLibraryManager htmlLibraryManager = mock(HtmlLibraryManager.class);
    Map<String, ClientLibrary> clientLibraries = new HashMap<>();
    when(htmlLibraryManager.getLibraries()).thenReturn(clientLibraries);
    // A. JavaScript only
    ClientLibrary libraryA = mock(ClientLibrary.class);
    when(libraryA.getCategories()).thenReturn(CLIENTLIB_A_CATEGORIES);
    clientLibraries.put(libraryA.getPath(), libraryA);
    // B. CSS only
    ClientLibrary libraryB = mock(ClientLibrary.class);
    when(libraryB.getPath()).thenReturn(CLIENTLIB_B_PATH);
    when(libraryB.getCategories()).thenReturn(CLIENTLIB_B_CATEGORIES);
    clientLibraries.put(libraryB.getPath(), libraryB);
    // C. JavaScript and CSS
    ClientLibrary libraryC = mock(ClientLibrary.class);
    when(libraryC.getPath()).thenReturn(CLIENTLIB_C_PATH);
    when(libraryC.getCategories()).thenReturn(CLIENTLIB_C_CATEGORIES);
    clientLibraries.put(libraryC.getPath(), libraryC);
    Utils.setInternalState(dataSourceServlet, "htmlLibraryManager", htmlLibraryManager);
}
Also used : ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary) HtmlLibraryManager(com.adobe.granite.ui.clientlibs.HtmlLibraryManager) HashMap(java.util.HashMap) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with HtmlLibraryManager

use of com.adobe.granite.ui.clientlibs.HtmlLibraryManager in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class ClientLibrariesImplTest method setUp.

@BeforeEach
void setUp() {
    context.load().json(BASE + CoreComponentTestContext.TEST_CONTENT_JSON, CONTENT_ROOT);
    context.load().json(BASE + CoreComponentTestContext.TEST_APPS_JSON, APPS_ROOT);
    context.load().json(BASE + TEST_CONTENT_XF_JSON, XF_ROOT);
    context.load().json(BASE + TEST_CONF_TEMPLATES_JSON, TEMPLATES_ROOT);
    jsIncludes = new HashMap<>();
    jsIncludes.put(TEASER_CATEGORY, "<script src=\"" + TEASER_CLIENTLIB_PATH + ".js\"></script>");
    jsIncludes.put(ACCORDION_CATEGORY, "<script src=\"" + ACCORDION_CLIENTLIB_PATH + ".js\"></script>");
    jsIncludes.put(CAROUSEL_CATEGORY, "<script src=\"" + CAROUSEL_CLIENTLIB_PATH + ".js\"></script>");
    jsIncludesWithAttributes = new HashMap<>();
    jsIncludesWithAttributes.put(TEASER_CATEGORY, "<script async defer crossorigin=\"anonymous\" onload=\"myFunction()\" src=\"" + TEASER_CLIENTLIB_PATH + ".js\"></script>");
    jsIncludesWithAttributes.put(ACCORDION_CATEGORY, "<script async defer crossorigin=\"anonymous\" onload=\"myFunction()\" src=\"" + ACCORDION_CLIENTLIB_PATH + ".js\"></script>");
    jsIncludesWithAttributes.put(CAROUSEL_CATEGORY, "<script async defer crossorigin=\"anonymous\" onload=\"myFunction()\" src=\"" + CAROUSEL_CLIENTLIB_PATH + ".js\"></script>");
    cssIncludes = new HashMap<>();
    cssIncludes.put(TEASER_CATEGORY, "<link rel=\"stylesheet\" href=\"" + TEASER_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    cssIncludes.put(ACCORDION_CATEGORY, "<link rel=\"stylesheet\" href=\"" + ACCORDION_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    cssIncludes.put(CAROUSEL_CATEGORY, "<link rel=\"stylesheet\" href=\"" + CAROUSEL_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    cssIncludesWithAttributes = new HashMap<>();
    cssIncludesWithAttributes.put(TEASER_CATEGORY, "<link media=\"print\" rel=\"stylesheet\" href=\"" + TEASER_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    cssIncludesWithAttributes.put(ACCORDION_CATEGORY, "<link media=\"print\" rel=\"stylesheet\" href=\"" + ACCORDION_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    cssIncludesWithAttributes.put(CAROUSEL_CATEGORY, "<link media=\"print\" rel=\"stylesheet\" href=\"" + CAROUSEL_CLIENTLIB_PATH + ".css\" type=\"text/css\">");
    jsInlines = new HashMap<>();
    jsInlines.put(TEASER_CATEGORY, "console.log('teaser clientlib js');");
    jsInlines.put(ACCORDION_CATEGORY, "console.log('accordion clientlib js');");
    jsInlines.put(CAROUSEL_CATEGORY, "console.log('carousel clientlib js');");
    cssInlines = new HashMap<>();
    cssInlines.put(TEASER_CATEGORY, "html, .teaser { \n box-sizing: border-box;\n font-size: 14px; \n}");
    cssInlines.put(ACCORDION_CATEGORY, "html, .accordion { \n box-sizing: border-box;\n font-size: 14px; \n}");
    cssInlines.put(CAROUSEL_CATEGORY, "html, .carousel { \n box-sizing: border-box;\n font-size: 14px; \n}");
    // Mock ClientLibrary
    ClientLibrary teaserClientLibrary = mock(ClientLibrary.class);
    ClientLibrary accordionClientLibrary = mock(ClientLibrary.class);
    ClientLibrary carouselClientLibrary = mock(ClientLibrary.class);
    String[] teaserCategories = new String[] { TEASER_CATEGORY };
    when(teaserClientLibrary.getCategories()).thenReturn(teaserCategories);
    when(teaserClientLibrary.getPath()).thenReturn(TEASER_CLIENTLIB_PATH);
    String[] accordionCategories = new String[] { ACCORDION_CATEGORY };
    when(accordionClientLibrary.getCategories()).thenReturn(accordionCategories);
    when(accordionClientLibrary.getPath()).thenReturn(ACCORDION_CLIENTLIB_PATH);
    String[] carouselCategories = new String[] { CAROUSEL_CATEGORY };
    when(carouselClientLibrary.getCategories()).thenReturn(carouselCategories);
    when(carouselClientLibrary.getPath()).thenReturn(CAROUSEL_CLIENTLIB_PATH);
    librariesMap = new HashMap<>();
    librariesMap.put(TEASER_CATEGORY, teaserClientLibrary);
    librariesMap.put(ACCORDION_CATEGORY, accordionClientLibrary);
    librariesMap.put(CAROUSEL_CATEGORY, carouselClientLibrary);
    // Mock HtmlLibrary
    HtmlLibrary teaserJsHtmlLibrary = mock(HtmlLibrary.class);
    HtmlLibrary accordionJsHtmlLibrary = mock(HtmlLibrary.class);
    HtmlLibrary carouselJsHtmlLibrary = mock(HtmlLibrary.class);
    HtmlLibrary teaserCssHtmlLibrary = mock(HtmlLibrary.class);
    HtmlLibrary accordionCssHtmlLibrary = mock(HtmlLibrary.class);
    HtmlLibrary carouselCssHtmlLibrary = mock(HtmlLibrary.class);
    Resource teaserJsClientLibResource = context.currentResource(TEASER_CLIENTLIB_PATH + JS_FILE_REL_PATH);
    Resource accordionJsClientLibResource = context.currentResource(ACCORDION_CLIENTLIB_PATH + JS_FILE_REL_PATH);
    Resource carouselJsClientLibResource = context.currentResource(CAROUSEL_CLIENTLIB_PATH + JS_FILE_REL_PATH);
    Resource teaserCssClientLibResource = context.currentResource(TEASER_CLIENTLIB_PATH + CSS_FILE_REL_PATH);
    Resource accordionCssClientLibResource = context.currentResource(ACCORDION_CLIENTLIB_PATH + CSS_FILE_REL_PATH);
    Resource carouselCssClientLibResource = context.currentResource(CAROUSEL_CLIENTLIB_PATH + CSS_FILE_REL_PATH);
    try {
        when(teaserJsHtmlLibrary.getInputStream(anyBoolean())).thenReturn(teaserJsClientLibResource.adaptTo(InputStream.class));
        when(accordionJsHtmlLibrary.getInputStream(anyBoolean())).thenReturn(accordionJsClientLibResource.adaptTo(InputStream.class));
        when(carouselJsHtmlLibrary.getInputStream(anyBoolean())).thenReturn(carouselJsClientLibResource.adaptTo(InputStream.class));
        when(teaserCssHtmlLibrary.getInputStream(anyBoolean())).thenReturn(teaserCssClientLibResource.adaptTo(InputStream.class));
        when(accordionCssHtmlLibrary.getInputStream(anyBoolean())).thenReturn(accordionCssClientLibResource.adaptTo(InputStream.class));
        when(carouselCssHtmlLibrary.getInputStream(anyBoolean())).thenReturn(carouselCssClientLibResource.adaptTo(InputStream.class));
    } catch (IOException e) {
        fail(String.format("Unable to get input stream from the library: %s", e.getMessage()));
    }
    // Mock htmlLibraryManager.getLibraries()
    allLibraries = new HashMap<>();
    allLibraries.put(TEASER_CLIENTLIB_PATH, teaserClientLibrary);
    allLibraries.put(ACCORDION_CLIENTLIB_PATH, accordionClientLibrary);
    allLibraries.put(CAROUSEL_CLIENTLIB_PATH, carouselClientLibrary);
    HtmlLibraryManager htmlLibraryManager = context.registerInjectActivateService(mock(MockHtmlLibraryManager.class));
    when(htmlLibraryManager.getLibraries()).thenReturn(allLibraries);
    // Mock htmlLibraryManager.getLibraries(a, b, c, d)
    doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        String[] categories = (String[]) args[0];
        Collection<ClientLibrary> libraries = new ArrayList<>();
        for (String category : categories) {
            libraries.add(librariesMap.get(category));
        }
        return libraries;
    }).when(htmlLibraryManager).getLibraries(any(String[].class), any(LibraryType.class), anyBoolean(), anyBoolean());
    // Mock htmlLibraryManager.getLibrary(libraryType, clientlib.getPath())
    when(htmlLibraryManager.getLibrary(eq(LibraryType.JS), eq(TEASER_CLIENTLIB_PATH))).thenReturn(teaserJsHtmlLibrary);
    when(htmlLibraryManager.getLibrary(eq(LibraryType.JS), eq(ACCORDION_CLIENTLIB_PATH))).thenReturn(accordionJsHtmlLibrary);
    when(htmlLibraryManager.getLibrary(eq(LibraryType.JS), eq(CAROUSEL_CLIENTLIB_PATH))).thenReturn(carouselJsHtmlLibrary);
    when(htmlLibraryManager.getLibrary(eq(LibraryType.CSS), eq(TEASER_CLIENTLIB_PATH))).thenReturn(teaserCssHtmlLibrary);
    when(htmlLibraryManager.getLibrary(eq(LibraryType.CSS), eq(ACCORDION_CLIENTLIB_PATH))).thenReturn(accordionCssHtmlLibrary);
    when(htmlLibraryManager.getLibrary(eq(LibraryType.CSS), eq(CAROUSEL_CLIENTLIB_PATH))).thenReturn(carouselCssHtmlLibrary);
    // Mock htmlLibraryManager.writeJsInclude
    try {
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();
            StringBuilder scriptIncludes = new StringBuilder();
            for (int i = 2; i < args.length; i++) {
                String category = (String) args[i];
                String script = jsIncludes.get(category);
                scriptIncludes.append(script);
            }
            ((PrintWriter) args[1]).write(scriptIncludes.toString());
            return null;
        }).when(htmlLibraryManager).writeJsInclude(any(SlingHttpServletRequest.class), any(Writer.class), any(String.class));
    } catch (IOException e) {
        fail(String.format("Unable to write JS include: %s", e.getMessage()));
    }
    // Mock htmlLibraryManager.writeCssInclude
    try {
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();
            StringBuilder linkIncludes = new StringBuilder();
            for (int i = 2; i < args.length; i++) {
                String category = (String) args[i];
                String link = cssIncludes.get(category);
                linkIncludes.append(link);
            }
            ((PrintWriter) args[1]).write(linkIncludes.toString());
            return null;
        }).when(htmlLibraryManager).writeCssInclude(any(SlingHttpServletRequest.class), any(Writer.class), any(String.class));
    } catch (IOException e) {
        fail(String.format("Unable to write CSS include: %s", e.getMessage()));
    }
    // Mock htmlLibraryManager.writeIncludes
    try {
        doAnswer(invocation -> {
            Object[] args = invocation.getArguments();
            StringBuilder includes = new StringBuilder();
            for (int i = 2; i < args.length; i++) {
                String category = (String) args[i];
                String script = jsIncludes.get(category);
                includes.append(script);
            }
            for (int i = 2; i < args.length; i++) {
                String category = (String) args[i];
                String link = cssIncludes.get(category);
                includes.append(link);
            }
            ((PrintWriter) args[1]).write(includes.toString());
            return null;
        }).when(htmlLibraryManager).writeIncludes(any(SlingHttpServletRequest.class), any(Writer.class), any(String.class));
    } catch (IOException e) {
        fail(String.format("Unable to write include: %s", e.getMessage()));
    }
}
Also used : ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary) HtmlLibraryManager(com.adobe.granite.ui.clientlibs.HtmlLibraryManager) MockHtmlLibraryManager(com.adobe.cq.wcm.core.components.testing.MockHtmlLibraryManager) InputStream(java.io.InputStream) LibraryType(com.adobe.granite.ui.clientlibs.LibraryType) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) IOException(java.io.IOException) HtmlLibrary(com.adobe.granite.ui.clientlibs.HtmlLibrary) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) MockHtmlLibraryManager(com.adobe.cq.wcm.core.components.testing.MockHtmlLibraryManager) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with HtmlLibraryManager

use of com.adobe.granite.ui.clientlibs.HtmlLibraryManager in project acs-aem-commons by Adobe-Consulting-Services.

the class DesignHtmlLibraryManagerImplTest method setUp.

@Before
public void setUp() throws Exception {
    HtmlLibraryManager bridge = new BridgeHtmlLibraryManager();
    PrivateAccessor.setField(instance, "htmlLibraryManager", bridge);
}
Also used : HtmlLibraryManager(com.adobe.granite.ui.clientlibs.HtmlLibraryManager) DesignHtmlLibraryManager(com.adobe.acs.commons.designer.DesignHtmlLibraryManager) Before(org.junit.Before)

Aggregations

HtmlLibraryManager (com.adobe.granite.ui.clientlibs.HtmlLibraryManager)3 ClientLibrary (com.adobe.granite.ui.clientlibs.ClientLibrary)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 DesignHtmlLibraryManager (com.adobe.acs.commons.designer.DesignHtmlLibraryManager)1 MockHtmlLibraryManager (com.adobe.cq.wcm.core.components.testing.MockHtmlLibraryManager)1 HtmlLibrary (com.adobe.granite.ui.clientlibs.HtmlLibrary)1 LibraryType (com.adobe.granite.ui.clientlibs.LibraryType)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 Resource (org.apache.sling.api.resource.Resource)1 Before (org.junit.Before)1