public void onCheckoutStarted() { Check.isMainThread(); synchronized (mLock) { mCheckoutCount++; if (mCheckoutCount > 0 && mConfiguration.isAutoConnect()) { connect(); } } }
<R> int runWhenConnected(@Nonnull Request<R> request, @Nullable RequestListener<R> listener, @Nullable Object tag) { if (listener != null) { if (mCache.hasCache()) { listener = new CachingRequestListener<>(request, listener); } request.setListener(listener); } if (tag != null) { request.setTag(tag); } mPendingRequests.add(onConnectedService(request)); connect(); return request.getId(); }
@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); }
connect(); return false; } else {
@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 testStates() throws Exception { final Billing.ServiceConnector connector = mock(Billing.ServiceConnector.class); when(connector.connect()).thenReturn(true); mBilling.setConnector(connector); mBilling.connect(); assertEquals(Billing.State.CONNECTING, mBilling.getState()); mBilling.setService(mock(IInAppBillingService.class), true); assertEquals(Billing.State.CONNECTED, mBilling.getState()); mBilling.disconnect(); assertEquals(Billing.State.DISCONNECTING, mBilling.getState()); mBilling.setService(null, false); assertEquals(Billing.State.DISCONNECTED, mBilling.getState()); }