AMImageHTMLTagFactoryImpl implements AMImageHTMLTagFactory { @Override public String create(String originalImgTag, FileEntry fileEntry) throws PortalException { List<String> sourceElements = _getSourceElements(fileEntry); if (sourceElements.isEmpty()) { return originalImgTag; } StringBundler sb = new StringBundler(5 + sourceElements.size()); sb.append("<picture "); sb.append(AMImageHTMLConstants.ATTRIBUTE_NAME_FILE_ENTRY_ID); sb.append("=\""); sb.append(fileEntry.getFileEntryId()); sb.append("\">"); sourceElements.forEach(sb::append); sb.append(originalImgTag); sb.append("</picture>"); return sb.toString(); }  @Override String create(String originalImgTag, FileEntry fileEntry);  }
@Test public void testCreatesAPictureTag() throws Exception { _addMediaQueries(_createMediaQuery(1989, "adaptiveURL")); String originalImgTag = "<img src=\"originalURL\" data-fileentryid=\"1234\"/>"; StringBundler expectedSB = new StringBundler(5); expectedSB.append("<picture data-fileentryid=\"1234\">"); expectedSB.append("<source media=\"(max-width:1989px)\" "); expectedSB.append("srcset=\"adaptiveURL\" />"); expectedSB.append(originalImgTag); expectedSB.append("</picture>"); String pictureTag = _amImageHTMLTagFactoryImpl.create( originalImgTag, _fileEntry); Assert.assertEquals(expectedSB.toString(), pictureTag); }
@Test public void testCreatesAPictureTagWithSeveralMediaQueries() throws Exception { _addMediaQueries( _createMediaQuery(1986, "adaptiveURL1"), _createMediaQuery(1989, "adaptiveURL2")); String originalImgTag = "<img src=\"originalURL\" data-fileentryid=\"1234\"/>"; StringBundler expectedSB = new StringBundler(7); expectedSB.append("<picture data-fileentryid=\"1234\">"); expectedSB.append("<source media=\"(max-width:1986px)\" "); expectedSB.append("srcset=\"adaptiveURL1\" />"); expectedSB.append("<source media=\"(max-width:1989px)\" "); expectedSB.append("srcset=\"adaptiveURL2\" />"); expectedSB.append(originalImgTag); expectedSB.append("</picture>"); String pictureTag = _amImageHTMLTagFactoryImpl.create( originalImgTag, _fileEntry); Assert.assertEquals(expectedSB.toString(), pictureTag); }
@Test public void testNoSourceIsCreatedIfNoConditionIsPresent() throws Exception { _addMediaQueries( new MediaQuery( Collections.emptyList(), RandomTestUtil.randomString())); String originalImgTag = "<img src=\"originalURL\" data-fileentryid=\"1234\"/>"; StringBundler expectedSB = new StringBundler(3); expectedSB.append("<picture data-fileentryid=\"1234\">"); expectedSB.append(originalImgTag); expectedSB.append("</picture>"); String pictureTag = _amImageHTMLTagFactoryImpl.create( originalImgTag, _fileEntry); Assert.assertEquals(expectedSB.toString(), pictureTag); }
@Test public void testReturnsTheOriginalImgTagIfThereAreNoMediaQueries() throws Exception { _addMediaQueries(); String originalImgTag = "<img src=\"originalURL\" data-fileentryid=\"1234\"/>"; String pictureTag = _amImageHTMLTagFactoryImpl.create( originalImgTag, _fileEntry); Assert.assertSame(originalImgTag, pictureTag); }
@Test public void testSupportsImageTagsWithNewLineCharacters() throws Exception { _addMediaQueries(_createMediaQuery(1989, "adaptiveURL")); StringBundler originalSB = new StringBundler(3); originalSB.append("<img data-fileentryid=\"1234\" "); originalSB.append(CharPool.NEW_LINE); originalSB.append("src=\"adaptable\"/>"); StringBundler expectedSB = new StringBundler(5); expectedSB.append("<picture data-fileentryid=\"1234\">"); expectedSB.append("<source media=\"(max-width:1989px)\" "); expectedSB.append("srcset=\"adaptiveURL\" />"); expectedSB.append(originalSB.toString()); expectedSB.append("</picture>"); String pictureTag = _amImageHTMLTagFactoryImpl.create( originalSB.toString(), _fileEntry); Assert.assertEquals(expectedSB.toString(), pictureTag); }
@Test public void testTheAttributeIsCaseInsensitive() throws Exception { _addMediaQueries(_createMediaQuery(1989, "adaptiveURL")); String originalImgTag = "<img src=\"originalURL\" datA-fileENTryID=\"1234\"/>"; StringBundler expectedSB = new StringBundler(5); expectedSB.append("<picture data-fileentryid=\"1234\">"); expectedSB.append("<source media=\"(max-width:1989px)\" "); expectedSB.append("srcset=\"adaptiveURL\" />"); expectedSB.append(originalImgTag); expectedSB.append("</picture>"); String pictureTag = _amImageHTMLTagFactoryImpl.create( originalImgTag, _fileEntry); Assert.assertEquals(expectedSB.toString(), pictureTag); }