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
18 * Unit test for [ImageBrowserPage].
20 class ImageBrowserPageTest: WebPageTest(::ImageBrowserPage) {
23 fun `page returns correct path`() {
24 assertThat(page.path, equalTo("imageBrowser.html"))
28 fun `page requires login`() {
29 assertThat(page.requiresLogin(), equalTo(true))
33 fun `page returns correct title`() {
34 whenever(l10n.getString("Page.ImageBrowser.Title")).thenReturn("image browser page title")
35 assertThat(page.getPageTitle(freenetRequest), equalTo("image browser page title"))
39 fun `get request with album sets album and page in template context`() {
40 val album = mock<Album>()
41 addAlbum("album-id", album)
42 addHttpRequestParameter("album", "album-id")
43 addHttpRequestParameter("page", "5")
45 assertThat(templateContext["albumRequested"], equalTo<Any>(true))
46 assertThat(templateContext["album"], equalTo<Any>(album))
47 assertThat(templateContext["page"], equalTo<Any>("5"))
52 fun `get request with image sets image in template context`() {
53 val image = mock<Image>()
54 addImage("image-id", image)
55 addHttpRequestParameter("image", "image-id")
57 assertThat(templateContext["imageRequested"], equalTo<Any>(true))
58 assertThat(templateContext["image"], equalTo<Any>(image))
63 fun `get request with sone sets sone in template context`() {
64 val sone = mock<Sone>()
65 addSone("sone-id", sone)
66 addHttpRequestParameter("sone", "sone-id")
68 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
69 assertThat(templateContext["sone"], equalTo<Any>(sone))
74 fun `get request with mode of gallery sets albums and page in template context`() {
75 val firstSone = createSone("first album", "second album")
76 addSone("sone1", firstSone)
77 val secondSone = createSone("third album", "fourth album")
78 addSone("sone2", secondSone)
79 addHttpRequestParameter("mode", "gallery")
81 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
82 @Suppress("UNCHECKED_CAST")
83 assertThat(templateContext["albums"] as Iterable<Album>, contains(
84 firstSone.rootAlbum.albums[0],
85 secondSone.rootAlbum.albums[1],
86 firstSone.rootAlbum.albums[1],
87 secondSone.rootAlbum.albums[0]
93 fun `get request for gallery can show second page`() {
94 core.preferences.newImagesPerPage = 2
95 val firstSone = createSone("first album", "second album")
96 addSone("sone1", firstSone)
97 val secondSone = createSone("third album", "fourth album")
98 addSone("sone2", secondSone)
99 addHttpRequestParameter("mode", "gallery")
100 addHttpRequestParameter("page", "1")
102 assertThat(templateContext["galleryRequested"], equalTo<Any>(true))
103 @Suppress("UNCHECKED_CAST")
104 assertThat(templateContext["albums"] as Iterable<Album>, contains(
105 firstSone.rootAlbum.albums[1],
106 secondSone.rootAlbum.albums[0]
111 private fun createSone(firstAlbumTitle: String, secondAlbumTitle: String): Sone {
112 return mock<Sone>().apply {
113 val rootAlbum = mock<Album>()
114 val firstAlbum = mock<Album>()
115 val firstImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
116 whenever(firstAlbum.images).thenReturn(listOf(firstImage))
117 val secondAlbum = mock<Album>()
118 val secondImage = mock<Image>().run { whenever(isInserted).thenReturn(true); this }
119 whenever(secondAlbum.images).thenReturn(listOf(secondImage))
120 whenever(firstAlbum.title).thenReturn(firstAlbumTitle)
121 whenever(secondAlbum.title).thenReturn(secondAlbumTitle)
122 whenever(rootAlbum.albums).thenReturn(listOf(firstAlbum, secondAlbum))
123 whenever(this.rootAlbum).thenReturn(rootAlbum)
128 fun `requesting nothing will show the albums of the current sone`() {
130 assertThat(templateContext["soneRequested"], equalTo<Any>(true))
131 assertThat(templateContext["sone"], equalTo<Any>(currentSone))
136 fun `page is link-excepted`() {
137 assertThat(page.isLinkExcepted(URI("")), equalTo(true))
141 fun `page can be created by dependency injection`() {
142 assertThat(baseInjector.getInstance<ImageBrowserPage>(), notNullValue())