### Question:
Gateway { public static boolean handleGooglePayResult(int requestCode, int resultCode, Intent data, GatewayGooglePayCallback callback) { if (callback == null) { return false; } if (requestCode == REQUEST_GOOGLE_PAY_LOAD_PAYMENT_DATA) { if (resultCode == Activity.RESULT_OK) { try { PaymentData paymentData = PaymentData.getFromIntent(data); JSONObject json = new JSONObject(paymentData.toJson()); callback.onReceivedPaymentData(json); } catch (Exception e) { callback.onGooglePayError(Status.RESULT_INTERNAL_ERROR); } } else if (resultCode == Activity.RESULT_CANCELED) { callback.onGooglePayCancelled(); } else if (resultCode == AutoResolveHelper.RESULT_ERROR) { Status status = AutoResolveHelper.getStatusFromIntent(data); callback.onGooglePayError(status); } return true; } return false; } Gateway(); String getMerchantId(); Gateway setMerchantId(String merchantId); Region getRegion(); Gateway setRegion(Region region); void updateSession(String sessionId, String apiVersion, GatewayMap payload, GatewayCallback callback); Single<GatewayMap> updateSession(String sessionId, String apiVersion, GatewayMap payload); static void start3DSecureActivity(Activity activity, String html); static void start3DSecureActivity(Activity activity, String html, String title); static boolean handle3DSecureResult(int requestCode, int resultCode, Intent data, Gateway3DSecureCallback callback); static void requestGooglePayData(PaymentsClient paymentsClient, PaymentDataRequest request, Activity activity); static boolean handleGooglePayResult(int requestCode, int resultCode, Intent data, GatewayGooglePayCallback callback);  }### Answer:
@Test public void testHandleGooglePayResultReturnsFalseWithNullCallback() { assertFalse(Gateway.handleGooglePayResult(0, 0, null, null)); }
@Test public void testHandleGooglePayResultReturnsFalseIfInvalidRequestCode() { int invalidRequestCode = 10; GatewayGooglePayCallback callback = mock(GatewayGooglePayCallback.class); assertFalse(Gateway.handleGooglePayResult(invalidRequestCode, 0, null, callback)); }
@Test public void testHandleGooglePayResultCallsError() { int requestCode = Gateway.REQUEST_GOOGLE_PAY_LOAD_PAYMENT_DATA; int resultCode = AutoResolveHelper.RESULT_ERROR; Status mockStatus = mock(Status.class); Intent mockData = mock(Intent.class); doReturn(mockStatus).when(mockData).getParcelableExtra("com.google.android.gms.common.api.AutoResolveHelper.status"); GatewayGooglePayCallback callback = spy(new GatewayGooglePayCallback() { @Override public void onReceivedPaymentData(JSONObject paymentData) { fail("Should not have received payment data"); } @Override public void onGooglePayCancelled() { fail("Should not have called cancelled"); } @Override public void onGooglePayError(Status status) { assertEquals(mockStatus, status); } }); boolean result = Gateway.handleGooglePayResult(requestCode, resultCode, mockData, callback); assertTrue(result); verify(callback).onGooglePayError(any()); }
@Test public void testHandleGooglePayResultCallsCancelled() { int requestCode = Gateway.REQUEST_GOOGLE_PAY_LOAD_PAYMENT_DATA; int resultCode = Activity.RESULT_CANCELED; GatewayGooglePayCallback callback = mock(GatewayGooglePayCallback.class); boolean result = Gateway.handleGooglePayResult(requestCode, resultCode, null, callback); assertTrue(result); verify(callback).onGooglePayCancelled(); }
@Test public void testHandleGooglePayResultCallsPaymentDataOnSuccess() { int requestCode = Gateway.REQUEST_GOOGLE_PAY_LOAD_PAYMENT_DATA; int resultCode = Activity.RESULT_OK; PaymentData pData = PaymentData.fromJson("{}"); Intent data = new Intent(); pData.putIntoIntent(data); GatewayGooglePayCallback callback = mock(GatewayGooglePayCallback.class); boolean result = Gateway.handleGooglePayResult(requestCode, resultCode, data, callback); assertTrue(result); verify(callback).onReceivedPaymentData(any()); }