Commit 1383972d authored by Peter E Conn's avatar Peter E Conn Committed by Commit Bot

💸 Parse subscription fields into Digital Goods API.

This CL populates the fields added in:
  https://chromium-review.googlesource.com/c/chromium/src/+/2486525

It will require changes in Android Browser Helper to actually put
those fields into Bundles we receive.

Change-Id: I55f6980f51702b768c46214d37496369a142b081
Bug: 1140345
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2506443
Commit-Queue: Peter Conn <peconn@chromium.org>
Reviewed-by: default avatarMichael van Ouwerkerk <mvanouwerkerk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822239}
parent 53e3dade
...@@ -34,13 +34,18 @@ public class GetDetailsConverter { ...@@ -34,13 +34,18 @@ public class GetDetailsConverter {
static final String RESPONSE_GET_DETAILS_RESPONSE_CODE = "getDetails.responseCode"; static final String RESPONSE_GET_DETAILS_RESPONSE_CODE = "getDetails.responseCode";
static final String RESPONSE_GET_DETAILS_DETAILS_LIST = "getDetails.detailsList"; static final String RESPONSE_GET_DETAILS_DETAILS_LIST = "getDetails.detailsList";
static final String ITEM_DETAILS_ID = "itemDetails.id"; static final String KEY_ID = "itemDetails.id";
static final String ITEM_DETAILS_TITLE = "itemDetails.title"; static final String KEY_TITLE = "itemDetails.title";
static final String ITEM_DETAILS_DESC = "itemDetails.description"; static final String KEY_DESC = "itemDetails.description";
static final String ITEM_DETAILS_CURRENCY = "itemDetails.currency"; static final String KEY_CURRENCY = "itemDetails.currency";
static final String ITEM_DETAILS_VALUE = "itemDetails.value"; static final String KEY_VALUE = "itemDetails.value";
static final String[] ITEM_DETAILS_ALL_FIELDS = {ITEM_DETAILS_ID, ITEM_DETAILS_TITLE, static final String[] REQUIRED_FIELDS = {KEY_ID, KEY_TITLE, KEY_DESC, KEY_CURRENCY, KEY_VALUE};
ITEM_DETAILS_DESC, ITEM_DETAILS_CURRENCY, ITEM_DETAILS_VALUE};
static final String KEY_SUBS_PERIOD = "itemDetails.subsPeriod";
static final String KEY_FREE_TRIAL_PERIOD = "itemDetails.freeTrialPeriod";
static final String KEY_INTRO_CURRENCY = "itemDetails.introPriceCurrency";
static final String KEY_INTRO_VALUE = "itemDetails.introPriceValue";
static final String KEY_INTRO_PERIOD = "itemDetails.introPricePeriod";
private GetDetailsConverter() {} private GetDetailsConverter() {}
...@@ -106,23 +111,39 @@ public class GetDetailsConverter { ...@@ -106,23 +111,39 @@ public class GetDetailsConverter {
Bundle item = (Bundle) itemAsParcelable; Bundle item = (Bundle) itemAsParcelable;
for (String field : ITEM_DETAILS_ALL_FIELDS) { for (String field : REQUIRED_FIELDS) {
if (item.containsKey(field) && (item.get(field) instanceof String)) continue; if (item.containsKey(field) && (item.get(field) instanceof String)) continue;
Log.w(TAG, "Item does not contain field String " + field + "."); Log.w(TAG, "Item does not contain field String " + field + ".");
return null; return null;
} }
PaymentCurrencyAmount amount = new PaymentCurrencyAmount(); // Mandatory fields.
amount.currency = item.getString(ITEM_DETAILS_CURRENCY); PaymentCurrencyAmount price = new PaymentCurrencyAmount();
amount.value = item.getString(ITEM_DETAILS_VALUE); price.currency = item.getString(KEY_CURRENCY);
price.value = item.getString(KEY_VALUE);
ItemDetails res = new ItemDetails();
res.itemId = item.getString(ITEM_DETAILS_ID); ItemDetails result = new ItemDetails();
res.title = item.getString(ITEM_DETAILS_TITLE); result.itemId = item.getString(KEY_ID);
res.description = item.getString(ITEM_DETAILS_DESC); result.title = item.getString(KEY_TITLE);
res.price = amount; result.description = item.getString(KEY_DESC);
result.price = price;
// Optional fields.
result.subscriptionPeriod = item.getString(KEY_SUBS_PERIOD);
result.freeTrialPeriod = item.getString(KEY_FREE_TRIAL_PERIOD);
result.introductoryPricePeriod = item.getString(KEY_INTRO_PERIOD);
String introPriceCurrency = item.getString(KEY_INTRO_CURRENCY);
String introPriceValue = item.getString(KEY_INTRO_VALUE);
if (introPriceCurrency != null && introPriceValue != null) {
PaymentCurrencyAmount introPrice = new PaymentCurrencyAmount();
introPrice.currency = introPriceCurrency;
introPrice.value = introPriceValue;
result.introductoryPrice = introPrice;
}
return res; return result;
} }
public static void returnClientAppUnavailable(GetDetailsResponse callback) { public static void returnClientAppUnavailable(GetDetailsResponse callback) {
...@@ -138,15 +159,34 @@ public class GetDetailsConverter { ...@@ -138,15 +159,34 @@ public class GetDetailsConverter {
* This would be used by the client app and is here only to help testing. * This would be used by the client app and is here only to help testing.
*/ */
@VisibleForTesting @VisibleForTesting
public static Bundle createItemDetailsBundle(String id, String title, String desc,
String currency, String value, @Nullable String subsPeriod,
@Nullable String freeTrialPeriod, @Nullable String introPriceCurrency,
@Nullable String introPriceValue, @Nullable String intoPricePeriod) {
Bundle bundle = createItemDetailsBundle(id, title, desc, currency, value);
bundle.putString(KEY_SUBS_PERIOD, subsPeriod);
bundle.putString(KEY_FREE_TRIAL_PERIOD, freeTrialPeriod);
bundle.putString(KEY_INTRO_CURRENCY, introPriceCurrency);
bundle.putString(KEY_INTRO_VALUE, introPriceValue);
bundle.putString(KEY_INTRO_PERIOD, intoPricePeriod);
return bundle;
}
/**
* Like the above method, but provides {@code null} for all optional parameters.
*/
@VisibleForTesting
public static Bundle createItemDetailsBundle( public static Bundle createItemDetailsBundle(
String id, String title, String desc, String currency, String value) { String id, String title, String desc, String currency, String value) {
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString(ITEM_DETAILS_ID, id); bundle.putString(KEY_ID, id);
bundle.putString(ITEM_DETAILS_TITLE, title); bundle.putString(KEY_TITLE, title);
bundle.putString(ITEM_DETAILS_DESC, desc); bundle.putString(KEY_DESC, desc);
bundle.putString(ITEM_DETAILS_CURRENCY, currency); bundle.putString(KEY_CURRENCY, currency);
bundle.putString(ITEM_DETAILS_VALUE, value); bundle.putString(KEY_VALUE, value);
return bundle; return bundle;
} }
......
...@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.browserservices.digitalgoods; ...@@ -6,6 +6,7 @@ package org.chromium.chrome.browser.browserservices.digitalgoods;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.chromium.chrome.browser.browserservices.digitalgoods.AcknowledgeConverter.PARAM_ACKNOWLEDGE_MAKE_AVAILABLE_AGAIN; import static org.chromium.chrome.browser.browserservices.digitalgoods.AcknowledgeConverter.PARAM_ACKNOWLEDGE_MAKE_AVAILABLE_AGAIN;
import static org.chromium.chrome.browser.browserservices.digitalgoods.AcknowledgeConverter.PARAM_ACKNOWLEDGE_PURCHASE_TOKEN; import static org.chromium.chrome.browser.browserservices.digitalgoods.AcknowledgeConverter.PARAM_ACKNOWLEDGE_PURCHASE_TOKEN;
...@@ -23,6 +24,7 @@ import static org.chromium.chrome.browser.browserservices.digitalgoods.GetDetail ...@@ -23,6 +24,7 @@ import static org.chromium.chrome.browser.browserservices.digitalgoods.GetDetail
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
import androidx.annotation.Nullable;
import androidx.browser.trusted.TrustedWebActivityCallback; import androidx.browser.trusted.TrustedWebActivityCallback;
import org.junit.Test; import org.junit.Test;
...@@ -68,6 +70,24 @@ public class DigitalGoodsConverterTest { ...@@ -68,6 +70,24 @@ public class DigitalGoodsConverterTest {
ItemDetails item = GetDetailsConverter.convertItemDetails(bundle); ItemDetails item = GetDetailsConverter.convertItemDetails(bundle);
assertItemDetails(item, id, title, desc, currency, value); assertItemDetails(item, id, title, desc, currency, value);
assertSubsItemDetails(item, null, null, null, null, null);
}
@Test
public void convertItemDetails_subscriptions() {
String subsPeriod = "2 weeks";
String freeTrialPeriod = "1 week";
String introPriceCurrency = "GBP";
String introPriceValue = "3.0";
String introPricePeriod = "1 month";
Bundle bundle = GetDetailsConverter.createItemDetailsBundle("id", "Title", "desc", "GBP",
"10.0", subsPeriod, freeTrialPeriod, introPriceCurrency, introPriceValue,
introPricePeriod);
ItemDetails item = GetDetailsConverter.convertItemDetails(bundle);
assertSubsItemDetails(item, subsPeriod, freeTrialPeriod, introPriceCurrency,
introPriceValue, introPricePeriod);
} }
/** /**
...@@ -93,7 +113,9 @@ public class DigitalGoodsConverterTest { ...@@ -93,7 +113,9 @@ public class DigitalGoodsConverterTest {
int responseCode = 0; int responseCode = 0;
Parcelable[] items = { Parcelable[] items = {
GetDetailsConverter.createItemDetailsBundle("1", "t1", "d1", "c1", "v1"), GetDetailsConverter.createItemDetailsBundle("1", "t1", "d1", "c1", "v1"),
GetDetailsConverter.createItemDetailsBundle("2", "t2", "d2", "c2", "v2")}; GetDetailsConverter.createItemDetailsBundle(
"2", "t2", "d2", "c2", "v2", "sp2", "ftp2", "ipc2", "ipv2", "ipp2")};
args.putInt(RESPONSE_GET_DETAILS_RESPONSE_CODE, responseCode); args.putInt(RESPONSE_GET_DETAILS_RESPONSE_CODE, responseCode);
args.putParcelableArray(RESPONSE_GET_DETAILS_DETAILS_LIST, items); args.putParcelableArray(RESPONSE_GET_DETAILS_DETAILS_LIST, items);
...@@ -101,7 +123,9 @@ public class DigitalGoodsConverterTest { ...@@ -101,7 +123,9 @@ public class DigitalGoodsConverterTest {
assertEquals(responseCode, state.responseCode); assertEquals(responseCode, state.responseCode);
assertItemDetails(state.itemDetails[0], "1", "t1", "d1", "c1", "v1"); assertItemDetails(state.itemDetails[0], "1", "t1", "d1", "c1", "v1");
assertSubsItemDetails(state.itemDetails[0], null, null, null, null, null);
assertItemDetails(state.itemDetails[1], "2", "t2", "d2", "c2", "v2"); assertItemDetails(state.itemDetails[1], "2", "t2", "d2", "c2", "v2");
assertSubsItemDetails(state.itemDetails[1], "sp2", "ftp2", "ipc2", "ipv2", "ipp2");
} }
private static void assertItemDetails(ItemDetails item, String id, String title, String desc, private static void assertItemDetails(ItemDetails item, String id, String title, String desc,
...@@ -113,6 +137,20 @@ public class DigitalGoodsConverterTest { ...@@ -113,6 +137,20 @@ public class DigitalGoodsConverterTest {
assertEquals(value, item.price.value); assertEquals(value, item.price.value);
} }
private static void assertSubsItemDetails(ItemDetails item, @Nullable String subsPeriod,
@Nullable String freeTrialPeriod, @Nullable String introPriceCurrency,
@Nullable String introPriceValue, @Nullable String intoPricePeriod) {
assertEquals(subsPeriod, item.subscriptionPeriod);
assertEquals(freeTrialPeriod, item.freeTrialPeriod);
if (introPriceCurrency == null || introPriceValue == null) {
assertNull(item.introductoryPrice);
} else {
assertEquals(introPriceCurrency, item.introductoryPrice.currency);
assertEquals(introPriceValue, item.introductoryPrice.value);
}
assertEquals(intoPricePeriod, item.introductoryPricePeriod);
}
@Test @Test
public void convertAcknowledgeParams() { public void convertAcknowledgeParams() {
String token = "abcdef"; String token = "abcdef";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment