🚧 Add interactions helper for page maker
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / page / PageMakerInteractionTest.kt
1 package net.pterodactylus.sone.web.page
2
3 import freenet.clients.http.*
4 import freenet.support.*
5 import freenet.support.HTMLNode.*
6 import net.pterodactylus.sone.test.*
7 import net.pterodactylus.sone.test.TestUtil.*
8 import org.hamcrest.MatcherAssert.*
9 import org.hamcrest.Matchers.*
10 import org.junit.*
11
12 @Dirty
13 class PageMakerInteractionTest {
14
15         private val toadletContext = deepMock<ToadletContext>()
16         private val pageMaker: PageMaker = toadletContext.pageMaker
17         private val outerNode = HTMLDoctype("html", "-//W3C//DTD XHTML 1.1//EN")
18         private val htmlNode: HTMLNode = outerNode.addChild("html")
19         private val headNode: HTMLNode = htmlNode.addChild("head")
20         private val contentNode: HTMLNode = htmlNode.addChild("body").addChild("div")
21         private val pageNode: PageNode = createObject(PageNode::class.java, arrayOf(HTMLNode::class.java, HTMLNode::class.java, HTMLNode::class.java), outerNode, headNode, contentNode)
22
23         init {
24                 whenever(pageMaker.getPageNode("page title", toadletContext)).thenReturn(pageNode)
25         }
26
27         private val pageMakerInteractions = PageMakerInteraction(toadletContext, "page title")
28
29         @Test
30         fun `interactions can add style sheet`() {
31                 pageMakerInteractions.addStyleSheet("style.sheet")
32                 assertThat(headNode.children.filter { it.name == "link" }.map { it.attributes }, contains(
33                                 mapOf("rel" to "stylesheet", "href" to "style.sheet", "type" to "text/css", "media" to "screen")
34                 ))
35         }
36
37         @Test
38         fun `link nodes can be added`() {
39                 pageMakerInteractions.addLinkNode(mapOf("foo" to "bar"))
40                 assertThat(headNode.children.filter { it.name == "link" }.map { it.attributes }, contains(
41                                 mapOf("foo" to "bar")
42                 ))
43         }
44
45         @Test
46         fun `shortcut icon can be added`() {
47                 pageMakerInteractions.addShortcutIcon("shortcut.icon")
48                 assertThat(headNode.children.filter { it.name == "link" }.map { it.attributes }, contains(
49                                 mapOf("rel" to "icon", "href" to "shortcut.icon")
50                 ))
51         }
52
53         @Test
54         fun `content can be set`() {
55                 pageMakerInteractions.setContent("foo<bar")
56                 assertThat(contentNode.generate(), containsString("foo<bar"))
57         }
58
59         @Test
60         fun `whole page can be rendered`() {
61                 pageMakerInteractions.setContent("foo<bar")
62                 assertThat(pageMakerInteractions.renderPage(), containsString("foo<bar"))
63         }
64
65         private val HTMLNode.name: String get() = firstTag
66
67 }