1+ package com.segment.analytics.liveplugins.kotlin
2+
3+ import androidx.test.ext.junit.runners.AndroidJUnit4
4+ import com.segment.analytics.liveplugins.kotlin.utils.MemorySharedPreferences
5+ import com.segment.analytics.substrata.kotlin.JSScope
6+ import kotlinx.coroutines.ExperimentalCoroutinesApi
7+ import org.junit.Assert.assertEquals
8+ import org.junit.Assert.assertNull
9+ import org.junit.Before
10+ import org.junit.Test
11+ import org.junit.runner.RunWith
12+
13+
14+ @OptIn(ExperimentalCoroutinesApi ::class )
15+ @RunWith(AndroidJUnit4 ::class )
16+ class JSStorageTest {
17+
18+ private lateinit var engine: JSScope
19+ private lateinit var jsStorage: JSStorage
20+ private var exceptionThrown: Throwable ? = null
21+
22+ @Before
23+ fun setUp () {
24+ exceptionThrown = null
25+
26+ engine = JSScope { exception ->
27+ exceptionThrown = exception
28+ }
29+ jsStorage = JSStorage (MemorySharedPreferences (), engine)
30+ // Setup the engine similar to LivePlugins.configureEngine
31+ engine.sync {
32+ export(jsStorage, " Storage" , " storage" )
33+ }
34+ }
35+
36+ @Test
37+ fun testJSStorageWithInt () {
38+ // set from js
39+ var value = engine.await {
40+ evaluate(""" storage.setValue("int", 1)""" )
41+ evaluate(""" storage.getValue("int")""" )
42+ }
43+ assertNull(exceptionThrown)
44+ assertEquals(1 , value)
45+ assertEquals(1 , jsStorage.getValue(" int" ))
46+
47+ // set from native
48+ jsStorage.setValue(" int" , 2 )
49+ value = engine.await {
50+ evaluate(""" storage.getValue("int")""" )
51+ }
52+ assertEquals(2 , value)
53+ assertEquals(2 , jsStorage.getValue(" int" ))
54+ }
55+ }
0 commit comments