From: David ‘Bombe’ Roden Date: Thu, 25 Oct 2018 20:01:38 +0000 (+0200) Subject: Send generic “preference changed” event when changing insertion delay X-Git-Tag: v79^2~119 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=be9b08105333cb78ecef49ffbc4841dd62781bce;p=Sone.git Send generic “preference changed” event when changing insertion delay --- 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 index 0000000..2ebb62d --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/core/PreferenceChangedEvent.kt @@ -0,0 +1,3 @@ +package net.pterodactylus.sone.core + +data class PreferenceChangedEvent(val preferenceName: String, val newValue: Any) diff --git a/src/main/java/net/pterodactylus/sone/core/Preferences.java b/src/main/java/net/pterodactylus/sone/core/Preferences.java index cf7d3dd..ce56e8b 100644 --- a/src/main/java/net/pterodactylus/sone/core/Preferences.java +++ b/src/main/java/net/pterodactylus/sone/core/Preferences.java @@ -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; } diff --git a/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java b/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java index ec2930f..fdae222 100644 --- a/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java +++ b/src/test/java/net/pterodactylus/sone/core/PreferencesTest.java @@ -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 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 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 eventsCaptor = forClass(Object.class); + verify(eventBus, atLeastOnce()).post(eventsCaptor.capture()); + assertThat(eventsCaptor.getAllValues(), hasItem(new PreferenceChangedEvent("InsertionDelay", 30))); + } + }