From be9b08105333cb78ecef49ffbc4841dd62781bce Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 25 Oct 2018 22:01:38 +0200 Subject: [PATCH] =?utf8?q?Send=20generic=20=E2=80=9Cpreference=20changed?= =?utf8?q?=E2=80=9D=20event=20when=20changing=20insertion=20delay?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/core/PreferenceChangedEvent.kt | 3 +++ .../net/pterodactylus/sone/core/Preferences.java | 1 + .../pterodactylus/sone/core/PreferencesTest.java | 30 ++++++++++++++-------- 3 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/core/PreferenceChangedEvent.kt 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))); + } + } -- 2.7.4