2 * Sone - ImageInsertHandlerTest.kt - Copyright © 2019–2020 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 freenet.keys.FreenetURI.*
22 import net.pterodactylus.sone.core.event.*
23 import net.pterodactylus.sone.data.*
24 import net.pterodactylus.sone.data.impl.*
25 import net.pterodactylus.sone.notify.*
26 import net.pterodactylus.util.notify.*
27 import net.pterodactylus.util.template.*
28 import org.hamcrest.MatcherAssert.*
29 import org.hamcrest.Matchers.*
33 * Unit test for [ImageInsertHandler].
35 @Suppress("UnstableApiUsage")
36 class ImageInsertHandlerTest {
38 private val eventBus = EventBus()
39 private val notificationManager = NotificationManager()
40 private val imageInsertingNotification = ListNotification<Image>("", "", Template())
41 private val imageFailedNotification = ListNotification<Image>("", "", Template())
42 private val imageInsertedNotification = ListNotification<Image>("", "", Template())
45 eventBus.register(ImageInsertHandler(notificationManager, imageInsertingNotification, imageFailedNotification, imageInsertedNotification))
49 fun `handler adds notification when image insert starts`() {
50 eventBus.post(ImageInsertStartedEvent(image))
51 assertThat(notificationManager.notifications, contains<Notification>(imageInsertingNotification))
55 fun `handler adds image to notification when image insert starts`() {
56 eventBus.post(ImageInsertStartedEvent(image))
57 assertThat(imageInsertingNotification.elements, contains(image))
61 fun `handler removes image from inserting notification when insert is aborted`() {
62 eventBus.post(ImageInsertStartedEvent(image))
63 eventBus.post(ImageInsertAbortedEvent(image))
64 assertThat(imageInsertingNotification.elements, emptyIterable())
68 fun `handler removes image from inserting notification when insert fails`() {
69 eventBus.post(ImageInsertStartedEvent(image))
70 eventBus.post(ImageInsertFailedEvent(image, Throwable()))
71 assertThat(imageInsertingNotification.elements, emptyIterable())
75 fun `handler adds image to insert-failed notification when insert fails`() {
76 eventBus.post(ImageInsertFailedEvent(image, Throwable()))
77 assertThat(imageFailedNotification.elements, contains(image))
81 fun `handler adds insert-failed notification to manager when insert fails`() {
82 eventBus.post(ImageInsertFailedEvent(image, Throwable()))
83 assertThat(notificationManager.notifications, contains<Notification>(imageFailedNotification))
87 fun `handler removes image from inserting notification when insert succeeds`() {
88 eventBus.post(ImageInsertStartedEvent(image))
89 eventBus.post(ImageInsertFinishedEvent(image, EMPTY_CHK_URI))
90 assertThat(imageInsertingNotification.elements, emptyIterable())
94 fun `handler adds image to inserted notification when insert succeeds`() {
95 eventBus.post(ImageInsertFinishedEvent(image, EMPTY_CHK_URI))
96 assertThat(imageInsertedNotification.elements, contains(image))
100 fun `handler adds inserted notification to manager when insert succeeds`() {
101 eventBus.post(ImageInsertFinishedEvent(image, EMPTY_CHK_URI))
102 assertThat(notificationManager.notifications, contains<Notification>(imageInsertedNotification))
107 private val image: Image = ImageImpl()