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 org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.contains
12 import org.hamcrest.Matchers.equalTo
13 import org.hamcrest.Matchers.notNullValue
17 * Unit test for [ImageBrowserPage].
19 class ImageBrowserPageTest: WebPageTest(::ImageBrowserPage) {
22 fun `page returns correct path`() {
23 assertThat(page.path, equalTo("imageBrowser.html"))
27 fun `page requires login`() {
28 assertThat(page.requiresLogin(), equalTo(true))
32 fun `page returns correct title`() {
33 whenever(l10n.getString("Page.ImageBrowser.Title")).thenReturn("image browser page title")
34 assertThat(page.getPageTitle(freenetRequest), equalTo("image browser page title"))
38 fun `get request with album sets album and page in template context`() {
39 val album = mock<Album>()
40 addAlbum("album-id", album)
41 addHttpRequestParameter("album", "album-id")
42 addHttpRequestParameter("page", "5")
44 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
45 assertThat(templateContext["album"], equalTo<Any>(album))
46 assertThat(templateContext["page"], equalTo<Any>("5"))
51 fun `get request with image sets image in template context`() {
52 val image = mock<Image>()
53 addImage("image-id", image)
54 addHttpRequestParameter("image", "image-id")
56 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
57 assertThat(templateContext["image"], equalTo<Any>(image))
62 fun `get request with sone sets sone in template context`() {
63 val sone = mock<Sone>()
64 addSone("sone-id", sone)
65 addHttpRequestParameter("sone", "sone-id")
67 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
68 assertThat(templateContext["sone"], equalTo<Any>(sone))
73 fun `get request with mode of gallery sets albums and page in template context`() {
74 val firstSone = createSone("first album", "second album")
75 addSone("sone1", firstSone)
76 val secondSone = createSone("third album", "fourth album")
77 addSone("sone2", secondSone)
78 addHttpRequestParameter("mode", "gallery")
80 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
81 @Suppress("UNCHECKED_CAST")
82 assertThat(templateContext["albums"] as Iterable<Album>, contains(
83 firstSone.rootAlbum.albums[0],
84 secondSone.rootAlbum.albums[1],
85 firstSone.rootAlbum.albums[1],
86 secondSone.rootAlbum.albums[0]
92 fun `get request for gallery can show second page`() {
93 core.preferences.imagesPerPage = 2
94 val firstSone = createSone("first album", "second album")
95 addSone("sone1", firstSone)
96 val secondSone = createSone("third album", "fourth album")
97 addSone("sone2", secondSone)
98 addHttpRequestParameter("mode", "gallery")
99 addHttpRequestParameter("page", "1")
101 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
102 @Suppress("UNCHECKED_CAST")
103 assertThat(templateContext["albums"] as Iterable<Album>, contains(
104 firstSone.rootAlbum.albums[1],
105 secondSone.rootAlbum.albums[0]
110 private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
111 return mock<Sone>().apply {
112 val rootAlbum = mock<Album>()
113 val firstAlbum = mock<Album>()
114 val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
115 whenever(firstAlbum.images).thenReturn(listOf(firstImage))
116 val secondAlbum = mock<Album>()
117 val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
118 whenever(secondAlbum.images).thenReturn(listOf(secondImage))
119 whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
120 whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
121 whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
122 whenever(this.rootAlbum).thenReturn(rootAlbum)
127 fun `requesting nothing will show the albums of the current sone`() {
129 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
130 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
135 fun `page is link-excepted`() {
136 assertThat(page.isLinkExcepted(null), equalTo(true))
140 fun `page can be created by dependency injection`() {
141 assertThat(baseInjector.getInstance<ImageBrowserPage>(), notNullValue())