git.iko.soy/opencode-android
on 9f0326a help sign in
51 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 kotlinx.coroutines.cancel
9import kotlinx.coroutines.runBlocking
10import org.junit.Assert.assertEquals
11import org.junit.Test
12import org.junit.runner.RunWith
14@RunWith(AndroidJUnit4::class)
15class DraftStoreTest {
17 private val context = ApplicationProvider.getApplicationContext<android.content.Context>()
19 @Test
20 fun setGetRemoveRoundTrip() = runBlocking {
21 val scope = CoroutineScope(SupervisorJob() + Dispatchers.Unconfined)
22 val store = DraftStore(context, scope)
24 // Use a unique session ID so it doesn't collide with other tests.
25 val sid = "test-${System.nanoTime()}"
27 store.set(sid, "hello world")
28 assertEquals("hello world", store.get(sid))
30 // Overwrite
31 store.set(sid, "updated")
32 assertEquals("updated", store.get(sid))
34 // Blank clears the entry
35 store.set(sid, " ")
36 assertEquals("", store.get(sid))
38 // Explicit remove
39 store.set(sid, "text")
40 store.remove(sid)
41 assertEquals("", store.get(sid))
43 scope.cancel()
44 }
46 @Test
47 fun getReturnsEmptyForUnknownSession() {
48 val store = DraftStore(context, CoroutineScope(SupervisorJob()))
49 assertEquals("", store.get("never-set-${System.nanoTime()}"))
50 }