Skip to content

Commit 50dfb8c

Browse files
authored
Merge pull request #724 from Adyen/android-tests
Refactor Android tests
2 parents 43006d2 + af971de commit 50dfb8c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3183
-2579
lines changed

android/src/main/java/com/adyenreactnativesdk/AdyenCheckout.kt

Lines changed: 102 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -28,112 +28,121 @@ import java.lang.ref.WeakReference
2828
* Umbrella class for setting DropIn and Component specific parameters
2929
*/
3030
object AdyenCheckout {
31-
private var currentComponent: WeakReference<Component> = WeakReference(null)
32-
private val dropInCallback = DropInCallbackListener()
33-
internal var dropInLauncher: ActivityResultLauncher<DropInResultContractParams>? = null
34-
internal var dropInSessionLauncher: ActivityResultLauncher<SessionDropInResultContractParams>? =
35-
null
31+
private var currentComponent: WeakReference<Component> = WeakReference(null)
32+
private val dropInCallback = DropInCallbackListener()
33+
internal var dropInLauncher: ActivityResultLauncher<DropInResultContractParams>? = null
34+
internal var dropInSessionLauncher: ActivityResultLauncher<SessionDropInResultContractParams>? =
35+
null
3636

37-
@JvmStatic
38-
internal fun addDropInListener(callback: ReactDropInCallback) {
39-
dropInCallback.callback = WeakReference(callback)
40-
}
37+
@JvmStatic
38+
internal fun addDropInListener(callback: ReactDropInCallback) {
39+
dropInCallback.callback = WeakReference(callback)
40+
}
4141

42-
@JvmStatic
43-
internal fun removeDropInListener() {
44-
dropInCallback.callback.clear()
45-
}
42+
@JvmStatic
43+
internal fun removeDropInListener() {
44+
dropInCallback.callback.clear()
45+
}
4646

47-
/**
48-
* Persist a reference to Activity that will present DropIn or Component
49-
* @param activity parent activity for DropIn or Component
50-
*/
51-
@JvmStatic
52-
fun setLauncherActivity(activity: ActivityResultCaller) {
53-
dropInLauncher = DropIn.registerForDropInResult(
54-
activity, dropInCallback as DropInCallback
55-
)
56-
dropInSessionLauncher = DropIn.registerForDropInResult(
57-
activity, dropInCallback as SessionDropInCallback
58-
)
59-
}
47+
/**
48+
* Persist a reference to Activity that will present DropIn or Component
49+
* @param activity parent activity for DropIn or Component
50+
*/
51+
@JvmStatic
52+
fun setLauncherActivity(activity: ActivityResultCaller) {
53+
dropInLauncher =
54+
DropIn.registerForDropInResult(
55+
activity,
56+
dropInCallback as DropInCallback,
57+
)
58+
dropInSessionLauncher =
59+
DropIn.registerForDropInResult(
60+
activity,
61+
dropInCallback as SessionDropInCallback,
62+
)
63+
}
6064

61-
/**
62-
* Allow Adyen Components to process intents.
63-
* @param intent received redirect intent
64-
* @return `true` when intent could be handled by AdyenCheckout
65-
*/
66-
@JvmStatic
67-
fun handleIntent(intent: Intent): Boolean {
68-
if (intent.data == null) {
69-
return false
70-
}
71-
val actionHandlingComponent = currentComponent.get() as? ActionHandlingComponent
72-
return if (actionHandlingComponent != null) {
73-
actionHandlingComponent.handleIntent(intent)
74-
true
75-
} else {
76-
Log.e(TAG, "Nothing registered as ActivityResultHandling")
77-
false
78-
}
65+
/**
66+
* Allow Adyen Components to process intents.
67+
* @param intent received redirect intent
68+
* @return `true` when intent could be handled by AdyenCheckout
69+
*/
70+
@JvmStatic
71+
fun handleIntent(intent: Intent): Boolean {
72+
if (intent.data == null) {
73+
return false
7974
}
80-
81-
@JvmStatic
82-
internal fun setComponent(component: Component) {
83-
currentComponent = WeakReference(component)
75+
val actionHandlingComponent = currentComponent.get() as? ActionHandlingComponent
76+
return if (actionHandlingComponent != null) {
77+
actionHandlingComponent.handleIntent(intent)
78+
true
79+
} else {
80+
Log.e(TAG, "Nothing registered as ActivityResultHandling")
81+
false
8482
}
83+
}
8584

86-
@JvmStatic
87-
internal fun removeComponent() {
88-
currentComponent.clear()
89-
}
85+
@JvmStatic
86+
internal fun setComponent(component: Component) {
87+
currentComponent = WeakReference(component)
88+
}
89+
90+
@JvmStatic
91+
internal fun removeComponent() {
92+
currentComponent.clear()
93+
}
9094

91-
/**
92-
* Allow Adyen Components to process intents.
93-
* @param requestCode received redirect intent
94-
* @param resultCode received redirect intent
95-
* @param data received redirect intent
96-
*/
97-
@JvmStatic
98-
fun handleActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
99-
if (requestCode == GooglePayModule.GOOGLEPAY_REQUEST_CODE) {
100-
val activityResultHandlingComponent =
101-
currentComponent.get() as? ActivityResultHandlingComponent
102-
if (activityResultHandlingComponent != null) {
103-
activityResultHandlingComponent.handleActivityResult(resultCode, data)
104-
} else {
105-
Log.e(TAG, "Nothing registered as ActivityResultHandling")
106-
}
107-
}
95+
/**
96+
* Allow Adyen Components to process intents.
97+
* @param requestCode received redirect intent
98+
* @param resultCode received redirect intent
99+
* @param data received redirect intent
100+
*/
101+
@JvmStatic
102+
fun handleActivityResult(
103+
requestCode: Int,
104+
resultCode: Int,
105+
data: Intent?,
106+
) {
107+
if (requestCode == GooglePayModule.GOOGLEPAY_REQUEST_CODE) {
108+
val activityResultHandlingComponent =
109+
currentComponent.get() as? ActivityResultHandlingComponent
110+
if (activityResultHandlingComponent != null) {
111+
activityResultHandlingComponent.handleActivityResult(resultCode, data)
112+
} else {
113+
Log.e(TAG, "Nothing registered as ActivityResultHandling")
114+
}
108115
}
116+
}
109117

110-
private const val TAG = "AdyenCheckout"
118+
private const val TAG = "AdyenCheckout"
111119
}
112120

113-
private class DropInCallbackListener : DropInCallback, SessionDropInCallback {
121+
private class DropInCallbackListener :
122+
DropInCallback,
123+
SessionDropInCallback {
124+
var callback: WeakReference<ReactDropInCallback> =
125+
WeakReference(null)
114126

115-
var callback: WeakReference<ReactDropInCallback> =
116-
WeakReference(null)
117-
118-
override fun onDropInResult(dropInResult: DropInResult?) {
119-
callback.get()?.let {
120-
when (dropInResult) {
121-
is DropInResult.CancelledByUser -> it.onCancel()
122-
is DropInResult.Error -> it.onError(dropInResult.reason)
123-
is DropInResult.Finished -> it.onCompleted(dropInResult.result)
124-
null -> return
125-
}
126-
}
127+
override fun onDropInResult(dropInResult: DropInResult?) {
128+
callback.get()?.let {
129+
when (dropInResult) {
130+
is DropInResult.CancelledByUser -> it.onCancel()
131+
is DropInResult.Error -> it.onError(dropInResult.reason)
132+
is DropInResult.Finished -> it.onCompleted(dropInResult.result)
133+
null -> return
134+
}
127135
}
136+
}
128137

129-
override fun onDropInResult(sessionDropInResult: SessionDropInResult?) {
130-
callback.get()?.let {
131-
when (sessionDropInResult) {
132-
is SessionDropInResult.CancelledByUser -> it.onCancel()
133-
is SessionDropInResult.Error -> it.onError(sessionDropInResult.reason)
134-
is SessionDropInResult.Finished -> it.onFinished(sessionDropInResult.result)
135-
null -> return
136-
}
137-
}
138+
override fun onDropInResult(sessionDropInResult: SessionDropInResult?) {
139+
callback.get()?.let {
140+
when (sessionDropInResult) {
141+
is SessionDropInResult.CancelledByUser -> it.onCancel()
142+
is SessionDropInResult.Error -> it.onError(sessionDropInResult.reason)
143+
is SessionDropInResult.Finished -> it.onFinished(sessionDropInResult.result)
144+
null -> return
145+
}
138146
}
139-
}
147+
}
148+
}

android/src/main/java/com/adyenreactnativesdk/AdyenPaymentPackage.kt

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,25 @@ import com.facebook.react.bridge.NativeModule
2222
import com.facebook.react.bridge.ReactApplicationContext
2323

2424
class AdyenPaymentPackage : ReactPackage {
25-
override fun createViewManagers(
26-
reactContext: ReactApplicationContext
27-
) = listOf(PlatformPayViewManager())
25+
override fun createViewManagers(reactContext: ReactApplicationContext) = listOf(PlatformPayViewManager())
2826

29-
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
30-
configureAnalytics()
31-
return listOf(
32-
DropInModule(reactContext),
33-
InstantModule(reactContext),
34-
GooglePayModule(reactContext),
35-
ApplePayModuleMock(reactContext),
36-
AdyenCSEModule(reactContext),
37-
SessionHelperModule(reactContext),
38-
ActionModule(reactContext),
39-
)
40-
}
27+
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
28+
configureAnalytics()
29+
return listOf(
30+
DropInModule(reactContext),
31+
InstantModule(reactContext),
32+
GooglePayModule(reactContext),
33+
ApplePayModuleMock(reactContext),
34+
AdyenCSEModule(reactContext),
35+
SessionHelperModule(reactContext),
36+
ActionModule(reactContext),
37+
)
38+
}
4139

42-
// This is intended.
43-
@SuppressLint("RestrictedApi")
44-
private fun configureAnalytics() {
45-
val version = BuildConfig.CHECKOUT_VERSION
46-
AnalyticsPlatformParams.overrideForCrossPlatform(AnalyticsPlatform.REACT_NATIVE, version)
47-
}
48-
}
40+
// This is intended.
41+
@SuppressLint("RestrictedApi")
42+
private fun configureAnalytics() {
43+
val version = BuildConfig.CHECKOUT_VERSION
44+
AnalyticsPlatformParams.overrideForCrossPlatform(AnalyticsPlatform.REACT_NATIVE, version)
45+
}
46+
}

android/src/main/java/com/adyenreactnativesdk/component/CheckoutProxy.kt

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,35 @@ import org.json.JSONObject
1818
import java.lang.ref.WeakReference
1919

2020
class CheckoutProxy private constructor() {
21-
private var _componentListener = WeakReference<ComponentEventListener>(null)
21+
private var _componentListener = WeakReference<ComponentEventListener>(null)
2222

23-
var sessionService: BaseDropInServiceContract? = null
23+
var sessionService: BaseDropInServiceContract? = null
2424

25-
var advancedService: BaseDropInServiceContract? = null
25+
var advancedService: BaseDropInServiceContract? = null
2626

27-
var componentListener: ComponentEventListener?
28-
get() = _componentListener.get()
29-
set(value) {
30-
_componentListener = WeakReference(value)
31-
}
32-
33-
/** Base events coming from Components */
34-
interface ComponentEventListener: DropInServiceContract {
35-
fun onException(exception: CheckoutException)
36-
fun onFinished(result: SessionPaymentResult)
37-
fun onRemove(storedPaymentMethod: StoredPaymentMethod)
27+
var componentListener: ComponentEventListener?
28+
get() = _componentListener.get()
29+
set(value) {
30+
_componentListener = WeakReference(value)
3831
}
3932

40-
/** Events coming from Card Component */
41-
interface CardComponentEventListener: ComponentEventListener {
42-
fun onBinValue(binValue: String)
43-
fun onBinLookup(data: List<BinLookupData>)
44-
}
33+
/** Base events coming from Components */
34+
interface ComponentEventListener : DropInServiceContract {
35+
fun onException(exception: CheckoutException)
4536

46-
companion object {
47-
var shared = CheckoutProxy()
48-
}
49-
}
37+
fun onFinished(result: SessionPaymentResult)
38+
39+
fun onRemove(storedPaymentMethod: StoredPaymentMethod)
40+
}
41+
42+
/** Events coming from Card Component */
43+
interface CardComponentEventListener : ComponentEventListener {
44+
fun onBinValue(binValue: String)
45+
46+
fun onBinLookup(data: List<BinLookupData>)
47+
}
48+
49+
companion object {
50+
var shared = CheckoutProxy()
51+
}
52+
}

0 commit comments

Comments
 (0)