@adshift/react-native-plugin 3.0.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 (176) hide show
  1. package/AdShiftReactNativePlugin.podspec +20 -4
  2. package/CHANGELOG.md +30 -17
  3. package/README.md +55 -47
  4. package/android/build.gradle +2 -3
  5. package/android/consumer-rules.pro +2 -21
  6. package/android/src/main/AndroidManifest.xml +1 -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 +83 -157
  9. package/android/src/test/java/com/adshift/reactnativeplugin/ConsentSnapshotTest.kt +0 -20
  10. package/android/src/test/java/com/adshift/reactnativeplugin/DeepLinkDeliveryTest.kt +0 -4
  11. package/android/src/test/java/com/adshift/reactnativeplugin/InitializeOrderingTest.kt +37 -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 +7 -26
  14. package/android/src/test/java/com/adshift/reactnativeplugin/LaunchLinkRecordTest.kt +420 -0
  15. package/android/src/test/java/com/adshift/reactnativeplugin/ModuleTestSupport.kt +93 -34
  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 +2 -14
  19. package/ios/AdShiftReactNativePluginHelper.swift +14 -73
  20. package/ios/Core/BridgeRules.swift +24 -29
  21. package/lib/commonjs/Adshift.js +78 -285
  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.map +1 -1
  26. package/lib/commonjs/jest/index.js +4 -18
  27. package/lib/commonjs/jest/index.js.map +1 -1
  28. package/lib/commonjs/nativeSdkVersions.js +4 -6
  29. package/lib/commonjs/nativeSdkVersions.js.map +1 -1
  30. package/lib/commonjs/types/AdshiftAdRevenue.js +0 -24
  31. package/lib/commonjs/types/AdshiftAdRevenue.js.map +1 -1
  32. package/lib/commonjs/types/AdshiftConfig.js +3 -24
  33. package/lib/commonjs/types/AdshiftConfig.js.map +1 -1
  34. package/lib/commonjs/types/AdshiftConsent.js +9 -45
  35. package/lib/commonjs/types/AdshiftConsent.js.map +1 -1
  36. package/lib/commonjs/types/AdshiftConsentSnapshot.js +5 -24
  37. package/lib/commonjs/types/AdshiftConsentSnapshot.js.map +1 -1
  38. package/lib/commonjs/types/AdshiftDeepLink.js +3 -30
  39. package/lib/commonjs/types/AdshiftDeepLink.js.map +1 -1
  40. package/lib/commonjs/types/AdshiftError.js +7 -27
  41. package/lib/commonjs/types/AdshiftError.js.map +1 -1
  42. package/lib/commonjs/types/AdshiftEventParam.js +4 -38
  43. package/lib/commonjs/types/AdshiftEventParam.js.map +1 -1
  44. package/lib/commonjs/types/AdshiftEventType.js +1 -39
  45. package/lib/commonjs/types/AdshiftEventType.js.map +1 -1
  46. package/lib/commonjs/types/AdshiftThirdPartySharing.js +7 -38
  47. package/lib/commonjs/types/AdshiftThirdPartySharing.js.map +1 -1
  48. package/lib/commonjs/version.js +1 -1
  49. package/lib/module/Adshift.js +78 -285
  50. package/lib/module/Adshift.js.map +1 -1
  51. package/lib/module/NativeAdShiftReactNativePlugin.js +2 -12
  52. package/lib/module/NativeAdShiftReactNativePlugin.js.map +1 -1
  53. package/lib/module/index.js +1 -46
  54. package/lib/module/index.js.map +1 -1
  55. package/lib/module/jest/index.js +4 -18
  56. package/lib/module/jest/index.js.map +1 -1
  57. package/lib/module/nativeSdkVersions.js +4 -6
  58. package/lib/module/nativeSdkVersions.js.map +1 -1
  59. package/lib/module/types/AdshiftAdRevenue.js +0 -24
  60. package/lib/module/types/AdshiftAdRevenue.js.map +1 -1
  61. package/lib/module/types/AdshiftConfig.js +3 -24
  62. package/lib/module/types/AdshiftConfig.js.map +1 -1
  63. package/lib/module/types/AdshiftConsent.js +9 -45
  64. package/lib/module/types/AdshiftConsent.js.map +1 -1
  65. package/lib/module/types/AdshiftConsentSnapshot.js +5 -24
  66. package/lib/module/types/AdshiftConsentSnapshot.js.map +1 -1
  67. package/lib/module/types/AdshiftDeepLink.js +3 -31
  68. package/lib/module/types/AdshiftDeepLink.js.map +1 -1
  69. package/lib/module/types/AdshiftError.js +7 -28
  70. package/lib/module/types/AdshiftError.js.map +1 -1
  71. package/lib/module/types/AdshiftEventParam.js +4 -38
  72. package/lib/module/types/AdshiftEventParam.js.map +1 -1
  73. package/lib/module/types/AdshiftEventType.js +1 -39
  74. package/lib/module/types/AdshiftEventType.js.map +1 -1
  75. package/lib/module/types/AdshiftThirdPartySharing.js +7 -38
  76. package/lib/module/types/AdshiftThirdPartySharing.js.map +1 -1
  77. package/lib/module/version.js +1 -1
  78. package/lib/typescript/commonjs/src/Adshift.d.ts +61 -185
  79. package/lib/typescript/commonjs/src/Adshift.d.ts.map +1 -1
  80. package/lib/typescript/commonjs/src/NativeAdShiftReactNativePlugin.d.ts +10 -36
  81. package/lib/typescript/commonjs/src/NativeAdShiftReactNativePlugin.d.ts.map +1 -1
  82. package/lib/typescript/commonjs/src/index.d.ts +0 -32
  83. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  84. package/lib/typescript/commonjs/src/jest/index.d.ts +6 -20
  85. package/lib/typescript/commonjs/src/jest/index.d.ts.map +1 -1
  86. package/lib/typescript/commonjs/src/nativeSdkVersions.d.ts +4 -6
  87. package/lib/typescript/commonjs/src/nativeSdkVersions.d.ts.map +1 -1
  88. package/lib/typescript/commonjs/src/types/AdshiftAdRevenue.d.ts +0 -24
  89. package/lib/typescript/commonjs/src/types/AdshiftAdRevenue.d.ts.map +1 -1
  90. package/lib/typescript/commonjs/src/types/AdshiftConfig.d.ts +10 -37
  91. package/lib/typescript/commonjs/src/types/AdshiftConfig.d.ts.map +1 -1
  92. package/lib/typescript/commonjs/src/types/AdshiftConsent.d.ts +9 -49
  93. package/lib/typescript/commonjs/src/types/AdshiftConsent.d.ts.map +1 -1
  94. package/lib/typescript/commonjs/src/types/AdshiftConsentSnapshot.d.ts +10 -28
  95. package/lib/typescript/commonjs/src/types/AdshiftConsentSnapshot.d.ts.map +1 -1
  96. package/lib/typescript/commonjs/src/types/AdshiftDeepLink.d.ts +6 -34
  97. package/lib/typescript/commonjs/src/types/AdshiftDeepLink.d.ts.map +1 -1
  98. package/lib/typescript/commonjs/src/types/AdshiftError.d.ts +7 -26
  99. package/lib/typescript/commonjs/src/types/AdshiftError.d.ts.map +1 -1
  100. package/lib/typescript/commonjs/src/types/AdshiftEventParam.d.ts +3 -19
  101. package/lib/typescript/commonjs/src/types/AdshiftEventParam.d.ts.map +1 -1
  102. package/lib/typescript/commonjs/src/types/AdshiftEventType.d.ts +1 -23
  103. package/lib/typescript/commonjs/src/types/AdshiftEventType.d.ts.map +1 -1
  104. package/lib/typescript/commonjs/src/types/AdshiftThirdPartySharing.d.ts +7 -38
  105. package/lib/typescript/commonjs/src/types/AdshiftThirdPartySharing.d.ts.map +1 -1
  106. package/lib/typescript/commonjs/src/version.d.ts +1 -1
  107. package/lib/typescript/module/src/Adshift.d.ts +61 -185
  108. package/lib/typescript/module/src/Adshift.d.ts.map +1 -1
  109. package/lib/typescript/module/src/NativeAdShiftReactNativePlugin.d.ts +10 -36
  110. package/lib/typescript/module/src/NativeAdShiftReactNativePlugin.d.ts.map +1 -1
  111. package/lib/typescript/module/src/index.d.ts +0 -32
  112. package/lib/typescript/module/src/index.d.ts.map +1 -1
  113. package/lib/typescript/module/src/jest/index.d.ts +6 -20
  114. package/lib/typescript/module/src/jest/index.d.ts.map +1 -1
  115. package/lib/typescript/module/src/nativeSdkVersions.d.ts +4 -6
  116. package/lib/typescript/module/src/nativeSdkVersions.d.ts.map +1 -1
  117. package/lib/typescript/module/src/types/AdshiftAdRevenue.d.ts +0 -24
  118. package/lib/typescript/module/src/types/AdshiftAdRevenue.d.ts.map +1 -1
  119. package/lib/typescript/module/src/types/AdshiftConfig.d.ts +10 -37
  120. package/lib/typescript/module/src/types/AdshiftConfig.d.ts.map +1 -1
  121. package/lib/typescript/module/src/types/AdshiftConsent.d.ts +9 -49
  122. package/lib/typescript/module/src/types/AdshiftConsent.d.ts.map +1 -1
  123. package/lib/typescript/module/src/types/AdshiftConsentSnapshot.d.ts +10 -28
  124. package/lib/typescript/module/src/types/AdshiftConsentSnapshot.d.ts.map +1 -1
  125. package/lib/typescript/module/src/types/AdshiftDeepLink.d.ts +6 -34
  126. package/lib/typescript/module/src/types/AdshiftDeepLink.d.ts.map +1 -1
  127. package/lib/typescript/module/src/types/AdshiftError.d.ts +7 -26
  128. package/lib/typescript/module/src/types/AdshiftError.d.ts.map +1 -1
  129. package/lib/typescript/module/src/types/AdshiftEventParam.d.ts +3 -19
  130. package/lib/typescript/module/src/types/AdshiftEventParam.d.ts.map +1 -1
  131. package/lib/typescript/module/src/types/AdshiftEventType.d.ts +1 -23
  132. package/lib/typescript/module/src/types/AdshiftEventType.d.ts.map +1 -1
  133. package/lib/typescript/module/src/types/AdshiftThirdPartySharing.d.ts +7 -38
  134. package/lib/typescript/module/src/types/AdshiftThirdPartySharing.d.ts.map +1 -1
  135. package/lib/typescript/module/src/version.d.ts +1 -1
  136. package/package.json +11 -1
  137. package/src/Adshift.ts +82 -286
  138. package/src/NativeAdShiftReactNativePlugin.ts +10 -48
  139. package/src/index.tsx +1 -44
  140. package/src/jest/index.ts +6 -20
  141. package/src/nativeSdkVersions.ts +4 -6
  142. package/src/types/AdshiftAdRevenue.ts +0 -24
  143. package/src/types/AdshiftConfig.ts +11 -54
  144. package/src/types/AdshiftConsent.ts +9 -49
  145. package/src/types/AdshiftConsentSnapshot.ts +11 -38
  146. package/src/types/AdshiftDeepLink.ts +6 -34
  147. package/src/types/AdshiftError.ts +7 -26
  148. package/src/types/AdshiftEventParam.ts +4 -38
  149. package/src/types/AdshiftEventType.ts +1 -39
  150. package/src/types/AdshiftThirdPartySharing.ts +7 -38
  151. package/src/version.ts +1 -1
  152. package/vendor/ios/AdshiftSDK.files.json +30 -0
  153. package/vendor/ios/AdshiftSDK.xcframework/Info.plist +44 -0
  154. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/AdshiftSDK +0 -0
  155. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Headers/AdshiftSDK-Swift.h +315 -0
  156. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Info.plist +0 -0
  157. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.abi.json +18729 -0
  158. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.private.swiftinterface +414 -0
  159. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.swiftdoc +0 -0
  160. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios.swiftinterface +414 -0
  161. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/Modules/module.modulemap +4 -0
  162. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64/AdshiftSDK.framework/PrivacyInfo.xcprivacy +132 -0
  163. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/AdshiftSDK +0 -0
  164. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Headers/AdshiftSDK-Swift.h +626 -0
  165. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Info.plist +0 -0
  166. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.abi.json +18729 -0
  167. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface +414 -0
  168. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.swiftdoc +0 -0
  169. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/arm64-apple-ios-simulator.swiftinterface +414 -0
  170. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.abi.json +18729 -0
  171. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.private.swiftinterface +414 -0
  172. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.swiftdoc +0 -0
  173. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/AdshiftSDK.swiftmodule/x86_64-apple-ios-simulator.swiftinterface +414 -0
  174. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/Modules/module.modulemap +4 -0
  175. package/vendor/ios/AdshiftSDK.xcframework/ios-arm64_x86_64-simulator/AdshiftSDK.framework/PrivacyInfo.xcprivacy +132 -0
  176. package/android/src/main/java/com/adshift/reactnativeplugin/RestoredIntents.kt +0 -64
@@ -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 {
@@ -18,10 +18,6 @@ import org.junit.runner.RunWith
18
18
  import org.robolectric.RobolectricTestRunner
19
19
  import org.robolectric.annotation.Config
20
20
 
21
- /**
22
- * Launch Intents reach the SDK once per delivery: a new payload is forwarded, while the same
23
- * payload redelivered to a recreated Activity is not.
24
- */
25
21
  @RunWith(RobolectricTestRunner::class)
26
22
  @Config(sdk = [35])
27
23
  class LaunchIntentForwardingTest {
@@ -74,14 +70,11 @@ class LaunchIntentForwardingTest {
74
70
 
75
71
  @Test
76
72
  fun `skips a launch intent redelivered to a recreated activity`() {
77
- context.launchIntent = launcherIntent()
73
+ context.activity = FakeActivity(notificationIntent(PUSH_LINK, MESSAGE_ID)).create(null)
78
74
  module.initialize(initializeConfig())
79
75
 
80
- context.launchIntent = notificationIntent(PUSH_LINK, MESSAGE_ID)
81
- module.onHostResume()
82
-
83
- // Activity recreation hands over an equal Intent in a new instance.
84
- 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())
85
78
  module.onHostResume()
86
79
 
87
80
  assertEquals(
@@ -103,29 +96,17 @@ class LaunchIntentForwardingTest {
103
96
  assertEquals("deliveries forwarded to the SDK", 1, forwardedExtras.size)
104
97
  }
105
98
 
106
- @Test
107
- fun `skips the intent of an activity the system re-created from saved state`() {
108
- val opened = Intent(Intent.ACTION_VIEW, Uri.parse(PUSH_LINK))
109
- recreateActivityHolding(opened)
110
- context.launchIntent = opened
111
-
112
- module.initialize(initializeConfig())
113
- module.onHostResume()
114
-
115
- assertEquals("links forwarded to the SDK", emptyList<String?>(), forwardedLinks)
116
- assertEquals("payloads forwarded to the SDK", 0, forwardedExtras.size)
117
- }
118
-
119
99
  @Test
120
100
  fun `forwards a link tapped after the activity was re-created`() {
121
101
  val opened = Intent(Intent.ACTION_VIEW, Uri.parse(PUSH_LINK))
122
- recreateActivityHolding(opened)
123
- context.launchIntent = opened
102
+ context.activity = FakeActivity(opened).create(null)
124
103
  module.initialize(initializeConfig())
125
104
 
105
+ context.activity = recreateActivityHolding(Intent(Intent.ACTION_VIEW, Uri.parse(PUSH_LINK)))
106
+ module.onHostResume()
126
107
  module.onNewIntent(Intent(Intent.ACTION_VIEW, Uri.parse(PUSH_LINK)))
127
108
 
128
- assertEquals(listOf<String?>(PUSH_LINK), forwardedLinks)
109
+ assertEquals(listOf<String?>(PUSH_LINK, PUSH_LINK), forwardedLinks)
129
110
  }
130
111
 
131
112
  private companion object {
@@ -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
+ }