@Override public String toString() { return mSku.getDisplayTitle(); } }
@Nonnull static Sku fromJson(@Nonnull String json, @Nonnull String product) throws JSONException { return new Sku(json, product); }
@Nonnull static Skus fromBundle(@Nonnull Bundle bundle, @Nonnull String product) throws RequestException { final List<String> list = extractList(bundle); final List<Sku> skus = new ArrayList<>(list.size()); for (String response : list) { try { skus.add(Sku.fromJson(response, product)); } catch (JSONException e) { throw new RequestException(e); } } return new Skus(product, skus); }
@Test public void testShouldStripSimpleAppNameFromTitleWithBrackets() throws Exception { final JSONObject json = newInAppJsonObject("1"); json.put("title", "Test #1 (test) (Test App name)"); final Sku sku = Sku.fromJson(json.toString(), "test"); final String displayTitle = sku.getDisplayTitle(); assertEquals("Test #1 (test)", displayTitle); }
@Test public void testShouldNotPutEmptyFieldsInJson() throws Exception { final Sku sku = Sku.fromJson(newInAppJsonObject("1").toString(), "test"); final JSONObject json = sku.toJsonObject(); assertTrue(!json.has("subscriptionPeriod")); assertTrue(!json.has("freeTrialPeriod")); assertTrue(!json.has("introductoryPricePeriod")); assertTrue(!json.has("introductoryPriceAmountMicros")); }
@Nonnull public String toJson() throws JSONException { return toJsonObject().toString(); }
/** * @return {@link #title} without application name in it (the last group of characters * surrounded by brackets is removed) */ @Nonnull public String getDisplayTitle() { if (mDisplayTitle == null) { mDisplayTitle = makeDisplayTitle(title); } return mDisplayTitle; }
@Nonnull private static String makeDisplayTitle(String title) { if (TextUtils.isEmpty(title)) { return ""; } final char lastChar = title.charAt(title.length() - 1); if (lastChar != ')') { return title; } final int i = indexOfAppName(title); if (i > 0) { return title.substring(0, i).trim(); } return title; }
@Override public Bundle answer(InvocationOnMock invocation) throws Throwable { final Bundle in = (Bundle) invocation.getArguments()[3]; final ArrayList<String> skuIds = in.getStringArrayList("ITEM_ID_LIST"); final Bundle bundle = newBundle(OK); final ArrayList<String> list = new ArrayList<String>(); for (Sku sku : skus) { if (skuIds.contains(sku.id.code)) { list.add(sku.toJson()); } } bundle.putStringArrayList(Skus.BUNDLE_LIST, list); return bundle; } });
@Test public void testShouldStripSimpleAppNameFromTitle() throws Exception { final JSONObject json = newInAppJsonObject("1"); json.put("title", "Test #1 (Test App name)"); final Sku sku = Sku.fromJson(json.toString(), "test"); final String displayTitle = sku.getDisplayTitle(); assertEquals("Test #1", displayTitle); }
@Test public void testShouldStripSimpleAppNameWithBracketsFromTitleWithBrackets() throws Exception { final JSONObject json = newInAppJsonObject("1"); json.put("title", "Test #1 (test) (Test App name (test))"); final Sku sku = Sku.fromJson(json.toString(), "test"); final String displayTitle = sku.getDisplayTitle(); assertEquals("Test #1 (test)", displayTitle); }
protected void populateSkus() throws Exception { final List<Sku> expectedInAppSkus = asList(Sku.fromJson(SkuTest.newInAppJson("1"), IN_APP), Sku.fromJson(SkuTest.newInAppJson("2"), IN_APP), Sku.fromJson(SkuTest.newInAppJson("3"), IN_APP), Sku.fromJson(SkuTest.newInAppJson("4"), IN_APP), Sku.fromJson(SkuTest.newInAppJson("6"), IN_APP)); insertSkus(IN_APP, expectedInAppSkus); final List<Sku> expectedSubSkus = asList( Sku.fromJson(SkuTest.newSubscriptionJson("sub1"), SUBSCRIPTION), Sku.fromJson(SkuTest.newSubscriptionJson("sub2"), SUBSCRIPTION), Sku.fromJson(SkuTest.newSubscriptionJson("sub3"), SUBSCRIPTION), Sku.fromJson(SkuTest.newSubscriptionJson("sub4"), SUBSCRIPTION)); insertSkus(SUBSCRIPTION, expectedSubSkus); }
@Override public String toString() { return id + "{" + getDisplayTitle() + ", " + price + "}"; }
private Sku newSku(String id) { return new Sku("test", id, id, Sku.Price.EMPTY, id, id, id, Sku.Price.EMPTY, id, id, id, 0); } }
@Test public void testShouldReadIntroductoryPrice() throws Exception { final JSONObject json = newInAppJsonObject("1"); json.put("introductoryPrice", "1 000 000EUR"); final Sku sku = Sku.fromJson(json.toString(), "test"); assertEquals("1 000 000EUR", sku.introductoryPrice); }
public void onBind(Sku sku) { mSku = sku; mSubscription.setText(sku.getDisplayTitle()); mSubscription.setChecked(mAdapter.isChecked(sku)); }
@Test public void testShouldReadIntroductoryPriceCycles() throws Exception { final JSONObject json = newInAppJsonObject("1"); json.put("introductoryPriceCycles", 42); final Sku sku = Sku.fromJson(json.toString(), "test"); assertEquals(sku.introductoryPriceCycles, 42); } }
private void testDetailedPrice(long amount, @Nonnull String currency) throws JSONException { final JSONObject json = newInAppJsonObject("1"); json.put("price_amount_micros", amount); json.put("price_currency_code", currency); final Sku sku = Sku.fromJson(json.toString(), "test"); assertTrue(sku.detailedPrice.isValid()); assertEquals(currency, sku.detailedPrice.currency); assertEquals(amount, sku.detailedPrice.amount); }
private void testIntroductoryPrice(long amount, @Nonnull String currency) throws JSONException { final JSONObject json = newSubscriptionJsonObject("1"); json.put("introductoryPriceAmountMicros", amount); json.put("price_currency_code", currency); final Sku sku = Sku.fromJson(json.toString(), "test"); assertNotNull(sku.detailedIntroductoryPrice); assertTrue(sku.detailedIntroductoryPrice.isValid()); assertEquals(currency, sku.detailedIntroductoryPrice.currency); assertEquals(amount, sku.detailedIntroductoryPrice.amount); }
@Test public void testSubscriptionSkuShouldBeCreatedFromJson() throws Exception { final Sku sku = Sku.fromJson(newSubscriptionJson("1"), ProductTypes.SUBSCRIPTION); verifySku(sku, "1"); }