git.iko.soy/opencode-android
on a40d3a2 help sign in
49 lines 1.5 kB history raw
1package soy.iko.opencode.data.repo
3import androidx.test.core.app.ApplicationProvider
4import androidx.test.ext.junit.runners.AndroidJUnit4
5import kotlinx.coroutines.CoroutineScope
6import kotlinx.coroutines.Dispatchers
7import kotlinx.coroutines.SupervisorJob
8import org.junit.Assert.assertEquals
9import org.junit.Test
10import org.junit.runner.RunWith
12@RunWith(AndroidJUnit4::class)
13class DraftStoreTest {
15 private val context = ApplicationProvider.getApplicationContext<android.content.Context>()
17 @Test
18 fun setGetRemoveRoundTrip() {
19 val scope = CoroutineScope(SupervisorJob() + Dispatchers.Unconfined)
20 val store = DraftStore(context, scope)
22 // Use a unique session ID so it doesn't collide with other tests.
23 val sid = "test-${System.nanoTime()}"
25 store.set(sid, "hello world")
26 assertEquals("hello world", store.get(sid))
28 // Overwrite
29 store.set(sid, "updated")
30 assertEquals("updated", store.get(sid))
32 // Blank clears the entry
33 store.set(sid, " ")
34 assertEquals("", store.get(sid))
36 // Explicit remove
37 store.set(sid, "text")
38 store.remove(sid)
39 assertEquals("", store.get(sid))
41 scope.cancel()
42 }
44 @Test
45 fun getReturnsEmptyForUnknownSession() {
46 val store = DraftStore(context, CoroutineScope(SupervisorJob()))
47 assertEquals("", store.get("never-set-${System.nanoTime()}"))
48 }