♻️ Extract handler for sone-locked notification
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / notification / SoneLockedHandlerTest.kt
1 /**
2  * Sone - SoneLockedHandlerTest.kt - Copyright © 2019 David ‘Bombe’ Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.web.notification
19
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.*
30 import kotlin.test.*
31
32 /**
33  * Unit test for [SoneLockedHandler].
34  */
35 @Suppress("UnstableApiUsage")
36 class SoneLockedHandlerTest {
37
38         private val eventBus = EventBus()
39         private val notificationManager = NotificationManager()
40         private val notification = ListNotification<Sone>("", "", Template())
41         private val executor = TestScheduledThreadPoolExecutor()
42
43         init {
44                 SoneLockedHandler(notificationManager, notification, executor).also(eventBus::register)
45         }
46
47         @Test
48         fun `notification is not added during the first five minutes`() {
49                 eventBus.post(SoneLockedEvent(sone))
50                 assertThat(notificationManager.notifications, emptyIterable())
51         }
52
53         @Test
54         fun `sone is added to notification from command`() {
55                 eventBus.post(SoneLockedEvent(sone))
56                 executor.scheduledDelay.single().command.run()
57                 assertThat(notification.elements, contains(sone))
58         }
59
60         @Test
61         fun `notification is added to notification manager from command`() {
62                 eventBus.post(SoneLockedEvent(sone))
63                 executor.scheduledDelay.single().command.run()
64                 assertThat(notificationManager.notifications, contains<Any>(notification))
65         }
66
67         @Test
68         fun `command is registered with a delay of five minutes`() {
69                 eventBus.post(SoneLockedEvent(sone))
70                 with(executor.scheduledDelay.single()) {
71                         assertThat(timeUnit.toNanos(delay), equalTo(TimeUnit.MINUTES.toNanos(5)))
72                 }
73         }
74
75         @Test
76         fun `unlocking sone after locking will cancel the future`() {
77                 eventBus.post(SoneLockedEvent(sone))
78                 eventBus.post(SoneUnlockedEvent(sone))
79                 assertThat(executor.scheduledDelay.first().future.isCancelled, equalTo(true))
80         }
81
82         @Test
83         fun `unlocking sone after locking will remove the sone from the notification`() {
84                 eventBus.post(SoneLockedEvent(sone))
85                 eventBus.post(SoneUnlockedEvent(sone))
86                 assertThat(notification.elements, emptyIterable())
87         }
88
89         @Test
90         fun `unlocking sone after showing the notification will remove the sone from the notification`() {
91                 eventBus.post(SoneLockedEvent(sone))
92                 executor.scheduledDelay.single().command.run()
93                 eventBus.post(SoneUnlockedEvent(sone))
94                 assertThat(notification.elements, emptyIterable())
95         }
96
97         @Test
98         fun `locking two sones will cancel the first command`() {
99                 eventBus.post(SoneLockedEvent(sone))
100                 eventBus.post(SoneLockedEvent(sone))
101                 assertThat(executor.scheduledDelay.first().future.isCancelled, equalTo(true))
102         }
103
104         @Test
105         fun `locking two sones will schedule a second command`() {
106                 eventBus.post(SoneLockedEvent(sone))
107                 eventBus.post(SoneLockedEvent(sone))
108                 assertThat(executor.scheduledDelay[1], notNullValue())
109         }
110
111 }
112
113 private val sone: Sone = IdOnlySone("sone")
114
115 private data class Scheduled(val command: Runnable, val delay: Long, val timeUnit: TimeUnit, val future: ScheduledFuture<*>)
116
117 private class TestScheduledThreadPoolExecutor : ScheduledThreadPoolExecutor(1) {
118
119         val scheduledDelay = mutableListOf<Scheduled>()
120
121         override fun schedule(command: Runnable, delay: Long, unit: TimeUnit): ScheduledFuture<*> =
122                         super.schedule(command, delay, unit)
123                                         .also { scheduledDelay += Scheduled(command, delay, unit, it) }
124
125 }