From aa82f7eaab5b33951ca7aa0d240f0537c31e3247 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 18 Apr 2020 23:47:52 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20Add=20option=20for=20=E2=80=9Cstr?= =?utf8?q?ict=20filtering=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../net/pterodactylus/sone/core/Preferences.kt | 14 +++++ .../sone/core/event/StrictFilteringEvents.kt | 13 ++++ .../net/pterodactylus/sone/core/PreferencesTest.kt | 70 +++++++++++++++++++++- 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/core/event/StrictFilteringEvents.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/core/Preferences.kt b/src/main/kotlin/net/pterodactylus/sone/core/Preferences.kt index 5a7a464..fe1e3c3 100644 --- a/src/main/kotlin/net/pterodactylus/sone/core/Preferences.kt +++ b/src/main/kotlin/net/pterodactylus/sone/core/Preferences.kt @@ -19,6 +19,8 @@ package net.pterodactylus.sone.core import com.google.common.eventbus.EventBus import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent +import net.pterodactylus.sone.core.event.StrictFilteringActivatedEvent +import net.pterodactylus.sone.core.event.StrictFilteringDeactivatedEvent import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS import net.pterodactylus.sone.fcp.event.FcpInterfaceActivatedEvent @@ -99,6 +101,17 @@ class Preferences(private val eventBus: EventBus) { eventBus.post(FullAccessRequiredChanged(fcpFullAccessRequired)) } + private val _strictFiltering = DefaultOption(false) + val strictFiltering: Boolean get() = _strictFiltering.get() + var newStrictFiltering: Boolean? = false + set(value) { + _strictFiltering.set(value) + when (strictFiltering) { + true -> eventBus.post(StrictFilteringActivatedEvent()) + else -> eventBus.post(StrictFilteringDeactivatedEvent()) + } + } + @Throws(ConfigurationException::class) fun saveTo(configuration: Configuration) { configuration.getIntValue("Option/ConfigurationVersion").value = 0 @@ -110,6 +123,7 @@ class Preferences(private val eventBus: EventBus) { configuration.getBooleanValue("Option/RequireFullAccess").value = _requireFullAccess.real configuration.getBooleanValue("Option/ActivateFcpInterface").value = _fcpInterfaceActive.real configuration.getIntValue("Option/FcpFullAccessRequired").value = toInt(_fcpFullAccessRequired.real) + configuration.getBooleanValue("Option/StrictFiltering").value = _strictFiltering.real } private fun toInt(fullAccessRequired: FullAccessRequired?): Int? { diff --git a/src/main/kotlin/net/pterodactylus/sone/core/event/StrictFilteringEvents.kt b/src/main/kotlin/net/pterodactylus/sone/core/event/StrictFilteringEvents.kt new file mode 100644 index 0000000..ea12459 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/core/event/StrictFilteringEvents.kt @@ -0,0 +1,13 @@ +package net.pterodactylus.sone.core.event + +/** + * Event that signals that the “[strict filtering][net.pterodactylus.sone.core.Preferences.strictFiltering]” + * preference was activated. + */ +class StrictFilteringActivatedEvent + +/** + * Event that signals that the “[strict filtering][net.pterodactylus.sone.core.Preferences.strictFiltering]” + * preference was deactivated. + */ +class StrictFilteringDeactivatedEvent diff --git a/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt b/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt index 24bda21..dafe782 100644 --- a/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt @@ -3,6 +3,8 @@ package net.pterodactylus.sone.core import com.google.common.eventbus.EventBus import com.google.common.eventbus.Subscribe import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent +import net.pterodactylus.sone.core.event.StrictFilteringActivatedEvent +import net.pterodactylus.sone.core.event.StrictFilteringDeactivatedEvent import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.NO @@ -12,6 +14,7 @@ import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged import net.pterodactylus.util.config.Configuration import net.pterodactylus.util.config.MapConfigurationBackend +import org.hamcrest.Matcher import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.emptyIterable import org.hamcrest.Matchers.equalTo @@ -81,9 +84,7 @@ class PreferencesTest { @Test fun `preferences saves null for default insertion delay setting`() { - val configuration = Configuration(MapConfigurationBackend()) - preferences.saveTo(configuration) - assertThat(configuration.getIntValue("Option/InsertionDelay").getValue(null), nullValue()) + verifySavedOption(nullValue()) { it.getIntValue("Option/InsertionDelay").getValue(null) } } @Test @@ -293,6 +294,69 @@ class PreferencesTest { testPreferencesChangedEvent("PostsPerPage", { preferences.newPostsPerPage = it }, 31) } + @Test + fun `default strict filtering is false`() { + assertThat(preferences.strictFiltering, equalTo(false)) + } + + @Test + fun `strict filtering can be set`() { + preferences.newStrictFiltering = true + assertThat(preferences.strictFiltering, equalTo(true)) + } + + @Test + fun `strict filtering returns to default on null`() { + preferences.newStrictFiltering = true + preferences.newStrictFiltering = null + assertThat(preferences.strictFiltering, equalTo(false)) + } + + @Test + fun `event is generated when strict filtering is activated`() { + val events = mutableListOf() + eventBus.register(object { + @Subscribe fun strictFilteringActivatedEvent(event: StrictFilteringActivatedEvent) = + events.add(event) + }) + preferences.newStrictFiltering = true + assertThat(events, hasItem(instanceOf(StrictFilteringActivatedEvent::class.java))) + } + + @Test + fun `event is generated when strict filtering is deactivated`() { + val events = mutableListOf() + eventBus.register(object { + @Subscribe fun strictFilteringDeactivatedEvent(event: StrictFilteringDeactivatedEvent) = + events.add(event) + }) + preferences.newStrictFiltering = false + assertThat(events, hasItem(instanceOf(StrictFilteringDeactivatedEvent::class.java))) + } + + @Test + fun `default strict filtering is saved as null`() { + verifySavedOption(nullValue()) { it.getBooleanValue("Option/StrictFiltering").value } + } + + @Test + fun `activated strict filtering is saved as true`() { + preferences.newStrictFiltering = true + verifySavedOption(equalTo(true)) { it.getBooleanValue("Option/StrictFiltering").value } + } + + @Test + fun `deactivated strict filtering is saved as false`() { + preferences.newStrictFiltering = false + verifySavedOption(equalTo(false)) { it.getBooleanValue("Option/StrictFiltering").value } + } + + private fun verifySavedOption(matcher: Matcher, getter: (Configuration) -> T) { + val configuration = Configuration(MapConfigurationBackend()) + preferences.saveTo(configuration) + assertThat(getter(configuration), matcher) + } + private fun testPreferencesChangedEvent(name: String, setter: (T) -> Unit, value: T) { val events = mutableListOf() eventBus.register(object { -- 2.7.4