@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()); }
static void setService(@Nonnull final Billing billing, @Nonnull final IInAppBillingService service) { if (billing.getState() != Billing.State.INITIAL) { billing.disconnect(); } billing.setConnector(new TestServiceConnector(billing, service)); }
@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 testShouldDisconnectServiceIfBillingIsInactive() throws Exception { final Billing.ServiceConnector connector = mock(Billing.ServiceConnector.class); mBilling.setConnector(connector); mBilling.setState(Billing.State.CONNECTING); mBilling.setState(Billing.State.DISCONNECTED); mBilling.setService(mock(IInAppBillingService.class), true); assertEquals(Billing.State.DISCONNECTED, mBilling.getState()); verify(connector, times(1)).disconnect(); }
@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()); }
@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)); }