Add test for NotificationEventPersistence
This commit is contained in:
parent
73ebffab2e
commit
a09fa1c5ad
2 changed files with 65 additions and 1 deletions
|
|
@ -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 io.element.android.libraries.push.impl.notifications.model.SimpleNotifiableEvent
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
data class NotificationEventQueue constructor(
|
data class NotificationEventQueue(
|
||||||
private val queue: MutableList<NotifiableEvent>,
|
private val queue: MutableList<NotifiableEvent>,
|
||||||
/**
|
/**
|
||||||
* An in memory FIFO cache of the seen events.
|
* An in memory FIFO cache of the seen events.
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue