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.mock
7 import net.pterodactylus.sone.test.whenever
8 import org.hamcrest.MatcherAssert.assertThat
9 import org.hamcrest.Matchers.contains
10 import org.hamcrest.Matchers.equalTo
14 * Unit test for [ImageBrowserPage].
16 class ImageBrowserPageTest: WebPageTest(::ImageBrowserPage) {
19 fun `page returns correct path`() {
20 assertThat(page.path, equalTo("imageBrowser.html"))
24 fun `page requires login`() {
25 assertThat(page.requiresLogin(), equalTo(true))
29 fun `page returns correct title`() {
30 whenever(l10n.getString("Page.ImageBrowser.Title")).thenReturn("image browser page title")
31 assertThat(page.getPageTitle(freenetRequest), equalTo("image browser page title"))
35 fun `get request with album sets album and page in template context`() {
36 val album = mock<Album>()
37 addAlbum("album-id", album)
38 addHttpRequestParameter("album", "album-id")
39 addHttpRequestParameter("page", "5")
41 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
42 assertThat(templateContext["album"], equalTo<Any>(album))
43 assertThat(templateContext["page"], equalTo<Any>("5"))
48 fun `get request with image sets image in template context`() {
49 val image = mock<Image>()
50 addImage("image-id", image)
51 addHttpRequestParameter("image", "image-id")
53 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
54 assertThat(templateContext["image"], equalTo<Any>(image))
59 fun `get request with sone sets sone in template context`() {
60 val sone = mock<Sone>()
61 addSone("sone-id", sone)
62 addHttpRequestParameter("sone", "sone-id")
64 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
65 assertThat(templateContext["sone"], equalTo<Any>(sone))
70 fun `get request with mode of gallery sets albums and page in template context`() {
71 val firstSone = createSone("first album", "second album")
72 addSone("sone1", firstSone)
73 val secondSone = createSone("third album", "fourth album")
74 addSone("sone2", secondSone)
75 addHttpRequestParameter("mode", "gallery")
77 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
78 @Suppress("UNCHECKED_CAST")
79 assertThat(templateContext["albums"] as Iterable<Album>, contains(
80 firstSone.rootAlbum.albums[0],
81 secondSone.rootAlbum.albums[1],
82 firstSone.rootAlbum.albums[1],
83 secondSone.rootAlbum.albums[0]
89 fun `get request for gallery can show second page`() {
90 core.preferences.imagesPerPage = 2
91 val firstSone = createSone("first album", "second album")
92 addSone("sone1", firstSone)
93 val secondSone = createSone("third album", "fourth album")
94 addSone("sone2", secondSone)
95 addHttpRequestParameter("mode", "gallery")
96 addHttpRequestParameter("page", "1")
98 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
99 @Suppress("UNCHECKED_CAST")
100 assertThat(templateContext["albums"] as Iterable<Album>, contains(
101 firstSone.rootAlbum.albums[1],
102 secondSone.rootAlbum.albums[0]
107 private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
108 return mock<Sone>().apply {
109 val rootAlbum = mock<Album>()
110 val firstAlbum = mock<Album>()
111 val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
112 whenever(firstAlbum.images).thenReturn(listOf(firstImage))
113 val secondAlbum = mock<Album>()
114 val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
115 whenever(secondAlbum.images).thenReturn(listOf(secondImage))
116 whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
117 whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
118 whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
119 whenever(this.rootAlbum).thenReturn(rootAlbum)
124 fun `requesting nothing will show the albums of the current sone`() {
126 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
127 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
132 fun `page is link-excepted`() {
133 assertThat(page.isLinkExcepted(null), equalTo(true))