Send generic “preference changed” event when changing insertion delay
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 25 Oct 2018 20:01:38 +0000 (22:01 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 12 Feb 2019 14:59:02 +0000 (15:59 +0100)
src/main/java/net/pterodactylus/sone/core/PreferenceChangedEvent.kt [new file with mode: 0644]
src/main/java/net/pterodactylus/sone/core/Preferences.java
src/test/java/net/pterodactylus/sone/core/PreferencesTest.java

diff --git a/src/main/java/net/pterodactylus/sone/core/PreferenceChangedEvent.kt b/src/main/java/net/pterodactylus/sone/core/PreferenceChangedEvent.kt
new file mode 100644 (file)
index 0000000..2ebb62d
--- /dev/null
@@ -0,0 +1,3 @@
+package net.pterodactylus.sone.core
+
+data class PreferenceChangedEvent(val preferenceName: String, val newValue: Any)
index cf7d3dd..ce56e8b 100644 (file)
@@ -103,6 +103,7 @@ public class Preferences {
        public Preferences setInsertionDelay(Integer insertionDelay) {
                this.insertionDelay.set(insertionDelay);
                eventBus.post(new InsertionDelayChangedEvent(getInsertionDelay()));
+               eventBus.post(new PreferenceChangedEvent("InsertionDelay", getInsertionDelay()));
                return this;
        }
 
index ec2930f..fdae222 100644 (file)
@@ -4,11 +4,13 @@ import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.ALWAYS;
 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.NO;
 import static net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired.WRITING;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.hasItem;
 import static org.hamcrest.Matchers.is;
+import static org.mockito.ArgumentCaptor.forClass;
 import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.atLeastOnce;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
 
 import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent;
 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
@@ -17,7 +19,6 @@ import net.pterodactylus.sone.fcp.event.FcpInterfaceDeactivatedEvent;
 import net.pterodactylus.sone.fcp.event.FullAccessRequiredChanged;
 
 import com.google.common.eventbus.EventBus;
-import org.junit.After;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
@@ -29,16 +30,18 @@ public class PreferencesTest {
        private final EventBus eventBus = mock(EventBus.class);
        private final Preferences preferences = new Preferences(eventBus);
 
-       @After
-       public void tearDown() {
-               verifyNoMoreInteractions(eventBus);
-       }
-
        @Test
        public void preferencesRetainInsertionDelay() {
                preferences.setInsertionDelay(15);
                assertThat(preferences.getInsertionDelay(), is(15));
-               verify(eventBus).post(any(InsertionDelayChangedEvent.class));
+       }
+
+       @Test
+       public void preferencesSendsEventOnSettingInsertionDelay() {
+               preferences.setInsertionDelay(15);
+               ArgumentCaptor<Object> eventsCaptor = forClass(Object.class);
+               verify(eventBus, atLeastOnce()).post(eventsCaptor.capture());
+               assertThat(eventsCaptor.getAllValues(), hasItem(new InsertionDelayChangedEvent(15)));
        }
 
        @Test(expected = IllegalArgumentException.class)
@@ -50,7 +53,6 @@ public class PreferencesTest {
        public void preferencesReturnDefaultValueWhenInsertionDelayIsSetToNull() {
                preferences.setInsertionDelay(null);
                assertThat(preferences.getInsertionDelay(), is(60));
-               verify(eventBus).post(any(InsertionDelayChangedEvent.class));
        }
 
        @Test
@@ -273,7 +275,7 @@ public class PreferencesTest {
        private void verifyFullAccessRequiredChangedEvent(
                        FullAccessRequired fullAccessRequired) {
                ArgumentCaptor<FullAccessRequiredChanged> fullAccessRequiredCaptor =
-                               ArgumentCaptor.forClass(FullAccessRequiredChanged.class);
+                               forClass(FullAccessRequiredChanged.class);
                verify(eventBus).post(fullAccessRequiredCaptor.capture());
                assertThat(
                                fullAccessRequiredCaptor.getValue().getFullAccessRequired(),
@@ -306,4 +308,12 @@ public class PreferencesTest {
                assertThat(preferences.getFcpFullAccessRequired(), is(ALWAYS));
        }
 
+       @Test
+       public void settingInsertionDelayToValidValueSendsChangeEvent() {
+               preferences.setInsertionDelay(30);
+               ArgumentCaptor<Object> eventsCaptor = forClass(Object.class);
+               verify(eventBus, atLeastOnce()).post(eventsCaptor.capture());
+               assertThat(eventsCaptor.getAllValues(), hasItem(new PreferenceChangedEvent("InsertionDelay", 30)));
+       }
+
 }