-
Notifications
You must be signed in to change notification settings - Fork 33
Add deep link tests #258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add deep link tests #258
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package com.segment.analytics.kotlin.android.utilities | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import com.segment.analytics.kotlin.core.Analytics | ||
import kotlinx.serialization.json.JsonObject | ||
import kotlinx.serialization.json.buildJsonObject | ||
import kotlinx.serialization.json.put | ||
|
||
|
@@ -13,13 +15,32 @@ class DeepLinkUtils(val analytics: Analytics) { | |
return | ||
} | ||
|
||
val properties = buildJsonObject { | ||
val properties = extractLinkProperties(referrer, intent.data) | ||
analytics.track("Deep Link Opened", properties) | ||
} | ||
|
||
/** | ||
* Builds a JsonObject with the parameters of a given Uri. | ||
* | ||
* Note: The Uri must be hierarchical (myUri.isHierarchical == true) for parameters to be | ||
* extracted. | ||
* | ||
* Example hierarchical Uri: http://example.com/ | ||
* Example non-hierarchical Uri: mailto:[email protected] | ||
* | ||
* Note: we return the given Uri as a property named: "url" since this is what is expected | ||
* upstream. | ||
*/ | ||
fun extractLinkProperties( | ||
referrer: String?, | ||
uri: Uri? | ||
): JsonObject { | ||
val properties = buildJsonObject { | ||
referrer?.let { | ||
put("referrer", it) | ||
} | ||
|
||
intent.data?.let { uri -> | ||
if (uri != null) { | ||
if (uri.isHierarchical) { | ||
for (parameter in uri.queryParameterNames) { | ||
val value = uri.getQueryParameter(parameter) | ||
|
@@ -31,6 +52,7 @@ class DeepLinkUtils(val analytics: Analytics) { | |
put("url", uri.toString()) | ||
} | ||
} | ||
analytics.track("Deep Link Opened", properties) | ||
|
||
return properties | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
android/src/test/java/com/segment/analytics/kotlin/android/utilities/DeepLinkUtilsTests.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.segment.analytics.kotlin.android.utilities | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.net.Uri | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.segment.analytics.kotlin.android.AndroidStorageProvider | ||
import com.segment.analytics.kotlin.android.plugins.getUniqueID | ||
import com.segment.analytics.kotlin.android.utils.MemorySharedPreferences | ||
import com.segment.analytics.kotlin.android.utils.testAnalytics | ||
import com.segment.analytics.kotlin.core.Analytics | ||
import com.segment.analytics.kotlin.core.Configuration | ||
import com.segment.analytics.kotlin.core.emptyJsonObject | ||
import io.mockk.every | ||
import io.mockk.mockkStatic | ||
import io.mockk.spyk | ||
import kotlinx.coroutines.test.TestScope | ||
import kotlinx.coroutines.test.UnconfinedTestDispatcher | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.buildJsonObject | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import org.robolectric.annotation.Config | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
@Config(manifest = Config.NONE) | ||
class DeepLinkUtilsTests { | ||
lateinit var appContext: Context | ||
lateinit var analytics: Analytics | ||
lateinit var deepLinkUtils: DeepLinkUtils | ||
private val testDispatcher = UnconfinedTestDispatcher() | ||
private val testScope = TestScope(testDispatcher) | ||
|
||
|
||
@Before | ||
fun setup() { | ||
appContext = spyk(InstrumentationRegistry.getInstrumentation().targetContext) | ||
val sharedPreferences: SharedPreferences = MemorySharedPreferences() | ||
every { appContext.getSharedPreferences(any(), any()) } returns sharedPreferences | ||
mockkStatic("com.segment.analytics.kotlin.android.plugins.AndroidContextPluginKt") | ||
every { getUniqueID() } returns "unknown" | ||
|
||
analytics = testAnalytics( | ||
Configuration( | ||
writeKey = "123", | ||
application = appContext, | ||
storageProvider = AndroidStorageProvider | ||
), | ||
testScope, testDispatcher | ||
) | ||
deepLinkUtils = DeepLinkUtils(analytics) | ||
} | ||
|
||
@Test | ||
fun extractLinkPropertiesTest() { | ||
val link = | ||
"https://stockx.com/?utm_source=af&utm_medium=imp&utm_campaign=1310690&impactSiteId=VupTG%3ASM2xyKUReTwuwulVAxUksw710t1yqKR80&clickid=VupTG%3ASM2xyKUReTwuwulVAxUksw710t1yqKR80&utm_term=VupTG%3ASM2xyKUReTwuwulVAxUksw710t1yqKR80&utm_content=1868737_570105&irgwc=1&irclickid=VupTG%3ASM2xyKUReTwuwulVAxUksw710t1yqKR80&ir_campaignid=9060&ir_adid=570105&ir_partnerid=1310690&gad_source=1&referrer=gclid%3DCjwKCAiAiaC-BhBEEiwAjY99qHbSPJ49pAI83Lo4L7bV3GKaUxSyOX4lah88GFkcNGYQ_MLIZGwXcBoCFAwQAvD_BwE&gref=EkQKPAoICICJoL4GEEQSLACNj32odtI8nj2kAjzcujgvttXcYppTFLI5fiVqHzwYWRw0ZhD8wshkbBdwGgIUDBAC8P8HARjt_K_sKQ" | ||
|
||
val expectedProperties = buildJsonObject { | ||
put( | ||
"referrer", | ||
JsonPrimitive("gclid=CjwKCAiAiaC-BhBEEiwAjY99qHbSPJ49pAI83Lo4L7bV3GKaUxSyOX4lah88GFkcNGYQ_MLIZGwXcBoCFAwQAvD_BwE") | ||
) | ||
put("utm_source", JsonPrimitive("af")) | ||
put("utm_medium", JsonPrimitive("imp")) | ||
put("utm_campaign", JsonPrimitive("1310690")) | ||
put("impactSiteId", JsonPrimitive("VupTG:SM2xyKUReTwuwulVAxUksw710t1yqKR80")) | ||
put("clickid", JsonPrimitive("VupTG:SM2xyKUReTwuwulVAxUksw710t1yqKR80")) | ||
put("utm_term", JsonPrimitive("VupTG:SM2xyKUReTwuwulVAxUksw710t1yqKR80")) | ||
put("utm_content", JsonPrimitive("1868737_570105")) | ||
put("irgwc", JsonPrimitive("1")) | ||
put("irclickid", JsonPrimitive("VupTG:SM2xyKUReTwuwulVAxUksw710t1yqKR80")) | ||
put("ir_campaignid", JsonPrimitive("9060")) | ||
put("ir_adid", JsonPrimitive("570105")) | ||
put("ir_partnerid", JsonPrimitive("1310690")) | ||
put("gad_source", JsonPrimitive("1")) | ||
put( | ||
"gref", | ||
JsonPrimitive("EkQKPAoICICJoL4GEEQSLACNj32odtI8nj2kAjzcujgvttXcYppTFLI5fiVqHzwYWRw0ZhD8wshkbBdwGgIUDBAC8P8HARjt_K_sKQ") | ||
) | ||
put( | ||
"url", | ||
JsonPrimitive("https://stockx.com/?utm_source=af&utm_medium=imp&utm_campaign=1310690&impactSiteId=VupTG%3ASM2xyKUReTwuwulVAxUksw710t1yqKR80&clickid=VupTG%3ASM2xyKUReTwuwulVAxUksw710t1yqKR80&utm_term=VupTG%3ASM2xyKUReTwuwulVAxUksw710t1yqKR80&utm_content=1868737_570105&irgwc=1&irclickid=VupTG%3ASM2xyKUReTwuwulVAxUksw710t1yqKR80&ir_campaignid=9060&ir_adid=570105&ir_partnerid=1310690&gad_source=1&referrer=gclid%3DCjwKCAiAiaC-BhBEEiwAjY99qHbSPJ49pAI83Lo4L7bV3GKaUxSyOX4lah88GFkcNGYQ_MLIZGwXcBoCFAwQAvD_BwE&gref=EkQKPAoICICJoL4GEEQSLACNj32odtI8nj2kAjzcujgvttXcYppTFLI5fiVqHzwYWRw0ZhD8wshkbBdwGgIUDBAC8P8HARjt_K_sKQ") | ||
) | ||
} | ||
|
||
// This should extract all query parameters as properties including a value for the referer property | ||
val properties = deepLinkUtils.extractLinkProperties("not used", Uri.parse(link)) | ||
|
||
assertEquals(expectedProperties, properties) | ||
} | ||
|
||
@Test | ||
fun differentUriTest() { | ||
var properties = deepLinkUtils.extractLinkProperties(null, Uri.parse("http://example.com?prop1=foo")) | ||
assertEquals( | ||
buildJsonObject { | ||
put("prop1", JsonPrimitive("foo")) | ||
put("url", JsonPrimitive("http://example.com?prop1=foo")) | ||
}, | ||
properties | ||
) | ||
|
||
properties = deepLinkUtils.extractLinkProperties(null, Uri.parse("example.com?prop1=foo")) | ||
assertEquals( | ||
buildJsonObject { | ||
put("prop1", JsonPrimitive("foo")) | ||
put("url", JsonPrimitive("example.com?prop1=foo")) | ||
}, | ||
properties | ||
) | ||
|
||
// Even though this Uri has a "?prop1=foo" string at the end, it's not a known part of | ||
// the Uri scheme so we won't be able to use it. | ||
properties = deepLinkUtils.extractLinkProperties(null, Uri.parse("mailto:[email protected]?prop1=foo")) | ||
assertEquals( | ||
buildJsonObject { | ||
put("url", JsonPrimitive("mailto:[email protected]?prop1=foo")) | ||
}, | ||
properties | ||
) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.