/** * Same as {@link #start(Listener)} but with no initial request listener. */ public void start() { start(null); }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_history); ButterKnife.bind(this); final Adapter adapter = new Adapter(); mRecycler.setLayoutManager(new LinearLayoutManager(this)); mRecycler.setAdapter(adapter); final Billing billing = CheckoutApplication.get(this).getBilling(); mCheckout = Checkout.forActivity(this, billing); mCheckout.start(); mCheckout.whenReady(new HistoryLoader(adapter)); }
@Test public void testAllProductsShouldBeSupported() throws Exception { final AwaitingListener l = new AwaitingListener(); mCheckout.start(l); l.waitWhileLoading(); verify(l.mListener, times(2)).onReady(any(BillingRequests.class), anyString(), eq(true)); verify(l.mListener, never()).onReady(any(BillingRequests.class), anyString(), eq(false)); verify(l.mListener).onReady(any(BillingRequests.class)); }
@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()); }
@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()); }
@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()); }
@Test public void testShouldLoadPurchasesWhenProductsBecameSupported() throws Exception { when(mService.isBillingSupported(eq(3), anyString(), eq(IN_APP))).thenReturn(BILLING_UNAVAILABLE); when(mService.isBillingSupported(eq(3), anyString(), eq(SUBSCRIPTION))).thenReturn(BILLING_UNAVAILABLE); when(mService.getPurchases(anyInt(), anyString(), anyString(), isNull(String.class))).thenReturn(newPurchasesBundle(0, false)); mCheckout.start(); final AwaitingCallback c1 = new AwaitingCallback(); mCheckout.loadInventory(Inventory.Request.create().loadAllPurchases(), c1); c1.waitWhileLoading(); assertFalse(c1.mProducts.get(IN_APP).supported); assertFalse(c1.mProducts.get(SUBSCRIPTION).supported); assertTrue(c1.mProducts.get(IN_APP).getPurchases().isEmpty()); when(mService.isBillingSupported(eq(3), anyString(), eq(IN_APP))).thenReturn(OK); when(mService.isBillingSupported(eq(3), anyString(), eq(SUBSCRIPTION))).thenReturn(OK); final AwaitingCallback c2 = new AwaitingCallback(); mCheckout.loadInventory(Inventory.Request.create().loadAllPurchases(), c2); c2.waitWhileLoading(); assertTrue(c2.mProducts.get(IN_APP).supported); assertTrue(c2.mProducts.get(SUBSCRIPTION).supported); assertTrue(c2.mProducts.get(IN_APP).getPurchases().size() == 1); }
@Test public void testShouldCancelAllTasks() throws Exception { mCheckout.start(); mInventory.load(mRequest, mock(Inventory.Callback.class)); mInventory.load(mRequest, mock(Inventory.Callback.class)); Assert.assertTrue(mInventory.isLoading()); mInventory.cancel(); Assert.assertFalse(mInventory.isLoading()); }
@Test public void testShouldCancelTaskById() throws Exception { mCheckout.start(); final int task1 = mInventory.load(mRequest, mock(Inventory.Callback.class)); final int task2 = mInventory.load(mRequest, mock(Inventory.Callback.class)); Assert.assertTrue(mInventory.isLoading()); mInventory.cancel(task1); Assert.assertTrue(mInventory.isLoading()); mInventory.cancel(task2); Assert.assertFalse(mInventory.isLoading()); }
@Test public void testShouldLoadSkus() throws Exception { populateSkus(); mCheckout.start(); final TestCallback c1 = new TestCallback(); mInventory.load(Inventory.Request.create().loadSkus(SUBSCRIPTION, asList("subX", "sub2", "sub3")), c1); final TestCallback c2 = new TestCallback(); mInventory.load(Inventory.Request.create().loadSkus(IN_APP, asList("1", "2", "5")), c2); Tests.waitWhileLoading(mInventory); assertEquals(0, c1.mProducts.get(SUBSCRIPTION).getSkus().size()); assertEquals(2, c2.mProducts.get(IN_APP).getSkus().size()); }
@Test public void testShouldLoadPurchases() throws Exception { populatePurchases(); populateSkus(); final TestCallback listener = new TestCallback(); mCheckout.start(); mInventory.load(mRequest, listener); Tests.waitWhileLoading(mInventory); final boolean complete = shouldVerifyPurchaseCompletely(); final Inventory.Product inApp = listener.mProducts.get(IN_APP); final List<Purchase> actualInApps = inApp.getPurchases(); assertEquals(4, actualInApps.size()); verifyPurchase(actualInApps.get(0), 4, EXPIRED, complete, false); verifyPurchase(actualInApps.get(1), 3, REFUNDED, complete, false); verifyPurchase(actualInApps.get(2), 2, CANCELLED, complete, false); verifyPurchase(actualInApps.get(3), 1, PURCHASED, complete, false); final Inventory.Product sub = listener.mProducts.get(SUBSCRIPTION); final List<Purchase> actualSubs = sub.getPurchases(); assertEquals(4, actualSubs.size()); verifyPurchase(actualSubs.get(0), 4, EXPIRED, complete, true); verifyPurchase(actualSubs.get(1), 3, REFUNDED, complete, true); verifyPurchase(actualSubs.get(2), 2, CANCELLED, complete, true); verifyPurchase(actualSubs.get(3), 1, PURCHASED, complete, true); }
@Test public void testShouldLoadSkus() throws Exception { populateSkus(); mCheckout.start(); final TestCallback c1 = new TestCallback(); mInventory.load(Inventory.Request.create().loadSkus(SUBSCRIPTION, asList("subX", "sub2", "sub3")), c1); final TestCallback c2 = new TestCallback(); mInventory.load(Inventory.Request.create().loadSkus(IN_APP, asList("1", "2", "5")), c2); Tests.waitWhileLoading(mInventory); assertEquals(2, c1.mProducts.get(SUBSCRIPTION).getSkus().size()); assertEquals(2, c2.mProducts.get(IN_APP).getSkus().size()); }
@Test public void testShouldContinueAfterCacheException() throws Exception { populatePurchases(); final CheckoutInventory inventory = new CheckoutInventory(mCheckout); final InventoryTestBase.TestCallback listener = new InventoryTestBase.TestCallback(); mCheckout.start(); inventory.load(mRequest, listener); Tests.waitWhileLoading(inventory); assertTrue(mFailingCache.mExceptionThrown); }
@Test public void testIsLoadedWithEmptySkusList() throws Exception { populatePurchases(); populateSkus(); final Inventory.Request request = Inventory.Request.create() .loadAllPurchases() .loadSkus(IN_APP, "in_app_01") .loadSkus(SUBSCRIPTION, "sub_01"); final Checkout checkout = Checkout.forApplication(mBilling); final CheckoutInventory inventory = new CheckoutInventory(checkout); final TestCallback listener = new TestCallback(); checkout.start(); inventory.load(request, listener); Tests.waitWhileLoading(inventory); final Inventory.Product app = listener.mProducts.get(IN_APP); Assert.assertTrue(app.getSkus().isEmpty()); Assert.assertFalse(app.getPurchases().isEmpty()); final Inventory.Product sub = listener.mProducts.get(SUBSCRIPTION); Assert.assertTrue(sub.getSkus().isEmpty()); Assert.assertFalse(sub.getPurchases().isEmpty()); }
@Test public void testShouldLoadSeveralTasksAsynchronously() throws Exception { populatePurchases(); populateSkus(); mCheckout.start(); final CountingCallback c = new CountingCallback(); mInventory.load(Inventory.Request.create().loadPurchases(IN_APP), c); mInventory.load(Inventory.Request.create().loadPurchases(SUBSCRIPTION), c); mInventory.load(Inventory.Request.create().loadSkus(SUBSCRIPTION, asList("sub1", "sub2", "sub3", "sub4")), c); mInventory.load(Inventory.Request.create().loadSkus(IN_APP, asList("1", "2", "3", "4", "6")), c); mInventory.load(mRequest, c); Tests.waitWhileLoading(mInventory); assertEquals(5, c.mCount); }
@Test public void testShouldContinueAfterListenerException() throws Exception { populatePurchases(); final Inventory.Request request = Inventory.Request.create() .loadSkus(IN_APP, "in_app_01") .loadSkus(SUBSCRIPTION, "sub_01"); final Checkout checkout = Checkout.forApplication(mBilling); final CrashingCallback listener = new CrashingCallback(); final CheckoutInventory inventory = new CheckoutInventory(checkout); checkout.start(); inventory.load(request, listener); Tests.waitWhileLoading(inventory); Assert.assertTrue(listener.exceptionThrown); }