@adshift/react-native-plugin 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. package/AdShiftReactNativePlugin.podspec +11 -7
  2. package/CHANGELOG.md +122 -0
  3. package/README.md +506 -542
  4. package/android/build.gradle +17 -3
  5. package/android/consumer-rules.pro +21 -0
  6. package/android/src/main/java/com/adshift/reactnativeplugin/AdShiftReactNativePluginModule.kt +786 -229
  7. package/android/src/test/java/com/adshift/reactnativeplugin/ConsentSnapshotTest.kt +180 -0
  8. package/android/src/test/java/com/adshift/reactnativeplugin/DeepLinkDeliveryTest.kt +146 -0
  9. package/android/src/test/java/com/adshift/reactnativeplugin/InitializeOrderingTest.kt +111 -0
  10. package/android/src/test/java/com/adshift/reactnativeplugin/IntentLinkDedupTest.kt +168 -0
  11. package/android/src/test/java/com/adshift/reactnativeplugin/LaunchIntentForwardingTest.kt +106 -0
  12. package/android/src/test/java/com/adshift/reactnativeplugin/ModuleTestSupport.kt +167 -0
  13. package/android/src/test/java/com/adshift/reactnativeplugin/StartVerdictTest.kt +164 -0
  14. package/ios/AdShiftReactNativePlugin.h +7 -7
  15. package/ios/AdShiftReactNativePlugin.mm +200 -181
  16. package/ios/AdShiftReactNativePluginHelper.swift +584 -286
  17. package/ios/Core/BridgeRules.swift +82 -0
  18. package/lib/commonjs/Adshift.js +1135 -0
  19. package/lib/commonjs/Adshift.js.map +1 -0
  20. package/lib/commonjs/NativeAdShiftReactNativePlugin.js +22 -0
  21. package/lib/commonjs/NativeAdShiftReactNativePlugin.js.map +1 -0
  22. package/lib/commonjs/index.js +94 -0
  23. package/lib/commonjs/index.js.map +1 -0
  24. package/lib/commonjs/jest/index.js +164 -0
  25. package/lib/commonjs/jest/index.js.map +1 -0
  26. package/lib/commonjs/nativeSdkVersions.js +17 -0
  27. package/lib/commonjs/nativeSdkVersions.js.map +1 -0
  28. package/lib/commonjs/package.json +1 -0
  29. package/lib/commonjs/types/AdshiftAdRevenue.js +47 -0
  30. package/lib/commonjs/types/AdshiftAdRevenue.js.map +1 -0
  31. package/lib/commonjs/types/AdshiftConfig.js +45 -0
  32. package/lib/commonjs/types/AdshiftConfig.js.map +1 -0
  33. package/lib/commonjs/types/AdshiftConsent.js +113 -0
  34. package/lib/commonjs/types/AdshiftConsent.js.map +1 -0
  35. package/lib/commonjs/types/AdshiftConsentSnapshot.js +69 -0
  36. package/lib/commonjs/types/AdshiftConsentSnapshot.js.map +1 -0
  37. package/lib/commonjs/types/AdshiftDeepLink.js +154 -0
  38. package/lib/commonjs/types/AdshiftDeepLink.js.map +1 -0
  39. package/lib/commonjs/types/AdshiftError.js +91 -0
  40. package/lib/commonjs/types/AdshiftError.js.map +1 -0
  41. package/lib/commonjs/types/AdshiftEventParam.js +140 -0
  42. package/lib/commonjs/types/AdshiftEventParam.js.map +1 -0
  43. package/lib/commonjs/types/AdshiftEventType.js +88 -0
  44. package/lib/commonjs/types/AdshiftEventType.js.map +1 -0
  45. package/lib/commonjs/types/AdshiftThirdPartySharing.js +70 -0
  46. package/lib/commonjs/types/AdshiftThirdPartySharing.js.map +1 -0
  47. package/lib/commonjs/types/index.js +123 -0
  48. package/lib/commonjs/types/index.js.map +1 -0
  49. package/lib/commonjs/version.js +9 -0
  50. package/lib/commonjs/version.js.map +1 -0
  51. package/lib/module/Adshift.js +959 -296
  52. package/lib/module/Adshift.js.map +1 -1
  53. package/lib/module/NativeAdShiftReactNativePlugin.js +10 -7
  54. package/lib/module/NativeAdShiftReactNativePlugin.js.map +1 -1
  55. package/lib/module/index.js +26 -25
  56. package/lib/module/index.js.map +1 -1
  57. package/lib/module/jest/index.js +144 -0
  58. package/lib/module/jest/index.js.map +1 -0
  59. package/lib/module/nativeSdkVersions.js +13 -0
  60. package/lib/module/nativeSdkVersions.js.map +1 -0
  61. package/lib/module/types/AdshiftAdRevenue.js +43 -0
  62. package/lib/module/types/AdshiftAdRevenue.js.map +1 -0
  63. package/lib/module/types/AdshiftConfig.js +23 -26
  64. package/lib/module/types/AdshiftConfig.js.map +1 -1
  65. package/lib/module/types/AdshiftConsent.js +57 -50
  66. package/lib/module/types/AdshiftConsent.js.map +1 -1
  67. package/lib/module/types/AdshiftConsentSnapshot.js +65 -0
  68. package/lib/module/types/AdshiftConsentSnapshot.js.map +1 -0
  69. package/lib/module/types/AdshiftDeepLink.js +97 -67
  70. package/lib/module/types/AdshiftDeepLink.js.map +1 -1
  71. package/lib/module/types/AdshiftError.js +87 -0
  72. package/lib/module/types/AdshiftError.js.map +1 -0
  73. package/lib/module/types/AdshiftEventParam.js +135 -0
  74. package/lib/module/types/AdshiftEventParam.js.map +1 -0
  75. package/lib/module/types/AdshiftEventType.js +11 -36
  76. package/lib/module/types/AdshiftEventType.js.map +1 -1
  77. package/lib/module/types/AdshiftThirdPartySharing.js +65 -0
  78. package/lib/module/types/AdshiftThirdPartySharing.js.map +1 -0
  79. package/lib/module/types/index.js +9 -7
  80. package/lib/module/types/index.js.map +1 -1
  81. package/lib/module/version.js +5 -0
  82. package/lib/module/version.js.map +1 -0
  83. package/lib/typescript/commonjs/package.json +1 -0
  84. package/lib/typescript/commonjs/src/Adshift.d.ts +311 -0
  85. package/lib/typescript/commonjs/src/Adshift.d.ts.map +1 -0
  86. package/lib/typescript/commonjs/src/NativeAdShiftReactNativePlugin.d.ts +102 -0
  87. package/lib/typescript/commonjs/src/NativeAdShiftReactNativePlugin.d.ts.map +1 -0
  88. package/lib/typescript/commonjs/src/index.d.ts +51 -0
  89. package/lib/typescript/commonjs/src/index.d.ts.map +1 -0
  90. package/lib/typescript/commonjs/src/jest/index.d.ts +103 -0
  91. package/lib/typescript/commonjs/src/jest/index.d.ts.map +1 -0
  92. package/lib/typescript/commonjs/src/nativeSdkVersions.d.ts +11 -0
  93. package/lib/typescript/commonjs/src/nativeSdkVersions.d.ts.map +1 -0
  94. package/lib/typescript/commonjs/src/types/AdshiftAdRevenue.d.ts +56 -0
  95. package/lib/typescript/commonjs/src/types/AdshiftAdRevenue.d.ts.map +1 -0
  96. package/lib/typescript/commonjs/src/types/AdshiftConfig.d.ts +104 -0
  97. package/lib/typescript/commonjs/src/types/AdshiftConfig.d.ts.map +1 -0
  98. package/lib/typescript/commonjs/src/types/AdshiftConsent.d.ts +76 -0
  99. package/lib/typescript/commonjs/src/types/AdshiftConsent.d.ts.map +1 -0
  100. package/lib/typescript/commonjs/src/types/AdshiftConsentSnapshot.d.ts +56 -0
  101. package/lib/typescript/commonjs/src/types/AdshiftConsentSnapshot.d.ts.map +1 -0
  102. package/lib/typescript/commonjs/src/types/AdshiftDeepLink.d.ts +89 -0
  103. package/lib/typescript/commonjs/src/types/AdshiftDeepLink.d.ts.map +1 -0
  104. package/lib/typescript/commonjs/src/types/AdshiftError.d.ts +57 -0
  105. package/lib/typescript/commonjs/src/types/AdshiftError.d.ts.map +1 -0
  106. package/lib/typescript/commonjs/src/types/AdshiftEventParam.d.ts +111 -0
  107. package/lib/typescript/commonjs/src/types/AdshiftEventParam.d.ts.map +1 -0
  108. package/lib/typescript/{src → commonjs/src}/types/AdshiftEventType.d.ts +12 -35
  109. package/lib/typescript/commonjs/src/types/AdshiftEventType.d.ts.map +1 -0
  110. package/lib/typescript/commonjs/src/types/AdshiftThirdPartySharing.d.ts +53 -0
  111. package/lib/typescript/commonjs/src/types/AdshiftThirdPartySharing.d.ts.map +1 -0
  112. package/lib/typescript/commonjs/src/types/index.d.ts +17 -0
  113. package/lib/typescript/commonjs/src/types/index.d.ts.map +1 -0
  114. package/lib/typescript/commonjs/src/version.d.ts +2 -0
  115. package/lib/typescript/commonjs/src/version.d.ts.map +1 -0
  116. package/lib/typescript/module/src/Adshift.d.ts +311 -0
  117. package/lib/typescript/module/src/Adshift.d.ts.map +1 -0
  118. package/lib/typescript/module/src/NativeAdShiftReactNativePlugin.d.ts +102 -0
  119. package/lib/typescript/module/src/NativeAdShiftReactNativePlugin.d.ts.map +1 -0
  120. package/lib/typescript/module/src/index.d.ts +51 -0
  121. package/lib/typescript/module/src/index.d.ts.map +1 -0
  122. package/lib/typescript/module/src/jest/index.d.ts +103 -0
  123. package/lib/typescript/module/src/jest/index.d.ts.map +1 -0
  124. package/lib/typescript/module/src/nativeSdkVersions.d.ts +11 -0
  125. package/lib/typescript/module/src/nativeSdkVersions.d.ts.map +1 -0
  126. package/lib/typescript/module/src/types/AdshiftAdRevenue.d.ts +56 -0
  127. package/lib/typescript/module/src/types/AdshiftAdRevenue.d.ts.map +1 -0
  128. package/lib/typescript/module/src/types/AdshiftConfig.d.ts +104 -0
  129. package/lib/typescript/module/src/types/AdshiftConfig.d.ts.map +1 -0
  130. package/lib/typescript/module/src/types/AdshiftConsent.d.ts +76 -0
  131. package/lib/typescript/module/src/types/AdshiftConsent.d.ts.map +1 -0
  132. package/lib/typescript/module/src/types/AdshiftConsentSnapshot.d.ts +56 -0
  133. package/lib/typescript/module/src/types/AdshiftConsentSnapshot.d.ts.map +1 -0
  134. package/lib/typescript/module/src/types/AdshiftDeepLink.d.ts +89 -0
  135. package/lib/typescript/module/src/types/AdshiftDeepLink.d.ts.map +1 -0
  136. package/lib/typescript/module/src/types/AdshiftError.d.ts +57 -0
  137. package/lib/typescript/module/src/types/AdshiftError.d.ts.map +1 -0
  138. package/lib/typescript/module/src/types/AdshiftEventParam.d.ts +111 -0
  139. package/lib/typescript/module/src/types/AdshiftEventParam.d.ts.map +1 -0
  140. package/lib/typescript/module/src/types/AdshiftEventType.d.ts +64 -0
  141. package/lib/typescript/module/src/types/AdshiftEventType.d.ts.map +1 -0
  142. package/lib/typescript/module/src/types/AdshiftThirdPartySharing.d.ts +53 -0
  143. package/lib/typescript/module/src/types/AdshiftThirdPartySharing.d.ts.map +1 -0
  144. package/lib/typescript/module/src/types/index.d.ts +17 -0
  145. package/lib/typescript/module/src/types/index.d.ts.map +1 -0
  146. package/lib/typescript/module/src/version.d.ts +2 -0
  147. package/lib/typescript/module/src/version.d.ts.map +1 -0
  148. package/package.json +49 -10
  149. package/src/Adshift.ts +1273 -342
  150. package/src/NativeAdShiftReactNativePlugin.ts +81 -115
  151. package/src/index.tsx +40 -25
  152. package/src/jest/index.ts +224 -0
  153. package/src/nativeSdkVersions.ts +10 -0
  154. package/src/types/AdshiftAdRevenue.ts +58 -0
  155. package/src/types/AdshiftConfig.ts +101 -53
  156. package/src/types/AdshiftConsent.ts +75 -72
  157. package/src/types/AdshiftConsentSnapshot.ts +103 -0
  158. package/src/types/AdshiftDeepLink.ts +123 -102
  159. package/src/types/AdshiftError.ts +107 -0
  160. package/src/types/AdshiftEventParam.ts +216 -0
  161. package/src/types/AdshiftEventType.ts +13 -35
  162. package/src/types/AdshiftThirdPartySharing.ts +63 -0
  163. package/src/types/index.ts +38 -6
  164. package/src/version.ts +2 -0
  165. package/lib/typescript/src/Adshift.d.ts +0 -335
  166. package/lib/typescript/src/Adshift.d.ts.map +0 -1
  167. package/lib/typescript/src/NativeAdShiftReactNativePlugin.d.ts +0 -143
  168. package/lib/typescript/src/NativeAdShiftReactNativePlugin.d.ts.map +0 -1
  169. package/lib/typescript/src/index.d.ts +0 -47
  170. package/lib/typescript/src/index.d.ts.map +0 -1
  171. package/lib/typescript/src/types/AdshiftConfig.d.ts +0 -74
  172. package/lib/typescript/src/types/AdshiftConfig.d.ts.map +0 -1
  173. package/lib/typescript/src/types/AdshiftConsent.d.ts +0 -88
  174. package/lib/typescript/src/types/AdshiftConsent.d.ts.map +0 -1
  175. package/lib/typescript/src/types/AdshiftDeepLink.d.ts +0 -93
  176. package/lib/typescript/src/types/AdshiftDeepLink.d.ts.map +0 -1
  177. package/lib/typescript/src/types/AdshiftEventType.d.ts.map +0 -1
  178. package/lib/typescript/src/types/index.d.ts +0 -5
  179. package/lib/typescript/src/types/index.d.ts.map +0 -1
  180. /package/lib/typescript/{package.json → module/package.json} +0 -0
@@ -0,0 +1,180 @@
1
+ package com.adshift.reactnativeplugin
2
+
3
+ import com.adshift.sdk.core.AdShiftLib
4
+ import com.adshift.sdk.core.AdShiftThirdPartySharing
5
+ import com.adshift.sdk.core.ConsentSnapshot
6
+ import com.adshift.sdk.core.ConsentSource
7
+ import com.facebook.react.bridge.Arguments
8
+ import com.facebook.react.bridge.JavaOnlyMap
9
+ import com.facebook.react.bridge.ReadableMap
10
+ import io.mockk.Runs
11
+ import io.mockk.every
12
+ import io.mockk.just
13
+ import io.mockk.mockkStatic
14
+ import io.mockk.unmockkStatic
15
+ import io.mockk.verify
16
+ import org.junit.After
17
+ import org.junit.Assert.assertEquals
18
+ import org.junit.Assert.assertFalse
19
+ import org.junit.Assert.assertTrue
20
+ import org.junit.Before
21
+ import org.junit.Test
22
+ import org.junit.runner.RunWith
23
+ import org.robolectric.RobolectricTestRunner
24
+ import org.robolectric.annotation.Config
25
+
26
+ /**
27
+ * The snapshot answers whether consent still lets the SDK use the advertising id.
28
+ * The rule differs from the one iOS applies, so it is settled here rather than in
29
+ * JavaScript: under GDPR an undecided flag counts as a refusal, and outside it
30
+ * only an explicit denial does.
31
+ */
32
+ @RunWith(RobolectricTestRunner::class)
33
+ @Config(sdk = [35])
34
+ class ConsentSnapshotTest {
35
+
36
+ private lateinit var context: TestReactContext
37
+ private lateinit var module: AdShiftReactNativePluginModule
38
+
39
+ @Before
40
+ fun setUp() {
41
+ ModuleState.reset()
42
+ mockkStatic(AdShiftLib::class)
43
+ mockkStatic(Arguments::class)
44
+ every { Arguments.createMap() } answers { JavaOnlyMap() }
45
+ every { AdShiftLib.initSdk(any(), any()) } just Runs
46
+ every { AdShiftLib.setDeepLinkListener(any()) } just Runs
47
+ every { AdShiftLib.setDebugLog(any()) } just Runs
48
+ every { AdShiftLib.setBrandedDomains(any()) } just Runs
49
+ every { AdShiftLib.addPushNotificationDeepLinkPath(*anyVararg()) } just Runs
50
+
51
+ context = TestReactContext()
52
+ context.launchIntent = launcherIntent()
53
+ module = AdShiftReactNativePluginModule(context.reactContext)
54
+ module.initialize(initializeConfig())
55
+ }
56
+
57
+ @After
58
+ fun tearDown() {
59
+ unmockkStatic(AdShiftLib::class)
60
+ unmockkStatic(Arguments::class)
61
+ ModuleState.reset()
62
+ }
63
+
64
+ @Test
65
+ fun `allows the advertising id when both flags are granted under gdpr`() {
66
+ assertTrue("allowed", allowed(gdprApplies = 1, storage = true, dataUsage = true))
67
+ }
68
+
69
+ /** The case a single "any flag granted" rule would get wrong. */
70
+ @Test
71
+ fun `withholds the advertising id when only one flag is granted under gdpr`() {
72
+ assertFalse("allowed", allowed(gdprApplies = 1, storage = true, dataUsage = false))
73
+ assertFalse("allowed", allowed(gdprApplies = 1, storage = false, dataUsage = true))
74
+ }
75
+
76
+ @Test
77
+ fun `treats an undecided flag under gdpr as a refusal`() {
78
+ assertFalse("allowed", allowed(gdprApplies = 1, storage = true, dataUsage = null))
79
+ }
80
+
81
+ @Test
82
+ fun `allows the advertising id outside gdpr when nothing was decided`() {
83
+ assertTrue("allowed", allowed(gdprApplies = 0, storage = null, dataUsage = null))
84
+ assertTrue("allowed", allowed(gdprApplies = null, storage = null, dataUsage = null))
85
+ }
86
+
87
+ @Test
88
+ fun `honours an explicit denial outside gdpr`() {
89
+ assertFalse("allowed", allowed(gdprApplies = 0, storage = false, dataUsage = true))
90
+ }
91
+
92
+ @Test
93
+ fun `reports the rest of the snapshot as the sdk stated it`() {
94
+ val snapshot = refreshConsent(
95
+ snapshot(gdprApplies = 1, storage = true, dataUsage = true)
96
+ )
97
+
98
+ assertEquals("manual", snapshot.getString("source"))
99
+ assertEquals(1, snapshot.getInt("gdprApplies"))
100
+ assertTrue("gppPresent", snapshot.getBoolean("gppPresent"))
101
+ assertTrue("adStorageEnabled", snapshot.getBoolean("adStorageEnabled"))
102
+ assertEquals(7.0, snapshot.getDouble("appliedAtMillis"), 0.0)
103
+ assertFalse("cmpDetected", snapshot.getBoolean("cmpDetected"))
104
+ }
105
+
106
+ /**
107
+ * A CMP that has run but has not been answered is indistinguishable from no
108
+ * CMP at all by every other field here, and the two are fixed by opposite
109
+ * actions. Dropping this field on the way through the bridge would leave
110
+ * JavaScript unable to tell them apart.
111
+ */
112
+ @Test
113
+ fun `forwards that a cmp has run even with nothing decided`() {
114
+ val snapshot = refreshConsent(
115
+ snapshot(gdprApplies = null, storage = null, dataUsage = null, cmpDetected = true)
116
+ )
117
+
118
+ assertTrue("cmpDetected", snapshot.getBoolean("cmpDetected"))
119
+ }
120
+
121
+ @Test
122
+ fun `states the sharing answer it was given`() {
123
+ every { AdShiftLib.setThirdPartySharing(any()) } just Runs
124
+ every { AdShiftLib.clearThirdPartySharing() } just Runs
125
+
126
+ module.setThirdPartySharing(JavaOnlyMap.of("optOut", true))
127
+ verify { AdShiftLib.setThirdPartySharing(AdShiftThirdPartySharing.optedOut()) }
128
+
129
+ module.setThirdPartySharing(JavaOnlyMap.of("optOut", false))
130
+ verify { AdShiftLib.setThirdPartySharing(AdShiftThirdPartySharing.allowed()) }
131
+
132
+ module.clearThirdPartySharing()
133
+ verify { AdShiftLib.clearThirdPartySharing() }
134
+ }
135
+
136
+ /**
137
+ * Nothing said on this axis means sharing is allowed, which is the opposite of
138
+ * how consent reads an absent answer. So a malformed call must not be
139
+ * defaulted: `allowed()` would record a declaration the app never made,
140
+ * `optedOut()` would withhold data the user never asked to withhold, and
141
+ * neither can be recalled for events already sent.
142
+ */
143
+ @Test
144
+ fun `refuses to guess a malformed sharing answer`() {
145
+ every { AdShiftLib.setThirdPartySharing(any()) } just Runs
146
+
147
+ module.setThirdPartySharing(JavaOnlyMap.of())
148
+ module.setThirdPartySharing(JavaOnlyMap.of("optOut", "yes"))
149
+
150
+ verify(exactly = 0) { AdShiftLib.setThirdPartySharing(any()) }
151
+ }
152
+
153
+ private fun allowed(gdprApplies: Int?, storage: Boolean?, dataUsage: Boolean?): Boolean =
154
+ refreshConsent(snapshot(gdprApplies, storage, dataUsage))
155
+ .getBoolean("adIdentifiersAllowed")
156
+
157
+ private fun refreshConsent(snapshot: ConsentSnapshot): ReadableMap {
158
+ every { AdShiftLib.refreshConsent() } returns snapshot
159
+ val promise = RecordingPromise()
160
+ module.refreshConsent(promise)
161
+ return promise.resolvedValue as ReadableMap
162
+ }
163
+
164
+ private fun snapshot(
165
+ gdprApplies: Int?,
166
+ storage: Boolean?,
167
+ dataUsage: Boolean?,
168
+ cmpDetected: Boolean = false,
169
+ ): ConsentSnapshot = ConsentSnapshot(
170
+ source = ConsentSource.MANUAL,
171
+ gdprApplies = gdprApplies,
172
+ tcStringPresent = false,
173
+ gppPresent = true,
174
+ adUserDataEnabled = dataUsage,
175
+ adPersonalizationEnabled = null,
176
+ adStorageEnabled = storage,
177
+ appliedAtMillis = 7L,
178
+ cmpDetected = cmpDetected,
179
+ )
180
+ }
@@ -0,0 +1,146 @@
1
+ package com.adshift.reactnativeplugin
2
+
3
+ import android.net.Uri
4
+ import android.os.Bundle
5
+ import com.adshift.sdk.core.AdShiftLib
6
+ import com.adshift.sdk.core.deeplink.DeepLinkListener
7
+ import com.adshift.sdk.core.deeplink.DeepLinkResult
8
+ import com.adshift.sdk.core.deeplink.DeepLinkStatus
9
+ import com.facebook.react.bridge.Arguments
10
+ import com.facebook.react.bridge.CxxCallbackImpl
11
+ import com.facebook.react.bridge.JavaOnlyMap
12
+ import com.facebook.react.bridge.ReadableMap
13
+ import io.mockk.Runs
14
+ import io.mockk.every
15
+ import io.mockk.just
16
+ import io.mockk.mockk
17
+ import io.mockk.mockkStatic
18
+ import io.mockk.slot
19
+ import io.mockk.unmockkStatic
20
+ import org.junit.After
21
+ import org.junit.Assert.assertEquals
22
+ import org.junit.Before
23
+ import org.junit.Test
24
+ import org.junit.runner.RunWith
25
+ import org.robolectric.RobolectricTestRunner
26
+ import org.robolectric.annotation.Config
27
+
28
+ /**
29
+ * Every deep link result reaches JavaScript exactly once: results that arrive while no listener
30
+ * is reachable wait for the next one, in the order the SDK produced them.
31
+ */
32
+ @RunWith(RobolectricTestRunner::class)
33
+ @Config(sdk = [35])
34
+ class DeepLinkDeliveryTest {
35
+
36
+ private val emitted = mutableListOf<String?>()
37
+
38
+ private lateinit var listener: DeepLinkListener
39
+ private lateinit var context: TestReactContext
40
+ private lateinit var module: AdShiftReactNativePluginModule
41
+
42
+ @Before
43
+ fun setUp() {
44
+ ModuleState.reset()
45
+ allowNativeCallbacks()
46
+ mockkStatic(AdShiftLib::class)
47
+ mockkStatic(Arguments::class)
48
+ every { Arguments.createMap() } answers { JavaOnlyMap() }
49
+
50
+ val registered = slot<DeepLinkListener>()
51
+ every { AdShiftLib.setDeepLinkListener(capture(registered)) } just Runs
52
+ every { AdShiftLib.initSdk(any(), any()) } just Runs
53
+ every { AdShiftLib.setDebugLog(any()) } just Runs
54
+ every { AdShiftLib.setBrandedDomains(any()) } just Runs
55
+ every { AdShiftLib.addPushNotificationDeepLinkPath(*anyVararg()) } just Runs
56
+ every { AdShiftLib.handlePushNotification(any<Bundle>()) } returns false
57
+ every { AdShiftLib.handleAppLinkIntent(any()) } just Runs
58
+
59
+ context = TestReactContext()
60
+ module = AdShiftReactNativePluginModule(context.reactContext)
61
+ module.initialize(initializeConfig())
62
+ listener = registered.captured
63
+ }
64
+
65
+ @After
66
+ fun tearDown() {
67
+ unmockkStatic(Arguments::class)
68
+ unmockkStatic(AdShiftLib::class)
69
+ ModuleState.reset()
70
+ }
71
+
72
+ @Test
73
+ fun `delivers results that arrived before a listener was reachable, oldest first`() {
74
+ listener.onDeepLinking(deepLinkResult(link(1)))
75
+ listener.onDeepLinking(deepLinkResult(link(2)))
76
+
77
+ assertEquals("results emitted so far", emptyList<String>(), emitted)
78
+
79
+ attachEventEmitter(reloadedModule())
80
+
81
+ assertEquals(listOf(link(1), link(2)), emitted)
82
+ assertEquals("results still waiting", 0, ModuleState.pendingDeepLinkCount())
83
+ }
84
+
85
+ @Test
86
+ fun `delivers a waiting result only once`() {
87
+ listener.onDeepLinking(deepLinkResult(link(1)))
88
+
89
+ attachEventEmitter(reloadedModule())
90
+ assertEquals(listOf(link(1)), emitted)
91
+
92
+ attachEventEmitter(reloadedModule())
93
+ assertEquals(listOf(link(1)), emitted)
94
+ }
95
+
96
+ @Test
97
+ fun `keeps the newest results when more arrive than can be held`() {
98
+ repeat(DISPATCHED) { index -> listener.onDeepLinking(deepLinkResult(link(index))) }
99
+
100
+ attachEventEmitter(reloadedModule())
101
+
102
+ assertEquals((DISPATCHED - LIMIT until DISPATCHED).map(::link), emitted)
103
+ }
104
+
105
+ @Test
106
+ fun `emits a result straight away while a listener is reachable`() {
107
+ attachEventEmitter(module)
108
+
109
+ listener.onDeepLinking(deepLinkResult(link(1)))
110
+
111
+ assertEquals(listOf(link(1)), emitted)
112
+ assertEquals("results still waiting", 0, ModuleState.pendingDeepLinkCount())
113
+ }
114
+
115
+ /** A module created the way a JavaScript reload creates one. */
116
+ private fun reloadedModule() = AdShiftReactNativePluginModule(context.reactContext)
117
+
118
+ private fun attachEventEmitter(target: AdShiftReactNativePluginModule) {
119
+ val callback = mockk<CxxCallbackImpl>(relaxed = true)
120
+ every { callback.invoke(*anyVararg()) } answers { emitted += deepLinkOf(args) }
121
+ ATTACH_EMITTER.invoke(target, callback)
122
+ }
123
+
124
+ private fun deepLinkOf(args: List<Any?>): String? = args
125
+ .flatMap { if (it is Array<*>) it.asList() else listOf(it) }
126
+ .filterIsInstance<ReadableMap>()
127
+ .firstOrNull()
128
+ ?.getString("deepLink")
129
+
130
+ private fun deepLinkResult(url: String) = DeepLinkResult(
131
+ uri = Uri.parse(url),
132
+ isDeferred = true,
133
+ status = DeepLinkStatus.FOUND,
134
+ )
135
+
136
+ private fun link(index: Int) = "https://$BRANDED_DOMAIN/deferred/$index"
137
+
138
+ private companion object {
139
+ const val LIMIT = 20
140
+ const val DISPATCHED = 25
141
+
142
+ val ATTACH_EMITTER = AdShiftReactNativePluginModule::class.java
143
+ .getDeclaredMethod("setEventEmitterCallback", CxxCallbackImpl::class.java)
144
+ .apply { isAccessible = true }
145
+ }
146
+ }
@@ -0,0 +1,111 @@
1
+ package com.adshift.reactnativeplugin
2
+
3
+ import android.content.Intent
4
+ import android.net.Uri
5
+ import android.os.Bundle
6
+ import com.adshift.sdk.core.AdShiftLib
7
+ import io.mockk.every
8
+ import io.mockk.mockkStatic
9
+ import io.mockk.unmockkStatic
10
+ import org.junit.After
11
+ import org.junit.Assert.assertTrue
12
+ import org.junit.Before
13
+ import org.junit.Test
14
+ import org.junit.runner.RunWith
15
+ import org.robolectric.RobolectricTestRunner
16
+ import org.robolectric.annotation.Config
17
+
18
+ /**
19
+ * The `initialize` options reach the SDK before any Intent does, whatever moment the host
20
+ * delivers that Intent.
21
+ */
22
+ @RunWith(RobolectricTestRunner::class)
23
+ @Config(sdk = [35])
24
+ class InitializeOrderingTest {
25
+
26
+ private val calls = mutableListOf<String>()
27
+
28
+ /** Runs while `initialize` is still in flight, when the SDK listener is registered. */
29
+ private var onListenerRegistered: () -> Unit = {}
30
+
31
+ private lateinit var context: TestReactContext
32
+ private lateinit var module: AdShiftReactNativePluginModule
33
+
34
+ @Before
35
+ fun setUp() {
36
+ ModuleState.reset()
37
+ mockkStatic(AdShiftLib::class)
38
+ every { AdShiftLib.initSdk(any(), any()) } answers { calls += "initSdk" }
39
+ every { AdShiftLib.setDebugLog(any()) } answers { calls += "setDebugLog" }
40
+ every { AdShiftLib.setBrandedDomains(any()) } answers { calls += SET_BRANDED_DOMAINS }
41
+ every { AdShiftLib.addPushNotificationDeepLinkPath(*anyVararg()) } answers {
42
+ calls += ADD_PUSH_PATH
43
+ }
44
+ every { AdShiftLib.handleAppLinkIntent(any()) } answers { calls += HANDLE_APP_LINK }
45
+ every { AdShiftLib.handlePushNotification(any<Bundle>()) } answers {
46
+ calls += HANDLE_PUSH
47
+ false
48
+ }
49
+ every { AdShiftLib.setDeepLinkListener(any()) } answers {
50
+ calls += "setDeepLinkListener"
51
+ onListenerRegistered()
52
+ }
53
+
54
+ context = TestReactContext()
55
+ module = AdShiftReactNativePluginModule(context.reactContext)
56
+ }
57
+
58
+ @After
59
+ fun tearDown() {
60
+ unmockkStatic(AdShiftLib::class)
61
+ ModuleState.reset()
62
+ }
63
+
64
+ @Test
65
+ fun `applies options before forwarding an intent from a host resumed during initialize`() {
66
+ context.launchIntent = deepLinkIntent()
67
+ onListenerRegistered = { module.onHostResume() }
68
+
69
+ module.initialize(initializeConfig())
70
+
71
+ assertOptionsPrecedeForwarding()
72
+ }
73
+
74
+ @Test
75
+ fun `forwards an intent received before initialize once the options are applied`() {
76
+ module.onNewIntent(deepLinkIntent())
77
+
78
+ assertTrue("intent forwarded before initialize: $calls", forwardIndex() < 0)
79
+
80
+ module.initialize(initializeConfig())
81
+
82
+ assertOptionsPrecedeForwarding()
83
+ }
84
+
85
+ private fun assertOptionsPrecedeForwarding() {
86
+ val forward = forwardIndex()
87
+ assertTrue("no intent reached the SDK: $calls", forward >= 0)
88
+ assertTrue(
89
+ "branded domains applied after the intent: $calls",
90
+ calls.indexOf(SET_BRANDED_DOMAINS) in 0 until forward,
91
+ )
92
+ assertTrue(
93
+ "push path registered after the intent: $calls",
94
+ calls.indexOf(ADD_PUSH_PATH) in 0 until forward,
95
+ )
96
+ assertTrue("app link not forwarded: $calls", calls.contains(HANDLE_APP_LINK))
97
+ }
98
+
99
+ private fun forwardIndex(): Int =
100
+ calls.indexOfFirst { it == HANDLE_PUSH || it == HANDLE_APP_LINK }
101
+
102
+ private fun deepLinkIntent(): Intent =
103
+ Intent(Intent.ACTION_VIEW, Uri.parse("https://$BRANDED_DOMAIN/promo?id=42"))
104
+
105
+ private companion object {
106
+ const val SET_BRANDED_DOMAINS = "setBrandedDomains"
107
+ const val ADD_PUSH_PATH = "addPushNotificationDeepLinkPath"
108
+ const val HANDLE_APP_LINK = "handleAppLinkIntent"
109
+ const val HANDLE_PUSH = "handlePushNotification"
110
+ }
111
+ }
@@ -0,0 +1,168 @@
1
+ package com.adshift.reactnativeplugin
2
+
3
+ import android.content.Intent
4
+ import android.net.Uri
5
+ import android.os.Bundle
6
+ import com.adshift.sdk.core.AdShiftLib
7
+ import io.mockk.Runs
8
+ import io.mockk.every
9
+ import io.mockk.just
10
+ import io.mockk.mockkStatic
11
+ import io.mockk.unmockkStatic
12
+ import org.junit.After
13
+ import org.junit.Assert.assertEquals
14
+ import org.junit.Assert.assertNull
15
+ import org.junit.Assert.assertTrue
16
+ import org.junit.Before
17
+ import org.junit.Test
18
+ import org.junit.runner.RunWith
19
+ import org.robolectric.RobolectricTestRunner
20
+ import org.robolectric.annotation.Config
21
+
22
+ /**
23
+ * A link the SDK already received with an Intent is recognised when JavaScript passes the same
24
+ * link to `handleDeepLink`, while unrelated links keep travelling to the SDK.
25
+ */
26
+ @RunWith(RobolectricTestRunner::class)
27
+ @Config(sdk = [35])
28
+ class IntentLinkDedupTest {
29
+
30
+ private val forwarded = mutableListOf<String?>()
31
+
32
+ private lateinit var context: TestReactContext
33
+ private lateinit var module: AdShiftReactNativePluginModule
34
+
35
+ @Before
36
+ fun setUp() {
37
+ ModuleState.reset()
38
+ mockkStatic(AdShiftLib::class)
39
+ every { AdShiftLib.initSdk(any(), any()) } just Runs
40
+ every { AdShiftLib.setDeepLinkListener(any()) } just Runs
41
+ every { AdShiftLib.setDebugLog(any()) } just Runs
42
+ every { AdShiftLib.setBrandedDomains(any()) } just Runs
43
+ every { AdShiftLib.addPushNotificationDeepLinkPath(*anyVararg()) } just Runs
44
+ every { AdShiftLib.handlePushNotification(any<Bundle>()) } returns false
45
+ every { AdShiftLib.handleAppLinkIntent(any()) } answers {
46
+ forwarded += firstArg<Intent?>()?.dataString
47
+ }
48
+
49
+ context = TestReactContext()
50
+ module = AdShiftReactNativePluginModule(context.reactContext)
51
+ }
52
+
53
+ @After
54
+ fun tearDown() {
55
+ unmockkStatic(AdShiftLib::class)
56
+ ModuleState.reset()
57
+ }
58
+
59
+ @Test
60
+ fun `resolves a link that arrived in the notification extras`() {
61
+ context.launchIntent = notificationIntent(PUSH_LINK, MESSAGE_ID)
62
+ module.initialize(initializeConfig())
63
+
64
+ val promise = RecordingPromise()
65
+ module.handleDeepLink(PUSH_LINK, false, promise)
66
+
67
+ assertResolved(promise)
68
+ assertEquals("links forwarded to the SDK", emptyList<String>(), forwardedLinks())
69
+ }
70
+
71
+ @Test
72
+ fun `resolves a link nested in a json payload extra`() {
73
+ context.launchIntent = launcherIntent().apply {
74
+ putExtra("as_push_payload", """{"$PUSH_LINK_KEY":"$PUSH_LINK"}""")
75
+ putExtra("google.message_id", MESSAGE_ID)
76
+ }
77
+ module.initialize(initializeConfig())
78
+
79
+ val promise = RecordingPromise()
80
+ module.handleDeepLink(PUSH_LINK, false, promise)
81
+
82
+ assertResolved(promise)
83
+ assertEquals("links forwarded to the SDK", emptyList<String>(), forwardedLinks())
84
+ }
85
+
86
+ @Test
87
+ fun `forwards a link that did not arrive with the intent`() {
88
+ context.launchIntent = notificationIntent(PUSH_LINK, MESSAGE_ID)
89
+ module.initialize(initializeConfig())
90
+
91
+ val promise = RecordingPromise()
92
+ module.handleDeepLink(OTHER_LINK, false, promise)
93
+
94
+ assertResolved(promise)
95
+ assertEquals("links forwarded to the SDK", listOf(OTHER_LINK), forwardedLinks())
96
+ }
97
+
98
+ @Test
99
+ fun `resolves a link that arrived as the intent data uri`() {
100
+ context.launchIntent = Intent(Intent.ACTION_VIEW, Uri.parse(PUSH_LINK))
101
+ module.initialize(initializeConfig())
102
+
103
+ val promise = RecordingPromise()
104
+ module.handleDeepLink(PUSH_LINK, false, promise)
105
+
106
+ assertResolved(promise)
107
+ assertEquals("links forwarded to the SDK", listOf(PUSH_LINK), forwardedLinks())
108
+ }
109
+
110
+ @Test
111
+ fun `keeps the launch link once when javascript reloads long after the launch`() {
112
+ context.launchIntent = Intent(Intent.ACTION_VIEW, Uri.parse(PUSH_LINK))
113
+ module.initialize(initializeConfig())
114
+ assertEquals("links forwarded on launch", listOf(PUSH_LINK), forwardedLinks())
115
+ ModuleState.expireAutoForwardWindow()
116
+
117
+ val promise = RecordingPromise()
118
+ module.handleDeepLink(PUSH_LINK, true, promise)
119
+
120
+ assertResolved(promise)
121
+ assertEquals("links forwarded to the SDK", listOf(PUSH_LINK), forwardedLinks())
122
+ }
123
+
124
+ @Test
125
+ fun `forwards a second tap on the launch link because it is not a replay`() {
126
+ context.launchIntent = Intent(Intent.ACTION_VIEW, Uri.parse(PUSH_LINK))
127
+ module.initialize(initializeConfig())
128
+ ModuleState.expireAutoForwardWindow()
129
+
130
+ val promise = RecordingPromise()
131
+ module.handleDeepLink(PUSH_LINK, false, promise)
132
+
133
+ assertResolved(promise)
134
+ assertEquals(
135
+ "links forwarded to the SDK",
136
+ listOf(PUSH_LINK, PUSH_LINK),
137
+ forwardedLinks(),
138
+ )
139
+ }
140
+
141
+ @Test
142
+ fun `keeps a launch link once when it never arrived with an intent`() {
143
+ context.launchIntent = launcherIntent()
144
+ module.initialize(initializeConfig())
145
+
146
+ module.handleDeepLink(PUSH_LINK, true, RecordingPromise())
147
+ ModuleState.expireAutoForwardWindow()
148
+ val replay = RecordingPromise()
149
+ module.handleDeepLink(PUSH_LINK, true, replay)
150
+
151
+ assertResolved(replay)
152
+ assertEquals("links forwarded to the SDK", listOf(PUSH_LINK), forwardedLinks())
153
+ }
154
+
155
+ private fun assertResolved(promise: RecordingPromise) {
156
+ assertNull("rejection code", promise.rejectionCode)
157
+ assertTrue("promise resolved", promise.resolved)
158
+ }
159
+
160
+ /** URLs the SDK received, ignoring Intents that carry none. */
161
+ private fun forwardedLinks(): List<String> = forwarded.filterNotNull()
162
+
163
+ private companion object {
164
+ const val PUSH_LINK = "https://$BRANDED_DOMAIN/promo?id=42"
165
+ const val OTHER_LINK = "https://$BRANDED_DOMAIN/other?id=7"
166
+ const val MESSAGE_ID = "0:1757600000000000%abcdef"
167
+ }
168
+ }