@Override protected void insertPurchases(@Nonnull String product, @Nonnull List<Purchase> purchases) throws RemoteException { Tests.mockGetPurchases(mBilling, product, purchases); }
@Override protected void insertSkus(@Nonnull String product, @Nonnull List<Sku> skus) throws Exception { Tests.mockGetSkuDetails(mBilling, product, skus); }
@Nonnull static PurchaseVerifier newMockVerifier(final boolean verified) { return mockVerifier(mock(PurchaseVerifier.class), verified); }
@Nonnull static Billing newBilling(boolean cache, boolean autoConnect) { return newBilling(newConfiguration(cache, autoConnect)); }
@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; }
@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; }
@Test public void testShouldCallListenerWhenLoaded() throws Exception { final Inventory.Callback c = mock(Inventory.Callback.class); mCheckout.start(); mInventory.load(mRequest, c); Tests.waitWhileLoading(mInventory); verify(c, times(1)).onLoaded(anyProducts()); }
@Nonnull @Override protected Inventory newInventory(@Nonnull Checkout checkout) { return new RobotmediaInventory(checkout, sameThreadExecutor()); }
@Nonnull static Billing newBilling() { return newBilling(true); }
@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 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)); }
@Override protected GetPurchasesRequest newRequest() { return new GetPurchasesRequest("test", null, Tests.newMockVerifier(true)); }
@Before public void setUp() throws Exception { mBilling = Tests.newSynchronousBilling(); mRandom = new Random(currentTimeMillis()); }
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); }
@Test public void testListenerShouldBeCalledOnlyOnce() throws Exception { final Inventory.Callback l = mock(Inventory.Callback.class); mCheckout.start(); mInventory.load(mRequest, mock(Inventory.Callback.class)); mInventory.load(mRequest, mock(Inventory.Callback.class)); mInventory.load(mRequest, l); Tests.waitWhileLoading(mInventory); mInventory.load(mRequest, mock(Inventory.Callback.class)); mInventory.load(mRequest, mock(Inventory.Callback.class)); verify(l, times(1)).onLoaded(anyProducts()); }
@Nonnull @Override protected Billing newBilling() { final Billing billing = super.newBilling(); billing.setMainThread(sameThreadExecutor()); return billing; }
@Nonnull static Billing newBilling(boolean cache) { return newBilling(cache, false); }
@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 testShouldCreateFromOldRequest() throws Exception { final GetPurchasesRequest oldRequest = new GetPurchasesRequest("test", "first", Tests.newMockVerifier(true)); final GetPurchasesRequest newRequest = new GetPurchasesRequest(oldRequest, "second"); assertEquals("second", newRequest.getContinuationToken()); assertEquals("test", newRequest.getProduct()); assertSame(oldRequest.getListener(), newRequest.getListener()); }
@Test public void testCanceledCallbackShouldNotGetCalled() throws Exception { mCheckout.start(); final Inventory.Callback c1 = mock(Inventory.Callback.class); final Inventory.Callback c2 = mock(Inventory.Callback.class); final int task1 = mInventory.load(mRequest, c1); mInventory.load(mRequest, c2); mInventory.cancel(task1); Tests.waitWhileLoading(mInventory); verify(c1, never()).onLoaded(anyProducts()); verify(c2, times(1)).onLoaded(anyProducts()); }