Cotton code
65 lines · 3.0 KB · KotlinRaw
package com.cottondb.notesimport android.app.Applicationimport android.net.Uriimport androidx.activity.compose.LocalActivityResultRegistryOwnerimport androidx.activity.result.ActivityResultRegistryimport androidx.activity.result.ActivityResultRegistryOwnerimport androidx.activity.result.contract.ActivityResultContractimport androidx.compose.runtime.CompositionLocalProviderimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.core.app.ActivityOptionsCompatimport androidx.test.core.app.ApplicationProviderimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Configimport java.io.Fileimport 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<Application>()        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 <I, O> onLaunch(requestCode: Int, contract: ActivityResultContract<I, O>, 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<String>()        // The core takes its time: the picker must stand (and keep its work) until the files are in.        val gate = CompletableDeferred<Unit>()        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)    }}