@Nonnull protected Billing newBilling() { return Tests.newBilling(); }
@Nonnull static Billing newBilling() { return newBilling(true); }
@Nonnull static Billing newBilling(boolean cache) { return newBilling(cache, false); }
@Nonnull protected Billing newBilling() { return Tests.newBilling(new Billing.Configuration() { @Nonnull @Override public String getPublicKey() { return "test"; } @Nullable @Override public Cache getCache() { return mFailingCache; } @Nonnull @Override public PurchaseVerifier getPurchaseVerifier() { return Billing.newPurchaseVerifier(this.getPublicKey()); } @Nullable @Override public Inventory getFallbackInventory(@Nonnull Checkout checkout, @Nonnull Executor onLoadExecutor) { return null; } @Override public boolean isAutoConnect() { return true; } }); }
@Nonnull private Billing prepareMultiPurchasesBilling() throws RemoteException, JSONException { final Billing billing = Tests.newBilling(true); prepareMultiPurchasesService(billing); return billing; }
@Nonnull static Billing newBilling(boolean cache, boolean autoConnect) { return newBilling(newConfiguration(cache, autoConnect)); }
@Before public void setUp() throws Exception { Billing billing = newBilling(); billing.connect(); mService = ((TestServiceConnector) billing.getConnector()).mService; when(mService.isBillingSupported(eq(3), anyString(), eq(IN_APP))).thenReturn(OK); when(mService.isBillingSupported(eq(3), anyString(), eq(SUBSCRIPTION))).thenReturn(OK); mCheckout = Checkout.forApplication(billing); }
@Test public void testShouldCancelRequests() throws Exception { final int REQUESTS = 10; final Billing b = Tests.newBilling(false); final CountDownLatch latch = new CountDownLatch(REQUESTS / 2); final RequestListener l = new CountDownListener(latch); final List<Integer> requestIds = new ArrayList<Integer>(); for (int i = 0; i < REQUESTS; i++) { requestIds.add(b.runWhenConnected(new SleepingRequest(100), l, null)); } Thread.sleep(100 * (REQUESTS / 2 - 1)); for (int i = REQUESTS / 2; i < REQUESTS; i++) { b.cancel(requestIds.get(i)); } assertTrue(latch.await(1, SECONDS)); }
@Before public void setUp() throws Exception { mBilling = Tests.newBilling(); mCheckout = Checkout.forActivity(new Activity(), mBilling); }
@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)); }
@Before public void setUp() throws Exception { final Billing billing = Tests.newBilling(); billing.setMainThread(sameThreadExecutor()); mRequest = Inventory.Request.create().loadSkus(IN_APP, asList("sku_0", "sku_1", "sku_2", "sku_3", "sku_4", "sku_6")).loadAllPurchases(); mCheckout = Checkout.forApplication(billing); mDb = new BillingDB(RuntimeEnvironment.application); }
@Test public void testShouldRunAllRequests() throws Exception { final int REQUESTS = 100; final int SLEEP = 10; final Billing b = Tests.newBilling(false); b.setMainThread(Tests.sameThreadExecutor()); final AsyncServiceConnector c = new AsyncServiceConnector(b); b.setConnector(c); final CountDownLatch latch = new CountDownLatch(REQUESTS); final RequestListener l = new CountDownListener(latch); for (int i = 0; i < REQUESTS; i++) { if (i % 10 == 0) { if (mRandom.nextBoolean()) { b.connect(); } else { // connector is called directly in order to avoid cancelling the pending // requests c.disconnect(); } } b.runWhenConnected(new SleepingRequest(mRandom.nextInt(SLEEP)), l, null); } b.connect(); assertTrue(latch.await(SLEEP * REQUESTS, TimeUnit.MILLISECONDS)); }
@Test public void testShouldAutoDisconnect() throws Exception { final Billing billing = Tests.newBilling(true, true); assertTrue(billing.getState() == Billing.State.INITIAL); billing.onCheckoutStarted(); assertTrue(billing.getState() == Billing.State.CONNECTED); billing.onCheckoutStarted(); assertTrue(billing.getState() == Billing.State.CONNECTED); billing.onCheckoutStopped(); assertTrue(billing.getState() == Billing.State.CONNECTED); billing.onCheckoutStopped(); assertTrue(billing.getState() == Billing.State.DISCONNECTED); }