From 33fe718540dfcc498c06c44c957ccb76bee57eae Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 28 Jul 2019 16:19:36 +0200 Subject: [PATCH] =?utf8?q?=E2=9C=A8=20Register=20debug=20pages=20that=20ca?= =?utf8?q?n=20be=20added=20later?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/web/WebInterface.java | 4 +- .../pterodactylus/sone/web/PageToadletRegistry.kt | 11 ++++- .../sone/web/PageToadletRegistryTest.kt | 50 ++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) 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/") -- 2.7.4