返回 CodeWhale
PetSharedIntegrationTest.kt
根目录 / pet / android / src / androidTest / kotlin / codewhale / pet / PetSharedIntegrationTest.kt
1 package codewhale.pet
2
3 import androidx.test.platform.app.InstrumentationRegistry
4 import androidx.test.ext.junit.runners.AndroidJUnit4
5 import org.junit.Test
6 import org.junit.Assume.assumeTrue
7 import org.junit.Assert.*
8 import org.junit.runner.RunWith
9 import org.json.JSONObject
10 import java.io.File
11
12 /** Opt-in real companion check. The fixture descriptor is injected into this
13 * debug app's private files; a secure adb reverse carries loopback traffic. */
14 @RunWith(AndroidJUnit4::class)
15 class PetSharedIntegrationTest {
16 @Test fun twoMobileViewsAttachWithoutAWorldAndInteractionAppearsInOwnerRecording() {
17 val app=InstrumentationRegistry.getInstrumentation().targetContext
18 val descriptor=File(app.filesDir,"shared-test-connection.json")
19 assumeTrue("No explicit local companion fixture",descriptor.isFile)
20 val first=PetSharedClient(descriptor);val second=PetSharedClient(descriptor)
21 val a=first.poll(false,false);val b=second.poll(true,false)
22 assertEquals(first.identity.substringBefore(" ·"),second.identity.substringBefore(" ·"))
23 assertEquals(1960,a.dots.size);assertEquals(1960,b.dots.size)
24 val before=JSONObject(String(first.export())).getJSONArray("interactions").length()
25 second.poll(false,false) // Refresh after export before an interaction's freshness check.
26 second.interact(true)
27 val deadline=android.os.SystemClock.elapsedRealtime()+5_000
28 var after=before
29 while(after==before && android.os.SystemClock.elapsedRealtime()<deadline) {
30 Thread.sleep(50)
31 after=JSONObject(String(first.export())).getJSONArray("interactions").length()
32 }
33 assertEquals(before+1,after)
34 first.detach();Thread.sleep(150)
35 assertTrue(second.poll(false,false).timeMs>b.timeMs)
36 second.detach()
37 }
38 }
39
39 lines Plain Text