Remove obsolete loading animation
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ImageBrowserPageTest.kt
1 package net.pterodactylus.sone.web
2
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.mock
7 import net.pterodactylus.sone.test.whenever
8 import net.pterodactylus.util.web.Method.GET
9 import org.hamcrest.MatcherAssert.assertThat
10 import org.hamcrest.Matchers.contains
11 import org.hamcrest.Matchers.equalTo
12 import org.junit.Test
13
14 /**
15  * Unit test for [ImageBrowserPage].
16  */
17 class ImageBrowserPageTest : WebPageTest() {
18
19         private val page = ImageBrowserPage(template, webInterface)
20
21         @Test
22         fun `get request with album sets album and page in template context`() {
23                 request("", GET)
24                 val album = mock<Album>()
25                 addAlbum("album-id", album)
26                 addHttpRequestParameter("album", "album-id")
27                 addHttpRequestParameter("page", "5")
28                 page.handleRequest(freenetRequest, templateContext)
29                 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
30                 assertThat(templateContext["album"], equalTo<Any>(album))
31                 assertThat(templateContext["page"], equalTo<Any>("5"))
32         }
33
34         @Test
35         fun `get request with image sets image in template context`() {
36                 request("", GET)
37                 val image = mock<Image>()
38                 addImage("image-id", image)
39                 addHttpRequestParameter("image", "image-id")
40                 page.handleRequest(freenetRequest, templateContext)
41                 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
42                 assertThat(templateContext["image"], equalTo<Any>(image))
43         }
44
45         @Test
46         fun `get request with sone sets sone in template context`() {
47                 request("", GET)
48                 val sone = mock<Sone>()
49                 addSone("sone-id", sone)
50                 addHttpRequestParameter("sone", "sone-id")
51                 page.handleRequest(freenetRequest, templateContext)
52                 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
53                 assertThat(templateContext["sone"], equalTo<Any>(sone))
54         }
55
56         @Test
57         fun `get request with mode of gallery sets albums and page in template context`() {
58                 request("", GET)
59                 val firstSone = createSone("first album", "second album")
60                 addSone("sone1", firstSone)
61                 val secondSone = createSone("third album", "fourth album")
62                 addSone("sone2", secondSone)
63                 addHttpRequestParameter("mode", "gallery")
64                 page.handleRequest(freenetRequest, templateContext)
65                 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
66                 @Suppress("UNCHECKED_CAST")
67                 assertThat(templateContext["albums"] as Iterable<Album>, contains(
68                                 firstSone.rootAlbum.albums[0],
69                                 secondSone.rootAlbum.albums[1],
70                                 firstSone.rootAlbum.albums[1],
71                                 secondSone.rootAlbum.albums[0]
72                 ))
73         }
74
75         private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
76                 return mock<Sone>().apply {
77                         val rootAlbum = mock<Album>()
78                         val firstAlbum = mock<Album>()
79                         val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
80                         whenever(firstAlbum.images).thenReturn(listOf(firstImage))
81                         val secondAlbum = mock<Album>()
82                         val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
83                         whenever(secondAlbum.images).thenReturn(listOf(secondImage))
84                         whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
85                         whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
86                         whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
87                         whenever(this.rootAlbum).thenReturn(rootAlbum)
88                 }
89         }
90
91         @Test
92         fun `requesting nothing will show the albums of the current sone`() {
93                 request("", GET)
94                 page.handleRequest(freenetRequest, templateContext)
95                 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
96                 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
97         }
98
99         @Test
100         fun `page is link-excepted`() {
101             assertThat(page.isLinkExcepted(null), equalTo(true))
102         }
103
104 }