Utility.logd(TAG, "In app billing service connected"); try { Bundle ownedItems = inAppBillingService.getPurchases( IN_APP_PURCHASE_VERSION, context.getPackageName(),
Bundle bundle = billingService.getPurchases(Constants.GOOGLE_API_VERSION, contextPackageName, type, null); if (bundle.getInt(Constants.RESPONSE_CODE) == Constants.BILLING_RESPONSE_RESULT_OK)
@Override protected Bundle request(@Nonnull IInAppBillingService service, @Nonnull String packageName) throws RemoteException { return service.getPurchases(mApiVersion, packageName, mProduct, mContinuationToken); }
private void prepareMultiPurchasesService(@Nonnull Billing billing) throws RemoteException, JSONException { final IInAppBillingService service = mock(IInAppBillingService.class); when(service.getPurchases(anyInt(), anyString(), anyString(), isNull(String.class))).thenReturn(newPurchasesBundle(0, true)); when(service.getPurchases(anyInt(), anyString(), anyString(), eq("1"))).thenReturn(newPurchasesBundle(1, true)); when(service.getPurchases(anyInt(), anyString(), anyString(), eq("2"))).thenReturn(newPurchasesBundle(2, true)); when(service.getPurchases(anyInt(), anyString(), anyString(), eq("3"))).thenReturn(newPurchasesBundle(3, true)); when(service.getPurchases(anyInt(), anyString(), anyString(), eq("4"))).thenReturn(newPurchasesBundle(4, false)); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), isNull(String.class), any(Bundle.class))).thenReturn(newPurchasesBundle(0, true, true)); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), eq("1"), any(Bundle.class))).thenReturn(newPurchasesBundle(1, true, true)); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), eq("2"), any(Bundle.class))).thenReturn(newPurchasesBundle(2, true, true)); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), eq("3"), any(Bundle.class))).thenReturn(newPurchasesBundle(3, true, true)); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), eq("4"), any(Bundle.class))).thenReturn(newPurchasesBundle(4, false, true)); Tests.setService(billing, service); }
static void mockGetPurchases(@Nonnull Billing billing, @Nonnull String product, @Nonnull List<Purchase> purchases) throws RemoteException { final Bundle bundle = newBundle(OK); final ArrayList<String> list = new ArrayList<String>(); for (Purchase purchase : purchases) { list.add(purchase.toJson()); } bundle.putStringArrayList(Purchases.BUNDLE_DATA_LIST, list); final IInAppBillingService service = ((TestServiceConnector) billing.getConnector()).mService; when(service.getPurchases(anyInt(), anyString(), eq(product), isNull(String.class))) .thenReturn(bundle); }
@Test public void testShouldError() throws Exception { final Request request = newRequest(); final RequestListener l = mock(RequestListener.class); request.setListener(l); final Bundle bundle = newBundle(BILLING_UNAVAILABLE); final IInAppBillingService service = mock(IInAppBillingService.class); when(service.isBillingSupported(anyInt(), anyString(), anyString())).thenReturn(BILLING_UNAVAILABLE); when(service.consumePurchase(anyInt(), anyString(), anyString())).thenReturn(BILLING_UNAVAILABLE); when(service.getPurchases(anyInt(), anyString(), anyString(), anyString())).thenReturn(bundle); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), anyString(), any(Bundle.class))).thenReturn(bundle); when(service.getSkuDetails(anyInt(), anyString(), anyString(), any(Bundle.class))).thenReturn(bundle); when(service.getBuyIntent(anyInt(), anyString(), anyString(), anyString(), anyString())).thenReturn(bundle); request.start(service, "testse"); verify(l).onError(eq(BILLING_UNAVAILABLE), any(Exception.class)); verify(l, never()).onSuccess(any()); }
@Test public void testShouldErrorIfJsonException() throws Exception { final GetPurchasesRequest request = newRequest(); final RequestListener l = mock(RequestListener.class); request.setListener(l); final IInAppBillingService service = mock(IInAppBillingService.class); final Bundle bundle = newBundle(OK); final ArrayList<String> datas = new ArrayList<String>(); datas.add("test"); bundle.putStringArrayList(Purchases.BUNDLE_DATA_LIST, datas); when(service.getPurchases(anyInt(), anyString(), anyString(), anyString())).thenReturn(bundle); request.start(service, "test"); verify(l, times(1)).onError(eq(EXCEPTION), any(JSONException.class)); }
@Test public void testEmptyListShouldNotBeVerified() throws Exception { final PurchaseVerifier verifier = mock(PurchaseVerifier.class); final GetPurchasesRequest request = new GetPurchasesRequest("test", null, verifier); final RequestListener listener = mock(RequestListener.class); request.setListener(listener); final IInAppBillingService service = mock(IInAppBillingService.class); when(service.getPurchases(anyInt(), anyString(), anyString(), anyString())).thenReturn(newBundle(OK)); request.start(service, "test"); verify(verifier, never()).verify(anyList(), any(RequestListener.class)); verify(listener, times(1)).onSuccess(anyObject()); }
@Test public void testShouldSuccess() throws Exception { final Request r = newRequest(); final RequestListener l = mock(RequestListener.class); r.setListener(l); final IInAppBillingService service = mock(IInAppBillingService.class); when(service.isBillingSupported(anyInt(), anyString(), anyString())).thenReturn(OK); when(service.consumePurchase(anyInt(), anyString(), anyString())).thenReturn(OK); final Bundle purchases = new Bundle(); purchases.putStringArrayList("INAPP_PURCHASE_DATA_LIST", new ArrayList<String>()); when(service.getPurchases(anyInt(), anyString(), anyString(), anyString())).thenReturn(purchases); when(service.getPurchaseHistory(anyInt(), anyString(), anyString(), anyString(), any(Bundle.class))).thenReturn(purchases); final Bundle skuDetails = new Bundle(); skuDetails.putStringArrayList("DETAILS_LIST", new ArrayList<String>()); when(service.getSkuDetails(anyInt(), anyString(), anyString(), any(Bundle.class))).thenReturn(skuDetails); final Bundle buyIntent = new Bundle(); buyIntent.putParcelable("BUY_INTENT", PendingIntent.getActivity(RuntimeEnvironment.application, 100, new Intent(), 0)); when(service.getBuyIntent(anyInt(), anyString(), anyString(), anyString(), anyString())).thenReturn(buyIntent); r.start(service, ""); verify(l).onSuccess(anyObject()); verify(l, never()).onError(anyInt(), any(Exception.class)); }
@Test public void testShouldCancelIsPurchasedListener() throws Exception { final Billing billing = Tests.newBilling(true); final CountDownLatch requestWaiter = new CountDownLatch(1); final CountDownLatch cancelWaiter = new CountDownLatch(1); final IInAppBillingService service = mock(IInAppBillingService.class); when(service.getPurchases(anyInt(), anyString(), anyString(), isNull(String.class))).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { requestWaiter.countDown(); return newPurchasesBundle(0, true); } }); when(service.getPurchases(anyInt(), anyString(), anyString(), eq("1"))).thenAnswer(new Answer<Bundle>() { @Override public Bundle answer(InvocationOnMock invocation) throws Throwable { cancelWaiter.await(1, SECONDS); return newPurchasesBundle(1, false); } }); Tests.setService(billing, service); final RequestListener l = mock(RequestListener.class); final BillingRequests requests = billing.getRequests(); requests.isPurchased(ProductTypes.IN_APP, "1", l); requestWaiter.await(1, SECONDS); requests.cancelAll(); cancelWaiter.countDown(); verify(l, never()).onSuccess(anyObject()); verify(l, never()).onError(anyInt(), any(Exception.class)); }
Bundle ownedItems = mService.getPurchases(3, mContext.getPackageName(), itemType, continueToken);
@Test public void testShouldVerifyPurchasesAsynchronously() throws Exception { final GetPurchasesRequest request = new GetPurchasesRequest("test", null, new AsyncPurchaseVerifier()); final PurchasesAwareRequestListener l = new PurchasesAwareRequestListener(); request.setListener(l); final IInAppBillingService service = mock(IInAppBillingService.class); final ArrayList<String> list = new ArrayList<String>(); list.add(PurchaseTest.newJson(0, Purchase.State.REFUNDED)); list.add(PurchaseTest.newJson(1, Purchase.State.REFUNDED)); list.add(PurchaseTest.newJson(2, Purchase.State.CANCELLED)); list.add(PurchaseTest.newJson(3, PURCHASED)); list.add(PurchaseTest.newJson(4, PURCHASED)); list.add(PurchaseTest.newJson(5, PURCHASED)); list.add(PurchaseTest.newJson(6, PURCHASED)); final Bundle bundle = newBundle(OK); bundle.putStringArrayList(Purchases.BUNDLE_DATA_LIST, list); when(service.getPurchases(anyInt(), anyString(), anyString(), anyString())).thenReturn(bundle); request.start(service, "test"); assertNotNull(l.mPurchases); assertTrue(l.mPurchases.list.size() == 4); }
@Test public void testShouldLoadPurchasesWhenProductsBecameSupported() throws Exception { when(mService.isBillingSupported(eq(3), anyString(), eq(IN_APP))).thenReturn(BILLING_UNAVAILABLE); when(mService.isBillingSupported(eq(3), anyString(), eq(SUBSCRIPTION))).thenReturn(BILLING_UNAVAILABLE); when(mService.getPurchases(anyInt(), anyString(), anyString(), isNull(String.class))).thenReturn(newPurchasesBundle(0, false)); mCheckout.start(); final AwaitingCallback c1 = new AwaitingCallback(); mCheckout.loadInventory(Inventory.Request.create().loadAllPurchases(), c1); c1.waitWhileLoading(); assertFalse(c1.mProducts.get(IN_APP).supported); assertFalse(c1.mProducts.get(SUBSCRIPTION).supported); assertTrue(c1.mProducts.get(IN_APP).getPurchases().isEmpty()); when(mService.isBillingSupported(eq(3), anyString(), eq(IN_APP))).thenReturn(OK); when(mService.isBillingSupported(eq(3), anyString(), eq(SUBSCRIPTION))).thenReturn(OK); final AwaitingCallback c2 = new AwaitingCallback(); mCheckout.loadInventory(Inventory.Request.create().loadAllPurchases(), c2); c2.waitWhileLoading(); assertTrue(c2.mProducts.get(IN_APP).supported); assertTrue(c2.mProducts.get(SUBSCRIPTION).supported); assertTrue(c2.mProducts.get(IN_APP).getPurchases().size() == 1); }
@Override public boolean ownsSku(@NonNull String sku) { try { final Bundle bundle = service.getPurchases(3, packageName, BILLING_TYPE, null); if (responseOk(bundle)) { final ArrayList<String> list = bundle.getStringArrayList(RESPONSE_ITEM_LIST); return null != list && list.contains(sku); } } catch (RemoteException ignore) { } return false; }
private void whenGetPurchasesRequestThrow(Exception exception) { try { when(nativeInAppBillingService.getPurchases(BILLING_API_VERSION, PACKAGE_NAME_GOOD, PURCHASE_TYPE_IN_APP, null)) .thenThrow(exception); } catch (RemoteException e) { throw new RuntimeException(e); } }
private void whenGetPurchasesRequestReturn(Bundle response) { try { when(nativeInAppBillingService.getPurchases(BILLING_API_VERSION, PACKAGE_NAME_GOOD, PURCHASE_TYPE_IN_APP, null)).thenReturn(response); when(nativeInAppBillingService.getPurchases(BILLING_API_VERSION, PACKAGE_NAME_GOOD, PURCHASE_TYPE_SUBSCRIPTION, null)).thenReturn(purchasesResponseEmptyResponse()); } catch (RemoteException e) { throw new RuntimeException(e); } }
@Test public void shouldNotCancelRealPurchaseWhenCancellingTestPurchases() throws Exception { activityBindAndConnect(); whenGetPurchasesRequestReturn(purchasesResponseOneTransactionFullEdition()); v3InAppbillingService.cancelTestPurchases(); verify(nativeInAppBillingService).getPurchases(BILLING_API_VERSION, PACKAGE_NAME_GOOD, PURCHASE_TYPE_IN_APP, null); verify(nativeInAppBillingService).getPurchases(BILLING_API_VERSION, PACKAGE_NAME_GOOD, PURCHASE_TYPE_SUBSCRIPTION, null); verifyNoMoreInteractions(nativeInAppBillingService); }
@Test public void shouldCancelTestPurchaseWhenCancellingTestPurchases() throws Exception { activityBindAndConnect(); whenGetPurchasesRequestReturn(purchasesResponseOneTransactionFullEditionSandboxOrder()); v3InAppbillingService.cancelTestPurchases(); verify(nativeInAppBillingService).getPurchases(BILLING_API_VERSION, PACKAGE_NAME_GOOD, PURCHASE_TYPE_IN_APP, null); verify(nativeInAppBillingService).consumePurchase(eq(BILLING_API_VERSION), eq(PACKAGE_NAME_GOOD), anyString()); }
@Test public void getSubscriptionPurchasesWithResultOkShouldReturnPurchaseTransactions() throws Exception { activityBindAndConnect(); whenGetPurchasesRequestReturn(purchasesResponseOneTransactionFullEdition()); List<Transaction> transactions = v3InAppbillingService.getPurchases(); verify(nativeInAppBillingService).getPurchases(BILLING_API_VERSION, PACKAGE_NAME_GOOD, PURCHASE_TYPE_SUBSCRIPTION, null); assertEquals(1, transactions.size()); assertEquals(offerFullEditionEntitlement().getIdentifier(), transactions.get(0).getIdentifier()); }
@Test public void getPurchasesWithResultOkShouldReturnPurchaseTransactions() throws Exception { activityBindAndConnect(); whenGetPurchasesRequestReturn(purchasesResponseOneTransactionFullEdition()); List<Transaction> transactions = v3InAppbillingService.getPurchases(); verify(nativeInAppBillingService).getPurchases(BILLING_API_VERSION, PACKAGE_NAME_GOOD, PURCHASE_TYPE_IN_APP, null); assertEquals(1, transactions.size()); assertEquals(offerFullEditionEntitlement().getIdentifier(), transactions.get(0).getIdentifier()); }