@@ -3,6 +3,9 @@ package com.parsely.parselyandroid
3
3
import android.app.Activity
4
4
import androidx.test.core.app.ActivityScenario
5
5
import androidx.test.ext.junit.runners.AndroidJUnit4
6
+ import androidx.test.platform.app.InstrumentationRegistry
7
+ import androidx.test.uiautomator.UiDevice
8
+ import androidx.test.uiautomator.UiSelector
6
9
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
7
10
import com.fasterxml.jackson.annotation.JsonProperty
8
11
import com.fasterxml.jackson.core.type.TypeReference
@@ -12,7 +15,10 @@ import java.io.FileInputStream
12
15
import java.io.ObjectInputStream
13
16
import java.lang.reflect.Field
14
17
import java.nio.file.Path
18
+ import java.util.concurrent.TimeUnit
15
19
import kotlin.io.path.Path
20
+ import kotlin.time.Duration
21
+ import kotlin.time.Duration.Companion.hours
16
22
import kotlin.time.Duration.Companion.milliseconds
17
23
import kotlin.time.Duration.Companion.seconds
18
24
import kotlinx.coroutines.runBlocking
@@ -70,6 +76,43 @@ class FunctionalTests {
70
76
}
71
77
}
72
78
79
+ /* *
80
+ * In this scenario, the consumer application:
81
+ * 1. Goes to the background
82
+ * 2. Is re-launched
83
+ * This pattern occurs twice, which allows us to confirm the following assertions:
84
+ * 1. The event request is triggered when the consumer application is moved to the background
85
+ * 2. If the consumer application is sent to the background again within a short interval,
86
+ * the request is not duplicated.
87
+ */
88
+ @Test
89
+ fun appSendsEventsWhenMovedToBackgroundAndDoesntSendDuplicatedRequestWhenItsMovedToBackgroundAgainQuickly () {
90
+ val device = UiDevice .getInstance(InstrumentationRegistry .getInstrumentation())
91
+ ActivityScenario .launch(SampleActivity ::class .java).use { scenario ->
92
+ scenario.onActivity { activity: Activity ->
93
+ beforeEach(activity)
94
+ server.enqueue(MockResponse ().setResponseCode(200 ))
95
+ server.enqueue(MockResponse ().setResponseCode(200 ))
96
+ parselyTracker = initializeTracker(activity, flushInterval = 1 .hours)
97
+
98
+ repeat(20 ) {
99
+ parselyTracker.trackPageview(" url" , null , null , null )
100
+ }
101
+ }
102
+
103
+ device.pressHome()
104
+ device.pressRecentApps()
105
+ device.findObject(UiSelector ().descriptionContains(" com.parsely" )).click()
106
+ device.pressHome()
107
+
108
+ val firstRequest = server.takeRequest(10000 , TimeUnit .MILLISECONDS )?.toMap()
109
+ val secondRequest = server.takeRequest(10000 , TimeUnit .MILLISECONDS )?.toMap()
110
+
111
+ assertThat(firstRequest!! [" events" ]).hasSize(20 )
112
+ assertThat(secondRequest).isNull()
113
+ }
114
+ }
115
+
73
116
private fun RecordedRequest.toMap (): Map <String , List <Event >> {
74
117
val listType: TypeReference <Map <String , List <Event >>> =
75
118
object : TypeReference <Map <String , List <Event >>>() {}
@@ -90,7 +133,10 @@ class FunctionalTests {
90
133
}
91
134
}
92
135
93
- private fun initializeTracker (activity : Activity ): ParselyTracker {
136
+ private fun initializeTracker (
137
+ activity : Activity ,
138
+ flushInterval : Duration = defaultFlushInterval
139
+ ): ParselyTracker {
94
140
return ParselyTracker .sharedInstance(
95
141
siteId, flushInterval.inWholeSeconds.toInt(), activity.application
96
142
).apply {
@@ -103,7 +149,7 @@ class FunctionalTests {
103
149
private companion object {
104
150
const val siteId = " 123"
105
151
const val localStorageFileName = " parsely-events.ser"
106
- val flushInterval = 10 .seconds
152
+ val defaultFlushInterval = 10 .seconds
107
153
}
108
154
109
155
class SampleActivity : Activity ()
0 commit comments