From c3eb6532617fca913330676f9872e27b5125b321 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 27 Nov 2023 14:49:19 +0100 Subject: [PATCH] Add test for NotificationEventPersistence --- .../notifications/NotificationEventQueue.kt | 2 +- .../NotificationEventPersistenceTest.kt | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/notifications/NotificationEventPersistenceTest.kt diff --git a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/NotificationEventQueue.kt b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/NotificationEventQueue.kt index 8eea6a9d5a..bb76de47d2 100644 --- a/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/NotificationEventQueue.kt +++ b/libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/NotificationEventQueue.kt @@ -28,7 +28,7 @@ import io.element.android.libraries.push.impl.notifications.model.NotifiableMess import io.element.android.libraries.push.impl.notifications.model.SimpleNotifiableEvent import timber.log.Timber -data class NotificationEventQueue constructor( +data class NotificationEventQueue( private val queue: MutableList, /** * An in memory FIFO cache of the seen events. diff --git a/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/notifications/NotificationEventPersistenceTest.kt b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/notifications/NotificationEventPersistenceTest.kt new file mode 100644 index 0000000000..79343278b9 --- /dev/null +++ b/libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/notifications/NotificationEventPersistenceTest.kt @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.element.android.libraries.push.impl.notifications + +import com.google.common.truth.Truth.assertThat +import io.element.android.libraries.core.cache.CircularCache +import io.element.android.libraries.push.impl.notifications.fixtures.aSimpleNotifiableEvent +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment + +@RunWith(RobolectricTestRunner::class) +class NotificationEventPersistenceTest { + @Test + fun `loadEvents should return empty NotificationEventQueue`() { + val sut = createNotificationEventPersistence() + val result = sut.loadEvents( + factory = { rawEvents -> + NotificationEventQueue(rawEvents.toMutableList(), seenEventIds = CircularCache.create(cacheSize = 25)) + } + ) + assertThat(result.isEmpty()).isTrue() + } + + @Test + fun `after persisting NotificationEventQueue, loadEvents should return non-empty NotificationEventQueue`() { + val sut = createNotificationEventPersistence() + val notificationEventQueue = NotificationEventQueue(mutableListOf(), seenEventIds = CircularCache.create(cacheSize = 25)) + // First persist an empty queue + sut.persistEvents(notificationEventQueue) + // Add an event + notificationEventQueue.add(aSimpleNotifiableEvent()) + // Persist + // Note: is cannot work because AndroidKeyStore is not available. But we check that the code does + // not crash. + sut.persistEvents(notificationEventQueue) + sut.loadEvents( + factory = { rawEvents -> + NotificationEventQueue(rawEvents.toMutableList(), seenEventIds = CircularCache.create(cacheSize = 25)) + } + ) + // assertThat(result.isEmpty()).isFalse() + } + + private fun createNotificationEventPersistence(): NotificationEventPersistence { + val context = RuntimeEnvironment.getApplication() + return NotificationEventPersistence(context) + } +}