@@ -2,7 +2,8 @@ package arcs.android.host
2
2
3
3
import android.app.Application
4
4
import androidx.lifecycle.Lifecycle
5
- import androidx.test.core.app.ActivityScenario
5
+ import androidx.lifecycle.LifecycleOwner
6
+ import androidx.lifecycle.LifecycleRegistry
6
7
import androidx.test.core.app.ApplicationProvider
7
8
import androidx.test.ext.junit.runners.AndroidJUnit4
8
9
import androidx.work.testing.WorkManagerTestInitHelper
@@ -27,8 +28,6 @@ import arcs.sdk.android.storage.service.testutil.TestBindingDelegate
27
28
import com.google.common.truth.Truth.assertThat
28
29
import kotlinx.coroutines.launch
29
30
import kotlinx.coroutines.runBlocking
30
- import kotlinx.coroutines.test.TestCoroutineScope
31
- import kotlinx.coroutines.test.runBlockingTest
32
31
import org.junit.Before
33
32
import org.junit.Test
34
33
import org.junit.runner.RunWith
@@ -38,12 +37,15 @@ typealias Person = TestParticleInternal1
38
37
39
38
@Suppress(" EXPERIMENTAL_API_USAGE" )
40
39
@RunWith(AndroidJUnit4 ::class )
41
- class AndroidEntityHandleManagerTest {
40
+ class AndroidEntityHandleManagerTest : LifecycleOwner {
42
41
private lateinit var app: Application
42
+ private lateinit var lifecycle: LifecycleRegistry
43
+ override fun getLifecycle () = lifecycle
43
44
44
45
val entity1 = Person (" Jason" , 21.0 , false )
45
46
val entity2 = Person (" Jason" , 22.0 , true )
46
47
lateinit var handleHolder: TestParticleHandles
48
+ private lateinit var handleManager: EntityHandleManager
47
49
48
50
private val schema = Schema (
49
51
listOf (SchemaName (" Person" )),
@@ -69,37 +71,24 @@ class AndroidEntityHandleManagerTest {
69
71
@Before
70
72
fun setUp () {
71
73
app = ApplicationProvider .getApplicationContext()
72
- app.setTheme(R .style.Theme_AppCompat );
74
+ lifecycle = LifecycleRegistry (this ).apply {
75
+ setCurrentState(Lifecycle .State .CREATED )
76
+ setCurrentState(Lifecycle .State .STARTED )
77
+ setCurrentState(Lifecycle .State .RESUMED )
78
+ }
73
79
74
80
// Initialize WorkManager for instrumentation tests.
75
81
WorkManagerTestInitHelper .initializeTestWorkManager(app)
76
82
77
83
handleHolder = TestParticleHandles ()
78
- }
79
84
80
- fun handleManagerTest (
81
- block : suspend TestCoroutineScope .(EntityHandleManager ) -> Unit
82
- ) = runBlockingTest {
83
- val scenario = ActivityScenario .launch(TestActivity ::class .java)
84
-
85
- scenario.moveToState(Lifecycle .State .STARTED )
86
-
87
- val activityJob = launch {
88
- scenario.onActivity { activity ->
89
- val hf = AndroidHandleManager (
90
- lifecycle = activity.lifecycle, context = activity,
91
- connectionFactory = DefaultConnectionFactory (
92
- activity, TestBindingDelegate (app), coroutineContext
93
- ),
94
- coroutineContext = coroutineContext
95
- )
96
- runBlocking {
97
- this @runBlockingTest.block(EntityHandleManager (hf))
98
- }
99
- scenario.close()
100
- }
101
- }
102
- activityJob.join()
85
+ handleManager = EntityHandleManager (
86
+ AndroidHandleManager (
87
+ lifecycle = lifecycle,
88
+ context = app,
89
+ connectionFactory = DefaultConnectionFactory (app, TestBindingDelegate (app))
90
+ )
91
+ )
103
92
}
104
93
105
94
private fun expectHandleException (handleName : String , block : () -> Unit ) {
@@ -110,7 +99,7 @@ class AndroidEntityHandleManagerTest {
110
99
}
111
100
112
101
@Test
113
- fun handle_uninitializedThrowsException () = handleManagerTest {
102
+ fun handle_uninitializedThrowsException () = runBlocking {
114
103
expectHandleException(" writeHandle" ) {
115
104
handleHolder.writeHandle
116
105
}
@@ -161,7 +150,7 @@ class AndroidEntityHandleManagerTest {
161
150
)
162
151
163
152
@Test
164
- fun singletonHandle_writeFollowedByReadWithOnUpdate () = handleManagerTest { handleManager ->
153
+ fun singletonHandle_writeFollowedByReadWithOnUpdate () = runBlocking {
165
154
val writeHandle = createSingletonHandle(
166
155
handleManager,
167
156
" writeHandle" ,
@@ -208,7 +197,7 @@ class AndroidEntityHandleManagerTest {
208
197
}
209
198
210
199
@Test
211
- fun setHandle_writeFollowedByReadWithOnUpdate () = handleManagerTest { handleManager ->
200
+ fun setHandle_writeFollowedByReadWithOnUpdate () = runBlocking< Unit > {
212
201
val writeSetHandle = createSetHandle(
213
202
handleManager,
214
203
" writeSetHandle" ,
0 commit comments