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); }
@Nonnull static Billing newBilling(@Nonnull Billing.Configuration configuration) { final Billing billing = new Billing(RuntimeEnvironment.application, configuration); billing.setPurchaseVerifier(Tests.newMockVerifier(true)); final IInAppBillingService service = mock(IInAppBillingService.class); setService(billing, service); return billing; }
@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)); }
@Nonnull static Billing newSynchronousBilling() { final Billing billing = new Billing(RuntimeEnvironment.application, newConfiguration(true, false)); billing.setPurchaseVerifier(Tests.newMockVerifier(true)); final IInAppBillingService service = mock(IInAppBillingService.class); final CancellableExecutor sameThreadExecutor = sameThreadExecutor(); billing.setBackground(sameThreadExecutor); billing.setMainThread(sameThreadExecutor); setService(billing, service); return billing; }