@adshift/react-native-plugin 2.1.0 → 3.1.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 (185) hide show
  1. package/AdShiftReactNativePlugin.podspec +20 -4
  2. package/CHANGELOG.md +57 -8
  3. package/README.md +131 -74
  4. package/android/build.gradle +3 -2
  5. package/android/consumer-rules.pro +2 -21
  6. package/android/src/main/AndroidManifest.xml +14 -1
  7. package/android/src/main/java/com/adshift/reactnativeplugin/ActivityIntents.kt +307 -0
  8. package/android/src/main/java/com/adshift/reactnativeplugin/AdShiftReactNativePluginModule.kt +90 -153
  9. package/android/src/test/java/com/adshift/reactnativeplugin/ConsentSnapshotTest.kt +0 -20
  10. package/android/src/test/java/com/adshift/reactnativeplugin/DeepLinkDeliveryTest.kt +40 -7
  11. package/android/src/test/java/com/adshift/reactnativeplugin/InitializeOrderingTest.kt +51 -5
  12. package/android/src/test/java/com/adshift/reactnativeplugin/IntentLinkDedupTest.kt +0 -5
  13. package/android/src/test/java/com/adshift/reactnativeplugin/LaunchIntentForwardingTest.kt +21 -11
  14. package/android/src/test/java/com/adshift/reactnativeplugin/LaunchLinkRecordTest.kt +420 -0
  15. package/android/src/test/java/com/adshift/reactnativeplugin/ModuleTestSupport.kt +97 -25
  16. package/android/src/test/java/com/adshift/reactnativeplugin/StartVerdictTest.kt +0 -4
  17. package/ios/AdShiftReactNativePlugin.h +0 -12
  18. package/ios/AdShiftReactNativePlugin.mm +7 -14
  19. package/ios/AdShiftReactNativePluginHelper.swift +38 -73
  20. package/ios/Core/BridgeRules.swift +42 -30
  21. package/lib/commonjs/Adshift.js +100 -284
  22. package/lib/commonjs/Adshift.js.map +1 -1
  23. package/lib/commonjs/NativeAdShiftReactNativePlugin.js +2 -11
  24. package/lib/commonjs/NativeAdShiftReactNativePlugin.js.map +1 -1
  25. package/lib/commonjs/index.js +6 -0
  26. package/lib/commonjs/index.js.map +1 -1
  27. package/lib/commonjs/jest/index.js +6 -18
  28. package/lib/commonjs/jest/index.js.map +1 -1
  29. package/lib/commonjs/nativeSdkVersions.js +4 -6
  30. package/lib/commonjs/nativeSdkVersions.js.map +1 -1
  31. package/lib/commonjs/types/AdshiftAdRevenue.js +0 -24
  32. package/lib/commonjs/types/AdshiftAdRevenue.js.map +1 -1
  33. package/lib/commonjs/types/AdshiftConfig.js +11 -23
  34. package/lib/commonjs/types/AdshiftConfig.js.map +1 -1
  35. package/lib/commonjs/types/AdshiftConsent.js +9 -45
  36. package/lib/commonjs/types/AdshiftConsent.js.map +1 -1
  37. package/lib/commonjs/types/AdshiftConsentSnapshot.js +5 -24
  38. package/lib/commonjs/types/AdshiftConsentSnapshot.js.map +1 -1
  39. package/lib/commonjs/types/AdshiftDeepLink.js +50 -33
  40. package/lib/commonjs/types/AdshiftDeepLink.js.map +1 -1
  41. package/lib/commonjs/types/AdshiftError.js +7 -27
  42. package/lib/commonjs/types/AdshiftError.js.map +1 -1
  43. package/lib/commonjs/types/AdshiftEventParam.js +4 -38
  44. package/lib/commonjs/types/AdshiftEventParam.js.map +1 -1
  45. package/lib/commonjs/types/AdshiftEventType.js +1 -39
  46. package/lib/commonjs/types/AdshiftEventType.js.map +1 -1
  47. package/lib/commonjs/types/AdshiftThirdPartySharing.js +7 -38
  48. package/lib/commonjs/types/AdshiftThirdPartySharing.js.map +1 -1
  49. package/lib/commonjs/types/index.js +12 -0
  50. package/lib/commonjs/types/index.js.map +1 -1
  51. package/lib/commonjs/version.js +1 -1
  52. package/lib/module/Adshift.js +101 -285
  53. package/lib/module/Adshift.js.map +1 -1
  54. package/lib/module/NativeAdShiftReactNativePlugin.js +2 -12
  55. package/lib/module/NativeAdShiftReactNativePlugin.js.map +1 -1
  56. package/lib/module/index.js +2 -47
  57. package/lib/module/index.js.map +1 -1
  58. package/lib/module/jest/index.js +7 -19
  59. package/lib/module/jest/index.js.map +1 -1
  60. package/lib/module/nativeSdkVersions.js +4 -6
  61. package/lib/module/nativeSdkVersions.js.map +1 -1
  62. package/lib/module/types/AdshiftAdRevenue.js +0 -24
  63. package/lib/module/types/AdshiftAdRevenue.js.map +1 -1
  64. package/lib/module/types/AdshiftConfig.js +10 -23
  65. package/lib/module/types/AdshiftConfig.js.map +1 -1
  66. package/lib/module/types/AdshiftConsent.js +9 -45
  67. package/lib/module/types/AdshiftConsent.js.map +1 -1
  68. package/lib/module/types/AdshiftConsentSnapshot.js +5 -24
  69. package/lib/module/types/AdshiftConsentSnapshot.js.map +1 -1
  70. package/lib/module/types/AdshiftDeepLink.js +49 -32
  71. package/lib/module/types/AdshiftDeepLink.js.map +1 -1
  72. package/lib/module/types/AdshiftError.js +7 -28
  73. package/lib/module/types/AdshiftError.js.map +1 -1
  74. package/lib/module/types/AdshiftEventParam.js +4 -38
  75. package/lib/module/types/AdshiftEventParam.js.map +1 -1
  76. package/lib/module/types/AdshiftEventType.js +1 -39
  77. package/lib/module/types/AdshiftEventType.js.map +1 -1
  78. package/lib/module/types/AdshiftThirdPartySharing.js +7 -38
  79. package/lib/module/types/AdshiftThirdPartySharing.js.map +1 -1
  80. package/lib/module/types/index.js +2 -2
  81. package/lib/module/types/index.js.map +1 -1
  82. package/lib/module/version.js +1 -1
  83. package/lib/typescript/commonjs/src/Adshift.d.ts +68 -184
  84. package/lib/typescript/commonjs/src/Adshift.d.ts.map +1 -1
  85. package/lib/typescript/commonjs/src/NativeAdShiftReactNativePlugin.d.ts +18 -35
  86. package/lib/typescript/commonjs/src/NativeAdShiftReactNativePlugin.d.ts.map +1 -1
  87. package/lib/typescript/commonjs/src/index.d.ts +2 -34
  88. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  89. package/lib/typescript/commonjs/src/jest/index.d.ts +9 -21
  90. package/lib/typescript/commonjs/src/jest/index.d.ts.map +1 -1
  91. package/lib/typescript/commonjs/src/nativeSdkVersions.d.ts +4 -6
  92. package/lib/typescript/commonjs/src/nativeSdkVersions.d.ts.map +1 -1
  93. package/lib/typescript/commonjs/src/types/AdshiftAdRevenue.d.ts +0 -24
  94. package/lib/typescript/commonjs/src/types/AdshiftAdRevenue.d.ts.map +1 -1
  95. package/lib/typescript/commonjs/src/types/AdshiftConfig.d.ts +35 -39
  96. package/lib/typescript/commonjs/src/types/AdshiftConfig.d.ts.map +1 -1
  97. package/lib/typescript/commonjs/src/types/AdshiftConsent.d.ts +9 -49
  98. package/lib/typescript/commonjs/src/types/AdshiftConsent.d.ts.map +1 -1
  99. package/lib/typescript/commonjs/src/types/AdshiftConsentSnapshot.d.ts +10 -28
  100. package/lib/typescript/commonjs/src/types/AdshiftConsentSnapshot.d.ts.map +1 -1
  101. package/lib/typescript/commonjs/src/types/AdshiftDeepLink.d.ts +55 -39
  102. package/lib/typescript/commonjs/src/types/AdshiftDeepLink.d.ts.map +1 -1
  103. package/lib/typescript/commonjs/src/types/AdshiftError.d.ts +7 -26
  104. package/lib/typescript/commonjs/src/types/AdshiftError.d.ts.map +1 -1
  105. package/lib/typescript/commonjs/src/types/AdshiftEventParam.d.ts +3 -19
  106. package/lib/typescript/commonjs/src/types/AdshiftEventParam.d.ts.map +1 -1
  107. package/lib/typescript/commonjs/src/types/AdshiftEventType.d.ts +1 -23
  108. package/lib/typescript/commonjs/src/types/AdshiftEventType.d.ts.map +1 -1
  109. package/lib/typescript/commonjs/src/types/AdshiftThirdPartySharing.d.ts +7 -38
  110. package/lib/typescript/commonjs/src/types/AdshiftThirdPartySharing.d.ts.map +1 -1
  111. package/lib/typescript/commonjs/src/types/index.d.ts +3 -3
  112. package/lib/typescript/commonjs/src/types/index.d.ts.map +1 -1
  113. package/lib/typescript/commonjs/src/version.d.ts +1 -1
  114. package/lib/typescript/module/src/Adshift.d.ts +68 -184
  115. package/lib/typescript/module/src/Adshift.d.ts.map +1 -1
  116. package/lib/typescript/module/src/NativeAdShiftReactNativePlugin.d.ts +18 -35
  117. package/lib/typescript/module/src/NativeAdShiftReactNativePlugin.d.ts.map +1 -1
  118. package/lib/typescript/module/src/index.d.ts +2 -34
  119. package/lib/typescript/module/src/index.d.ts.map +1 -1
  120. package/lib/typescript/module/src/jest/index.d.ts +9 -21
  121. package/lib/typescript/module/src/jest/index.d.ts.map +1 -1
  122. package/lib/typescript/module/src/nativeSdkVersions.d.ts +4 -6
  123. package/lib/typescript/module/src/nativeSdkVersions.d.ts.map +1 -1
  124. package/lib/typescript/module/src/types/AdshiftAdRevenue.d.ts +0 -24
  125. package/lib/typescript/module/src/types/AdshiftAdRevenue.d.ts.map +1 -1
  126. package/lib/typescript/module/src/types/AdshiftConfig.d.ts +35 -39
  127. package/lib/typescript/module/src/types/AdshiftConfig.d.ts.map +1 -1
  128. package/lib/typescript/module/src/types/AdshiftConsent.d.ts +9 -49
  129. package/lib/typescript/module/src/types/AdshiftConsent.d.ts.map +1 -1
  130. package/lib/typescript/module/src/types/AdshiftConsentSnapshot.d.ts +10 -28
  131. package/lib/typescript/module/src/types/AdshiftConsentSnapshot.d.ts.map +1 -1
  132. package/lib/typescript/module/src/types/AdshiftDeepLink.d.ts +55 -39
  133. package/lib/typescript/module/src/types/AdshiftDeepLink.d.ts.map +1 -1
  134. package/lib/typescript/module/src/types/AdshiftError.d.ts +7 -26
  135. package/lib/typescript/module/src/types/AdshiftError.d.ts.map +1 -1
  136. package/lib/typescript/module/src/types/AdshiftEventParam.d.ts +3 -19
  137. package/lib/typescript/module/src/types/AdshiftEventParam.d.ts.map +1 -1
  138. package/lib/typescript/module/src/types/AdshiftEventType.d.ts +1 -23
  139. package/lib/typescript/module/src/types/AdshiftEventType.d.ts.map +1 -1
  140. package/lib/typescript/module/src/types/AdshiftThirdPartySharing.d.ts +7 -38
  141. package/lib/typescript/module/src/types/AdshiftThirdPartySharing.d.ts.map +1 -1
  142. package/lib/typescript/module/src/types/index.d.ts +3 -3
  143. package/lib/typescript/module/src/types/index.d.ts.map +1 -1
  144. package/lib/typescript/module/src/version.d.ts +1 -1
  145. package/package.json +11 -1
  146. package/src/Adshift.ts +118 -285
  147. package/src/NativeAdShiftReactNativePlugin.ts +19 -47
  148. package/src/index.tsx +7 -46
  149. package/src/jest/index.ts +11 -20
  150. package/src/nativeSdkVersions.ts +4 -6
  151. package/src/types/AdshiftAdRevenue.ts +0 -24
  152. package/src/types/AdshiftConfig.ts +48 -55
  153. package/src/types/AdshiftConsent.ts +9 -49
  154. package/src/types/AdshiftConsentSnapshot.ts +11 -38
  155. package/src/types/AdshiftDeepLink.ts +87 -39
  156. package/src/types/AdshiftError.ts +7 -26
  157. package/src/types/AdshiftEventParam.ts +4 -38
  158. package/src/types/AdshiftEventType.ts +1 -39
  159. package/src/types/AdshiftThirdPartySharing.ts +7 -38
  160. package/src/types/index.ts +9 -2
  161. package/src/version.ts +1 -1
  162. package/vendor/ios/AdshiftSDK.files.json +30 -0
  163. package/vendor/ios/AdshiftSDK.xcframework/Info.plist +44 -0
  164. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/AdshiftSDK +0 -0
  165. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Headers/AdshiftSDK-Swift.h +315 -0
  166. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Info.plist +0 -0
  167. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.abi.json +18729 -0
  168. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.private.swiftinterface +414 -0
  169. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.swiftdoc +0 -0
  170. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.swiftinterface +414 -0
  171. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/module.modulemap +4 -0
  172. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/PrivacyInfo.xcprivacy +132 -0
  173. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/AdshiftSDK +0 -0
  174. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Headers/AdshiftSDK-Swift.h +626 -0
  175. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Info.plist +0 -0
  176. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.abi.json +18729 -0
  177. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface +414 -0
  178. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.swiftdoc +0 -0
  179. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.swiftinterface +414 -0
  180. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.abi.json +18729 -0
  181. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.private.swiftinterface +414 -0
  182. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.swiftdoc +0 -0
  183. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +414 -0
  184. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/module.modulemap +4 -0
  185. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/PrivacyInfo.xcprivacy +132 -0
@@ -3,22 +3,21 @@ package com.adshift.reactnativeplugin
3
3
  import android.content.Intent
4
4
  import android.net.Uri
5
5
  import android.os.Bundle
6
+ import android.os.Looper
6
7
  import com.adshift.sdk.core.AdShiftLib
7
8
  import io.mockk.every
8
9
  import io.mockk.mockkStatic
9
10
  import io.mockk.unmockkStatic
10
11
  import org.junit.After
12
+ import org.junit.Assert.assertFalse
11
13
  import org.junit.Assert.assertTrue
12
14
  import org.junit.Before
13
15
  import org.junit.Test
14
16
  import org.junit.runner.RunWith
15
17
  import org.robolectric.RobolectricTestRunner
18
+ import org.robolectric.Shadows.shadowOf
16
19
  import org.robolectric.annotation.Config
17
20
 
18
- /**
19
- * The `initialize` options reach the SDK before any Intent does, whatever moment the host
20
- * delivers that Intent.
21
- */
22
21
  @RunWith(RobolectricTestRunner::class)
23
22
  @Config(sdk = [35])
24
23
  class InitializeOrderingTest {
@@ -35,9 +34,16 @@ class InitializeOrderingTest {
35
34
  fun setUp() {
36
35
  ModuleState.reset()
37
36
  mockkStatic(AdShiftLib::class)
38
- every { AdShiftLib.initSdk(any(), any()) } answers { calls += "initSdk" }
37
+ every { AdShiftLib.initSdk(any(), any()) } answers { calls += INIT_SDK }
38
+ // As the SDK does: naming the plugin before initSdk throws.
39
+ every { AdShiftLib.setWrapper(any()) } answers {
40
+ if (INIT_SDK !in calls) throw IllegalArgumentException("SDK not initialized")
41
+ calls += SET_WRAPPER
42
+ }
43
+ every { AdShiftLib.start(any()) } answers { calls += START }
39
44
  every { AdShiftLib.setDebugLog(any()) } answers { calls += "setDebugLog" }
40
45
  every { AdShiftLib.setBrandedDomains(any()) } answers { calls += SET_BRANDED_DOMAINS }
46
+ every { AdShiftLib.setTestMode(any()) } answers { calls += SET_TEST_MODE }
41
47
  every { AdShiftLib.addPushNotificationDeepLinkPath(*anyVararg()) } answers {
42
48
  calls += ADD_PUSH_PATH
43
49
  }
@@ -82,6 +88,42 @@ class InitializeOrderingTest {
82
88
  assertOptionsPrecedeForwarding()
83
89
  }
84
90
 
91
+ @Test
92
+ fun `turns test mode on before forwarding an intent`() {
93
+ context.launchIntent = deepLinkIntent()
94
+
95
+ module.initialize(initializeConfig().apply { putBoolean("testMode", true) })
96
+
97
+ assertTrue(
98
+ "test mode set after the intent: $calls",
99
+ calls.indexOf(SET_TEST_MODE) in 0 until forwardIndex(),
100
+ )
101
+ }
102
+
103
+ @Test
104
+ fun `names the plugin right after the sdk is initialized and before start`() {
105
+ context.launchIntent = deepLinkIntent()
106
+
107
+ module.initialize(initializeConfig())
108
+ module.start(RecordingPromise())
109
+ shadowOf(Looper.getMainLooper()).idle()
110
+
111
+ val wrapper = calls.indexOf(SET_WRAPPER)
112
+ assertTrue("plugin not named: $calls", wrapper >= 0)
113
+ assertTrue("named before initSdk: $calls", calls.indexOf(INIT_SDK) in 0 until wrapper)
114
+ assertTrue("named after start: $calls", wrapper < calls.indexOf(START))
115
+ assertTrue("named after an intent was forwarded: $calls", wrapper < forwardIndex())
116
+ }
117
+
118
+ @Test
119
+ fun `does not name the plugin when the sdk could not be initialized`() {
120
+ every { AdShiftLib.initSdk(any(), any()) } throws IllegalStateException("no context")
121
+
122
+ module.initialize(initializeConfig())
123
+
124
+ assertFalse("named without an SDK: $calls", calls.contains(SET_WRAPPER))
125
+ }
126
+
85
127
  private fun assertOptionsPrecedeForwarding() {
86
128
  val forward = forwardIndex()
87
129
  assertTrue("no intent reached the SDK: $calls", forward >= 0)
@@ -103,7 +145,11 @@ class InitializeOrderingTest {
103
145
  Intent(Intent.ACTION_VIEW, Uri.parse("https://$BRANDED_DOMAIN/promo?id=42"))
104
146
 
105
147
  private companion object {
148
+ const val INIT_SDK = "initSdk"
149
+ const val SET_WRAPPER = "setWrapper"
150
+ const val START = "start"
106
151
  const val SET_BRANDED_DOMAINS = "setBrandedDomains"
152
+ const val SET_TEST_MODE = "setTestMode"
107
153
  const val ADD_PUSH_PATH = "addPushNotificationDeepLinkPath"
108
154
  const val HANDLE_APP_LINK = "handleAppLinkIntent"
109
155
  const val HANDLE_PUSH = "handlePushNotification"
@@ -19,10 +19,6 @@ import org.junit.runner.RunWith
19
19
  import org.robolectric.RobolectricTestRunner
20
20
  import org.robolectric.annotation.Config
21
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
22
  @RunWith(RobolectricTestRunner::class)
27
23
  @Config(sdk = [35])
28
24
  class IntentLinkDedupTest {
@@ -157,7 +153,6 @@ class IntentLinkDedupTest {
157
153
  assertTrue("promise resolved", promise.resolved)
158
154
  }
159
155
 
160
- /** URLs the SDK received, ignoring Intents that carry none. */
161
156
  private fun forwardedLinks(): List<String> = forwarded.filterNotNull()
162
157
 
163
158
  private companion object {
@@ -1,6 +1,7 @@
1
1
  package com.adshift.reactnativeplugin
2
2
 
3
3
  import android.content.Intent
4
+ import android.net.Uri
4
5
  import android.os.Bundle
5
6
  import com.adshift.sdk.core.AdShiftLib
6
7
  import io.mockk.Runs
@@ -17,15 +18,12 @@ import org.junit.runner.RunWith
17
18
  import org.robolectric.RobolectricTestRunner
18
19
  import org.robolectric.annotation.Config
19
20
 
20
- /**
21
- * Launch Intents reach the SDK once per delivery: a new payload is forwarded, while the same
22
- * payload redelivered to a recreated Activity is not.
23
- */
24
21
  @RunWith(RobolectricTestRunner::class)
25
22
  @Config(sdk = [35])
26
23
  class LaunchIntentForwardingTest {
27
24
 
28
25
  private val forwardedExtras = mutableListOf<Bundle?>()
26
+ private val forwardedLinks = mutableListOf<String?>()
29
27
 
30
28
  private lateinit var context: TestReactContext
31
29
  private lateinit var module: AdShiftReactNativePluginModule
@@ -39,7 +37,9 @@ class LaunchIntentForwardingTest {
39
37
  every { AdShiftLib.setDebugLog(any()) } just Runs
40
38
  every { AdShiftLib.setBrandedDomains(any()) } just Runs
41
39
  every { AdShiftLib.addPushNotificationDeepLinkPath(*anyVararg()) } just Runs
42
- every { AdShiftLib.handleAppLinkIntent(any()) } just Runs
40
+ every { AdShiftLib.handleAppLinkIntent(any()) } answers {
41
+ forwardedLinks += firstArg<Intent?>()?.dataString
42
+ }
43
43
  every { AdShiftLib.handlePushNotification(any<Bundle>()) } answers {
44
44
  forwardedExtras += firstArg<Bundle?>()
45
45
  false
@@ -70,14 +70,11 @@ class LaunchIntentForwardingTest {
70
70
 
71
71
  @Test
72
72
  fun `skips a launch intent redelivered to a recreated activity`() {
73
- context.launchIntent = launcherIntent()
73
+ context.activity = FakeActivity(notificationIntent(PUSH_LINK, MESSAGE_ID)).create(null)
74
74
  module.initialize(initializeConfig())
75
75
 
76
- context.launchIntent = notificationIntent(PUSH_LINK, MESSAGE_ID)
77
- module.onHostResume()
78
-
79
- // Activity recreation hands over an equal Intent in a new instance.
80
- context.launchIntent = notificationIntent(PUSH_LINK, MESSAGE_ID)
76
+ // A rotation: the Activity is re-created from saved state with an equal Intent.
77
+ context.activity = FakeActivity(notificationIntent(PUSH_LINK, MESSAGE_ID)).create(Bundle())
81
78
  module.onHostResume()
82
79
 
83
80
  assertEquals(
@@ -99,6 +96,19 @@ class LaunchIntentForwardingTest {
99
96
  assertEquals("deliveries forwarded to the SDK", 1, forwardedExtras.size)
100
97
  }
101
98
 
99
+ @Test
100
+ fun `forwards a link tapped after the activity was re-created`() {
101
+ val opened = Intent(Intent.ACTION_VIEW, Uri.parse(PUSH_LINK))
102
+ context.activity = FakeActivity(opened).create(null)
103
+ module.initialize(initializeConfig())
104
+
105
+ context.activity = recreateActivityHolding(Intent(Intent.ACTION_VIEW, Uri.parse(PUSH_LINK)))
106
+ module.onHostResume()
107
+ module.onNewIntent(Intent(Intent.ACTION_VIEW, Uri.parse(PUSH_LINK)))
108
+
109
+ assertEquals(listOf<String?>(PUSH_LINK, PUSH_LINK), forwardedLinks)
110
+ }
111
+
102
112
  private companion object {
103
113
  const val PUSH_LINK = "https://links.example.com/promo?id=42"
104
114
  const val MESSAGE_ID = "0:1757600000000000%abcdef"
@@ -0,0 +1,420 @@
1
+ package com.adshift.reactnativeplugin
2
+
3
+ import android.app.ActivityManager
4
+ import android.content.Context
5
+ import android.content.Intent
6
+ import android.net.Uri
7
+ import android.os.Bundle
8
+ import android.os.Looper
9
+ import androidx.test.core.app.ApplicationProvider
10
+ import com.adshift.sdk.core.AdShiftLib
11
+ import com.facebook.react.bridge.JavaOnlyMap
12
+ import io.mockk.Runs
13
+ import io.mockk.every
14
+ import io.mockk.just
15
+ import io.mockk.mockk
16
+ import io.mockk.mockkStatic
17
+ import io.mockk.unmockkStatic
18
+ import org.junit.After
19
+ import org.junit.Assert.assertEquals
20
+ import org.junit.Assert.assertFalse
21
+ import org.junit.Assert.assertNull
22
+ import org.junit.Assert.assertTrue
23
+ import org.junit.Before
24
+ import org.junit.Test
25
+ import org.junit.runner.RunWith
26
+ import org.robolectric.RobolectricTestRunner
27
+ import org.robolectric.Shadows.shadowOf
28
+ import org.robolectric.annotation.Config
29
+ import org.robolectric.shadows.ShadowAppTask
30
+
31
+ @RunWith(RobolectricTestRunner::class)
32
+ @Config(sdk = [35])
33
+ class LaunchLinkRecordTest {
34
+
35
+ private val forwarded = mutableListOf<String?>()
36
+ private val wrappers = mutableListOf<String?>()
37
+
38
+ private lateinit var context: TestReactContext
39
+ private lateinit var module: AdShiftReactNativePluginModule
40
+
41
+ @Before
42
+ fun setUp() {
43
+ ModuleState.reset()
44
+ mockkStatic(AdShiftLib::class)
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
+ every { AdShiftLib.setWrapper(any()) } answers { wrappers += firstArg<String?>() }
51
+ every { AdShiftLib.handlePushNotification(any<Bundle>()) } returns false
52
+ every { AdShiftLib.handleAppLinkIntent(any()) } answers {
53
+ forwarded += firstArg<Intent?>()?.dataString
54
+ }
55
+ newProcess()
56
+ }
57
+
58
+ @After
59
+ fun tearDown() {
60
+ unmockkStatic(AdShiftLib::class)
61
+ ModuleState.reset()
62
+ }
63
+
64
+ @Test
65
+ fun `skips the link of an activity re-created in a new process`() {
66
+ openedBy(link()).also { module.initialize(initializeConfig()) }
67
+
68
+ newProcess()
69
+ context.activity = FakeActivity(link()).create(Bundle())
70
+ module.initialize(initializeConfig())
71
+ module.onHostResume()
72
+
73
+ assertEquals(listOf(LINK), links())
74
+ }
75
+
76
+ @Test
77
+ fun `forwards the link of a process killed before initialize`() {
78
+ // The app waits for consent before initialize(); Android kills it in the background.
79
+ openedBy(link())
80
+ module.onHostResume()
81
+
82
+ newProcess()
83
+ context.activity = FakeActivity(link()).create(Bundle())
84
+ module.initialize(initializeConfig())
85
+
86
+ assertEquals(listOf(LINK), links())
87
+ }
88
+
89
+ @Test
90
+ @Config(sdk = [28])
91
+ fun `android 9 skips the link of a re-created activity whose app passes no state`() {
92
+ openedBy(link(), appPassesState = false).also { module.initialize(initializeConfig()) }
93
+
94
+ newProcess()
95
+ context.activity = FakeActivity(link()).create(Bundle(), appPassesState = false)
96
+ module.initialize(initializeConfig())
97
+
98
+ assertEquals(listOf(LINK), links())
99
+ }
100
+
101
+ @Test
102
+ @Config(sdk = [24])
103
+ fun `android 7 skips the link of a re-created activity whose app passes no state`() {
104
+ openedBy(link(), appPassesState = false).also { module.initialize(initializeConfig()) }
105
+
106
+ newProcess()
107
+ context.activity = FakeActivity(link()).create(Bundle(), appPassesState = false)
108
+ module.initialize(initializeConfig())
109
+
110
+ assertEquals(listOf(LINK), links())
111
+ }
112
+
113
+ @Test
114
+ @Config(sdk = [28])
115
+ fun `android 9 skips the link of a re-created activity whose app passes its state`() {
116
+ openedBy(link()).also { module.initialize(initializeConfig()) }
117
+
118
+ newProcess()
119
+ context.activity = FakeActivity(link()).create(Bundle())
120
+ module.initialize(initializeConfig())
121
+
122
+ assertEquals(listOf(LINK), links())
123
+ }
124
+
125
+ @Test
126
+ fun `skips the link of a re-created activity whose intent another library marked`() {
127
+ // Another library adds to the Intent before initialize().
128
+ val first = link()
129
+ context.activity = FakeActivity(first).create(null)
130
+ first.putExtra("consumed_at", 1_000L)
131
+ module.initialize(initializeConfig())
132
+
133
+ newProcess()
134
+ val restored = link()
135
+ context.activity = FakeActivity(restored).create(Bundle())
136
+ restored.putExtra("consumed_at", 2_000L)
137
+ module.initialize(initializeConfig())
138
+
139
+ assertEquals(listOf(LINK), links())
140
+ }
141
+
142
+ @Test
143
+ fun `initialize survives a launch intent whose extras cannot be read`() {
144
+ // Below Android 13 a Bundle holding a class the app does not know throws on unparcelling.
145
+ val unreadable = mockk<Intent>(relaxed = true)
146
+ val extras = mockk<Bundle>(relaxed = true)
147
+ every { unreadable.data } returns null
148
+ every { unreadable.dataString } returns null
149
+ every { unreadable.flags } returns 0
150
+ every { unreadable.extras } returns extras
151
+ every { extras.isEmpty } throws RuntimeException("Parcelable encountered ClassNotFoundException")
152
+ context.activity = FakeActivity(unreadable).create(null)
153
+
154
+ module.initialize(initializeConfig())
155
+
156
+ assertEquals("the Intent still reaches the SDK", 1, forwarded.size)
157
+ }
158
+
159
+ @Test
160
+ @Config(sdk = [28])
161
+ fun `android 9 forwards the same link tapped again after the process was killed`() {
162
+ openedBy(link(), appPassesState = false).also { module.initialize(initializeConfig()) }
163
+
164
+ // The tap re-creates the Activity with its first Intent; MainActivity keeps the new one.
165
+ newProcess()
166
+ context.activity = FakeActivity(link()).create(Bundle(), appPassesState = false)
167
+ .apply { keepNewIntent(link()) }
168
+ module.initialize(initializeConfig())
169
+ module.onHostResume()
170
+
171
+ assertEquals(listOf(LINK, LINK), links())
172
+ }
173
+
174
+ @Test
175
+ @Config(sdk = [24])
176
+ fun `android 7 forwards the same link tapped again after the process was killed`() {
177
+ openedBy(link(), appPassesState = false).also { module.initialize(initializeConfig()) }
178
+
179
+ newProcess()
180
+ context.activity = FakeActivity(link()).create(Bundle(), appPassesState = false)
181
+ .apply { keepNewIntent(link()) }
182
+ module.initialize(initializeConfig())
183
+
184
+ assertEquals(listOf(LINK, LINK), links())
185
+ }
186
+
187
+ @Test
188
+ fun `forwards the same link tapped again after the process was killed`() {
189
+ openedBy(link()).also { module.initialize(initializeConfig()) }
190
+
191
+ newProcess()
192
+ context.activity = FakeActivity(link()).create(Bundle())
193
+ module.initialize(initializeConfig())
194
+ module.onNewIntent(link().also { context.activity?.keepNewIntent(it) })
195
+
196
+ assertEquals(listOf(LINK, LINK), links())
197
+ }
198
+
199
+ @Test
200
+ @Config(sdk = [28])
201
+ fun `android 9 opened from the icon after the process was killed sends the old link nowhere`() {
202
+ openedBy(link(), appPassesState = false).also { module.initialize(initializeConfig()) }
203
+
204
+ newProcess()
205
+ context.activity = FakeActivity(link()).create(Bundle(), appPassesState = false)
206
+ .apply { keepNewIntent(launcherIntent()) }
207
+ module.initialize(initializeConfig())
208
+ module.onNewIntent(link().also { context.activity?.keepNewIntent(it) })
209
+
210
+ assertEquals("the old link once, the new tap once", listOf(LINK, LINK), links())
211
+ }
212
+
213
+ @Test
214
+ fun `forwards the same link opening a new activity after back closed the first`() {
215
+ openedBy(link()).also { module.initialize(initializeConfig()) }
216
+ context.activity?.finish()
217
+
218
+ ModuleState.expireAutoForwardWindow()
219
+ context.activity = FakeActivity(link()).create(null)
220
+ module.onHostResume()
221
+
222
+ assertEquals(listOf(LINK, LINK), links())
223
+ }
224
+
225
+ @Test
226
+ @Config(sdk = [28])
227
+ fun `android 9 forwards the same link opening a new activity after back closed the first`() {
228
+ openedBy(link(), appPassesState = false).also { module.initialize(initializeConfig()) }
229
+ context.activity?.finish()
230
+
231
+ ModuleState.expireAutoForwardWindow()
232
+ context.activity = FakeActivity(link()).create(null, appPassesState = false)
233
+ module.onHostResume()
234
+
235
+ assertEquals(listOf(LINK, LINK), links())
236
+ }
237
+
238
+ @Test
239
+ @Config(sdk = [28])
240
+ fun `android 9 forwards a link in a task whose number came back after a restart`() {
241
+ LaunchRecordsState.restartPhone(bootCount = 7)
242
+ newProcess()
243
+ openedBy(link(), appPassesState = false).also { module.initialize(initializeConfig()) }
244
+
245
+ LaunchRecordsState.restartPhone(bootCount = 8)
246
+ newProcess()
247
+ openedBy(link(), appPassesState = false).also { module.initialize(initializeConfig()) }
248
+
249
+ assertEquals(listOf(LINK, LINK), links())
250
+ }
251
+
252
+ @Test
253
+ @Config(sdk = [24])
254
+ fun `android 7 forwards the same link in a new task that took the old task's number`() {
255
+ taskFirstActiveAt(1_000L)
256
+ openedBy(link()).also { module.initialize(initializeConfig()) }
257
+
258
+ // Removed from Recents while killed: Android 7 gives the number to the next new task.
259
+ newProcess()
260
+ taskFirstActiveAt(2_000L)
261
+ openedBy(link())
262
+ module.initialize(initializeConfig())
263
+
264
+ assertEquals(listOf(LINK, LINK), links())
265
+ }
266
+
267
+ @Test
268
+ @Config(sdk = [24])
269
+ fun `android 7 skips the link of an activity re-created in its own task`() {
270
+ taskFirstActiveAt(1_000L)
271
+ openedBy(link(), appPassesState = false).also { module.initialize(initializeConfig()) }
272
+
273
+ newProcess()
274
+ context.activity = FakeActivity(link()).create(Bundle(), appPassesState = false)
275
+ module.initialize(initializeConfig())
276
+
277
+ assertEquals(listOf(LINK), links())
278
+ }
279
+
280
+ @Test
281
+ fun `skips the launch link the app passes again after the activity was re-created`() {
282
+ openedBy(link())
283
+ module.initialize(manualConfig())
284
+ module.handleDeepLink(LINK, false, RecordingPromise())
285
+
286
+ newProcess()
287
+ context.activity = FakeActivity(link()).create(Bundle())
288
+ module.initialize(manualConfig())
289
+ val replay = RecordingPromise()
290
+ module.handleDeepLink(LINK, false, replay)
291
+
292
+ assertTrue("promise resolved", replay.resolved)
293
+ assertNull("rejection code", replay.rejectionCode)
294
+ assertEquals(listOf(LINK), links())
295
+ }
296
+
297
+ @Test
298
+ fun `forwards the launch link the app passes after the activity was handed a new intent`() {
299
+ openedBy(link())
300
+ module.initialize(manualConfig())
301
+ module.handleDeepLink(LINK, false, RecordingPromise())
302
+
303
+ newProcess()
304
+ context.activity = FakeActivity(link()).create(Bundle())
305
+ module.initialize(manualConfig())
306
+ module.onNewIntent(link())
307
+ module.handleDeepLink(LINK, false, RecordingPromise())
308
+
309
+ assertEquals(listOf(LINK, LINK), links())
310
+ }
311
+
312
+ @Test
313
+ fun `forwards the same link the app passes from the url event of a new tap`() {
314
+ openedBy(link())
315
+ module.initialize(manualConfig())
316
+ module.handleDeepLink(LINK, false, RecordingPromise())
317
+
318
+ newProcess()
319
+ context.activity = FakeActivity(link()).create(Bundle())
320
+ module.initialize(manualConfig())
321
+ ModuleState.expireAutoForwardWindow()
322
+
323
+ // React Native emits `url`, and JavaScript calls in, before the module sees the new Intent.
324
+ shadowOf(Looper.getMainLooper()).pause()
325
+ val call = Thread { module.handleDeepLink(LINK, false, RecordingPromise()) }
326
+ call.start()
327
+ call.join()
328
+ val newTap = link()
329
+ module.onNewIntent(newTap)
330
+ context.activity?.keepNewIntent(newTap)
331
+ shadowOf(Looper.getMainLooper()).idle()
332
+
333
+ assertEquals(listOf(LINK, LINK), links())
334
+ }
335
+
336
+ @Test
337
+ fun `forwards a link the app passes that is not the one the activity opened with`() {
338
+ openedBy(link())
339
+ module.initialize(manualConfig())
340
+ module.handleDeepLink(LINK, false, RecordingPromise())
341
+
342
+ newProcess()
343
+ context.activity = FakeActivity(link()).create(Bundle())
344
+ module.initialize(manualConfig())
345
+ module.handleDeepLink(OTHER_LINK, false, RecordingPromise())
346
+
347
+ assertEquals(listOf(LINK, OTHER_LINK), links())
348
+ }
349
+
350
+ @Test
351
+ fun `skips the link of a re-created activity the plugin did not see created`() {
352
+ openedBy(link()).also { module.initialize(initializeConfig()) }
353
+
354
+ newProcess()
355
+ context.activity = FakeActivity(link())
356
+ module.initialize(initializeConfig())
357
+
358
+ assertEquals(listOf(LINK), links())
359
+ }
360
+
361
+ @Test
362
+ fun `keeps no link in the app's files`() {
363
+ openedBy(link()).also { module.initialize(initializeConfig()) }
364
+
365
+ val stored = LaunchRecordsState.file.readText()
366
+ assertTrue("record written", stored.contains("\"task_id\":$TASK_ID"))
367
+ assertFalse("no link in the file", stored.contains("example.com"))
368
+ }
369
+
370
+ @Test
371
+ fun `names the plugin to the sdk on every initialize`() {
372
+ module.initialize(initializeConfig())
373
+ module.initialize(initializeConfig())
374
+
375
+ assertEquals(listOf<String?>("react_native", "react_native"), wrappers)
376
+ }
377
+
378
+ @Test
379
+ fun `forwards links when naming the plugin fails`() {
380
+ every { AdShiftLib.setWrapper(any()) } throws IllegalStateException("old sdk")
381
+ openedBy(link())
382
+
383
+ module.initialize(initializeConfig())
384
+
385
+ assertEquals(listOf(LINK), links())
386
+ }
387
+
388
+ private fun newProcess() {
389
+ ModuleState.killProcess()
390
+ context = TestReactContext()
391
+ module = AdShiftReactNativePluginModule(context.reactContext)
392
+ }
393
+
394
+ private fun openedBy(intent: Intent, appPassesState: Boolean = true) {
395
+ context.activity = FakeActivity(intent).create(null, appPassesState)
396
+ }
397
+
398
+ private fun taskFirstActiveAt(millis: Long) {
399
+ val info = ActivityManager.RecentTaskInfo().apply { persistentId = TASK_ID }
400
+ ActivityManager.RecentTaskInfo::class.java.getDeclaredField("firstActiveTime")
401
+ .apply { isAccessible = true }
402
+ .setLong(info, millis)
403
+ val task = ShadowAppTask.newInstance().also { shadowOf(it).setTaskInfo(info) }
404
+ val manager = ApplicationProvider.getApplicationContext<Context>()
405
+ .getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
406
+ shadowOf(manager).setAppTasks(listOf(task))
407
+ }
408
+
409
+ private fun manualConfig(): JavaOnlyMap =
410
+ initializeConfig().apply { putBoolean("autoHandleDeepLinks", false) }
411
+
412
+ private fun link() = Intent(Intent.ACTION_VIEW, Uri.parse(LINK))
413
+
414
+ private fun links(): List<String> = forwarded.filterNotNull()
415
+
416
+ private companion object {
417
+ const val LINK = "https://$BRANDED_DOMAIN/promo?id=42"
418
+ const val OTHER_LINK = "https://$BRANDED_DOMAIN/other?id=7"
419
+ }
420
+ }