Repository navigation
fix(attachment-preview): do not ask the content provider for the type during composition #6857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mahibi
merged 5 commits into
nextcloud:master
from
ToteMeiSter:fix/preview-mime-type-off-main-thread
Oct 16, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
087f23b
fix(attachment-preview): do not ask the content provider for the type…
ToteMeiSter 0169d98
fix(attachment-preview): enable send only after the files are described
ToteMeiSter 3e6dcee
fix(attachment-preview): do not hold Send back until the files are de…
ToteMeiSter a9d9fd6
fix(attachment-preview): publish describe results on main so a cancel…
ToteMeiSter 73d1c9f
refactor(attachment-preview): pass the view model test hooks as const…
ToteMeiSter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
app/src/test/java/com/nextcloud/talk/attachmentpreview/FileAttachmentPreviewViewModelTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| /* | ||
| * Nextcloud Talk - Android Client | ||
| * | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: GPL-3.0-or-later | ||
| */ | ||
| package com.nextcloud.talk.attachmentpreview | ||
|
|
||
| import android.app.Application | ||
| import android.content.Context | ||
| import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
| import kotlinx.coroutines.test.StandardTestDispatcher | ||
| import kotlinx.coroutines.test.TestScope | ||
| import kotlinx.coroutines.test.advanceUntilIdle | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import java.util.concurrent.CountDownLatch | ||
| import java.util.concurrent.TimeUnit | ||
| import org.mockito.Mockito.mock | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| @OptIn(ExperimentalCoroutinesApi::class) | ||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(application = Application::class, sdk = [33]) | ||
| class FileAttachmentPreviewViewModelTest { | ||
|
|
||
| private val testScope = TestScope() | ||
| private val described = mutableListOf<String>() | ||
| private lateinit var viewModel: FileAttachmentPreviewViewModel | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| viewModel = FileAttachmentPreviewViewModel( | ||
| context = mock(Context::class.java), | ||
| ioDispatcher = StandardTestDispatcher(testScope.testScheduler), | ||
| mainDispatcher = StandardTestDispatcher(testScope.testScheduler), | ||
| describer = { _, uri, _ -> | ||
| described.add(uri) | ||
| FileDescription(uri, uri, MediaKind.OTHER, "text/plain", null) | ||
| }, | ||
| mimeTypeResolver = { _, uri -> if (uri.endsWith(".mp4")) "video/mp4" else "text/plain" } | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `mime types are resolved without describing the files`() { | ||
| viewModel.setInitialFiles(listOf("content://a/clip.mp4")) | ||
|
|
||
| viewModel.resolveMimeTypes() | ||
| testScope.advanceUntilIdle() | ||
|
|
||
| assertEquals("video/mp4", viewModel.mimeTypesByUri["content://a/clip.mp4"]) | ||
| assertTrue(viewModel.descriptionsByUri.isEmpty()) | ||
| assertTrue(described.isEmpty()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `mime types of removed files are dropped`() { | ||
| viewModel.setInitialFiles(listOf("content://a/1.txt", "content://a/2.txt")) | ||
| viewModel.resolveMimeTypes() | ||
| testScope.advanceUntilIdle() | ||
|
|
||
| viewModel.removeFile("content://a/1.txt") | ||
| viewModel.resolveMimeTypes() | ||
| testScope.advanceUntilIdle() | ||
|
|
||
| assertEquals(setOf("content://a/2.txt"), viewModel.mimeTypesByUri.keys) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a newer describeFiles call cancels the previous job`() { | ||
| viewModel.setInitialFiles(listOf("content://a/1.txt")) | ||
| viewModel.describeFiles(true) | ||
| viewModel.addFiles(listOf("content://a/2.txt")) | ||
| viewModel.describeFiles(true) | ||
| testScope.advanceUntilIdle() | ||
|
|
||
| assertEquals(listOf("content://a/1.txt", "content://a/2.txt"), described) | ||
| assertEquals(setOf("content://a/1.txt", "content://a/2.txt"), viewModel.descriptionsByUri.keys) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a cancelled describeFiles job that is still copying does not drop a newer description`() { | ||
| val started = CountDownLatch(1) | ||
| val release = CountDownLatch(1) | ||
| viewModel = FileAttachmentPreviewViewModel( | ||
| context = mock(Context::class.java), | ||
| ioDispatcher = kotlinx.coroutines.Dispatchers.IO, | ||
| mainDispatcher = StandardTestDispatcher(testScope.testScheduler), | ||
| describer = { _, uri, _ -> | ||
| if (uri == "content://a/1.txt" && started.count == 1L) { | ||
| started.countDown() | ||
| release.await(5, TimeUnit.SECONDS) | ||
| } | ||
| FileDescription(uri, uri, MediaKind.OTHER, "text/plain", null) | ||
| } | ||
| ) | ||
| viewModel.setInitialFiles(listOf("content://a/1.txt")) | ||
| viewModel.describeFiles(true) | ||
| assertTrue(started.await(5, TimeUnit.SECONDS)) | ||
|
|
||
| viewModel.addFiles(listOf("content://a/2.txt")) | ||
| viewModel.describeFiles(true) | ||
| Thread.sleep(300) | ||
| testScope.advanceUntilIdle() | ||
| assertEquals( | ||
| setOf("content://a/1.txt", "content://a/2.txt"), | ||
| viewModel.descriptionsByUri.keys | ||
| ) | ||
|
|
||
| release.countDown() | ||
| Thread.sleep(300) | ||
| testScope.advanceUntilIdle() | ||
|
|
||
| assertEquals( | ||
| setOf("content://a/1.txt", "content://a/2.txt"), | ||
| viewModel.descriptionsByUri.keys | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.