1 package net.pterodactylus.sone.web.pages
3 import net.pterodactylus.sone.data.Album
4 import net.pterodactylus.sone.data.Image
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.getInstance
7 import net.pterodactylus.sone.test.mock
8 import net.pterodactylus.sone.test.whenever
9 import net.pterodactylus.sone.web.baseInjector
10 import net.pterodactylus.sone.web.page.*
11 import org.hamcrest.MatcherAssert.assertThat
12 import org.hamcrest.Matchers.contains
13 import org.hamcrest.Matchers.equalTo
14 import org.hamcrest.Matchers.notNullValue
19 * Unit test for [ImageBrowserPage].
21 class ImageBrowserPageTest: WebPageTest(::ImageBrowserPage) {
24 fun `page returns correct path`() {
25 assertThat(page.path, equalTo("imageBrowser.html"))
29 fun `page requires login`() {
30 assertThat(page.requiresLogin(), equalTo(true))
34 fun `page returns correct title`() {
35 whenever(l10n.getString("Page.ImageBrowser.Title")).thenReturn("image browser page title")
36 assertThat(page.getPageTitle(soneRequest), equalTo("image browser page title"))
40 fun `get request with album sets album and page in template context`() {
41 val album = mock<Album>()
42 addAlbum("album-id", album)
43 addHttpRequestParameter("album", "album-id")
44 addHttpRequestParameter("page", "5")
46 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
47 assertThat(templateContext["album"], equalTo<Any>(album))
48 assertThat(templateContext["page"], equalTo<Any>("5"))
53 fun `get request with image sets image in template context`() {
54 val image = mock<Image>()
55 addImage("image-id", image)
56 addHttpRequestParameter("image", "image-id")
58 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
59 assertThat(templateContext["image"], equalTo<Any>(image))
64 fun `get request with sone sets sone in template context`() {
65 val sone = mock<Sone>()
66 addSone("sone-id", sone)
67 addHttpRequestParameter("sone", "sone-id")
69 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
70 assertThat(templateContext["sone"], equalTo<Any>(sone))
75 fun `get request with mode of gallery sets albums and page in template context`() {
76 val firstSone = createSone("first album", "second album")
77 addSone("sone1", firstSone)
78 val secondSone = createSone("third album", "fourth album")
79 addSone("sone2", secondSone)
80 addHttpRequestParameter("mode", "gallery")
82 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
83 @Suppress("UNCHECKED_CAST")
84 assertThat(templateContext["albums"] as Iterable<Album>, contains(
85 firstSone.rootAlbum.albums[0],
86 secondSone.rootAlbum.albums[1],
87 firstSone.rootAlbum.albums[1],
88 secondSone.rootAlbum.albums[0]
94 fun `get request for gallery can show second page`() {
95 core.preferences.newImagesPerPage = 2
96 val firstSone = createSone("first album", "second album")
97 addSone("sone1", firstSone)
98 val secondSone = createSone("third album", "fourth album")
99 addSone("sone2", secondSone)
100 addHttpRequestParameter("mode", "gallery")
101 addHttpRequestParameter("page", "1")
103 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
104 @Suppress("UNCHECKED_CAST")
105 assertThat(templateContext["albums"] as Iterable<Album>, contains(
106 firstSone.rootAlbum.albums[1],
107 secondSone.rootAlbum.albums[0]
112 private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
113 return mock<Sone>().apply {
114 val rootAlbum = mock<Album>()
115 val firstAlbum = mock<Album>()
116 val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
117 whenever(firstAlbum.images).thenReturn(listOf(firstImage))
118 val secondAlbum = mock<Album>()
119 val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
120 whenever(secondAlbum.images).thenReturn(listOf(secondImage))
121 whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
122 whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
123 whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
124 whenever(this.rootAlbum).thenReturn(rootAlbum)
129 fun `requesting nothing will show the albums of the current sone`() {
131 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
132 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
137 fun `page is link-excepted`() {
138 assertThat(page.isLinkExcepted(URI("")), equalTo(true))
142 fun `page can be created by dependency injection`() {
143 assertThat(baseInjector.getInstance<ImageBrowserPage>(), notNullValue())
147 fun `page is annotated with correct menuname`() {
148 assertThat(page.menuName, equalTo("ImageBrowser"))