|
| 1 | +/* |
| 2 | + * Nextcloud Talk - Android Client |
| 3 | + * |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 6 | + */ |
| 7 | +package com.nextcloud.talk.data.database.dao |
| 8 | + |
| 9 | +import android.content.Context |
| 10 | +import androidx.room.Room |
| 11 | +import androidx.test.core.app.ApplicationProvider |
| 12 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 13 | +import com.nextcloud.talk.data.database.model.ConversationEntity |
| 14 | +import com.nextcloud.talk.data.source.local.TalkDatabase |
| 15 | +import com.nextcloud.talk.data.user.UsersDao |
| 16 | +import com.nextcloud.talk.data.user.model.UserEntity |
| 17 | +import com.nextcloud.talk.models.MessageDraft |
| 18 | +import com.nextcloud.talk.models.json.conversations.ConversationEnums |
| 19 | +import com.nextcloud.talk.models.json.participants.Participant |
| 20 | +import kotlinx.coroutines.flow.first |
| 21 | +import kotlinx.coroutines.test.runTest |
| 22 | +import org.junit.After |
| 23 | +import org.junit.Assert.assertEquals |
| 24 | +import org.junit.Assert.assertTrue |
| 25 | +import org.junit.Before |
| 26 | +import org.junit.Test |
| 27 | +import org.junit.runner.RunWith |
| 28 | + |
| 29 | +@RunWith(AndroidJUnit4::class) |
| 30 | +class ConversationsDaoTest { |
| 31 | + private lateinit var usersDao: UsersDao |
| 32 | + private lateinit var conversationsDao: ConversationsDao |
| 33 | + private lateinit var db: TalkDatabase |
| 34 | + private var accountId: Long = 0 |
| 35 | + |
| 36 | + @Before |
| 37 | + fun createDb() = |
| 38 | + runTest { |
| 39 | + val context = ApplicationProvider.getApplicationContext<Context>() |
| 40 | + db = Room.inMemoryDatabaseBuilder( |
| 41 | + context, |
| 42 | + TalkDatabase::class.java |
| 43 | + ).allowMainThreadQueries().build() |
| 44 | + usersDao = db.usersDao() |
| 45 | + conversationsDao = db.conversationsDao() |
| 46 | + |
| 47 | + val user = UserEntity(userId = "user1", username = "User 1") |
| 48 | + accountId = usersDao.saveUser(user) |
| 49 | + } |
| 50 | + |
| 51 | + @After |
| 52 | + fun closeDb() { |
| 53 | + db.close() |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + fun insertAndGetConversation() = |
| 58 | + runTest { |
| 59 | + val conversation = createConversationEntity(accountId, "token1", "Conversation 1") |
| 60 | + conversationsDao.insertConversation(conversation) |
| 61 | + |
| 62 | + val retrieved = conversationsDao.getConversationForUser(accountId, "token1").first() |
| 63 | + assertEquals("Conversation 1", retrieved?.name) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun updateConversation() = |
| 68 | + runTest { |
| 69 | + val conversation = createConversationEntity(accountId, "token1", "Conversation 1") |
| 70 | + conversationsDao.insertConversation(conversation) |
| 71 | + |
| 72 | + conversation.name = "Updated Name" |
| 73 | + conversationsDao.updateConversation(conversation) |
| 74 | + |
| 75 | + val retrieved = conversationsDao.getConversationForUser(accountId, "token1").first() |
| 76 | + assertEquals("Updated Name", retrieved?.name) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun upsertConversations_mergesLocalOnlyFields() = |
| 81 | + runTest { |
| 82 | + val conversation = createConversationEntity(accountId, "token1", "Conversation 1") |
| 83 | + val draft = MessageDraft("Draft message", 5) |
| 84 | + conversation.messageDraft = draft |
| 85 | + conversation.hiddenUpcomingEvent = "event1" |
| 86 | + conversationsDao.insertConversation(conversation) |
| 87 | + |
| 88 | + val serverConversation = createConversationEntity(accountId, "token1", "Server Name") |
| 89 | + // Server version won't have the local-only fields (they'll be default/null) |
| 90 | + serverConversation.messageDraft = MessageDraft() |
| 91 | + serverConversation.hiddenUpcomingEvent = null |
| 92 | + |
| 93 | + conversationsDao.upsertConversations(accountId, listOf(serverConversation)) |
| 94 | + |
| 95 | + val retrieved = conversationsDao.getConversationForUser(accountId, "token1").first() |
| 96 | + assertEquals("Server Name", retrieved?.name) |
| 97 | + assertEquals("Draft message", retrieved?.messageDraft?.messageText) |
| 98 | + assertEquals("event1", retrieved?.hiddenUpcomingEvent) |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + fun deleteConversations() = |
| 103 | + runTest { |
| 104 | + val conv1 = createConversationEntity(accountId, "token1", "Conv 1") |
| 105 | + val conv2 = createConversationEntity(accountId, "token2", "Conv 2") |
| 106 | + conversationsDao.insertConversation(conv1) |
| 107 | + conversationsDao.insertConversation(conv2) |
| 108 | + |
| 109 | + conversationsDao.deleteConversations(listOf(conv1.internalId)) |
| 110 | + |
| 111 | + val list = conversationsDao.getConversationsForUser(accountId).first() |
| 112 | + assertEquals(1, list.size) |
| 113 | + assertEquals("token2", list[0].token) |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun clearAllConversationsForUser() = |
| 118 | + runTest { |
| 119 | + val conv1 = createConversationEntity(accountId, "token1", "Conv 1") |
| 120 | + conversationsDao.insertConversation(conv1) |
| 121 | + |
| 122 | + conversationsDao.clearAllConversationsForUser(accountId) |
| 123 | + |
| 124 | + val list = conversationsDao.getConversationsForUser(accountId).first() |
| 125 | + assertTrue(list.isEmpty()) |
| 126 | + } |
| 127 | + |
| 128 | + private fun createConversationEntity(accountId: Long, token: String, roomName: String) = |
| 129 | + ConversationEntity( |
| 130 | + internalId = "$accountId@$token", |
| 131 | + accountId = accountId, |
| 132 | + token = token, |
| 133 | + name = roomName, |
| 134 | + displayName = roomName, |
| 135 | + actorId = "", |
| 136 | + actorType = "", |
| 137 | + avatarVersion = "", |
| 138 | + canDeleteConversation = false, |
| 139 | + canLeaveConversation = false, |
| 140 | + description = "", |
| 141 | + lobbyState = ConversationEnums.LobbyState.LOBBY_STATE_MODERATORS_ONLY, |
| 142 | + notificationLevel = ConversationEnums.NotificationLevel.ALWAYS, |
| 143 | + objectType = ConversationEnums.ObjectType.FILE, |
| 144 | + objectId = "", |
| 145 | + participantType = Participant.ParticipantType.DUMMY, |
| 146 | + conversationReadOnlyState = ConversationEnums.ConversationReadOnlyState.CONVERSATION_READ_WRITE, |
| 147 | + sessionId = "", |
| 148 | + type = ConversationEnums.ConversationType.DUMMY, |
| 149 | + unreadMentionDirect = false, |
| 150 | + hasCustomAvatar = false |
| 151 | + ) |
| 152 | +} |
0 commit comments