mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 12:07:28 +00:00
Cover both MediaStore save paths with a fake provider
DefaultFileSave had no test at all and AndroidLocalMediaActions only exercised the failing path, so neither the uniquifying retry nor the new IOException on a null insert was ever executed. FakeMediaStoreContentProvider stands in for the media authority: it records the display names an insert is attempted with, keeps whatever is copied into it, and can either collide a name the way MediaStore does past 32 files or refuse to create anything. Both call sites now go through it for the plain save, the retry and the rejected insert. Its name cannot follow the Fake<Interface> convention because it extends the framework ContentProvider, so it joins the existing exceptions in KonsistClassNameTest next to FakeFileSystem and FakeKeyStore.
This commit is contained in:
+60
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2026 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.features.viewfolder.impl.file
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.tests.testutils.fake.registerFakeMediaStoreContentProvider
|
||||
import io.element.android.tests.testutils.robolectric.RobolectricTest
|
||||
import io.element.android.tests.testutils.testCoroutineDispatchers
|
||||
import kotlinx.coroutines.test.TestScope
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
import java.io.File
|
||||
|
||||
class DefaultFileSaveTest : RobolectricTest() {
|
||||
@Test
|
||||
fun `save inserts the file in the Downloads collection under its own name`() = runTest {
|
||||
val mediaStore = registerFakeMediaStoreContentProvider()
|
||||
createFileSave().save(aFileToSave().path)
|
||||
assertThat(mediaStore.insertedDisplayNames).containsExactly("a-file.txt")
|
||||
assertThat(mediaStore.savedContent("a-file.txt")).isEqualTo("Some content")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `save retries with a suffixed name when the name cannot be made unique`() = runTest {
|
||||
val mediaStore = registerFakeMediaStoreContentProvider().apply {
|
||||
conflictingDisplayNames = setOf("a-file.txt")
|
||||
}
|
||||
createFileSave().save(aFileToSave().path)
|
||||
assertThat(mediaStore.insertedDisplayNames).hasSize(2)
|
||||
assertThat(mediaStore.insertedDisplayNames.last()).matches("a-file_\\d+\\.txt")
|
||||
assertThat(mediaStore.savedContent(mediaStore.insertedDisplayNames.last())).isEqualTo("Some content")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `save writes nothing when the destination cannot be created`() = runTest {
|
||||
val mediaStore = registerFakeMediaStoreContentProvider().apply {
|
||||
returnsNoUriOnInsert = true
|
||||
}
|
||||
createFileSave().save(aFileToSave().path)
|
||||
assertThat(mediaStore.insertedDisplayNames).containsExactly("a-file.txt")
|
||||
assertThat(mediaStore.savedFileNames()).isEmpty()
|
||||
}
|
||||
|
||||
private fun aFileToSave(): File {
|
||||
return File(RuntimeEnvironment.getApplication().cacheDir, "a-file.txt").apply {
|
||||
writeText("Some content")
|
||||
}
|
||||
}
|
||||
|
||||
private fun TestScope.createFileSave() = DefaultFileSave(
|
||||
context = RuntimeEnvironment.getApplication(),
|
||||
dispatchers = testCoroutineDispatchers(),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user