@@ -2,6 +2,8 @@ package arcs.android.storage.handle
2
2
3
3
import android.app.Application
4
4
import androidx.lifecycle.Lifecycle
5
+ import androidx.lifecycle.LifecycleOwner
6
+ import androidx.lifecycle.LifecycleRegistry
5
7
import androidx.test.core.app.ActivityScenario
6
8
import androidx.test.core.app.ApplicationProvider
7
9
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -26,10 +28,7 @@ import arcs.sdk.android.storage.service.DefaultConnectionFactory
26
28
import arcs.sdk.android.storage.service.testutil.TestBindingDelegate
27
29
import com.google.common.truth.Truth.assertThat
28
30
import com.nhaarman.mockitokotlin2.mock
29
- import kotlinx.coroutines.launch
30
31
import kotlinx.coroutines.runBlocking
31
- import kotlinx.coroutines.test.TestCoroutineScope
32
- import kotlinx.coroutines.test.runBlockingTest
33
32
import org.junit.Before
34
33
import org.junit.Test
35
34
import org.junit.runner.RunWith
@@ -39,27 +38,32 @@ import org.mockito.Mockito.verify
39
38
40
39
@Suppress(" EXPERIMENTAL_API_USAGE" )
41
40
@RunWith(AndroidJUnit4 ::class )
42
- class AndroidHandleManagerTest {
41
+ class AndroidHandleManagerTest : LifecycleOwner {
42
+ private lateinit var lifecycle: LifecycleRegistry
43
+ override fun getLifecycle () = lifecycle
44
+
43
45
private lateinit var app: Application
44
46
47
+ private lateinit var handleManager: HandleManager
48
+
45
49
val entity1 = RawEntity (
46
50
" entity1" ,
47
- singletons= mapOf (
51
+ singletons = mapOf (
48
52
" name" to " Jason" .toReferencable(),
49
53
" age" to 21 .toReferencable(),
50
54
" is_cool" to false .toReferencable()
51
55
),
52
- collections= emptyMap()
56
+ collections = emptyMap()
53
57
)
54
58
55
59
val entity2 = RawEntity (
56
60
" entity2" ,
57
- singletons= mapOf (
61
+ singletons = mapOf (
58
62
" name" to " Jason" .toReferencable(),
59
63
" age" to 22 .toReferencable(),
60
64
" is_cool" to true .toReferencable()
61
65
),
62
- collections= emptyMap()
66
+ collections = emptyMap()
63
67
)
64
68
65
69
private val schema = Schema (
@@ -88,140 +92,114 @@ class AndroidHandleManagerTest {
88
92
@Before
89
93
fun setUp () {
90
94
RamDisk .clear()
95
+ lifecycle = LifecycleRegistry (this ).apply {
96
+ setCurrentState(Lifecycle .State .CREATED )
97
+ setCurrentState(Lifecycle .State .STARTED )
98
+ setCurrentState(Lifecycle .State .RESUMED )
99
+ }
91
100
app = ApplicationProvider .getApplicationContext()
92
- app.setTheme(R .style.Theme_AppCompat );
93
101
94
102
// Initialize WorkManager for instrumentation tests.
95
103
WorkManagerTestInitHelper .initializeTestWorkManager(app)
96
- }
97
104
98
- fun handleManagerTest (
99
- block : suspend TestCoroutineScope .(HandleManager ) -> Unit
100
- ) = runBlockingTest {
101
- val scenario = ActivityScenario .launch(TestActivity ::class .java)
102
-
103
- scenario.moveToState(Lifecycle .State .STARTED )
104
-
105
- val activityJob = launch {
106
- scenario.onActivity { activity ->
107
- val hf = AndroidHandleManager (
108
- lifecycle = activity.lifecycle,
109
- context = activity,
110
- connectionFactory = DefaultConnectionFactory (activity, TestBindingDelegate (app)),
111
- coroutineContext = coroutineContext
112
- )
113
- runBlocking {
114
- this @runBlockingTest.block(hf)
115
- }
116
- scenario.close()
117
- }
118
- }
119
-
120
- activityJob.join()
105
+ handleManager = AndroidHandleManager (
106
+ lifecycle = lifecycle,
107
+ context = app,
108
+ connectionFactory = DefaultConnectionFactory (app, TestBindingDelegate (app))
109
+ )
121
110
}
122
111
123
112
@Test
124
- fun testCreateSingletonHandle () = runBlockingTest {
125
- handleManagerTest { hm ->
126
- val singletonHandle = hm.singletonHandle(singletonKey, schema)
127
- singletonHandle.store(entity1)
128
-
129
- // Now read back from a different handle
130
- val readbackHandle = hm.singletonHandle(singletonKey, schema)
131
- val readBack = readbackHandle.fetch()
132
- assertThat(readBack).isEqualTo(entity1)
133
- }
113
+ fun singleton_writeAndReadback () = runBlocking {
114
+ val singletonHandle = handleManager.singletonHandle(singletonKey, schema)
115
+ singletonHandle.store(entity1)
116
+
117
+ // Now read back from a different handle
118
+ val readbackHandle = handleManager.singletonHandle(singletonKey, schema)
119
+ val readBack = readbackHandle.fetch()
120
+ assertThat(readBack).isEqualTo(entity1)
121
+
134
122
}
135
123
136
124
@Test
137
- fun testCreateSetHandle () = runBlockingTest {
138
- handleManagerTest { hm ->
139
- val setHandle = hm.setHandle(setKey, schema)
140
- setHandle.store(entity1)
141
- setHandle.store(entity2)
142
-
143
- // Now read back from a different handle
144
- val secondHandle = hm.setHandle(setKey, schema)
145
- val readBack = secondHandle.fetchAll()
146
- assertThat(readBack).containsExactly(entity1, entity2)
147
- }
125
+ fun set_writeAndReadback () = runBlocking<Unit > {
126
+ val setHandle = handleManager.setHandle(setKey, schema)
127
+ setHandle.store(entity1)
128
+ setHandle.store(entity2)
129
+
130
+ // Now read back from a different handle
131
+ val secondHandle = handleManager.setHandle(setKey, schema)
132
+ val readBack = secondHandle.fetchAll()
133
+ assertThat(readBack).containsExactly(entity1, entity2)
148
134
}
149
135
150
136
private fun testMapForKey (key : StorageKey ) = VersionMap (key.toKeyString() to 1 )
151
137
152
138
@Test
153
- fun testSetHandleOnUpdate () = runBlockingTest {
154
- handleManagerTest { hm ->
155
- val testCallback1 = mock<SetCallbacks <RawEntity >>()
156
- val testCallback2 = mock<SetCallbacks <RawEntity >>()
157
- val firstHandle = hm.setHandle(setKey, schema, testCallback1)
158
- val secondHandle = hm.setHandle(setKey, schema, testCallback2)
159
-
160
- val expectedAdd = CrdtSet .Operation .Add (
161
- setKey.toKeyString(),
162
- testMapForKey(setKey),
163
- entity1
164
- )
165
- secondHandle.store(entity1)
166
- verify(testCallback1, times(1 )).onUpdate(firstHandle, expectedAdd)
167
- verify(testCallback2, times(1 )).onUpdate(secondHandle, expectedAdd)
168
-
169
- firstHandle.remove(entity1)
170
- val expectedRemove = CrdtSet .Operation .Remove (
171
- setKey.toKeyString(),
172
- testMapForKey(setKey),
173
- entity1
174
- )
175
- verify(testCallback1, times(1 )).onUpdate(firstHandle, expectedRemove)
176
- verify(testCallback2, times(1 )).onUpdate(secondHandle, expectedRemove)
177
- }
139
+ fun set_onHandleUpdate () = runBlocking<Unit > {
140
+ val testCallback1 = mock<SetCallbacks <RawEntity >>()
141
+ val testCallback2 = mock<SetCallbacks <RawEntity >>()
142
+ val firstHandle = handleManager.setHandle(setKey, schema, testCallback1)
143
+ val secondHandle = handleManager.setHandle(setKey, schema, testCallback2)
144
+
145
+ val expectedAdd = CrdtSet .Operation .Add (
146
+ setKey.toKeyString(),
147
+ testMapForKey(setKey),
148
+ entity1
149
+ )
150
+ secondHandle.store(entity1)
151
+ verify(testCallback1, times(1 )).onUpdate(firstHandle, expectedAdd)
152
+ verify(testCallback2, times(1 )).onUpdate(secondHandle, expectedAdd)
153
+
154
+ firstHandle.remove(entity1)
155
+ val expectedRemove = CrdtSet .Operation .Remove (
156
+ setKey.toKeyString(),
157
+ testMapForKey(setKey),
158
+ entity1
159
+ )
160
+ verify(testCallback1, times(1 )).onUpdate(firstHandle, expectedRemove)
161
+ verify(testCallback2, times(1 )).onUpdate(secondHandle, expectedRemove)
178
162
}
179
163
180
164
@Test
181
- fun testSingletonHandleOnUpdate () = runBlockingTest {
182
- handleManagerTest { hm ->
183
- val testCallback1 = mock<SingletonCallbacks <RawEntity >>()
184
- val testCallback2 = mock<SingletonCallbacks <RawEntity >>()
185
- val firstHandle = hm.singletonHandle(singletonKey, schema, testCallback1)
186
- val secondHandle = hm.singletonHandle(singletonKey, schema, testCallback2)
187
- secondHandle.store(entity1)
188
- val expectedAdd = CrdtSingleton .Operation .Update (
189
- singletonKey.toKeyString(),
190
- testMapForKey(singletonKey),
191
- entity1
192
- )
193
- verify(testCallback1, times(1 )).onUpdate(firstHandle, expectedAdd)
194
- verify(testCallback2, times(1 )).onUpdate(secondHandle, expectedAdd)
195
- firstHandle.clear()
196
-
197
- val expectedRemove = CrdtSingleton .Operation .Clear <RawEntity >(
198
- singletonKey.toKeyString(),
199
- testMapForKey(singletonKey)
200
- )
201
- verify(testCallback1, times(1 )).onUpdate(firstHandle, expectedRemove)
202
- verify(testCallback2, times(1 )).onUpdate(secondHandle, expectedRemove)
203
- }
165
+ fun singleton_OnHandleUpdate () = runBlocking<Unit > {
166
+ val testCallback1 = mock<SingletonCallbacks <RawEntity >>()
167
+ val testCallback2 = mock<SingletonCallbacks <RawEntity >>()
168
+ val firstHandle = handleManager.singletonHandle(singletonKey, schema, testCallback1)
169
+ val secondHandle = handleManager.singletonHandle(singletonKey, schema, testCallback2)
170
+ secondHandle.store(entity1)
171
+ val expectedAdd = CrdtSingleton .Operation .Update (
172
+ singletonKey.toKeyString(),
173
+ testMapForKey(singletonKey),
174
+ entity1
175
+ )
176
+ verify(testCallback1, times(1 )).onUpdate(firstHandle, expectedAdd)
177
+ verify(testCallback2, times(1 )).onUpdate(secondHandle, expectedAdd)
178
+ firstHandle.clear()
179
+
180
+ val expectedRemove = CrdtSingleton .Operation .Clear <RawEntity >(
181
+ singletonKey.toKeyString(),
182
+ testMapForKey(singletonKey)
183
+ )
184
+ verify(testCallback1, times(1 )).onUpdate(firstHandle, expectedRemove)
185
+ verify(testCallback2, times(1 )).onUpdate(secondHandle, expectedRemove)
204
186
}
205
187
206
188
@Test
207
- fun testSetSyncOnRegister () = runBlockingTest {
208
- handleManagerTest { hm ->
209
- val testCallback = mock<SetCallbacks <RawEntity >>()
210
- val firstHandle = hm.setHandle(setKey, schema, testCallback)
211
- verify(testCallback, times(1 )).onSync(firstHandle)
212
- firstHandle.fetchAll()
213
- verify(testCallback, times(1 )).onSync(firstHandle)
214
- }
189
+ fun set_syncOnRegister () = runBlocking<Unit > {
190
+ val testCallback = mock<SetCallbacks <RawEntity >>()
191
+ val firstHandle = handleManager.setHandle(setKey, schema, testCallback)
192
+ verify(testCallback, times(1 )).onSync(firstHandle)
193
+ firstHandle.fetchAll()
194
+ verify(testCallback, times(1 )).onSync(firstHandle)
215
195
}
216
196
217
197
@Test
218
- fun testSingletonSyncOnRegister () = runBlockingTest {
219
- handleManagerTest { hm ->
220
- val testCallback = mock<SingletonCallbacks <RawEntity >>()
221
- val firstHandle = hm.singletonHandle(setKey, schema, testCallback)
222
- verify(testCallback, times(1 )).onSync(firstHandle)
223
- firstHandle.fetch()
224
- verify(testCallback, times(1 )).onSync(firstHandle)
225
- }
198
+ fun testSingletonSyncOnRegister () = runBlocking<Unit > {
199
+ val testCallback = mock<SingletonCallbacks <RawEntity >>()
200
+ val firstHandle = handleManager.singletonHandle(setKey, schema, testCallback)
201
+ verify(testCallback, times(1 )).onSync(firstHandle)
202
+ firstHandle.fetch()
203
+ verify(testCallback, times(1 )).onSync(firstHandle)
226
204
}
227
205
}
0 commit comments