2 * Sone - SoneLockedHandlerTest.kt - Copyright © 2019 David ‘Bombe’ Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.web.notification
20 import com.google.common.eventbus.*
21 import net.pterodactylus.sone.core.event.*
22 import net.pterodactylus.sone.data.*
23 import net.pterodactylus.sone.data.impl.*
24 import net.pterodactylus.sone.notify.*
25 import net.pterodactylus.util.notify.*
26 import net.pterodactylus.util.template.*
27 import org.hamcrest.MatcherAssert.*
28 import org.hamcrest.Matchers.*
29 import java.util.concurrent.*
33 * Unit test for [SoneLockedHandler].
35 @Suppress("UnstableApiUsage")
36 class SoneLockedHandlerTest {
38 private val eventBus = EventBus()
39 private val notificationManager = NotificationManager()
40 private val notification = ListNotification<Sone>("", "", Template())
41 private val executor = TestScheduledThreadPoolExecutor()
44 SoneLockedHandler(notificationManager, notification, executor).also(eventBus::register)
48 fun shutdownExecutor() = executor.shutdown()
51 fun `notification is not added before the command is run`() {
52 eventBus.post(SoneLockedEvent(sone))
53 assertThat(notificationManager.notifications, emptyIterable())
57 fun `sone is added to notification immediately`() {
58 eventBus.post(SoneLockedEvent(sone))
59 assertThat(notification.elements, contains(sone))
63 fun `notification is added to notification manager from command`() {
64 eventBus.post(SoneLockedEvent(sone))
65 executor.scheduleds.single().command.run()
66 assertThat(notificationManager.notifications, contains<Any>(notification))
70 fun `command is registered with a delay of five minutes`() {
71 eventBus.post(SoneLockedEvent(sone))
72 with(executor.scheduleds.single()) {
73 assertThat(timeUnit.toNanos(delay), equalTo(TimeUnit.MINUTES.toNanos(5)))
78 fun `unlocking sone after locking will cancel the future`() {
79 eventBus.post(SoneLockedEvent(sone))
80 eventBus.post(SoneUnlockedEvent(sone))
81 assertThat(executor.scheduleds.first().future.isCancelled, equalTo(true))
85 fun `unlocking sone after locking will remove the sone from the notification`() {
86 eventBus.post(SoneLockedEvent(sone))
87 eventBus.post(SoneUnlockedEvent(sone))
88 assertThat(notification.elements, emptyIterable())
92 fun `unlocking sone after showing the notification will remove the sone from the notification`() {
93 eventBus.post(SoneLockedEvent(sone))
94 executor.scheduleds.single().command.run()
95 eventBus.post(SoneUnlockedEvent(sone))
96 assertThat(notification.elements, emptyIterable())
100 fun `locking two sones will cancel the first command`() {
101 eventBus.post(SoneLockedEvent(sone))
102 eventBus.post(SoneLockedEvent(sone))
103 assertThat(executor.scheduleds.first().future.isCancelled, equalTo(true))
107 fun `locking two sones will schedule a second command`() {
108 eventBus.post(SoneLockedEvent(sone))
109 eventBus.post(SoneLockedEvent(sone))
110 assertThat(executor.scheduleds[1], notNullValue())
115 private val sone: Sone = IdOnlySone("sone")