Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify the type of HashMap explicitly when creating it anonymously #234

Merged
merged 1 commit into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext r
@Override
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
return List.of(
new KlarnaPaymentViewManager(reactContext, (Application) reactContext.getApplicationContext()),
new KlarnaPaymentViewManager(reactContext),
new KlarnaStandaloneWebViewManager(reactContext, (Application) reactContext.getApplicationContext())
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.klarna.mobile.sdk.reactnative.payments;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand All @@ -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;
Expand Down Expand Up @@ -49,7 +46,7 @@ public class KlarnaPaymentViewManager extends RNKlarnaPaymentViewSpec<PaymentVie
// Store a list of views to event dispatchers so we send up events via the right views.
private final Map<WeakReference<PaymentViewWrapper>, EventDispatcher> viewToDispatcher;

public KlarnaPaymentViewManager(ReactApplicationContext reactApplicationContext, Application app) {
public KlarnaPaymentViewManager(ReactApplicationContext reactApplicationContext) {
super();
this.viewToDispatcher = new HashMap<>();
this.reactAppContext = reactApplicationContext;
Expand Down Expand Up @@ -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<String, Object>() {{
put("approved", approved);
put("authToken", authToken);
put("finalizeRequired", finalizeRequired);
Expand All @@ -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<String, Object>() {{
put("approved", approved);
put("authToken", authToken);
}}
Expand All @@ -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<String, Object>() {{
put("approved", approved);
put("authToken", authToken);
}}
Expand All @@ -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<String, Object>() {{
put("error", sdkError);
}}
);
Expand All @@ -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<String, Object>() {{
put("height", String.valueOf(value));
}}
);
Expand All @@ -321,7 +318,7 @@ public void onResized(PaymentViewWrapper paymentViewWrapper, int value) {

ReadableMap buildErrorMap(KlarnaPaymentsSDKError klarnaPaymentsSDKError) {
return ArgumentsUtil.createMap(
new HashMap<>() {{
new HashMap<String, Object>() {{
put("action", klarnaPaymentsSDKError.getAction());
put("isFatal", klarnaPaymentsSDKError.isFatal());
put("message", klarnaPaymentsSDKError.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object>() {{
put(PARAM_NAME_PROGRESS_EVENT, webViewMap);
}});
postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_LOAD_PROGRESS, params, view);
Expand All @@ -56,35 +56,35 @@ 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<String, Object>() {{
put(PARAM_NAME_NAVIGATION_ERROR, webViewMap);
}});
postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_ERROR, params, view);
}

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<String, Object>() {{
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<String, Object>() {{
put(PARAM_NAME_ACTION, klarnaProductEvent.getAction());
}});
WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{
WritableMap params = ArgumentsUtil.createMap(new HashMap<String, Object>() {{
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<String, Object>() {{
put(PARAM_NAME_DID_CRASH, didCrash);
}});
WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{
WritableMap params = ArgumentsUtil.createMap(new HashMap<String, Object>() {{
put(PARAM_NAME_RENDER_PROCESS_GONE_EVENT, eventMap);
}});
postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_RENDER_PROCESS_GONE, params, view);
Expand Down Expand Up @@ -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<String, Object>() {{
if (webView != null) {
put(PARAM_NAME_URL, url != null ? url : webView.getUrl());
put(PARAM_NAME_TITLE, webView.getTitle());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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})
Expand All @@ -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<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return new JavaOnlyArray();
}
});
.thenAnswer(invocation -> new JavaOnlyArray());
PowerMockito.when(Arguments.createMap())
.thenAnswer(
new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return new JavaOnlyMap();
}
});
.thenAnswer(invocation -> new JavaOnlyMap());
}

@After
Expand Down
Loading