@@ -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
@@ -27,10 +29,7 @@ import arcs.sdk.android.storage.service.DefaultConnectionFactory
27
29
import arcs.sdk.android.storage.service.testutil.TestBindingDelegate
28
30
import com.google.common.truth.Truth.assertThat
29
31
import com.nhaarman.mockitokotlin2.mock
30
- import kotlinx.coroutines.launch
31
32
import kotlinx.coroutines.runBlocking
32
- import kotlinx.coroutines.test.TestCoroutineScope
33
- import kotlinx.coroutines.test.runBlockingTest
34
33
import org.junit.Before
35
34
import org.junit.Test
36
35
import org.junit.runner.RunWith
@@ -39,11 +38,16 @@ 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
45
47
private val backingKey = RamDiskStorageKey (" entities" )
46
48
49
+ private lateinit var handleManager: HandleManager
50
+
47
51
val entity1 = RawEntity (
48
52
" entity1" ,
49
53
singletons = mapOf (
@@ -93,61 +97,35 @@ class AndroidHandleManagerTest {
93
97
@Before
94
98
fun setUp () {
95
99
RamDisk .clear()
100
+ lifecycle = LifecycleRegistry (this ).apply {
101
+ setCurrentState(Lifecycle .State .CREATED )
102
+ setCurrentState(Lifecycle .State .STARTED )
103
+ setCurrentState(Lifecycle .State .RESUMED )
104
+ }
96
105
app = ApplicationProvider .getApplicationContext()
97
- app.setTheme(R .style.Theme_AppCompat );
98
106
99
107
// Initialize WorkManager for instrumentation tests.
100
108
WorkManagerTestInitHelper .initializeTestWorkManager(app)
101
- }
102
-
103
- fun handleManagerTest (
104
- block : suspend TestCoroutineScope .(HandleManager ) -> Unit
105
- ) = runBlockingTest {
106
- val scenario = ActivityScenario .launch(TestActivity ::class .java)
107
-
108
- scenario.moveToState(Lifecycle .State .STARTED )
109
-
110
- val activityJob = launch {
111
- scenario.onActivity { activity ->
112
- val hf = AndroidHandleManager (
113
- lifecycle = activity.lifecycle,
114
- context = activity,
115
- connectionFactory = DefaultConnectionFactory (activity, TestBindingDelegate (app)),
116
- coroutineContext = coroutineContext
117
- )
118
- runBlocking {
119
- this @runBlockingTest.block(hf)
120
- }
121
- scenario.close()
122
- }
123
- }
124
-
125
- activityJob.join()
126
- }
127
109
128
- @Test
129
- fun testCreateSingletonHandle () = handleManagerTest { hm ->
130
- val singletonHandle = hm.singletonHandle(singletonKey, schema)
131
- singletonHandle.store(entity1)
132
-
133
- // Now read back from a different handle
134
- val readbackHandle = hm.singletonHandle(singletonKey, schema)
135
- val readBack = readbackHandle.fetch()
136
- assertThat(readBack).isEqualTo(entity1)
110
+ handleManager = AndroidHandleManager (
111
+ lifecycle = lifecycle,
112
+ context = app,
113
+ connectionFactory = DefaultConnectionFactory (app, TestBindingDelegate (app))
114
+ )
137
115
}
138
116
139
117
@Test
140
- fun testDereferencingFromSingletonEntity () = handleManagerTest { hm ->
141
- val singleton1Handle = hm .singletonHandle(singletonKey, schema)
142
- val singleton1Handle2 = hm .singletonHandle(singletonKey, schema)
118
+ fun singleton_dereferenceEntity () = runBlocking {
119
+ val singleton1Handle = handleManager .singletonHandle(singletonKey, schema)
120
+ val singleton1Handle2 = handleManager .singletonHandle(singletonKey, schema)
143
121
singleton1Handle.store(entity1)
144
122
145
123
// Create a second handle for the second entity, so we can store it.
146
- val singleton2Handle = hm .singletonHandle(
124
+ val singleton2Handle = handleManager .singletonHandle(
147
125
ReferenceModeStorageKey (backingKey, RamDiskStorageKey (" entity2" )),
148
126
schema
149
127
)
150
- val singleton2Handle2 = hm .singletonHandle(
128
+ val singleton2Handle2 = handleManager .singletonHandle(
151
129
ReferenceModeStorageKey (backingKey, RamDiskStorageKey (" entity2" )),
152
130
schema
153
131
)
@@ -171,40 +149,28 @@ class AndroidHandleManagerTest {
171
149
}
172
150
173
151
@Test
174
- fun testCreateSetHandle () = handleManagerTest { hm ->
175
- val setHandle = hm.setHandle(setKey, schema)
176
- setHandle.store(entity1)
177
- setHandle.store(entity2)
178
-
179
- // Now read back from a different handle
180
- val secondHandle = hm.setHandle(setKey, schema)
181
- val readBack = secondHandle.fetchAll()
182
- assertThat(readBack).containsExactly(entity1, entity2)
183
- }
184
-
185
- @Test
186
- fun testDereferencingFromSetHandleEntity () = handleManagerTest { hm ->
187
- val setHandle = hm.setHandle(setKey, schema)
152
+ fun set_dereferenceEntity () = runBlocking {
153
+ val setHandle = handleManager.setHandle(setKey, schema)
188
154
setHandle.store(entity1)
189
155
setHandle.store(entity2)
190
156
191
- val secondHandle = hm .setHandle(setKey, schema)
157
+ val secondHandle = handleManager .setHandle(setKey, schema)
192
158
secondHandle.fetchAll().also { assertThat(it).hasSize(2 ) }.forEach { entity ->
193
159
val expectedBestFriend = if (entity.id == " entity1" ) entity2 else entity1
194
160
val actualBestFriend = (entity.singletons[" best_friend" ] as Reference )
195
- .dereference()
161
+ .dereference(coroutineContext )
196
162
assertThat(actualBestFriend).isEqualTo(expectedBestFriend)
197
163
}
198
164
}
199
165
200
166
private fun testMapForKey (key : StorageKey ) = VersionMap (key.toKeyString() to 1 )
201
167
202
168
@Test
203
- fun testSetHandleOnUpdate () = handleManagerTest { hm ->
169
+ fun set_onHandleUpdate () = runBlocking< Unit > {
204
170
val testCallback1 = mock<SetCallbacks <RawEntity >>()
205
171
val testCallback2 = mock<SetCallbacks <RawEntity >>()
206
- val firstHandle = hm .setHandle(setKey, schema, testCallback1)
207
- val secondHandle = hm .setHandle(setKey, schema, testCallback2)
172
+ val firstHandle = handleManager .setHandle(setKey, schema, testCallback1)
173
+ val secondHandle = handleManager .setHandle(setKey, schema, testCallback2)
208
174
209
175
val expectedAdd = CrdtSet .Operation .Add (
210
176
setKey.toKeyString(),
@@ -226,11 +192,11 @@ class AndroidHandleManagerTest {
226
192
}
227
193
228
194
@Test
229
- fun testSingletonHandleOnUpdate () = handleManagerTest { hm ->
195
+ fun singleton_OnHandleUpdate () = runBlocking< Unit > {
230
196
val testCallback1 = mock<SingletonCallbacks <RawEntity >>()
231
197
val testCallback2 = mock<SingletonCallbacks <RawEntity >>()
232
- val firstHandle = hm .singletonHandle(singletonKey, schema, testCallback1)
233
- val secondHandle = hm .singletonHandle(singletonKey, schema, testCallback2)
198
+ val firstHandle = handleManager .singletonHandle(singletonKey, schema, testCallback1)
199
+ val secondHandle = handleManager .singletonHandle(singletonKey, schema, testCallback2)
234
200
secondHandle.store(entity1)
235
201
val expectedAdd = CrdtSingleton .Operation .Update (
236
202
singletonKey.toKeyString(),
@@ -250,18 +216,18 @@ class AndroidHandleManagerTest {
250
216
}
251
217
252
218
@Test
253
- fun testSetSyncOnRegister () = handleManagerTest { hm ->
219
+ fun set_syncOnRegister () = runBlocking< Unit > {
254
220
val testCallback = mock<SetCallbacks <RawEntity >>()
255
- val firstHandle = hm .setHandle(setKey, schema, testCallback)
221
+ val firstHandle = handleManager .setHandle(setKey, schema, testCallback)
256
222
verify(testCallback, times(1 )).onSync(firstHandle)
257
223
firstHandle.fetchAll()
258
224
verify(testCallback, times(1 )).onSync(firstHandle)
259
225
}
260
226
261
227
@Test
262
- fun testSingletonSyncOnRegister () = handleManagerTest { hm ->
228
+ fun singleton_syncOnRegister () = runBlocking< Unit > {
263
229
val testCallback = mock<SingletonCallbacks <RawEntity >>()
264
- val firstHandle = hm .singletonHandle(setKey, schema, testCallback)
230
+ val firstHandle = handleManager .singletonHandle(setKey, schema, testCallback)
265
231
verify(testCallback, times(1 )).onSync(firstHandle)
266
232
firstHandle.fetch()
267
233
verify(testCallback, times(1 )).onSync(firstHandle)
0 commit comments