Merge branch 'release-0.9.6'
[Sone.git] / src / test / java / net / pterodactylus / sone / template / ImageLinkFilterTest.java
1 package net.pterodactylus.sone.template;
2
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;
9
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;
15
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;
23
24 /**
25  * Unit test for {@link ImageLinkFilterTest}.
26  *
27  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
28  */
29 public class ImageLinkFilterTest {
30
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);
36
37         @Before
38         public void setupTemplateContextFactory() {
39                 templateContextFactory.addFilter("html", new HtmlFilter());
40         }
41
42         @Before
43         public void setupCore() {
44                 when(core.getImage("image-id", false)).thenReturn(image);
45         }
46
47         @Before
48         public void setupImage() {
49                 when(image.getId()).thenReturn("image-id");
50                 when(image.getKey()).thenReturn("image-key");
51                 when(image.isInserted()).thenReturn(true);
52                 when(image.getWidth()).thenReturn(640);
53                 when(image.getHeight()).thenReturn(270);
54                 when(image.getTitle()).thenReturn("image title");
55                 when(image.getDescription()).thenReturn("image description");
56         }
57
58         @Test
59         public void imageLinkIsGeneratedCorrectlyForNotInsertedImages() {
60                 when(image.isInserted()).thenReturn(false);
61                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of()));
62                 Element imageElement = getSingleElement(result);
63                 assertThat(imageElement.attr("class"), is(""));
64                 assertThat(imageElement.attr("src"), is("getImage.html?image=image-id"));
65                 assertThat(imageElement.attr("title"), is("image title"));
66                 assertThat(imageElement.attr("alt"), is("image description"));
67                 assertThat(imageElement.attr("width"), is("640"));
68                 assertThat(imageElement.attr("height"), is("270"));
69         }
70
71         @Test
72         public void imageLinkIsGeneratedCorrectlyForInsertedImages() {
73                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of()));
74                 Element imageElement = getSingleElement(result);
75                 assertThat(imageElement.attr("class"), is(""));
76                 assertThat(imageElement.attr("src"), is("/image-key"));
77                 assertThat(imageElement.attr("title"), is("image title"));
78                 assertThat(imageElement.attr("alt"), is("image description"));
79                 assertThat(imageElement.attr("width"), is("640"));
80                 assertThat(imageElement.attr("height"), is("270"));
81         }
82
83         @Test
84         public void imageTitleAndDescriptionAreOverriddenCorrectly() {
85                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("title", "Test Title")));
86                 Element imageElement = getSingleElement(result);
87                 assertThat(imageElement.attr("title"), is("Test Title"));
88                 assertThat(imageElement.attr("alt"), is("Test Title"));
89         }
90
91         @Test
92         public void imageIsScaledByWidthCorrectly() {
93                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("max-width", "320")));
94                 Element imageElement = getSingleElement(result);
95                 assertThat(imageElement.attr("width"), is("320"));
96                 assertThat(imageElement.attr("height"), is("135"));
97         }
98
99         @Test
100         public void imageIsScaledByHeightCorrectly() {
101                 String result = String.valueOf(imageLinkFilter.format(templateContext, image, ImmutableMap.<String, Object>of("max-height", "135")));
102                 Element imageElement = getSingleElement(result);
103                 assertThat(imageElement.attr("width"), is("320"));
104                 assertThat(imageElement.attr("height"), is("135"));
105         }
106
107         @Test
108         public void wideImageIsEnlargedCorrectly() {
109                 String result = String.valueOf(imageLinkFilter.format(templateContext, image,
110                                 ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "100", "max-height", "100")));
111                 Element imageElement = getSingleElement(result);
112                 assertThat(imageElement.attr("width"), is("237"));
113                 assertThat(imageElement.attr("height"), is("100"));
114                 assertThat(imageElement.attr("style"), containsString("left: -68px"));
115                 assertThat(imageElement.attr("style"), containsString("top: 0px"));
116         }
117
118         @Test
119         public void highImageIsEnlargedCorrectly() {
120                 when(image.getWidth()).thenReturn(270);
121                 when(image.getHeight()).thenReturn(640);
122                 String result = String.valueOf(imageLinkFilter.format(templateContext, image,
123                                 ImmutableMap.<String, Object>of("mode", "enlarge", "max-width", "100", "max-height", "100")));
124                 Element imageElement = getSingleElement(result);
125                 assertThat(imageElement.attr("width"), is("100"));
126                 assertThat(imageElement.attr("height"), is("237"));
127                 assertThat(imageElement.attr("style"), containsString("left: 0px"));
128                 assertThat(imageElement.attr("style"), containsString("top: -68px"));
129         }
130
131         @Test
132         public void nullImageIsReturnedAsNull() {
133                 assertThat(imageLinkFilter.format(templateContext, null, null), nullValue());
134         }
135
136         @Test
137         public void stringIsUsedToLoadImageFromCore() {
138                 String result = String.valueOf(imageLinkFilter.format(templateContext, "image-id", ImmutableMap.<String, Object>of()));
139                 Element imageElement = getSingleElement(result);
140                 assertThat(imageElement.attr("class"), is(""));
141                 assertThat(imageElement.attr("src"), is("/image-key"));
142                 assertThat(imageElement.attr("title"), is("image title"));
143                 assertThat(imageElement.attr("alt"), is("image description"));
144                 assertThat(imageElement.attr("width"), is("640"));
145                 assertThat(imageElement.attr("height"), is("270"));
146         }
147
148         private Element getSingleElement(String result) {
149                 Document document = Jsoup.parseBodyFragment(result);
150                 assertThatBodyHasASingleElement(document);
151                 return getSingleElement(document);
152         }
153
154         private void assertThatBodyHasASingleElement(Document document) {
155                 assertThat(document.body().select("> *"), Matchers.hasSize(1));
156         }
157
158         private Element getSingleElement(Document document) {
159                 return document.body().select("> *").get(0);
160         }
161
162 }