/** * This method must be called from {@link Activity#onActivityResult(int, int, Intent)} (or * {@link android.app.Fragment#onActivityResult(int, int, Intent)}) in order to finish a started * purchase flow. * * @return true if activity result was handled (there existed a purchase flow for the given * <var>requestCode</var>) * @see Activity#onActivityResult(int, int, Intent) * @see android.app.Fragment#onActivityResult(int, int, Intent) */ public boolean onActivityResult(int requestCode, int resultCode, Intent data) { final PurchaseFlow flow = mFlows.get(requestCode); if (flow == null) { Billing.warning("Purchase flow doesn't exist for requestCode=" + requestCode + ". Have you forgotten to create it?"); return false; } flow.onActivityResult(requestCode, resultCode, data); return true; }
@Test public void testShouldErrorIfRequestCodeIsDifferent() throws Exception { mFlow.onActivityResult(2, 1, new Intent()); verifyError(ResponseCodes.EXCEPTION, RuntimeException.class); }
@Test public void testShouldErrorIfResultCodeItNotOk() throws Exception { mFlow.onActivityResult(1, Activity.RESULT_CANCELED, new Intent()); verifyError(OK, BillingException.class); }
@Test public void testShouldFinishSuccessfully() throws Exception { Tests.mockVerifier(mVerifier, true); mFlow.onActivityResult(1, RESULT_OK, newOkIntent()); verify(mListener, never()).onError(anyInt(), any(Exception.class)); verify(mListener).onSuccess(any(Purchase.class)); }
@Test public void testShouldNotCallListenerIfCancelled() throws Exception { Tests.mockVerifier(mVerifier, true); mFlow.cancel(); mFlow.onActivityResult(1, RESULT_OK, newOkIntent()); mFlow.onActivityResult(1, RESULT_OK, newIntent(ACCOUNT_ERROR, "{productId:'test', purchaseTime:1000}", "signature")); verify(mListener, never()).onError(anyInt(), any(Exception.class)); verify(mListener, never()).onSuccess(any(Purchase.class)); } }
@Test public void testShouldErrorIfIntentIsNull() throws Exception { mFlow.onActivityResult(1, 1, null); verifyError(ResponseCodes.NULL_INTENT, BillingException.class); }
@Test public void testShouldErrorWithEmptySignature() throws Exception { mFlow.onActivityResult(1, RESULT_OK, newIntent(OK, "{productId:'test', purchaseTime:1000}", "")); verifyError(ResponseCodes.WRONG_SIGNATURE, RuntimeException.class); }
@Test public void testShouldErrorWithNoData() throws Exception { mFlow.onActivityResult(1, RESULT_OK, newIntent(OK, null, "signature")); verifyError(ResponseCodes.EXCEPTION, RuntimeException.class); }
@Test public void testShouldErrorWithNoSignature() throws Exception { mFlow.onActivityResult(1, RESULT_OK, newIntent(OK, "data", null)); verifyError(ResponseCodes.EXCEPTION, RuntimeException.class); }
@Test public void testShouldErrorIfResponseCodeItNotOk() throws Exception { mFlow.onActivityResult(1, RESULT_OK, newIntent(ResponseCodes.ACCOUNT_ERROR, null, null)); verifyError(ResponseCodes.ACCOUNT_ERROR, BillingException.class); }
@Test public void testShouldErrorIfVerificationFailed() throws Exception { Tests.mockVerifier(mVerifier, false); mFlow.onActivityResult(1, RESULT_OK, newIntent(OK, "{productId:'test', purchaseTime:1000}", "signature")); verifyError(ResponseCodes.WRONG_SIGNATURE, RuntimeException.class); }