From: David ‘Bombe’ Roden Date: Sun, 28 Jul 2019 14:19:36 +0000 (+0200) Subject: ✨ Register debug pages that can be added later X-Git-Tag: v81^2~167 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=33fe718540dfcc498c06c44c957ccb76bee57eae;p=Sone.git ✨ Register debug pages that can be added later --- diff --git a/src/main/java/net/pterodactylus/sone/web/WebInterface.java b/src/main/java/net/pterodactylus/sone/web/WebInterface.java index ee1bd1e..d7dd16c 100644 --- a/src/main/java/net/pterodactylus/sone/web/WebInterface.java +++ b/src/main/java/net/pterodactylus/sone/web/WebInterface.java @@ -616,9 +616,7 @@ public class WebInterface implements SessionProvider { pageToadletRegistry.addPage(new EmptyImageTitlePage(this, loaders, templateRenderer)); pageToadletRegistry.addPage(new EmptyAlbumTitlePage(this, loaders, templateRenderer)); pageToadletRegistry.addPage(new DismissNotificationPage(this, loaders, templateRenderer)); - if (getCore().getDebug()) { - pageToadletRegistry.addPage(new MetricsPage(this, loaders, templateRenderer, metricRegistry)); - } + pageToadletRegistry.addDebugPage(new MetricsPage(this, loaders, templateRenderer, metricRegistry)); pageToadletRegistry.addPage(loaders.loadStaticPage("css/", "/static/css/", "text/css")); pageToadletRegistry.addPage(loaders.loadStaticPage("javascript/", "/static/javascript/", "text/javascript")); pageToadletRegistry.addPage(loaders.loadStaticPage("images/", "/static/images/", "image/png")); diff --git a/src/main/kotlin/net/pterodactylus/sone/web/PageToadletRegistry.kt b/src/main/kotlin/net/pterodactylus/sone/web/PageToadletRegistry.kt index 97e6cfe..70105bc 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/PageToadletRegistry.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/PageToadletRegistry.kt @@ -18,6 +18,7 @@ class PageToadletRegistry @Inject constructor( ) { private val pages = mutableListOf>() + private val debugPages = mutableListOf>() private val registeredToadlets = mutableListOf() private val registered = AtomicBoolean(false) @@ -26,13 +27,18 @@ class PageToadletRegistry @Inject constructor( pages += page } + fun addDebugPage(page: Page) { + if (registered.get()) throw IllegalStateException() + debugPages += page + } + fun registerToadlets() { registered.set(true) pageMaker.addNavigationCategory("/Sone/index.html", soneMenuName, "$soneMenu.Tooltip", sonePlugin) addPages() } - private fun addPages() = + private fun addPages(pages: List> = this.pages) = pages .map { pageToadletFactory.createPageToadlet(it) } .onEach(registeredToadlets::plusAssign) @@ -55,4 +61,7 @@ class PageToadletRegistry @Inject constructor( registeredToadlets.forEach(toadletContainer::unregister) } + fun activateDebugMode() = + addPages(debugPages) + } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/PageToadletRegistryTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/PageToadletRegistryTest.kt index d242b32..5a19c7f 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/PageToadletRegistryTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/PageToadletRegistryTest.kt @@ -9,6 +9,7 @@ import net.pterodactylus.util.web.* import org.junit.* import org.junit.rules.* import org.mockito.Mockito.* +import kotlin.test.Test class PageToadletRegistryTest { @@ -80,6 +81,55 @@ class PageToadletRegistryTest { verify(toadletContainer).unregister(toadletWithMenuname) } + @Test + fun `adding a debug page will not add it to the container`() { + val toadlet = createPageToadlet() + whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet) + pageToadletRegistry.addDebugPage(page) + pageToadletRegistry.registerToadlets() + verify(toadletContainer, never()).register(toadlet, null, "/Sone/", true, false) + } + + @Test + fun `adding a debug page and activating debug mode will add it to the container`() { + val toadlet = createPageToadlet() + whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet) + pageToadletRegistry.addDebugPage(page) + pageToadletRegistry.registerToadlets() + pageToadletRegistry.activateDebugMode() + verify(toadletContainer).register(toadlet, null, "/Sone/", true, false) + } + + @Test + fun `debug pages are ungegistered from the container`() { + val toadlet = createPageToadlet() + whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet) + pageToadletRegistry.addDebugPage(page) + pageToadletRegistry.registerToadlets() + pageToadletRegistry.activateDebugMode() + pageToadletRegistry.unregisterToadlets() + verify(toadletContainer).unregister(toadlet) + } + + @Test + fun `inactive debug pages are not ungegistered from the container`() { + val toadlet = createPageToadlet() + whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet) + pageToadletRegistry.addDebugPage(page) + pageToadletRegistry.registerToadlets() + pageToadletRegistry.unregisterToadlets() + verify(toadletContainer, never()).unregister(toadlet) + } + + @Test + fun `debug page can not be added after registering`() { + val toadlet = createPageToadlet() + whenever(pageToadletFactory.createPageToadlet(page)).thenReturn(toadlet) + pageToadletRegistry.registerToadlets() + expectedException.expect(IllegalStateException::class.java) + pageToadletRegistry.addDebugPage(page) + } + private fun createPageToadlet(menuName: String? = null) = mock().apply { whenever(this.path()).thenReturn("/Sone/")