package com.cottondb.notes import android.app.Application import android.net.Uri import androidx.activity.compose.LocalActivityResultRegistryOwner import androidx.activity.result.ActivityResultRegistry import androidx.activity.result.ActivityResultRegistryOwner import androidx.activity.result.contract.ActivityResultContract import androidx.compose.runtime.CompositionLocalProvider import androidx.compose.ui.test.junit4.createComposeRule import androidx.core.app.ActivityOptionsCompat import androidx.test.core.app.ApplicationProvider import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config import java.io.File import kotlinx.coroutines.CompletableDeferred /** * Bring notes in, drawn as the surface draws it (only while the model is importing): a file picked reaches the core. * On the emulator nothing ever came in: the picker stopped importing as the files came back, left, and took the * copying with it (21 Nov 2026). */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], application = Application::class) class NotesImportTest { @get:Rule val compose = createComposeRule() @Test fun a_file_picked_is_brought_in() { val context = ApplicationProvider.getApplicationContext() val picked = File(context.cacheDir, "Garden plans.md").apply { writeText("# Garden plans\n\nInvented words.\n") } var launches = 0 val registry = object : ActivityResultRegistry() { override fun onLaunch(requestCode: Int, contract: ActivityResultContract, input: I, options: ActivityOptionsCompat?) { launches++ @Suppress("UNCHECKED_CAST") dispatchResult(requestCode, listOf(Uri.fromFile(picked)) as O) } } val owner = object : ActivityResultRegistryOwner { override val activityResultRegistry = registry } val model = NotesModel().apply { importing = true } val brought = mutableListOf() // The core takes its time: the picker must stand (and keep its work) until the files are in. val gate = CompletableDeferred() var bringing = false compose.setContent { CompositionLocalProvider(LocalActivityResultRegistryOwner provides owner) { if (model.importing) ImportPicker(model) { copies -> bringing = true; gate.await(); brought += copies.map { it.second.readText() } } } } compose.waitUntil(5_000) { bringing } compose.waitForIdle() assertTrue("still importing while the core brings the files in", model.importing) gate.complete(Unit) compose.waitUntil(5_000) { !model.importing } assertEquals(1, launches) assertEquals(listOf("# Garden plans\n\nInvented words.\n"), brought) assertFalse(model.importing) } }