private void loadPurchases(@Nonnull final BillingRequests requests, @Nonnull final Product product) { requests.getAllPurchases(product.id, synchronizedListener(new RequestListener<Purchases>() { @Override public void onSuccess(@Nonnull Purchases purchases) { product.setPurchases(purchases.list); countDown(); } @Override public void onError(int response, @Nonnull Exception e) { countDown(); } })); }
@Override public void onSuccess(@Nonnull Object result) { requests.getWholePurchaseHistory(ProductTypes.IN_APP, null, HistoryLoader.this); } });
@Override public void onReady(@Nonnull BillingRequests requests) { final PurchaseFlow flow = mCheckout.createOneShotPurchaseFlow(new PurchaseListener()); requests.changeSubscription(new ArrayList<>(old), sku, null, flow); } });
@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)); }
@Test public void testShouldNotifyErrorIfCantConnect() throws Exception { final Billing.ServiceConnector connector = mock(Billing.ServiceConnector.class); when(connector.connect()).thenReturn(false); mBilling.setConnector(connector); final RequestListener<Object> l = mock(RequestListener.class); mBilling.getRequests().isBillingSupported("p", l); verify(l, times(1)).onError(eq(ResponseCodes.SERVICE_NOT_CONNECTED), any(BillingException.class)); verify(l, times(0)).onSuccess(any()); }
@Override public void onReady(@Nonnull BillingRequests requests) { requests.consume(purchase.token, makeRequestListener()); } });
@Override public void onReady(@Nonnull final BillingRequests requests) { requests.isGetPurchaseHistorySupported(ProductTypes.IN_APP, new EmptyRequestListener<Object>() { @Override public void onSuccess(@Nonnull Object result) { requests.getWholePurchaseHistory(ProductTypes.IN_APP, null, HistoryLoader.this); } }); }
@Override public void onReady(@Nonnull BillingRequests requests) { requests.purchase(product, sku, payload, extraParams, getPurchaseFlow()); } });
private void loadSkus(@Nonnull BillingRequests requests, @Nonnull final Product product) { final List<String> skuIds = mTask.getRequest().getSkus(product.id); if (skuIds.isEmpty()) { Billing.warning("There are no SKUs for \"" + product.id + "\" product. No SKU information will be loaded"); synchronized (mLock) { countDown(); } return; } requests.getSkus(product.id, skuIds, synchronizedListener(new RequestListener<Skus>() { @Override public void onSuccess(@Nonnull Skus skus) { product.setSkus(skus.list); countDown(); } @Override public void onError(int response, @Nonnull Exception e) { countDown(); } })); } }
private void checkIsPurchased(@Nonnull String id, boolean purchased) throws RemoteException, JSONException, InterruptedException { final Billing billing = prepareMultiPurchasesBilling(); final CountDownLatch latch = new CountDownLatch(1); final CountDownListener l = new CountDownListener(latch); billing.getRequests().isPurchased(ProductTypes.IN_APP, id, l); assertTrue(latch.await(1, SECONDS)); verify(l.listener).onSuccess(eq(purchased)); verify(l.listener, never()).onSuccess(eq(!purchased)); }
@Test public void testShouldExecuteRequestIfConnected() throws Exception { final Billing.ServiceConnector connector = mock(Billing.ServiceConnector.class); final IInAppBillingService service = mock(IInAppBillingService.class); when(service.isBillingSupported(anyInt(), anyString(), anyString())).thenReturn(OK); when(connector.connect()).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { mBilling.setService(service, true); return true; } }); mBilling.setConnector(connector); final RequestListener<Object> l = mock(RequestListener.class); mBilling.getRequests().isBillingSupported("p", l); verify(l, times(0)).onError(anyInt(), any(BillingException.class)); verify(l, times(1)).onSuccess(any()); }
@Override public void onReady(@Nonnull BillingRequests requests) { requests.consume(purchase.token, new ConsumeListener()); } });
@Test public void testShouldLoadWholePurchaseHistory() throws Exception { final Billing billing = prepareMultiPurchasesBilling(); final CountDownLatch latch = new CountDownLatch(1); final CountDownListener l = new CountDownListener(latch); billing.getRequests().getWholePurchaseHistory(ProductTypes.IN_APP, null, l); assertTrue(latch.await(1, SECONDS)); verify(l.listener).onSuccess(argThat(new PurchasesMatcher())); }
@Test public void testShouldLoadAllPurchases() throws Exception { final Billing billing = prepareMultiPurchasesBilling(); final CountDownLatch latch = new CountDownLatch(1); final CountDownListener l = new CountDownListener(latch); billing.getRequests().getAllPurchases(ProductTypes.IN_APP, l); assertTrue(latch.await(1, SECONDS)); verify(l.listener).onSuccess(argThat(new PurchasesMatcher())); }
@Test public void testShouldNotifyErrorIfConnectorReturnedNull() throws Exception { final Billing.ServiceConnector connector = mock(Billing.ServiceConnector.class); when(connector.connect()).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { mBilling.setService(null, true); return true; } }); mBilling.setConnector(connector); final RequestListener<Object> l = mock(RequestListener.class); mBilling.getRequests().isBillingSupported("p", l); verify(l, times(1)).onError(eq(ResponseCodes.SERVICE_NOT_CONNECTED), any(BillingException.class)); verify(l, times(0)).onSuccess(any()); }