}
if (!hasFirstStartNotification()) {
notificationManager.addNotification(isLocal ? localReplyNotification : newReplyNotification);
- } else {
- getCore().markReplyKnown(reply);
}
}
--- /dev/null
+/**
+ * Sone - MarkPostReplyKnownDuringFirstStartHandlerTest.kt - Copyright © 2020 David ‘Bombe’ Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.notification
+
+import com.google.common.eventbus.*
+import net.pterodactylus.sone.core.event.*
+import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.notify.*
+import net.pterodactylus.sone.utils.*
+import net.pterodactylus.util.notify.*
+import java.util.function.*
+import javax.inject.*
+
+/**
+ * Handler that marks post replies [as known][net.pterodactylus.sone.core.Core.markReplyKnown]
+ * while the [first start notification][net.pterodactylus.util.notify.NotificationManager.hasFirstStartNotification]
+ * is shown.
+ */
+class MarkPostReplyKnownDuringFirstStartHandler @Inject constructor(private val notificationManager: NotificationManager, private val markAsKnown: Consumer<PostReply>) {
+
+ @Subscribe
+ fun newPostReply(event: NewPostReplyFoundEvent) {
+ if (notificationManager.hasFirstStartNotification()) {
+ markAsKnown(event.postReply)
+ }
+ }
+
+}
@Suppress("UNUSED_PARAMETER")
class NotificationHandler @Inject constructor(
markPostKnownDuringFirstStartHandler: MarkPostKnownDuringFirstStartHandler,
+ markPostReplyKnownDuringFirstStartHandler: MarkPostReplyKnownDuringFirstStartHandler,
newSoneHandler: NewSoneHandler,
newRemotePostHandler: NewRemotePostHandler,
soneLockedOnStartupHandler: SoneLockedOnStartupHandler,
override fun configure() {
bind(NotificationHandler::class.java).`in`(Singleton::class.java)
bind<MarkPostKnownDuringFirstStartHandler>().asSingleton()
+ bind<MarkPostReplyKnownDuringFirstStartHandler>().asSingleton()
bind<SoneLockedOnStartupHandler>().asSingleton()
bind<NewSoneHandler>().asSingleton()
bind<NewRemotePostHandler>().asSingleton()
fun getMarkPostKnownHandler(core: Core): Consumer<Post> = Consumer { core.markPostKnown(it) }
@Provides
+ fun getMarkPostReplyKnownHandler(core: Core): Consumer<PostReply> = Consumer { core.markReplyKnown(it) }
+
+ @Provides
@Singleton
@Named("soneLockedOnStartup")
fun getSoneLockedOnStartupNotification(loaders: Loaders) =
--- /dev/null
+/**
+ * Sone - MarkPostReplyKnownDuringFirstStartHandlerTest.kt - Copyright © 2020 David ‘Bombe’ Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.notification
+
+import net.pterodactylus.sone.core.event.*
+import net.pterodactylus.sone.data.*
+import net.pterodactylus.sone.test.*
+import org.hamcrest.MatcherAssert.*
+import org.hamcrest.Matchers.*
+import java.util.function.*
+import kotlin.test.*
+
+/**
+ * Unit test for [MarkPostReplyKnownDuringFirstStartHandler].
+ */
+class MarkPostReplyKnownDuringFirstStartHandlerTest {
+
+ private val markedAsKnown = mutableListOf<PostReply>()
+ private val notificationTester = NotificationHandlerTester { MarkPostReplyKnownDuringFirstStartHandler(it, Consumer { markedAsKnown += it }) }
+ private val postReply = emptyPostReply()
+
+ @Test
+ fun `post reply is marked as known on new reply during first start`() {
+ notificationTester.firstStart()
+ notificationTester.sendEvent(NewPostReplyFoundEvent(postReply))
+ assertThat(markedAsKnown, contains(postReply))
+ }
+
+ @Test
+ fun `post reply is not marked as known on new reply if not during first start`() {
+ notificationTester.sendEvent(NewPostReplyFoundEvent(postReply))
+ assertThat(markedAsKnown, not(hasItem(postReply)))
+ }
+
+}
}
@Test
+ fun `mark-post-reply-known-during-first-start handler is created as singleton`() {
+ injector.verifySingletonInstance<MarkPostReplyKnownDuringFirstStartHandler>()
+ }
+
+ @Test
+ fun `mark-post-reply-known-during-first-start handler is created with correct action`() {
+ notificationManager.firstStart()
+ val handler = injector.getInstance<MarkPostReplyKnownDuringFirstStartHandler>()
+ val postReply = mock<PostReply>()
+ handler.newPostReply(NewPostReplyFoundEvent(postReply))
+ verify(core).markReplyKnown(postReply)
+ }
+
+ @Test
fun `sone-locked-on-startup handler is created as singleton`() {
injector.verifySingletonInstance<SoneLockedOnStartupHandler>()
}
--- /dev/null
+/**
+ * Sone - NotificationHandlerTester.kt - Copyright © 2019 David ‘Bombe’ Roden
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.pterodactylus.sone.web.notification
+
+import com.google.common.eventbus.*
+import net.pterodactylus.util.notify.*
+
+/**
+ * Helper for testing event handlers that deal with notifications. It contains
+ * a notification manager and an [event bus][EventBus] and automatically
+ * registers the created handler on the event bus.
+ *
+ * ```
+ * val notification = SomeNotification()
+ * val notificationTester = NotificationTester { SomeHandler(it, notification) }
+ *
+ * fun test() {
+ * notificationTester.sendEvent(SomeEvent())
+ * assertThat(notificationTester.elements, hasItem(notification))
+ * }
+ * ```
+ */
+@Suppress("UnstableApiUsage")
+class NotificationHandlerTester(createHandler: (NotificationManager) -> Any) {
+
+ private val eventBus = EventBus()
+ private val notificationManager = NotificationManager()
+
+ /** Returns all notifications of the notification manager. */
+ val notifications: Set<Notification>
+ get() = notificationManager.notifications
+
+ init {
+ eventBus.register(createHandler(notificationManager))
+ }
+
+ /** Sends an event to the event bus. */
+ fun sendEvent(event: Any) = eventBus.post(event)
+
+ /** Sets the first-start notification on the notification manager. */
+ fun firstStart() = notificationManager.firstStart()
+
+}