1 package net.pterodactylus.sone.template;
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.containsString;
5 import static org.hamcrest.Matchers.is;
6 import static org.hamcrest.Matchers.nullValue;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
10 import net.pterodactylus.sone.core.Core;
11 import net.pterodactylus.sone.data.Image;
12 import net.pterodactylus.util.template.HtmlFilter;
13 import net.pterodactylus.util.template.TemplateContext;
14 import net.pterodactylus.util.template.TemplateContextFactory;
16 import com.google.common.collect.ImmutableMap;
17 import org.hamcrest.Matchers;
18 import org.jsoup.Jsoup;
19 import org.jsoup.nodes.Document;
20 import org.jsoup.nodes.Element;
21 import org.junit.Before;
22 import org.junit.Test;
25 * Unit test for {@link ImageLinkFilterTest}.
27 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
29 public class ImageLinkFilterTest {
31 private final Core core = mock(Core.class);
32 private final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
33 private final ImageLinkFilter imageLinkFilter = new ImageLinkFilter(core, templateContextFactory);
34 private final TemplateContext templateContext = null;
35 private final Image image = mock(Image.class);
38 public void setupTemplateContextFactory() {
39 templateContextFactory.addFilter("html", new HtmlFilter());
43 public void setupCore() {
44 when(core.getImage("image-id", false)).thenReturn(image);
48 public void setupImage() {
49 when(image.getId()).thenReturn("image-id");
50 when(image.getInternalId()).thenReturn("internal-image-id");
51 when(image.getKey()).thenReturn("image-key");
52 when(image.isInserted()).thenReturn(true);
53 when(image.getWidth()).thenReturn(640);
54 when(image.getHeight()).thenReturn(270);
55 when(image.getTitle()).thenReturn("image title");
56 when(image.getDescription()).thenReturn("image description");
60 public void imageLinkIsGeneratedCorrectlyForNotInsertedImages() {
61 when(image.isInserted()).thenReturn(false);
62 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of()));
63 Element imageElement = getSingleElement(result);
64 assertThat(imageElement.attr("class"), is(""));
65 assertThat(imageElement.attr("src"), is("getImage.html?image=internal-image-id"));
66 assertThat(imageElement.attr("title"), is("image title"));
67 assertThat(imageElement.attr("alt"), is("image description"));
68 assertThat(imageElement.attr("width"), is("640"));
69 assertThat(imageElement.attr("height"), is("270"));
73 public void imageLinkIsGeneratedCorrectlyForInsertedImages() {
74 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of()));
75 Element imageElement = getSingleElement(result);
76 assertThat(imageElement.attr("class"), is(""));
77 assertThat(imageElement.attr("src"), is("/image-key?forcedownload=true"));
78 assertThat(imageElement.attr("title"), is("image title"));
79 assertThat(imageElement.attr("alt"), is("image description"));
80 assertThat(imageElement.attr("width"), is("640"));
81 assertThat(imageElement.attr("height"), is("270"));
85 public void imageTitleAndDescriptionAreOverriddenCorrectly() {
86 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("title", "Test Title")));
87 Element imageElement = getSingleElement(result);
88 assertThat(imageElement.attr("title"), is("Test Title"));
89 assertThat(imageElement.attr("alt"), is("Test Title"));
93 public void imageIsScaledByWidthCorrectly() {
94 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("max-width", "320")));
95 Element imageElement = getSingleElement(result);
96 assertThat(imageElement.attr("width"), is("320"));
97 assertThat(imageElement.attr("height"), is("135"));
101 public void imageIsScaledByHeightCorrectly() {
102 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("max-height", "135")));
103 Element imageElement = getSingleElement(result);
104 assertThat(imageElement.attr("width"), is("320"));
105 assertThat(imageElement.attr("height"), is("135"));
109 public void wideImageIsEnlargedCorrectly() {
110 String result = String.valueOf(imageLinkFilter.format(templateContext, image,
111 ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "100", "max-height", "100")));
112 Element imageElement = getSingleElement(result);
113 assertThat(imageElement.attr("width"), is("237"));
114 assertThat(imageElement.attr("height"), is("100"));
115 assertThat(imageElement.attr("style"), containsString("left: -68px"));
116 assertThat(imageElement.attr("style"), containsString("top: 0px"));
120 public void highImageIsEnlargedCorrectly() {
121 when(image.getWidth()).thenReturn(270);
122 when(image.getHeight()).thenReturn(640);
123 String result = String.valueOf(imageLinkFilter.format(templateContext, image,
124 ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "100", "max-height", "100")));
125 Element imageElement = getSingleElement(result);
126 assertThat(imageElement.attr("width"), is("100"));
127 assertThat(imageElement.attr("height"), is("237"));
128 assertThat(imageElement.attr("style"), containsString("left: 0px"));
129 assertThat(imageElement.attr("style"), containsString("top: -68px"));
133 public void nullImageIsReturnedAsNull() {
134 assertThat(imageLinkFilter.format(templateContext, null, null), nullValue());
138 public void stringIsUsedToLoadImageFromCore() {
139 String result = String.valueOf(imageLinkFilter.format(templateContext, "image-id", ImmutableMap.<String, Object>of()));
140 Element imageElement = getSingleElement(result);
141 assertThat(imageElement.attr("class"), is(""));
142 assertThat(imageElement.attr("src"), is("/image-key?forcedownload=true"));
143 assertThat(imageElement.attr("title"), is("image title"));
144 assertThat(imageElement.attr("alt"), is("image description"));
145 assertThat(imageElement.attr("width"), is("640"));
146 assertThat(imageElement.attr("height"), is("270"));
149 private Element getSingleElement(String result) {
150 Document document = Jsoup.parseBodyFragment(result);
151 assertThatBodyHasASingleElement(document);
152 return getSingleElement(document);
155 private void assertThatBodyHasASingleElement(Document document) {
156 assertThat(document.body().select("> *"), Matchers.hasSize(1));
159 private Element getSingleElement(Document document) {
160 return document.body().select("> *").get(0);