192 lines
5.6 KiB
Kotlin
192 lines
5.6 KiB
Kotlin
|
/*
|
||
|
* Copyright (c) 2020 Proton Technologies AG
|
||
|
*
|
||
|
* This file is part of ProtonMail.
|
||
|
*
|
||
|
* ProtonMail is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* ProtonMail is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with ProtonMail. If not, see https://www.gnu.org/licenses/.
|
||
|
*/
|
||
|
|
||
|
package ch.protonmail.android.mailbox.data.local
|
||
|
|
||
|
import androidx.test.core.app.ApplicationProvider
|
||
|
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||
|
import ch.protonmail.android.data.local.MessageDatabase
|
||
|
import ch.protonmail.android.mailbox.data.local.model.UnreadCounterDatabaseModel
|
||
|
import kotlinx.coroutines.flow.first
|
||
|
import me.proton.core.domain.entity.UserId
|
||
|
import me.proton.core.test.android.runBlockingWithTimeout
|
||
|
import org.junit.runner.RunWith
|
||
|
import kotlin.random.Random
|
||
|
import kotlin.test.AfterTest
|
||
|
import kotlin.test.BeforeTest
|
||
|
import kotlin.test.Test
|
||
|
import kotlin.test.assertEquals
|
||
|
|
||
|
/**
|
||
|
* Test suite for [UnreadCounterDao]
|
||
|
*/
|
||
|
@RunWith(AndroidJUnit4::class)
|
||
|
class UnreadCounterDaoTest {
|
||
|
|
||
|
// region Test data
|
||
|
private val oneUserId = UserId("one")
|
||
|
private val twoUserId = UserId("two")
|
||
|
private val inboxLabelId = "inbox"
|
||
|
private val sentLabelId = "sent"
|
||
|
|
||
|
private val oneMessagesInboxCounter = UnreadCounterDatabaseModel(
|
||
|
userId = oneUserId,
|
||
|
type = UnreadCounterDatabaseModel.Type.MESSAGES,
|
||
|
labelId = inboxLabelId,
|
||
|
unreadCount = Random.nextInt()
|
||
|
)
|
||
|
private val oneMessagesSendCounter = UnreadCounterDatabaseModel(
|
||
|
userId = oneUserId,
|
||
|
type = UnreadCounterDatabaseModel.Type.MESSAGES,
|
||
|
labelId = sentLabelId,
|
||
|
unreadCount = Random.nextInt()
|
||
|
)
|
||
|
private val oneConversationsInboxCounter = UnreadCounterDatabaseModel(
|
||
|
userId = oneUserId,
|
||
|
type = UnreadCounterDatabaseModel.Type.CONVERSATIONS,
|
||
|
labelId = inboxLabelId,
|
||
|
unreadCount = Random.nextInt()
|
||
|
)
|
||
|
private val twoMessagesInboxCounter = UnreadCounterDatabaseModel(
|
||
|
userId = twoUserId,
|
||
|
type = UnreadCounterDatabaseModel.Type.MESSAGES,
|
||
|
labelId = inboxLabelId,
|
||
|
unreadCount = Random.nextInt()
|
||
|
)
|
||
|
// endregion
|
||
|
|
||
|
private lateinit var database: MessageDatabase
|
||
|
private lateinit var dao: UnreadCounterDao
|
||
|
|
||
|
@BeforeTest
|
||
|
fun setup() {
|
||
|
database = MessageDatabase.buildInMemoryDatabase(ApplicationProvider.getApplicationContext())
|
||
|
dao = database.getUnreadCounterDao()
|
||
|
}
|
||
|
|
||
|
@AfterTest
|
||
|
fun tearDown() {
|
||
|
database.close()
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun canInsertAndRetrieveMessagesCounter() = runBlockingWithTimeout {
|
||
|
// given
|
||
|
val input = oneMessagesInboxCounter
|
||
|
|
||
|
// when
|
||
|
dao.insertOrUpdate(input)
|
||
|
val result = dao.observeMessagesUnreadCounters(input.userId)
|
||
|
.first()
|
||
|
.first()
|
||
|
|
||
|
// then
|
||
|
assertEquals(input, result)
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun canInsertAndRetrieveConversationsCounter() = runBlockingWithTimeout {
|
||
|
// given
|
||
|
val input = oneConversationsInboxCounter
|
||
|
|
||
|
// when
|
||
|
dao.insertOrUpdate(input)
|
||
|
val result = dao.observeConversationsUnreadCounters(input.userId)
|
||
|
.first()
|
||
|
.first()
|
||
|
|
||
|
// then
|
||
|
assertEquals(input, result)
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun insertIfDifferentUserId() = runBlockingWithTimeout {
|
||
|
// given
|
||
|
val first = oneMessagesInboxCounter
|
||
|
val second = twoMessagesInboxCounter
|
||
|
|
||
|
// when
|
||
|
dao.insertOrUpdate(first, second)
|
||
|
val firstResult = dao.observeUnreadCounters(first.userId, first.type)
|
||
|
.first()
|
||
|
.first()
|
||
|
val secondResult = dao.observeUnreadCounters(second.userId, second.type)
|
||
|
.first()
|
||
|
.first()
|
||
|
|
||
|
// then
|
||
|
assertEquals(first, firstResult)
|
||
|
assertEquals(second, secondResult)
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun insertIfDifferentType() = runBlockingWithTimeout {
|
||
|
// given
|
||
|
val first = oneMessagesInboxCounter
|
||
|
val second = oneConversationsInboxCounter
|
||
|
|
||
|
// when
|
||
|
dao.insertOrUpdate(first, second)
|
||
|
val firstResult = dao.observeUnreadCounters(first.userId, first.type)
|
||
|
.first()
|
||
|
.first()
|
||
|
val secondResult = dao.observeUnreadCounters(second.userId, second.type)
|
||
|
.first()
|
||
|
.first()
|
||
|
|
||
|
// then
|
||
|
assertEquals(first, firstResult)
|
||
|
assertEquals(second, secondResult)
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun insertIfDifferentLabelId() = runBlockingWithTimeout {
|
||
|
// given
|
||
|
val first = oneMessagesInboxCounter
|
||
|
val second = oneMessagesSendCounter
|
||
|
|
||
|
// when
|
||
|
dao.insertOrUpdate(first, second)
|
||
|
val result = dao.observeUnreadCounters(first.userId, first.type)
|
||
|
.first()
|
||
|
|
||
|
// then
|
||
|
assertEquals(2, result.size)
|
||
|
assert(first in result)
|
||
|
assert(second in result)
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun updateIfSameUserIdTypeAndLabelId() = runBlockingWithTimeout {
|
||
|
// given
|
||
|
val first = oneMessagesInboxCounter
|
||
|
val second = first.copy(unreadCount = 15)
|
||
|
|
||
|
// when
|
||
|
dao.insertOrUpdate(first)
|
||
|
dao.insertOrUpdate(second)
|
||
|
val result = dao.observeUnreadCounters(first.userId, first.type)
|
||
|
.first()
|
||
|
.first()
|
||
|
|
||
|
// then
|
||
|
assertEquals(second, result)
|
||
|
}
|
||
|
}
|