@Test public void testErasePurchase() throws Exception { Inventory inventory = new Inventory(); Purchase p1 = mock(Purchase.class); when(p1.getSku()).thenReturn("sku1"); Purchase p2 = mock(Purchase.class); when(p2.getSku()).thenReturn("sku2"); inventory.addPurchase(p1); inventory.addPurchase(p2); assertThat(inventory.getAllOwnedSkus()).containsOnly("sku1", "sku2"); inventory.erasePurchase("sku1"); assertThat(inventory.getAllOwnedSkus()).containsOnly("sku2"); }
@Test public void shouldInitialiseEmptyRepository() throws Exception { Inventory inventory = new Inventory(); assertThat(inventory.getAllPurchases()).isEmpty(); assertThat(inventory.getSkuDetails()).isEmpty(); assertThat(inventory.getAllOwnedSkus()).isEmpty(); }
int querySkuDetails(Inventory inv, List<String> moreSkus) throws RemoteException, JSONException { ArrayList<String> skuList = new ArrayList<String>(); skuList.addAll(inv.getAllOwnedSkus(ITEM_TYPE_INAPP)); if (moreSkus != null) skuList.addAll(moreSkus); if (skuList.size() == 0) { return BILLING_RESPONSE_RESULT_OK; } Bundle querySkus = new Bundle(); querySkus.putStringArrayList(GET_SKU_DETAILS_ITEM_LIST, skuList); Bundle skuDetails = connection.getSkuDetails(querySkus); if (!skuDetails.containsKey(RESPONSE_GET_SKU_DETAILS_LIST)) { int response = getResponseCodeFromBundle(skuDetails); if (response != BILLING_RESPONSE_RESULT_OK) { return response; } else { return HELPER_BAD_RESPONSE; } } ArrayList<String> responseList = skuDetails.getStringArrayList(RESPONSE_GET_SKU_DETAILS_LIST); for (String thisResponse : responseList) { SkuDetails d = new SkuDetails(thisResponse); inv.addSkuDetails(d); } return BILLING_RESPONSE_RESULT_OK; }
@Test public void testGetAllOwnedSkusForItemType() throws Exception { Inventory inventory = new Inventory(); Purchase p1 = mock(Purchase.class); when(p1.getItemType()).thenReturn(ItemType.INAPP); when(p1.getSku()).thenReturn("sku_inapp"); Purchase p2 = mock(Purchase.class); when(p2.getItemType()).thenReturn(ItemType.SUBS); when(p2.getSku()).thenReturn("sku_sub"); inventory.addPurchase(p1); inventory.addPurchase(p2); assertThat(inventory.getAllOwnedSkus(ItemType.SUBS)).containsExactly("sku_sub"); }
int querySkuDetails(String itemType, Inventory inv, List<String> moreSkus) throws RemoteException, JSONException { logDebug("Querying SKU details."); ArrayList<String> skuList = new ArrayList<String>(); skuList.addAll(inv.getAllOwnedSkus(itemType)); if (moreSkus != null) { for (String sku : moreSkus) { if (!skuList.contains(sku)) { skuList.add(sku); } } } if (skuList.size() == 0) { logDebug("queryPrices: nothing to do because there are no SKUs."); return BILLING_RESPONSE_RESULT_OK; } Bundle querySkus = new Bundle(); querySkus.putStringArrayList(GET_SKU_DETAILS_ITEM_LIST, skuList); Bundle skuDetails = mService.getSkuDetails(3, mContext.getPackageName(), itemType, querySkus); if (!skuDetails.containsKey(RESPONSE_GET_SKU_DETAILS_LIST)) { int response = getResponseCodeFromBundle(skuDetails); if (response != BILLING_RESPONSE_RESULT_OK) { logDebug("getSkuDetails() failed: " + getResponseDesc(response)); return response; } else { logError("getSkuDetails() returned a bundle with neither an error nor a detail list."); return IABHELPER_BAD_RESPONSE; } } ArrayList<String> responseList = skuDetails.getStringArrayList( RESPONSE_GET_SKU_DETAILS_LIST); for (String thisResponse : responseList) { SkuDetails d = new SkuDetails(itemType, thisResponse); logDebug("Got sku details: " + d); inv.addSkuDetails(d); } return BILLING_RESPONSE_RESULT_OK; }