InventoryQuery { static void execute(@NonNull AbstractGooglePlayBillingApi api, @NonNull InventoryListener listener, @Nullable Collection<String> inappSkus, @Nullable Collection<String> subSkus) { execute(new Threading(), api, listener, inappSkus, subSkus); } private  InventoryQuery(@NonNull Threading threading, @NonNull AbstractGooglePlayBillingApi api, @NonNull InventoryListener listener, @Nullable Collection<String> inappSkus, @Nullable Collection<String> subSkus);   }
@Test public void returns_inventory() { InventoryListener listener = mock(InventoryListener.class); InventoryQuery.execute( TestHelper.mockThreading(), api, listener, TestData.allInAppSkus, TestData.allSubSkus ); ArgumentCaptor<Inventory> argument = ArgumentCaptor.forClass(Inventory.class); verify(api).getPurchases(BillingClient.SkuType.SUBS); verify(api).getPurchases(BillingClient.SkuType.INAPP); verify(api).getSkuDetails(eq(BillingClient.SkuType.SUBS), eq(TestData.allSubSkus), any(SkuDetailsResponseListener.class)); verify(api).getSkuDetails(eq(BillingClient.SkuType.INAPP), eq(TestData.allInAppSkus), any(SkuDetailsResponseListener.class)); verify(listener).success(argument.capture()); assertEquals(TestData.allProducts.size(), argument.getValue().products().size()); assertEquals(1, argument.getValue().purchases().size()); for (Product product : argument.getValue().products()) { assertTrue(TestData.allProducts.contains(product)); } }
@Test public void returns_error_when_inapp_purchases_call_fails() { when(api.getPurchases(BillingClient.SkuType.INAPP)).thenReturn(null); when(api.getPurchases(BillingClient.SkuType.SUBS)).thenReturn(new ArrayList<Purchase>()); InventoryListener listener = mock(InventoryListener.class); InventoryQuery.execute( TestHelper.mockThreading(), api, listener, TestData.allInAppSkus, TestData.allSubSkus ); verify(listener).failure(any(Vendor.Error.class)); }
@Test public void returns_error_when_sub_purchases_call_fails() { when(api.getPurchases(BillingClient.SkuType.SUBS)).thenReturn(null); when(api.getPurchases(BillingClient.SkuType.INAPP)).thenReturn(new ArrayList<Purchase>()); InventoryListener listener = mock(InventoryListener.class); InventoryQuery.execute( TestHelper.mockThreading(), api, listener, TestData.allInAppSkus, TestData.allSubSkus ); verify(listener).failure(any(Vendor.Error.class)); }
@Test public void returns_error_when_inapp_sku_details_call_fails() { TestHelper.mockSkuDetailsError(api, BillingClient.SkuType.INAPP); InventoryListener listener = mock(InventoryListener.class); InventoryQuery.execute( TestHelper.mockThreading(), api, listener, TestData.allInAppSkus, TestData.allSubSkus ); verify(listener).failure(any(Vendor.Error.class)); }
@Test public void returns_error_when_subs_sku_details_call_fails() { TestHelper.mockSkuDetailsError(api, BillingClient.SkuType.SUBS); InventoryListener listener = mock(InventoryListener.class); InventoryQuery.execute( TestHelper.mockThreading(), api, listener, TestData.allInAppSkus, TestData.allSubSkus ); verify(listener).failure(any(Vendor.Error.class)); }
@Test public void returns_error_when_billing_not_available() { TestHelper.mockApiUnavailable(api); InventoryListener listener = mock(InventoryListener.class); InventoryQuery.execute( TestHelper.mockThreading(), api, listener, TestData.allInAppSkus, TestData.allSubSkus ); verify(listener).failure(any(Vendor.Error.class)); }
@Test public void returns_inventory_when_subs_feature_not_available() { when(api.isBillingSupported(BillingClient.SkuType.SUBS)).thenReturn(BillingClient.BillingResponse.FEATURE_NOT_SUPPORTED); InventoryListener listener = mock(InventoryListener.class); InventoryQuery.execute( TestHelper.mockThreading(), api, listener, TestData.allInAppSkus, TestData.allSubSkus ); ArgumentCaptor<Inventory> argument = ArgumentCaptor.forClass(Inventory.class); verify(api).getPurchases(BillingClient.SkuType.INAPP); verify(api, never()).getPurchases(BillingClient.SkuType.SUBS); verify(api).getSkuDetails(eq(BillingClient.SkuType.INAPP), eq(TestData.allInAppSkus), any(SkuDetailsResponseListener.class)); verify(api, Mockito.never()).getSkuDetails(eq(BillingClient.SkuType.SUBS), eq(TestData.allSubSkus), any(SkuDetailsResponseListener.class)); verify(listener).success(argument.capture()); assertEquals(TestData.allInAppProducts.size(), argument.getValue().products().size()); assertEquals(1, argument.getValue().purchases().size()); for (Product product : argument.getValue().products()) { assertTrue(TestData.allProducts.contains(product)); } }