Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mscwilson committed Nov 26, 2024
1 parent 6277555 commit 5bb8661
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ class TrackerWebViewInterfaceV2Test {
pageUrl = "http://snowplow.com",
pageTitle = "Snowplow",
referrer = "http://google.com",
ping_xoffset_min = 10,
ping_xoffset_max = 20,
ping_yoffset_min = 30,
ping_yoffset_max = 40
pingXOffsetMin = 10,
pingXOffsetMax = 20,
pingYOffsetMin = 30,
pingYOffsetMax = 40
)

Thread.sleep(200)
Expand Down Expand Up @@ -137,6 +137,50 @@ class TrackerWebViewInterfaceV2Test {
assertEquals(10.0, payload[Parameters.SE_VALUE])
}

@Test
@Throws(JSONException::class, InterruptedException::class)
fun tracksEventWithCorrectTracker() {
// create the second tracker
val trackedEvents2: MutableList<InspectableEvent> = mutableListOf()
val networkConfig = NetworkConfiguration(MockNetworkConnection(HttpMethod.POST, 200))
val plugin2 = PluginConfiguration("plugin2")
plugin2.afterTrack {
trackedEvents2.add(it)
}
createTracker(
context,
namespace = "ns2",
network = networkConfig,
TrackerConfiguration("appId"),
plugin2
)

// track an event using the second tracker
webInterface!!.trackWebViewEvent(
eventName = "pv",
trackerVersion = "webview",
useragent = "Chrome",
pageUrl = "http://snowplow.com",
trackers = arrayOf("ns2")
)
Thread.sleep(200)

assertEquals(0, trackedEvents.size)
assertEquals(1, trackedEvents2.size)

// track an event using default tracker if not specified
webInterface!!.trackWebViewEvent(
eventName = "pp",
trackerVersion = "webview",
useragent = "Chrome",
pageUrl = "http://snowplow.com",
)
Thread.sleep(200)

assertEquals(1, trackedEvents.size)
assertEquals(1, trackedEvents2.size)
}


// --- PRIVATE
private val context: Context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package com.snowplowanalytics.core.event

import com.snowplowanalytics.core.constants.Parameters
import com.snowplowanalytics.core.constants.TrackerConstants
import com.snowplowanalytics.snowplow.event.AbstractPrimitive

/**
Expand All @@ -32,10 +31,10 @@ class WebViewReader(
val label: String? = null,
val property: String? = null,
val value: Double? = null,
val ping_xoffset_min: Int? = null,
val ping_xoffset_max: Int? = null,
val ping_yoffset_min: Int? = null,
val ping_yoffset_max: Int? = null
val pingXOffsetMin: Int? = null,
val pingXOffsetMax: Int? = null,
val pingYOffsetMin: Int? = null,
val pingYOffsetMax: Int? = null
) : AbstractPrimitive() {


Expand All @@ -54,10 +53,10 @@ class WebViewReader(
if (label != null) payload[Parameters.SE_LABEL] = label
if (property != null) payload[Parameters.SE_PROPERTY] = property
if (value != null) payload[Parameters.SE_VALUE] = value
if (ping_xoffset_min != null) payload[Parameters.PING_XOFFSET_MIN] = ping_xoffset_min
if (ping_xoffset_max != null) payload[Parameters.PING_XOFFSET_MAX] = ping_xoffset_max
if (ping_yoffset_min != null) payload[Parameters.PING_YOFFSET_MIN] = ping_yoffset_min
if (ping_yoffset_max != null) payload[Parameters.PING_YOFFSET_MAX] = ping_yoffset_max
if (pingXOffsetMin != null) payload[Parameters.PING_XOFFSET_MIN] = pingXOffsetMin
if (pingXOffsetMax != null) payload[Parameters.PING_XOFFSET_MAX] = pingXOffsetMax
if (pingYOffsetMin != null) payload[Parameters.PING_YOFFSET_MIN] = pingYOffsetMin
if (pingYOffsetMax != null) payload[Parameters.PING_YOFFSET_MAX] = pingYOffsetMax
return payload
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class TrackerWebViewInterfaceV2 {
label: String? = null,
property: String? = null,
value: Double? = null,
ping_xoffset_min: Int? = null,
ping_xoffset_max: Int? = null,
ping_yoffset_min: Int? = null,
ping_yoffset_max: Int? = null,
pingXOffsetMin: Int? = null,
pingXOffsetMax: Int? = null,
pingYOffsetMin: Int? = null,
pingYOffsetMax: Int? = null,
entities: String? = null,
trackers: Array<String>? = null
) {
Expand All @@ -62,75 +62,14 @@ class TrackerWebViewInterfaceV2 {
label,
property,
value,
ping_xoffset_min,
ping_xoffset_max,
ping_yoffset_min,
ping_yoffset_max
pingXOffsetMin,
pingXOffsetMax,
pingYOffsetMin,
pingYOffsetMax
)
trackEvent(event, entities, trackers)
}



/**
* Track a screen view event from the Web view
* @param name The name of the screen viewed
* @param id The id (UUID v4) of screen that was viewed
* @param type The type of screen that was viewed
* @param previousName The name of the previous screen that was viewed
* @param previousId The id (UUID v4) of the previous screen that was viewed
* @param previousType The type of screen that was viewed
* @param transitionType The type of transition that led to the screen being viewed
* @param context Optional JSON string with a list of context entities
* @param trackers Optional list of tracker namespaces to track with
* @throws JSONException In case of JSON parsing failures
*/
@JavascriptInterface
@Throws(JSONException::class)
fun trackScreenView(
name: String,
id: String,
type: String?,
previousName: String?,
previousId: String?,
previousType: String?,
transitionType: String?,
context: String?,
trackers: Array<String>?
) {
val event = ScreenView(name, UUID.fromString(id))
.type(type)
.previousId(previousId)
.previousName(previousName)
.previousType(previousType)
.transitionType(transitionType)
trackEvent(event, context, trackers)
}

/**
* Track a page view event from the Web view
* @param pageUrl Page URL
* @param pageTitle Page title
* @param referrer Referrer URL
* @param context Optional JSON string with a list of context entities
* @param trackers Optional list of tracker namespaces to track with
* @throws JSONException In case of JSON parsing failures
*/
@JavascriptInterface
@Throws(JSONException::class)
fun trackPageView(
pageUrl: String,
pageTitle: String?,
referrer: String?,
context: String?,
trackers: Array<String>?
) {
val event = PageView(pageUrl)
.pageTitle(pageTitle)
.referrer(referrer)
trackEvent(event, context, trackers)
}

@Throws(JSONException::class)
private fun trackEvent(event: AbstractEvent, context: String?, trackers: Array<String>?) {
if (context != null) {
Expand All @@ -139,7 +78,7 @@ class TrackerWebViewInterfaceV2 {
event.entities(contextEntities)
}
}
if (trackers == null || trackers.isEmpty()) {
if (trackers.isNullOrEmpty()) {
val tracker = defaultTracker
if (tracker != null) {
tracker.track(event)
Expand Down

0 comments on commit 5bb8661

Please sign in to comment.