From 6c2e2dab7d652fcc8f236de5444179de784558fd Mon Sep 17 00:00:00 2001 From: Masoud Fallahpourbaee Date: Wed, 7 Feb 2024 14:39:58 +0100 Subject: [PATCH] Specify the type of HashMap explicitly when creating it anonymously --- .../reactnative/KlarnaMobileSDKPackage.java | 2 +- .../payments/KlarnaPaymentViewManager.java | 17 ++++---- .../KlarnaStandaloneWebViewEventSender.java | 16 ++++---- .../KlarnaPaymentViewManagerTest.java | 41 ++++++------------- 4 files changed, 28 insertions(+), 48 deletions(-) diff --git a/android/src/main/java/com/klarna/mobile/sdk/reactnative/KlarnaMobileSDKPackage.java b/android/src/main/java/com/klarna/mobile/sdk/reactnative/KlarnaMobileSDKPackage.java index 3106e91b..39809dab 100644 --- a/android/src/main/java/com/klarna/mobile/sdk/reactnative/KlarnaMobileSDKPackage.java +++ b/android/src/main/java/com/klarna/mobile/sdk/reactnative/KlarnaMobileSDKPackage.java @@ -26,7 +26,7 @@ public List createNativeModules(@NonNull ReactApplicationContext r @Override public List createViewManagers(@NonNull ReactApplicationContext reactContext) { return List.of( - new KlarnaPaymentViewManager(reactContext, (Application) reactContext.getApplicationContext()), + new KlarnaPaymentViewManager(reactContext), new KlarnaStandaloneWebViewManager(reactContext, (Application) reactContext.getApplicationContext()) ); } diff --git a/android/src/main/java/com/klarna/mobile/sdk/reactnative/payments/KlarnaPaymentViewManager.java b/android/src/main/java/com/klarna/mobile/sdk/reactnative/payments/KlarnaPaymentViewManager.java index 95614c58..8fed3b93 100644 --- a/android/src/main/java/com/klarna/mobile/sdk/reactnative/payments/KlarnaPaymentViewManager.java +++ b/android/src/main/java/com/klarna/mobile/sdk/reactnative/payments/KlarnaPaymentViewManager.java @@ -1,7 +1,5 @@ package com.klarna.mobile.sdk.reactnative.payments; -import android.app.Application; - import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -19,7 +17,6 @@ import com.klarna.mobile.sdk.api.payments.KlarnaPaymentViewCallback; import com.klarna.mobile.sdk.api.payments.KlarnaPaymentsSDKError; import com.klarna.mobile.sdk.reactnative.common.ArgumentsUtil; -import com.klarna.mobile.sdk.reactnative.common.WebViewResizeObserver; import com.klarna.mobile.sdk.reactnative.spec.RNKlarnaPaymentViewSpec; import org.jetbrains.annotations.NotNull; @@ -49,7 +46,7 @@ public class KlarnaPaymentViewManager extends RNKlarnaPaymentViewSpec, EventDispatcher> viewToDispatcher; - public KlarnaPaymentViewManager(ReactApplicationContext reactApplicationContext, Application app) { + public KlarnaPaymentViewManager(ReactApplicationContext reactApplicationContext) { super(); this.viewToDispatcher = new HashMap<>(); this.reactAppContext = reactApplicationContext; @@ -264,7 +261,7 @@ public void onLoadPaymentReview(@NotNull KlarnaPaymentView paymentView, boolean public void onAuthorized(@NotNull KlarnaPaymentView paymentView, boolean approved, @org.jetbrains.annotations.Nullable String authToken, @org.jetbrains.annotations.Nullable Boolean finalizeRequired) { requestLayout(paymentView); WritableMap params = ArgumentsUtil.createMap( - new HashMap<>() {{ + new HashMap() {{ put("approved", approved); put("authToken", authToken); put("finalizeRequired", finalizeRequired); @@ -277,7 +274,7 @@ public void onAuthorized(@NotNull KlarnaPaymentView paymentView, boolean approve public void onReauthorized(@NotNull KlarnaPaymentView paymentView, boolean approved, @org.jetbrains.annotations.Nullable String authToken) { requestLayout(paymentView); WritableMap params = ArgumentsUtil.createMap( - new HashMap<>() {{ + new HashMap() {{ put("approved", approved); put("authToken", authToken); }} @@ -289,7 +286,7 @@ public void onReauthorized(@NotNull KlarnaPaymentView paymentView, boolean appro public void onFinalized(@NotNull KlarnaPaymentView paymentView, boolean approved, @org.jetbrains.annotations.Nullable String authToken) { requestLayout(paymentView); WritableMap params = ArgumentsUtil.createMap( - new HashMap<>() {{ + new HashMap() {{ put("approved", approved); put("authToken", authToken); }} @@ -302,7 +299,7 @@ public void onErrorOccurred(@NotNull KlarnaPaymentView klarnaPaymentView, @NotNu requestLayout(klarnaPaymentView); ReadableMap sdkError = buildErrorMap(klarnaPaymentsSDKError); WritableMap params = ArgumentsUtil.createMap( - new HashMap<>() {{ + new HashMap() {{ put("error", sdkError); }} ); @@ -312,7 +309,7 @@ public void onErrorOccurred(@NotNull KlarnaPaymentView klarnaPaymentView, @NotNu @Override public void onResized(PaymentViewWrapper paymentViewWrapper, int value) { WritableMap params = ArgumentsUtil.createMap( - new HashMap<>() {{ + new HashMap() {{ put("height", String.valueOf(value)); }} ); @@ -321,7 +318,7 @@ public void onResized(PaymentViewWrapper paymentViewWrapper, int value) { ReadableMap buildErrorMap(KlarnaPaymentsSDKError klarnaPaymentsSDKError) { return ArgumentsUtil.createMap( - new HashMap<>() {{ + new HashMap() {{ put("action", klarnaPaymentsSDKError.getAction()); put("isFatal", klarnaPaymentsSDKError.isFatal()); put("message", klarnaPaymentsSDKError.getMessage()); diff --git a/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java b/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java index 82b02225..662a7e05 100644 --- a/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java +++ b/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java @@ -46,7 +46,7 @@ public class KlarnaStandaloneWebViewEventSender { public void sendLoadProgressEvent(@Nullable KlarnaStandaloneWebView view, int progress) { WritableMap webViewMap = buildWebViewMap(view, null); webViewMap.putDouble(PARAM_NAME_PROGRESS, progress); - WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{ + WritableMap params = ArgumentsUtil.createMap(new HashMap() {{ put(PARAM_NAME_PROGRESS_EVENT, webViewMap); }}); postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_LOAD_PROGRESS, params, view); @@ -56,7 +56,7 @@ public void sendErrorEvent(@Nullable KlarnaStandaloneWebView view, int code, Str WritableMap webViewMap = buildWebViewMap(view, null); webViewMap.putDouble(PARAM_NAME_CODE, code); webViewMap.putString(PARAM_NAME_DESCRIPTION, description); - WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{ + WritableMap params = ArgumentsUtil.createMap(new HashMap() {{ put(PARAM_NAME_NAVIGATION_ERROR, webViewMap); }}); postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_ERROR, params, view); @@ -64,27 +64,27 @@ public void sendErrorEvent(@Nullable KlarnaStandaloneWebView view, int code, Str public void sendNavigationEvent(@Nullable KlarnaStandaloneWebView view, KlarnaStandaloneWebViewEvent.Event event, String url) { WritableMap webViewMap = buildWebViewMap(view, url); - WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{ + WritableMap params = ArgumentsUtil.createMap(new HashMap() {{ put(PARAM_NAME_NAVIGATION_EVENT, webViewMap); }}); postEventForView(event, params, view); } public void sendKlarnaMessageEvent(@Nullable KlarnaStandaloneWebView view, @NonNull KlarnaProductEvent klarnaProductEvent) { - ReadableMap eventMap = ArgumentsUtil.createMap(new HashMap<>() {{ + ReadableMap eventMap = ArgumentsUtil.createMap(new HashMap() {{ put(PARAM_NAME_ACTION, klarnaProductEvent.getAction()); }}); - WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{ + WritableMap params = ArgumentsUtil.createMap(new HashMap() {{ put(PARAM_NAME_KLARNA_MESSAGE_EVENT, eventMap); }}); postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_KLARNA_MESSAGE, params, view); } public void sendRenderProcessGoneEvent(@Nullable KlarnaStandaloneWebView view, boolean didCrash) { - ReadableMap eventMap = ArgumentsUtil.createMap(new HashMap<>() {{ + ReadableMap eventMap = ArgumentsUtil.createMap(new HashMap() {{ put(PARAM_NAME_DID_CRASH, didCrash); }}); - WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{ + WritableMap params = ArgumentsUtil.createMap(new HashMap() {{ put(PARAM_NAME_RENDER_PROCESS_GONE_EVENT, eventMap); }}); postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_RENDER_PROCESS_GONE, params, view); @@ -123,7 +123,7 @@ private KlarnaStandaloneWebView getKlarnaStandaloneWebView(@Nullable KlarnaStand } private WritableMap buildWebViewMap(KlarnaStandaloneWebView webView, String url) { - return ArgumentsUtil.createMap(new HashMap<>() {{ + return ArgumentsUtil.createMap(new HashMap() {{ if (webView != null) { put(PARAM_NAME_URL, url != null ? url : webView.getUrl()); put(PARAM_NAME_TITLE, webView.getTitle()); diff --git a/android/src/test/java/com/klarna/mobile/sdk/reactnative/payments/KlarnaPaymentViewManagerTest.java b/android/src/test/java/com/klarna/mobile/sdk/reactnative/payments/KlarnaPaymentViewManagerTest.java index 14f6faa7..9f78ef86 100644 --- a/android/src/test/java/com/klarna/mobile/sdk/reactnative/payments/KlarnaPaymentViewManagerTest.java +++ b/android/src/test/java/com/klarna/mobile/sdk/reactnative/payments/KlarnaPaymentViewManagerTest.java @@ -1,5 +1,12 @@ package com.klarna.mobile.sdk.reactnative.payments; +import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_AUTHORIZE; +import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_FINALIZE; +import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_INITIALIZE; +import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_LOAD; +import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_LOAD_PAYMENT_REVIEW; +import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_REAUTHORIZE; + import android.app.Application; import androidx.test.core.app.ApplicationProvider; @@ -20,21 +27,12 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.rule.PowerMockRule; import org.robolectric.RobolectricTestRunner; -import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_AUTHORIZE; -import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_FINALIZE; -import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_INITIALIZE; -import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_LOAD; -import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_LOAD_PAYMENT_REVIEW; -import static com.klarna.mobile.sdk.reactnative.payments.KlarnaPaymentViewManager.COMMAND_REAUTHORIZE; - import java.util.Collections; @PrepareForTest({Arguments.class}) @@ -45,39 +43,24 @@ public class KlarnaPaymentViewManagerTest { @Rule public PowerMockRule rule = new PowerMockRule(); - private Application application; - private ReactApplicationContext reactContext; - private KlarnaPaymentViewManager manager; private PaymentViewWrapper wrapper; @Before public void setup() { - application = ApplicationProvider.getApplicationContext(); - reactContext = new ReactApplicationContext(application); + Application application = ApplicationProvider.getApplicationContext(); + ReactApplicationContext reactContext = new ReactApplicationContext(application); - manager = new KlarnaPaymentViewManager(reactContext, application); + manager = new KlarnaPaymentViewManager(reactContext); wrapper = Mockito.mock(PaymentViewWrapper.class); wrapper.paymentView = Mockito.mock(KlarnaPaymentView.class); PowerMockito.mockStatic(Arguments.class); PowerMockito.when(Arguments.createArray()) - .thenAnswer( - new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - return new JavaOnlyArray(); - } - }); + .thenAnswer(invocation -> new JavaOnlyArray()); PowerMockito.when(Arguments.createMap()) - .thenAnswer( - new Answer() { - @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - return new JavaOnlyMap(); - } - }); + .thenAnswer(invocation -> new JavaOnlyMap()); } @After