-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add device identifiers to attributes #107
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
Open
nickolas-dimitrakas
wants to merge
5
commits into
main
Choose a base branch
from
feat/add-device-identifiers-to-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a451352
feat: add device identifiers to attributes
nickolas-dimitrakas 7c71647
add more test coverage
nickolas-dimitrakas a765622
address @Mansi-mParticle comments
nickolas-dimitrakas ad4222e
Update src/main/kotlin/com/mparticle/kits/RoktKit.kt
nickolas-dimitrakas 597a07e
address @jamesnrokt comments
nickolas-dimitrakas 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
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
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 |
|---|---|---|
|
|
@@ -36,11 +36,15 @@ import org.json.JSONArray | |
| import org.json.JSONObject | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertNotEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.mockito.Mockito | ||
| import org.mockito.Mockito.mock | ||
| import org.mockito.Mockito.mockStatic | ||
| import org.mockito.Mockito.`when` | ||
| import org.mockito.ArgumentMatchers.any as mockitoAny | ||
| import java.lang.ref.WeakReference | ||
| import java.lang.reflect.Field | ||
| import java.lang.reflect.Method | ||
|
|
@@ -1148,6 +1152,210 @@ class RoktKitTests { | |
| assertEquals("allowed_value", result["allowed_key"]) | ||
| } | ||
|
|
||
| @Test | ||
| fun testAddIdentityAttributes_adds_GAID_when_available() { | ||
| // Test that GAID is added when MPUtility.getAdIdInfo returns valid Google advertiser | ||
|
|
||
| // Set applicationContext using reflection (required for GAID lookup) | ||
| val contextField = RoktKit::class.java.getDeclaredField("applicationContext") | ||
| contextField.isAccessible = true | ||
| contextField.set(roktKit, context) | ||
|
|
||
| mockStatic(com.mparticle.internal.MPUtility::class.java).use { mockedMPUtility -> | ||
| // Create a mock AdIdInfo and set its fields using reflection | ||
| val mockAdIdInfo = mock(com.mparticle.internal.MPUtility.AdIdInfo::class.java) | ||
|
|
||
| // Set the id field | ||
| val idField = com.mparticle.internal.MPUtility.AdIdInfo::class.java.getDeclaredField("id") | ||
| idField.isAccessible = true | ||
| idField.set(mockAdIdInfo, "test-gaid-12345") | ||
|
|
||
| // Set the advertiser field | ||
| val advertiserField = com.mparticle.internal.MPUtility.AdIdInfo::class.java.getDeclaredField("advertiser") | ||
| advertiserField.isAccessible = true | ||
| advertiserField.set(mockAdIdInfo, com.mparticle.internal.MPUtility.AdIdInfo.Advertiser.GOOGLE) | ||
|
|
||
| mockedMPUtility.`when`<com.mparticle.internal.MPUtility.AdIdInfo> { | ||
| com.mparticle.internal.MPUtility.getAdIdInfo(mockitoAny(android.content.Context::class.java)) | ||
| }.thenReturn(mockAdIdInfo) | ||
|
|
||
| val mockFilterUser = mock(FilteredMParticleUser::class.java) | ||
| val userIdentities = HashMap<IdentityType, String>() | ||
| userIdentities[IdentityType.Email] = "[email protected]" | ||
| Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities) | ||
|
|
||
| val attributes: MutableMap<String, String> = mutableMapOf("existing_key" to "existing_value") | ||
|
|
||
| val method: Method = RoktKit::class.java.getDeclaredMethod( | ||
| "addIdentityAttributes", | ||
| MutableMap::class.java, | ||
| FilteredMParticleUser::class.java, | ||
| ) | ||
| method.isAccessible = true | ||
|
|
||
| // Act | ||
| val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String> | ||
|
|
||
| // Assert | ||
| assertTrue("GAID should be present in result", result.containsKey("gaid")) | ||
| assertEquals("test-gaid-12345", result["gaid"]) | ||
|
|
||
| // Verify user identities are still added | ||
| assertEquals("[email protected]", result["email"]) | ||
|
|
||
| // Verify existing attributes are preserved | ||
| assertEquals("existing_value", result["existing_key"]) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun testAddIdentityAttributes_GAID_not_added_when_null() { | ||
| // Test that GAID is not added when MPUtility.getAdIdInfo returns null | ||
|
|
||
| // Set applicationContext using reflection (required for GAID lookup) | ||
| val contextField = RoktKit::class.java.getDeclaredField("applicationContext") | ||
| contextField.isAccessible = true | ||
| contextField.set(roktKit, context) | ||
|
|
||
| mockStatic(com.mparticle.internal.MPUtility::class.java).use { mockedMPUtility -> | ||
| // Mock getAdIdInfo to return null (ad tracking not available or not authorized) | ||
| mockedMPUtility.`when`<com.mparticle.internal.MPUtility.AdIdInfo> { | ||
| com.mparticle.internal.MPUtility.getAdIdInfo(mockitoAny(android.content.Context::class.java)) | ||
nickolas-dimitrakas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }.thenReturn(null) | ||
|
|
||
| val mockFilterUser = mock(FilteredMParticleUser::class.java) | ||
| val userIdentities = HashMap<IdentityType, String>() | ||
| userIdentities[IdentityType.Email] = "[email protected]" | ||
| Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities) | ||
|
|
||
| val attributes: MutableMap<String, String> = mutableMapOf() | ||
|
|
||
| val method: Method = RoktKit::class.java.getDeclaredMethod( | ||
| "addIdentityAttributes", | ||
| MutableMap::class.java, | ||
| FilteredMParticleUser::class.java, | ||
| ) | ||
| method.isAccessible = true | ||
|
|
||
| // Act | ||
| val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String> | ||
|
|
||
| // Assert - GAID should NOT be present | ||
| assertFalse("GAID should not be present when getAdIdInfo returns null", result.containsKey("gaid")) | ||
|
|
||
| // Other identities should still be added | ||
| assertTrue(result.containsKey("email")) | ||
| assertEquals("[email protected]", result["email"]) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun testAddIdentityAttributes_GAID_coexists_with_google_user_identity() { | ||
| // Test that GAID (gaid key) coexists with Google user identity (google key) | ||
|
|
||
| // Set applicationContext using reflection (required for GAID lookup) | ||
| val contextField = RoktKit::class.java.getDeclaredField("applicationContext") | ||
| contextField.isAccessible = true | ||
| contextField.set(roktKit, context) | ||
|
|
||
| mockStatic(com.mparticle.internal.MPUtility::class.java).use { mockedMPUtility -> | ||
| // Create a mock AdIdInfo and set its fields using reflection | ||
| val mockAdIdInfo = mock(com.mparticle.internal.MPUtility.AdIdInfo::class.java) | ||
|
|
||
| val idField = com.mparticle.internal.MPUtility.AdIdInfo::class.java.getDeclaredField("id") | ||
| idField.isAccessible = true | ||
| idField.set(mockAdIdInfo, "device-gaid-value") | ||
|
|
||
| val advertiserField = com.mparticle.internal.MPUtility.AdIdInfo::class.java.getDeclaredField("advertiser") | ||
| advertiserField.isAccessible = true | ||
| advertiserField.set(mockAdIdInfo, com.mparticle.internal.MPUtility.AdIdInfo.Advertiser.GOOGLE) | ||
|
|
||
| mockedMPUtility.`when`<com.mparticle.internal.MPUtility.AdIdInfo> { | ||
| com.mparticle.internal.MPUtility.getAdIdInfo(mockitoAny(android.content.Context::class.java)) | ||
| }.thenReturn(mockAdIdInfo) | ||
|
|
||
| val mockFilterUser = mock(FilteredMParticleUser::class.java) | ||
| val userIdentities = HashMap<IdentityType, String>() | ||
| userIdentities[IdentityType.Email] = "[email protected]" | ||
| userIdentities[IdentityType.Google] = "user-google-identity" | ||
| Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities) | ||
|
|
||
| val attributes: MutableMap<String, String> = mutableMapOf() | ||
|
|
||
| val method: Method = RoktKit::class.java.getDeclaredMethod( | ||
| "addIdentityAttributes", | ||
| MutableMap::class.java, | ||
| FilteredMParticleUser::class.java, | ||
| ) | ||
| method.isAccessible = true | ||
|
|
||
| // Act | ||
| val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String> | ||
|
|
||
| // Assert - Both should be present with different keys and values | ||
| assertTrue(result.containsKey("google")) | ||
| assertEquals("user-google-identity", result["google"]) | ||
|
|
||
| assertTrue(result.containsKey("gaid")) | ||
| assertEquals("device-gaid-value", result["gaid"]) | ||
|
|
||
| // Verify they don't interfere with each other | ||
| assertNotEquals(result["google"], result["gaid"]) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun testAddIdentityAttributes_GAID_not_added_for_non_google_advertiser() { | ||
| // Test that GAID is NOT added when advertiser is not Google (e.g., Amazon Fire) | ||
| // Ensures we only add GAID for actual Google Advertising IDs | ||
|
|
||
| // Set applicationContext using reflection (required for GAID lookup) | ||
| val contextField = RoktKit::class.java.getDeclaredField("applicationContext") | ||
| contextField.isAccessible = true | ||
| contextField.set(roktKit, context) | ||
|
|
||
| mockStatic(com.mparticle.internal.MPUtility::class.java).use { mockedMPUtility -> | ||
| // Create a mock AdIdInfo with Amazon advertiser (not Google) | ||
| val mockAdIdInfo = mock(com.mparticle.internal.MPUtility.AdIdInfo::class.java) | ||
|
|
||
| val idField = com.mparticle.internal.MPUtility.AdIdInfo::class.java.getDeclaredField("id") | ||
| idField.isAccessible = true | ||
| idField.set(mockAdIdInfo, "amazon-ad-id-12345") | ||
|
|
||
| val advertiserField = com.mparticle.internal.MPUtility.AdIdInfo::class.java.getDeclaredField("advertiser") | ||
| advertiserField.isAccessible = true | ||
| advertiserField.set(mockAdIdInfo, com.mparticle.internal.MPUtility.AdIdInfo.Advertiser.AMAZON) | ||
|
|
||
| mockedMPUtility.`when`<com.mparticle.internal.MPUtility.AdIdInfo> { | ||
| com.mparticle.internal.MPUtility.getAdIdInfo(mockitoAny(android.content.Context::class.java)) | ||
| }.thenReturn(mockAdIdInfo) | ||
|
|
||
| val mockFilterUser = mock(FilteredMParticleUser::class.java) | ||
| val userIdentities = HashMap<IdentityType, String>() | ||
| userIdentities[IdentityType.Email] = "[email protected]" | ||
| Mockito.`when`(mockFilterUser.userIdentities).thenReturn(userIdentities) | ||
|
|
||
| val attributes: MutableMap<String, String> = mutableMapOf() | ||
|
|
||
| val method: Method = RoktKit::class.java.getDeclaredMethod( | ||
| "addIdentityAttributes", | ||
| MutableMap::class.java, | ||
| FilteredMParticleUser::class.java, | ||
| ) | ||
| method.isAccessible = true | ||
|
|
||
| // Act | ||
| val result = method.invoke(roktKit, attributes, mockFilterUser) as Map<String, String> | ||
|
|
||
| // Assert - GAID should NOT be added for Amazon advertiser | ||
| assertFalse("GAID should not be added for non-Google advertiser", result.containsKey("gaid")) | ||
|
|
||
| // Other identities should still be added | ||
| assertTrue(result.containsKey("email")) | ||
| assertEquals("[email protected]", result["email"]) | ||
| } | ||
| } | ||
|
|
||
| internal inner class TestCoreCallbacks : CoreCallbacks { | ||
| override fun isBackgrounded(): Boolean = false | ||
| override fun getUserBucket(): Int = 0 | ||
|
|
||
Oops, something went wrong.
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.