/** * A factory method of {@link BillingRequests}. The constructed object is marked with the given * <var>tag</var>. All methods of {@link RequestListener} used in this {@link BillingRequests} * are called on the main application thread. * * @param tag requests marker * @return requests for the given <var>tag</var> */ @Nonnull public Requests getRequests(@Nullable Object tag) { if (tag == null) { return (Requests) getRequests(); } return (Requests) new RequestsBuilder().withTag(tag).onMainThread().create(); }
/** * Starts this {@link Checkout} and sends an initial request that checks whether billing is * supported for each product available in the Billing API. * * @param listener initial request listener */ public void start(@Nullable final Listener listener) { Check.isMainThread(); synchronized (mLock) { Check.isFalse(mState == State.STARTED, "Already started"); Check.isNull(mRequests, "Already started"); mState = State.STARTED; mBilling.onCheckoutStarted(); mRequests = mBilling.getRequests(mTag); } whenReady(listener == null ? new EmptyListener() {} : listener); }
@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()); }
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()); }
@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()); }
@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 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())); }