🚧 Add option for “strict filtering”
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Apr 2020 21:47:52 +0000 (23:47 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 18 Apr 2020 21:47:52 +0000 (23:47 +0200)
src/main/kotlin/net/pterodactylus/sone/core/Preferences.kt
src/main/kotlin/net/pterodactylus/sone/core/event/StrictFilteringEvents.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/core/PreferencesTest.kt

index 5a7a464..fe1e3c3 100644 (file)
@@ -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 (file)
index 0000000..ea12459
--- /dev/null
@@ -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
index 24bda21..dafe782 100644 (file)
@@ -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<StrictFilteringActivatedEvent>()
+               eventBus.register(object {
+                       @Subscribe fun strictFilteringActivatedEvent(event: StrictFilteringActivatedEvent) =
+                                       events.add(event)
+               })
+               preferences.newStrictFiltering = true
+               assertThat(events, hasItem<StrictFilteringActivatedEvent>(instanceOf(StrictFilteringActivatedEvent::class.java)))
+       }
+
+       @Test
+       fun `event is generated when strict filtering is deactivated`() {
+               val events = mutableListOf<StrictFilteringDeactivatedEvent>()
+               eventBus.register(object {
+                       @Subscribe fun strictFilteringDeactivatedEvent(event: StrictFilteringDeactivatedEvent) =
+                                       events.add(event)
+               })
+               preferences.newStrictFiltering = false
+               assertThat(events, hasItem<StrictFilteringDeactivatedEvent>(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 <T> verifySavedOption(matcher: Matcher<T>, getter: (Configuration) -> T) {
+               val configuration = Configuration(MapConfigurationBackend())
+               preferences.saveTo(configuration)
+               assertThat(getter(configuration), matcher)
+       }
+
        private fun <T : Any> testPreferencesChangedEvent(name: String, setter: (T) -> Unit, value: T) {
                val events = mutableListOf<PreferenceChangedEvent>()
                eventBus.register(object {