Search in sources :

Example 1 with ClientLibrary

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

the class OptionsServletTest method addLibrary.

private void addLibrary(Map<String, ClientLibrary> libraries, String path, String[] types, String[] categories) {
    ClientLibrary library = mock(ClientLibrary.class);
    when(library.getTypes()).thenReturn(toLibraryTypeSet(types));
    when(library.getCategories()).thenReturn(categories);
    libraries.put(path, library);
}
Also used : ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary)

Example 2 with ClientLibrary

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

the class DynamicTouchUiClientLibraryServletTest method setup.

@Before
public void setup() throws Exception {
    writer = new StringWriter();
    when(response.getWriter()).thenReturn(new PrintWriter(writer));
    when(request.getResourceResolver()).thenReturn(resourceResolver);
    when(resourceResolver.map(any(SlingHttpServletRequest.class), anyString())).then(i -> {
        SlingHttpServletRequest request = i.getArgumentAt(0, SlingHttpServletRequest.class);
        String path = i.getArgumentAt(1, String.class);
        if (request != null && StringUtils.isNotBlank(request.getContextPath())) {
            return request.getContextPath().concat(path);
        } else {
            return path;
        }
    });
    when(htmlLibraryManager.getLibraries(any(String[].class), any(LibraryType.class), eq(true), eq(true))).thenAnswer(i -> {
        Set<ClientLibrary> result = new HashSet<>();
        for (String category : i.getArgumentAt(0, String[].class)) {
            if (category.equals(LIMIT.id)) {
                result.add(LIMIT.getClientLibrary());
            } else if (category.equals(PLACEHOLDER.id)) {
                result.add(PLACEHOLDER.getClientLibrary());
            } else if (category.equals(CUSTOM.id)) {
                result.add(CUSTOM.getClientLibrary());
            }
        }
        return result;
    });
    when(htmlLibraryManager.isMinifyEnabled()).thenReturn(false);
}
Also used : StringWriter(java.io.StringWriter) ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary) LibraryType(com.adobe.granite.ui.clientlibs.LibraryType) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 3 with ClientLibrary

use of com.adobe.granite.ui.clientlibs.ClientLibrary 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 4 with ClientLibrary

use of com.adobe.granite.ui.clientlibs.ClientLibrary 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 5 with ClientLibrary

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

the class PageImplTest method internalSetup.

@Override
protected void internalSetup() {
    super.internalSetup();
    ClientLibrary mockClientLibrary = Mockito.mock(ClientLibrary.class);
    lenient().when(mockClientLibrary.getPath()).thenReturn("/apps/wcm/core/page/clientlibs/favicon");
    lenient().when(mockClientLibrary.allowProxy()).thenReturn(true);
    context.registerInjectActivateService(new MockHtmlLibraryManager(mockClientLibrary));
    context.registerInjectActivateService(mockProductInfoProvider);
    context.registerInjectActivateService(new MockPersistenceStrategy(), ImmutableMap.of(Constants.SERVICE_RANKING, Integer.MAX_VALUE));
}
Also used : ClientLibrary(com.adobe.granite.ui.clientlibs.ClientLibrary) MockHtmlLibraryManager(com.adobe.cq.wcm.core.components.testing.MockHtmlLibraryManager) MockPersistenceStrategy(com.adobe.cq.wcm.core.components.testing.MockPersistenceStrategy)

Aggregations

ClientLibrary (com.adobe.granite.ui.clientlibs.ClientLibrary)12 LibraryType (com.adobe.granite.ui.clientlibs.LibraryType)4 PrintWriter (java.io.PrintWriter)3 HashSet (java.util.HashSet)3 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)3 Resource (org.apache.sling.api.resource.Resource)3 MockHtmlLibraryManager (com.adobe.cq.wcm.core.components.testing.MockHtmlLibraryManager)2 HtmlLibrary (com.adobe.granite.ui.clientlibs.HtmlLibrary)2 HtmlLibraryManager (com.adobe.granite.ui.clientlibs.HtmlLibraryManager)2 JsonWriter (com.google.gson.stream.JsonWriter)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 MockPersistenceStrategy (com.adobe.cq.wcm.core.components.testing.MockPersistenceStrategy)1 InputStream (java.io.InputStream)1 Writer (java.io.Writer)1 LinkedHashSet (java.util.LinkedHashSet)1