@@ -23,10 +23,10 @@ import com.snowplowanalytics.core.tracker.TrackerWebViewInterfaceV2
23
23
import com.snowplowanalytics.snowplow.Snowplow.createTracker
24
24
import com.snowplowanalytics.snowplow.Snowplow.removeAllTrackers
25
25
import com.snowplowanalytics.snowplow.configuration.NetworkConfiguration
26
- import com.snowplowanalytics.snowplow.configuration.PluginConfiguration
27
26
import com.snowplowanalytics.snowplow.configuration.TrackerConfiguration
28
27
import com.snowplowanalytics.snowplow.controller.TrackerController
29
28
import com.snowplowanalytics.snowplow.network.HttpMethod
29
+ import com.snowplowanalytics.snowplow.util.EventSink
30
30
import org.json.JSONException
31
31
import org.json.JSONObject
32
32
import org.junit.After
@@ -38,26 +38,29 @@ import org.junit.runner.RunWith
38
38
@RunWith(AndroidJUnit4 ::class )
39
39
class TrackerWebViewInterfaceV2Test {
40
40
private var webInterface: TrackerWebViewInterfaceV2 ? = null
41
- private var networkConnection = MockNetworkConnection (HttpMethod .GET , 200 )
42
- private var tracker: TrackerController ? = null
43
41
44
42
@Before
45
43
fun setUp () {
46
44
webInterface = TrackerWebViewInterfaceV2 ()
47
- tracker = createTracker()
48
45
}
49
46
50
47
@After
51
48
fun tearDown () {
52
- tracker?.pause()
53
- tracker = null
54
49
removeAllTrackers()
55
50
Executor .shutdown()
56
51
}
57
52
58
53
@Test
59
54
@Throws(JSONException ::class , InterruptedException ::class )
60
55
fun tracksEventWithAllOptions () {
56
+ val networkConnection = MockNetworkConnection (HttpMethod .GET , 200 )
57
+ createTracker(
58
+ context,
59
+ " ns${Math .random()} " ,
60
+ NetworkConfiguration (networkConnection),
61
+ TrackerConfiguration (" appId" ).base64encoding(false )
62
+ )
63
+
61
64
val data = " {\" schema\" :\" iglu:etc\" ,\" data\" :{\" key\" :\" val\" }}"
62
65
val atomic = " {\" eventName\" :\" ue\" ,\" trackerVersion\" :\" webview\" ," +
63
66
" \" useragent\" :\" Chrome\" ,\" pageUrl\" :\" http://snowplow.com\" ," +
@@ -71,8 +74,11 @@ class TrackerWebViewInterfaceV2Test {
71
74
atomicProperties = atomic
72
75
)
73
76
74
- Thread .sleep(200 )
75
- waitForEvents(networkConnection, 1 )
77
+ var i = 0
78
+ while (i < 10 && networkConnection.countRequests() == 0 ) {
79
+ Thread .sleep(1000 )
80
+ i++
81
+ }
76
82
77
83
assertEquals(1 , networkConnection.countRequests())
78
84
@@ -104,118 +110,86 @@ class TrackerWebViewInterfaceV2Test {
104
110
@Test
105
111
@Throws(JSONException ::class , InterruptedException ::class )
106
112
fun tracksEventWithCorrectTracker () {
107
- // create the second tracker
108
- val networkConnection2 = MockNetworkConnection (HttpMethod .GET , 200 )
109
- createTracker(
110
- context,
111
- namespace = " ns2" ,
112
- NetworkConfiguration (networkConnection2),
113
- TrackerConfiguration (" appId" )
114
- )
113
+ val eventSink1 = EventSink ()
114
+ val eventSink2 = EventSink ()
115
+
116
+ createTracker(" ns1" , eventSink1)
117
+ createTracker(" ns2" , eventSink2)
115
118
Thread .sleep(200 )
116
119
117
120
// track an event using the second tracker
118
121
webInterface!! .trackWebViewEvent(
119
- atomicProperties = " {\" eventName \" : \" pv \" , \" trackerVersion \" : \" webview \" }" ,
122
+ atomicProperties = " {}" ,
120
123
trackers = arrayOf(" ns2" )
121
124
)
122
125
Thread .sleep(200 )
123
- waitForEvents(networkConnection2, 1 )
124
-
125
- assertEquals(0 , networkConnection.countRequests())
126
- assertEquals(1 , networkConnection2.countRequests())
127
-
128
- assertEquals(" pv" , networkConnection2.allRequests[0 ].payload.map[Parameters .EVENT ])
126
+
127
+ assertEquals(0 , eventSink1.trackedEvents.size)
128
+ assertEquals(1 , eventSink2.trackedEvents.size)
129
129
130
130
// tracks using default tracker if not specified
131
131
webInterface!! .trackWebViewEvent(atomicProperties = " {}" )
132
132
Thread .sleep(200 )
133
- waitForEvents(networkConnection, 1 )
134
133
135
- assertEquals(1 , networkConnection.countRequests() )
136
- assertEquals(1 , networkConnection2.countRequests() )
134
+ assertEquals(1 , eventSink1.trackedEvents.size )
135
+ assertEquals(1 , eventSink2.trackedEvents.size )
137
136
}
138
137
139
138
@Test
140
139
@Throws(JSONException ::class , InterruptedException ::class )
141
140
fun tracksEventWithEntity () {
141
+ val namespace = " ns" + Math .random().toString()
142
+ val eventSink = EventSink ()
143
+ createTracker(namespace, eventSink)
144
+
142
145
webInterface!! .trackWebViewEvent(
143
146
atomicProperties = " {}" ,
144
- entities = " [{\" schema\" :\" iglu:com.example/etc\" ,\" data\" :{\" key\" :\" val\" }}]"
147
+ entities = " [{\" schema\" :\" iglu:com.example/etc\" ,\" data\" :{\" key\" :\" val\" }}]" ,
148
+ trackers = arrayOf(namespace)
145
149
)
146
150
Thread .sleep(200 )
147
- waitForEvents(networkConnection, 1 )
148
-
149
- assertEquals(1 , networkConnection.countRequests())
151
+ val events = eventSink.trackedEvents
152
+ assertEquals(1 , events.size)
150
153
151
- val relevantEntities = ArrayList <JSONObject >()
152
- val allEntities = JSONObject (networkConnection.allRequests[0 ].payload.map[Parameters .CONTEXT ] as String )
153
- .getJSONArray(" data" )
154
- for (i in 0 until allEntities.length()) {
155
- if (allEntities.getJSONObject(i).getString(" schema" ) == " iglu:com.example/etc" ) {
156
- relevantEntities.add(allEntities.getJSONObject(i).getJSONObject(" data" ))
157
- }
158
- }
154
+ val relevantEntities = events[0 ].entities.filter { it.map[" schema" ] == " iglu:com.example/etc" }
159
155
assertEquals(1 , relevantEntities.size)
160
- assertEquals(" val" , relevantEntities[0 ].get(" key" ) as ? String )
156
+
157
+ val entityData = relevantEntities[0 ].map[" data" ] as HashMap <* , * >?
158
+ assertEquals(" val" , entityData?.get(" key" ))
161
159
}
162
160
163
161
@Test
164
162
@Throws(JSONException ::class , InterruptedException ::class )
165
163
fun addsEventNameAndSchemaForInspection () {
166
- val trackedEvents: MutableList <InspectableEvent > = mutableListOf ()
167
-
168
164
val namespace = " ns" + Math .random().toString()
169
- val networkConfig = NetworkConfiguration (MockNetworkConnection (HttpMethod .POST , 200 ))
170
-
171
- val plugin = PluginConfiguration (" plugin" )
172
- plugin.afterTrack { trackedEvents.add(it) }
173
-
174
- createTracker(
175
- context,
176
- namespace,
177
- networkConfig,
178
- TrackerConfiguration (" appId" ),
179
- plugin
180
- )
165
+ val eventSink = EventSink ()
166
+ createTracker(namespace, eventSink)
181
167
182
168
webInterface!! .trackWebViewEvent(
183
- atomicProperties = " {\" eventName\" :\" se\" , \" trackerVersion \" : \" webview \" }" ,
169
+ atomicProperties = " {\" eventName\" :\" se\" }" ,
184
170
selfDescribingEventData = " {\" schema\" :\" iglu:etc\" ,\" data\" :{\" key\" :\" val\" }}" ,
185
171
trackers = arrayOf(namespace)
186
172
)
187
173
188
174
Thread .sleep(200 )
189
- assertEquals(1 , trackedEvents.size)
190
- assertEquals(" se" , trackedEvents[0 ].name)
191
- assertEquals(" iglu:etc" , trackedEvents[0 ].schema)
175
+ val events = eventSink.trackedEvents
176
+
177
+ assertEquals(1 , events.size)
178
+ assertEquals(" se" , events[0 ].name)
179
+ assertEquals(" iglu:etc" , events[0 ].schema)
192
180
}
193
181
194
182
// --- PRIVATE
195
183
private val context: Context
196
184
get() = InstrumentationRegistry .getInstrumentation().targetContext
197
185
198
- private fun createTracker (): TrackerController {
199
- val trackerConfig = TrackerConfiguration (" appId" )
200
- .installAutotracking(false )
201
- .lifecycleAutotracking(false )
202
- .platformContext(false )
203
- .base64encoding(false )
204
-
186
+ private fun createTracker (namespace : String , eventSink : EventSink ): TrackerController {
187
+ val networkConfig = NetworkConfiguration (MockNetworkConnection (HttpMethod .POST , 200 ))
205
188
return createTracker(
206
189
context,
207
- " ns ${ Math .random()} " ,
208
- NetworkConfiguration (networkConnection) ,
209
- trackerConfig
190
+ namespace = namespace ,
191
+ network = networkConfig ,
192
+ configurations = arrayOf(eventSink)
210
193
)
211
194
}
212
-
213
- @Throws(Exception ::class )
214
- fun waitForEvents (networkConnection : MockNetworkConnection , eventsExpected : Int ) {
215
- var i = 0
216
- while (i < 10 && networkConnection.countRequests() == eventsExpected - 1 ) {
217
- Thread .sleep(1000 )
218
- i++
219
- }
220
- }
221
195
}
0 commit comments