Commit 9ffc0db3 authored by Peter E Conn's avatar Peter E Conn Committed by Commit Bot

💸 Split DigitalGoodsConverter based on function call.

This CL is a refactoring that should contain no behavioural changes.
It splits parts of the DigitalGoodsConverter into two additional
classes, the GetDetailsConverter and the AcknowledgeConverter, and it
deduplicates some of the logic in the DigitalGoodsAdapter.

This should put the code in a slightly nicer state to add the new
method call, ListPurchases.

Change-Id: I6e56a5dc2077371a8de8bbb16eb0799bdb971a31
Bug: 1139795
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2487087Reviewed-by: default avatarElla Ge <eirage@chromium.org>
Commit-Queue: Peter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#819898}
parent 810f256a
...@@ -189,11 +189,13 @@ chrome_java_sources = [ ...@@ -189,11 +189,13 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/browserservices/TrustedWebActivitySettingsLauncher.java", "java/src/org/chromium/chrome/browser/browserservices/TrustedWebActivitySettingsLauncher.java",
"java/src/org/chromium/chrome/browser/browserservices/TrustedWebActivityUmaRecorder.java", "java/src/org/chromium/chrome/browser/browserservices/TrustedWebActivityUmaRecorder.java",
"java/src/org/chromium/chrome/browser/browserservices/VerificationResultStore.java", "java/src/org/chromium/chrome/browser/browserservices/VerificationResultStore.java",
"java/src/org/chromium/chrome/browser/browserservices/digitalgoods/AcknowledgeConverter.java",
"java/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsAdapter.java", "java/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsAdapter.java",
"java/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsConverter.java", "java/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsConverter.java",
"java/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsFactoryFactory.java", "java/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsFactoryFactory.java",
"java/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsFactoryImpl.java", "java/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsFactoryImpl.java",
"java/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsImpl.java", "java/src/org/chromium/chrome/browser/browserservices/digitalgoods/DigitalGoodsImpl.java",
"java/src/org/chromium/chrome/browser/browserservices/digitalgoods/GetDetailsConverter.java",
"java/src/org/chromium/chrome/browser/browserservices/permissiondelegation/InstalledWebappBridge.java", "java/src/org/chromium/chrome/browser/browserservices/permissiondelegation/InstalledWebappBridge.java",
"java/src/org/chromium/chrome/browser/browserservices/permissiondelegation/InstalledWebappGeolocationBridge.java", "java/src/org/chromium/chrome/browser/browserservices/permissiondelegation/InstalledWebappGeolocationBridge.java",
"java/src/org/chromium/chrome/browser/browserservices/permissiondelegation/LocationPermissionUpdater.java", "java/src/org/chromium/chrome/browser/browserservices/permissiondelegation/LocationPermissionUpdater.java",
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.browserservices.digitalgoods;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.convertResponseCodes;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.browser.trusted.TrustedWebActivityCallback;
import org.chromium.base.Log;
import org.chromium.payments.mojom.BillingResponseCode;
import org.chromium.payments.mojom.DigitalGoods.AcknowledgeResponse;
/**
* A converter that deals with the parameters and result for Acknowledge calls.
*/
class AcknowledgeConverter {
private static final String TAG = "DigitalGoods";
static final String PARAM_ACKNOWLEDGE_PURCHASE_TOKEN = "acknowledge.purchaseToken";
static final String PARAM_ACKNOWLEDGE_MAKE_AVAILABLE_AGAIN = "acknowledge.makeAvailableAgain";
static final String RESPONSE_ACKNOWLEDGE = "acknowledge.response";
static final String RESPONSE_ACKNOWLEDGE_RESPONSE_CODE = "acknowledge.responseCode";
private AcknowledgeConverter() {}
static Bundle convertParams(String purchaseToken, boolean makeAvailableAgain) {
Bundle bundle = new Bundle();
bundle.putString(PARAM_ACKNOWLEDGE_PURCHASE_TOKEN, purchaseToken);
bundle.putBoolean(PARAM_ACKNOWLEDGE_MAKE_AVAILABLE_AGAIN, makeAvailableAgain);
return bundle;
}
static TrustedWebActivityCallback convertCallback(AcknowledgeResponse callback) {
return new TrustedWebActivityCallback() {
@Override
public void onExtraCallback(@NonNull String callbackName, @Nullable Bundle args) {
if (!RESPONSE_ACKNOWLEDGE.equals(callbackName)) {
Log.w(TAG, "Wrong callback name given: " + callbackName + ".");
returnClientAppError(callback);
return;
}
if (args == null) {
Log.w(TAG, "No args provided.");
returnClientAppError(callback);
return;
}
if (!(args.get(RESPONSE_ACKNOWLEDGE_RESPONSE_CODE) instanceof Integer)) {
Log.w(TAG, "Poorly formed args provided.");
returnClientAppError(callback);
return;
}
int code = args.getInt(RESPONSE_ACKNOWLEDGE_RESPONSE_CODE);
callback.call(convertResponseCodes(code));
}
};
}
public static void returnClientAppUnavailable(AcknowledgeResponse callback) {
callback.call(BillingResponseCode.CLIENT_APP_UNAVAILABLE);
}
public static void returnClientAppError(AcknowledgeResponse callback) {
callback.call(BillingResponseCode.CLIENT_APP_ERROR);
}
/**
* Creates a {@link Bundle} that represents the result of an acknowledge call. This would be
* carried out by the client app and is only here to help testing.
*/
@VisibleForTesting
public static Bundle createResponseBundle(int responseCode) {
Bundle bundle = new Bundle();
bundle.putInt(RESPONSE_ACKNOWLEDGE_RESPONSE_CODE, responseCode);
return bundle;
}
}
...@@ -4,24 +4,18 @@ ...@@ -4,24 +4,18 @@
package org.chromium.chrome.browser.browserservices.digitalgoods; package org.chromium.chrome.browser.browserservices.digitalgoods;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.convertAcknowledgeCallback;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.convertAcknowledgeParams;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.convertGetDetailsCallback;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.convertGetDetailsParams;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.returnClientAppError;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.returnClientAppUnavailable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import androidx.browser.trusted.TrustedWebActivityCallback;
import androidx.browser.trusted.TrustedWebActivityServiceConnection;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.chrome.browser.browserservices.TrustedWebActivityClient; import org.chromium.chrome.browser.browserservices.TrustedWebActivityClient;
import org.chromium.components.embedder_support.util.Origin; import org.chromium.components.embedder_support.util.Origin;
import org.chromium.payments.mojom.DigitalGoods.AcknowledgeResponse; import org.chromium.payments.mojom.DigitalGoods.AcknowledgeResponse;
import org.chromium.payments.mojom.DigitalGoods.GetDetailsResponse; import org.chromium.payments.mojom.DigitalGoods.GetDetailsResponse;
import androidx.browser.trusted.TrustedWebActivityServiceConnection;
/** /**
* This class uses the {@link DigitalGoodsConverter} to convert data types between mojo types and * This class uses the {@link DigitalGoodsConverter} to convert data types between mojo types and
* Android types and then uses the {@link TrustedWebActivityClient} to call into the Trusted Web * Android types and then uses the {@link TrustedWebActivityClient} to call into the Trusted Web
...@@ -40,62 +34,50 @@ public class DigitalGoodsAdapter { ...@@ -40,62 +34,50 @@ public class DigitalGoodsAdapter {
mClient = client; mClient = client;
} }
public void getDetails(Uri scope, String[] itemIds, GetDetailsResponse callback) { public void getDetails(Uri scope, String[] itemIds, GetDetailsResponse response) {
// TODO(1096428): Combine this and the below method (the difficulty comes from callback Bundle args = GetDetailsConverter.convertParams(itemIds);
// being different types). TrustedWebActivityCallback callback = GetDetailsConverter.convertCallback(response);
mClient.connectAndExecute(scope, new TrustedWebActivityClient.ExecutionCallback() { Runnable onError = () -> GetDetailsConverter.returnClientAppError(response);
@Override Runnable onUnavailable = () -> GetDetailsConverter.returnClientAppUnavailable(response);
public void onConnected(Origin origin, TrustedWebActivityServiceConnection service) {
// Wrap this call so that crashes in the TWA client don't cause crashes in Chrome.
Bundle result = null;
try {
result = service.sendExtraCommand(COMMAND_GET_DETAILS,
convertGetDetailsParams(itemIds), convertGetDetailsCallback(callback));
} catch (Exception e) {
Log.w(TAG, "Exception communicating with client.");
returnClientAppError(callback);
}
boolean success = result != null && execute(scope, COMMAND_GET_DETAILS, args, callback, onError, onUnavailable);
result.getBoolean(KEY_SUCCESS, false);
if (!success) {
returnClientAppError(callback);
}
}
@Override
public void onNoTwaFound() {
returnClientAppUnavailable(callback);
}
});
} }
public void acknowledge(Uri scope, String purchaseToken, boolean makeAvailableAgain, public void acknowledge(Uri scope, String purchaseToken, boolean makeAvailableAgain,
AcknowledgeResponse callback) { AcknowledgeResponse response) {
Bundle args = AcknowledgeConverter.convertParams(purchaseToken, makeAvailableAgain);
TrustedWebActivityCallback callback = AcknowledgeConverter.convertCallback(response);
Runnable onError = () -> AcknowledgeConverter.returnClientAppError(response);
Runnable onUnavailable = () -> AcknowledgeConverter.returnClientAppUnavailable(response);
execute(scope, COMMAND_ACKNOWLEDGE, args, callback, onError, onUnavailable);
}
private void execute(Uri scope, String command, Bundle args,
TrustedWebActivityCallback callback, Runnable onClientAppError,
Runnable onClientAppUnavailable) {
mClient.connectAndExecute(scope, new TrustedWebActivityClient.ExecutionCallback() { mClient.connectAndExecute(scope, new TrustedWebActivityClient.ExecutionCallback() {
@Override @Override
public void onConnected(Origin origin, TrustedWebActivityServiceConnection service) { public void onConnected(Origin origin, TrustedWebActivityServiceConnection service) {
// Wrap this call so that crashes in the TWA client don't cause crashes in Chrome. // Wrap this call so that crashes in the TWA client don't cause crashes in Chrome.
Bundle result = null; Bundle result = null;
try { try {
result = service.sendExtraCommand(COMMAND_ACKNOWLEDGE, result = service.sendExtraCommand(command, args, callback);
convertAcknowledgeParams(purchaseToken, makeAvailableAgain),
convertAcknowledgeCallback(callback));
} catch (Exception e) { } catch (Exception e) {
Log.w(TAG, "Exception communicating with client."); Log.w(TAG, "Exception communicating with client.");
returnClientAppError(callback); onClientAppError.run();
} }
boolean success = result != null && boolean success = result != null &&
result.getBoolean(KEY_SUCCESS, false); result.getBoolean(KEY_SUCCESS, false);
if (!success) { if (!success) {
returnClientAppError(callback); onClientAppError.run();
} }
} }
@Override @Override
public void onNoTwaFound() { public void onNoTwaFound() {
returnClientAppUnavailable(callback); onClientAppUnavailable.run();
} }
}); });
} }
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.chrome.browser.browserservices.digitalgoods;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.convertResponseCodes;
import android.os.Bundle;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.browser.trusted.TrustedWebActivityCallback;
import org.chromium.base.Log;
import org.chromium.payments.mojom.BillingResponseCode;
import org.chromium.payments.mojom.DigitalGoods.GetDetailsResponse;
import org.chromium.payments.mojom.ItemDetails;
import org.chromium.payments.mojom.PaymentCurrencyAmount;
import java.util.ArrayList;
import java.util.List;
/**
* A converter that deals with the parameters and result for GetDetails calls.
*/
public class GetDetailsConverter {
private static final String TAG = "DigitalGoods";
static final String PARAM_GET_DETAILS_ITEM_IDS = "getDetails.itemIds";
public static final String RESPONSE_GET_DETAILS = "getDetails.response";
static final String RESPONSE_GET_DETAILS_RESPONSE_CODE = "getDetails.responseCode";
static final String RESPONSE_GET_DETAILS_DETAILS_LIST = "getDetails.detailsList";
static final String ITEM_DETAILS_ID = "itemDetails.id";
static final String ITEM_DETAILS_TITLE = "itemDetails.title";
static final String ITEM_DETAILS_DESC = "itemDetails.description";
static final String ITEM_DETAILS_CURRENCY = "itemDetails.currency";
static final String ITEM_DETAILS_VALUE = "itemDetails.value";
static final String[] ITEM_DETAILS_ALL_FIELDS = {ITEM_DETAILS_ID, ITEM_DETAILS_TITLE,
ITEM_DETAILS_DESC, ITEM_DETAILS_CURRENCY, ITEM_DETAILS_VALUE};
private GetDetailsConverter() {}
/**
* Converts the parameters to the getDetails.
*/
static Bundle convertParams(String[] itemIds) {
Bundle args = new Bundle();
args.putStringArray(GetDetailsConverter.PARAM_GET_DETAILS_ITEM_IDS, itemIds);
return args;
}
/**
* Produces a {@link TrustedWebActivityCallback} that calls the given
* {@link GetDetailsResponse}.
*/
static TrustedWebActivityCallback convertCallback(GetDetailsResponse callback) {
return new TrustedWebActivityCallback() {
@Override
public void onExtraCallback(@NonNull String callbackName, @Nullable Bundle args) {
if (!RESPONSE_GET_DETAILS.equals(callbackName)) {
Log.w(TAG, "Wrong callback name given: " + callbackName + ".");
returnClientAppError(callback);
return;
}
if (args == null) {
Log.w(TAG, "No args provided.");
returnClientAppError(callback);
return;
}
if (!(args.get(RESPONSE_GET_DETAILS_RESPONSE_CODE) instanceof Integer)
|| !(args.get(RESPONSE_GET_DETAILS_DETAILS_LIST) instanceof Parcelable[])) {
Log.w(TAG, "Poorly formed args provided.");
returnClientAppError(callback);
return;
}
int code = args.getInt(RESPONSE_GET_DETAILS_RESPONSE_CODE);
ItemDetails[] details = convertItemDetailsList(
args.getParcelableArray(RESPONSE_GET_DETAILS_DETAILS_LIST));
callback.call(convertResponseCodes(code), details);
}
};
}
private static ItemDetails[] convertItemDetailsList(Parcelable[] list) {
List<ItemDetails> details = new ArrayList<>();
for (Parcelable item : list) {
details.add(convertItemDetails(item));
}
return details.toArray(new ItemDetails[0]);
}
/** Extracts an {@link ItemDetails} from a {@link Parcelable}. */
@Nullable
static ItemDetails convertItemDetails(Parcelable itemAsParcelable) {
if (!(itemAsParcelable instanceof Bundle)) {
Log.w(TAG, "Item is not a Bundle.");
return null;
}
Bundle item = (Bundle) itemAsParcelable;
for (String field : ITEM_DETAILS_ALL_FIELDS) {
if (item.containsKey(field) && (item.get(field) instanceof String)) continue;
Log.w(TAG, "Item does not contain field String " + field + ".");
return null;
}
PaymentCurrencyAmount amount = new PaymentCurrencyAmount();
amount.currency = item.getString(ITEM_DETAILS_CURRENCY);
amount.value = item.getString(ITEM_DETAILS_VALUE);
ItemDetails res = new ItemDetails();
res.itemId = item.getString(ITEM_DETAILS_ID);
res.title = item.getString(ITEM_DETAILS_TITLE);
res.description = item.getString(ITEM_DETAILS_DESC);
res.price = amount;
return res;
}
public static void returnClientAppUnavailable(GetDetailsResponse callback) {
callback.call(BillingResponseCode.CLIENT_APP_UNAVAILABLE, new ItemDetails[0]);
}
public static void returnClientAppError(GetDetailsResponse callback) {
callback.call(BillingResponseCode.CLIENT_APP_ERROR, new ItemDetails[0]);
}
/**
* Creates a {@link Bundle} that represents an {@link ItemDetails} with the given values.
* This would be used by the client app and is here only to help testing.
*/
@VisibleForTesting
public static Bundle createItemDetailsBundle(
String id, String title, String desc, String currency, String value) {
Bundle bundle = new Bundle();
bundle.putString(ITEM_DETAILS_ID, id);
bundle.putString(ITEM_DETAILS_TITLE, title);
bundle.putString(ITEM_DETAILS_DESC, desc);
bundle.putString(ITEM_DETAILS_CURRENCY, currency);
bundle.putString(ITEM_DETAILS_VALUE, value);
return bundle;
}
/**
* Creates a {@link Bundle} that represents the result of a getDetails call. This would be
* carried out by the client app and is only here to help testing.
*/
@VisibleForTesting
public static Bundle createResponseBundle(int responseCode, Bundle... itemDetails) {
Bundle bundle = new Bundle();
bundle.putInt(RESPONSE_GET_DETAILS_RESPONSE_CODE, responseCode);
bundle.putParcelableArray(RESPONSE_GET_DETAILS_DETAILS_LIST, itemDetails);
return bundle;
}
}
...@@ -19,5 +19,5 @@ Android APIs are the source of truth for information such as price. ...@@ -19,5 +19,5 @@ Android APIs are the source of truth for information such as price.
* `TrustedWebActivityClient` is the class that talks to Trusted Web Activities. * `TrustedWebActivityClient` is the class that talks to Trusted Web Activities.
* `DigitalGoodsAdapter` sits between `DigitalGoodsImpl` and * `DigitalGoodsAdapter` sits between `DigitalGoodsImpl` and
`TrustedWebActivityClient`, transforming between appropriate data types. `TrustedWebActivityClient`, transforming between appropriate data types.
* `DigitalGoodsConverter` contains the lower level transformations that * `DigitalGoodsConverter`, `GetDetailsConverter` and `AcknowledgeConverter` contain the lower level
`DigitalGoodsAdapter` uses. transformations that `DigitalGoodsAdapter` uses.
...@@ -9,16 +9,18 @@ import static org.junit.Assert.assertEquals; ...@@ -9,16 +9,18 @@ import static org.junit.Assert.assertEquals;
import static org.chromium.chrome.browser.browserservices.TestTrustedWebActivityService.COMMAND_SET_RESPONSE; import static org.chromium.chrome.browser.browserservices.TestTrustedWebActivityService.COMMAND_SET_RESPONSE;
import static org.chromium.chrome.browser.browserservices.TestTrustedWebActivityService.SET_RESPONSE_BUNDLE; import static org.chromium.chrome.browser.browserservices.TestTrustedWebActivityService.SET_RESPONSE_BUNDLE;
import static org.chromium.chrome.browser.browserservices.TestTrustedWebActivityService.SET_RESPONSE_NAME; import static org.chromium.chrome.browser.browserservices.TestTrustedWebActivityService.SET_RESPONSE_NAME;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.RESPONSE_ACKNOWLEDGE; import static org.chromium.chrome.browser.browserservices.digitalgoods.AcknowledgeConverter.RESPONSE_ACKNOWLEDGE;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.RESPONSE_GET_DETAILS; import static org.chromium.chrome.browser.browserservices.digitalgoods.GetDetailsConverter.RESPONSE_GET_DETAILS;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.createAcknowledgeResponseBundle;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.createGetDetailsResponseBundle;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.createItemDetailsBundle;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.browser.trusted.TrustedWebActivityCallback;
import androidx.test.filters.MediumTest;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
...@@ -45,11 +47,6 @@ import org.chromium.payments.mojom.ItemDetails; ...@@ -45,11 +47,6 @@ import org.chromium.payments.mojom.ItemDetails;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.browser.trusted.TrustedWebActivityCallback;
import androidx.test.filters.MediumTest;
/** /**
* Tests for the Digital Goods flow. * Tests for the Digital Goods flow.
*/ */
...@@ -118,9 +115,10 @@ public class DigitalGoodsTest { ...@@ -118,9 +115,10 @@ public class DigitalGoodsTest {
public void twaServiceConnected() throws TimeoutException { public void twaServiceConnected() throws TimeoutException {
DigitalGoodsImpl impl = createFixedDigitalGoods(); DigitalGoodsImpl impl = createFixedDigitalGoods();
setTwaServiceResponse(RESPONSE_GET_DETAILS, createGetDetailsResponseBundle(0, setTwaServiceResponse(RESPONSE_GET_DETAILS,
createItemDetailsBundle("id1", "Item 1", "Desc 1", "GBP", "10") GetDetailsConverter.createResponseBundle(0,
)); GetDetailsConverter.createItemDetailsBundle(
"id1", "Item 1", "Desc 1", "GBP", "10")));
CallbackHelper helper = new CallbackHelper(); CallbackHelper helper = new CallbackHelper();
impl.getDetails(new String[] { "id1" }, new GetDetailsResponse() { impl.getDetails(new String[] { "id1" }, new GetDetailsResponse() {
...@@ -148,9 +146,10 @@ public class DigitalGoodsTest { ...@@ -148,9 +146,10 @@ public class DigitalGoodsTest {
// Note: The response code much be 0 for success otherwise it doesn't propagate through to // Note: The response code much be 0 for success otherwise it doesn't propagate through to
// JS. // JS.
setTwaServiceResponse(RESPONSE_GET_DETAILS, createGetDetailsResponseBundle(0, setTwaServiceResponse(RESPONSE_GET_DETAILS,
createItemDetailsBundle("id1", "Item 1", "Desc 1", "GBP", "10") GetDetailsConverter.createResponseBundle(0,
)); GetDetailsConverter.createItemDetailsBundle(
"id1", "Item 1", "Desc 1", "GBP", "10")));
exec("populateDigitalGoodsService()"); exec("populateDigitalGoodsService()");
waitForNonNull("digitalGoodsService"); waitForNonNull("digitalGoodsService");
...@@ -168,7 +167,7 @@ public class DigitalGoodsTest { ...@@ -168,7 +167,7 @@ public class DigitalGoodsTest {
public void acknowledge() throws TimeoutException { public void acknowledge() throws TimeoutException {
DigitalGoodsFactoryImpl.setDigitalGoodsForTesting(createFixedDigitalGoods()); DigitalGoodsFactoryImpl.setDigitalGoodsForTesting(createFixedDigitalGoods());
setTwaServiceResponse(RESPONSE_ACKNOWLEDGE, createAcknowledgeResponseBundle(0)); setTwaServiceResponse(RESPONSE_ACKNOWLEDGE, AcknowledgeConverter.createResponseBundle(0));
exec("populateDigitalGoodsService()"); exec("populateDigitalGoodsService()");
waitForNonNull("digitalGoodsService"); waitForNonNull("digitalGoodsService");
...@@ -184,7 +183,7 @@ public class DigitalGoodsTest { ...@@ -184,7 +183,7 @@ public class DigitalGoodsTest {
public void acknowledge_failsOnNonZeroResponse() throws TimeoutException { public void acknowledge_failsOnNonZeroResponse() throws TimeoutException {
DigitalGoodsFactoryImpl.setDigitalGoodsForTesting(createFixedDigitalGoods()); DigitalGoodsFactoryImpl.setDigitalGoodsForTesting(createFixedDigitalGoods());
setTwaServiceResponse(RESPONSE_ACKNOWLEDGE, createAcknowledgeResponseBundle(1)); setTwaServiceResponse(RESPONSE_ACKNOWLEDGE, AcknowledgeConverter.createResponseBundle(1));
exec("populateDigitalGoodsService()"); exec("populateDigitalGoodsService()");
waitForNonNull("digitalGoodsService"); waitForNonNull("digitalGoodsService");
......
...@@ -7,22 +7,24 @@ package org.chromium.chrome.browser.browserservices.digitalgoods; ...@@ -7,22 +7,24 @@ 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.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.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.DigitalGoodsConverter.PARAM_ACKNOWLEDGE_PURCHASE_TOKEN; import static org.chromium.chrome.browser.browserservices.digitalgoods.AcknowledgeConverter.PARAM_ACKNOWLEDGE_PURCHASE_TOKEN;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.PARAM_GET_DETAILS_ITEM_IDS; import static org.chromium.chrome.browser.browserservices.digitalgoods.AcknowledgeConverter.RESPONSE_ACKNOWLEDGE;
import static org.chromium.chrome.browser.browserservices.digitalgoods.AcknowledgeConverter.RESPONSE_ACKNOWLEDGE_RESPONSE_CODE;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.PLAY_BILLING_ITEM_ALREADY_OWNED; import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.PLAY_BILLING_ITEM_ALREADY_OWNED;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.PLAY_BILLING_ITEM_NOT_OWNED; import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.PLAY_BILLING_ITEM_NOT_OWNED;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.PLAY_BILLING_ITEM_UNAVAILABLE; import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.PLAY_BILLING_ITEM_UNAVAILABLE;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.PLAY_BILLING_OK; import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.PLAY_BILLING_OK;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.RESPONSE_ACKNOWLEDGE; import static org.chromium.chrome.browser.browserservices.digitalgoods.GetDetailsConverter.PARAM_GET_DETAILS_ITEM_IDS;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.RESPONSE_ACKNOWLEDGE_RESPONSE_CODE; import static org.chromium.chrome.browser.browserservices.digitalgoods.GetDetailsConverter.RESPONSE_GET_DETAILS;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.RESPONSE_GET_DETAILS; import static org.chromium.chrome.browser.browserservices.digitalgoods.GetDetailsConverter.RESPONSE_GET_DETAILS_DETAILS_LIST;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.RESPONSE_GET_DETAILS_DETAILS_LIST; import static org.chromium.chrome.browser.browserservices.digitalgoods.GetDetailsConverter.RESPONSE_GET_DETAILS_RESPONSE_CODE;
import static org.chromium.chrome.browser.browserservices.digitalgoods.DigitalGoodsConverter.RESPONSE_GET_DETAILS_RESPONSE_CODE;
import android.os.Bundle; import android.os.Bundle;
import android.os.Parcelable; import android.os.Parcelable;
import androidx.browser.trusted.TrustedWebActivityCallback;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
...@@ -35,8 +37,6 @@ import org.chromium.payments.mojom.ItemDetails; ...@@ -35,8 +37,6 @@ import org.chromium.payments.mojom.ItemDetails;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import androidx.browser.trusted.TrustedWebActivityCallback;
/** /**
* Tests for {@link DigitalGoodsConverterTest}. * Tests for {@link DigitalGoodsConverterTest}.
*/ */
...@@ -49,7 +49,7 @@ public class DigitalGoodsConverterTest { ...@@ -49,7 +49,7 @@ public class DigitalGoodsConverterTest {
public void convertGetDetailsParams() { public void convertGetDetailsParams() {
String[] itemIds = { "id1", "id2" }; String[] itemIds = { "id1", "id2" };
Bundle b = DigitalGoodsConverter.convertGetDetailsParams(itemIds); Bundle b = GetDetailsConverter.convertParams(itemIds);
String[] out = b.getStringArray(PARAM_GET_DETAILS_ITEM_IDS); String[] out = b.getStringArray(PARAM_GET_DETAILS_ITEM_IDS);
assertArrayEquals(itemIds, out); assertArrayEquals(itemIds, out);
...@@ -64,9 +64,9 @@ public class DigitalGoodsConverterTest { ...@@ -64,9 +64,9 @@ public class DigitalGoodsConverterTest {
String value = "10"; String value = "10";
Bundle bundle = Bundle bundle =
DigitalGoodsConverter.createItemDetailsBundle(id, title, desc, currency, value); GetDetailsConverter.createItemDetailsBundle(id, title, desc, currency, value);
ItemDetails item = DigitalGoodsConverter.convertItemDetails(bundle); ItemDetails item = GetDetailsConverter.convertItemDetails(bundle);
assertItemDetails(item, id, title, desc, currency, value); assertItemDetails(item, id, title, desc, currency, value);
} }
...@@ -87,14 +87,13 @@ public class DigitalGoodsConverterTest { ...@@ -87,14 +87,13 @@ public class DigitalGoodsConverterTest {
}; };
TrustedWebActivityCallback convertedCallback = TrustedWebActivityCallback convertedCallback =
DigitalGoodsConverter.convertGetDetailsCallback(callback); GetDetailsConverter.convertCallback(callback);
Bundle args = new Bundle(); Bundle args = new Bundle();
int responseCode = 0; int responseCode = 0;
Parcelable[] items = { Parcelable[] items = {
DigitalGoodsConverter.createItemDetailsBundle("1", "t1", "d1", "c1", "v1"), GetDetailsConverter.createItemDetailsBundle("1", "t1", "d1", "c1", "v1"),
DigitalGoodsConverter.createItemDetailsBundle("2", "t2", "d2", "c2", "v2") GetDetailsConverter.createItemDetailsBundle("2", "t2", "d2", "c2", "v2")};
};
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);
...@@ -119,7 +118,7 @@ public class DigitalGoodsConverterTest { ...@@ -119,7 +118,7 @@ public class DigitalGoodsConverterTest {
String token = "abcdef"; String token = "abcdef";
boolean makeAvailableAgain = true; boolean makeAvailableAgain = true;
Bundle b = DigitalGoodsConverter.convertAcknowledgeParams(token, makeAvailableAgain); Bundle b = AcknowledgeConverter.convertParams(token, makeAvailableAgain);
String outToken = b.getString(PARAM_ACKNOWLEDGE_PURCHASE_TOKEN); String outToken = b.getString(PARAM_ACKNOWLEDGE_PURCHASE_TOKEN);
boolean outMakeAvailableAgain = b.getBoolean(PARAM_ACKNOWLEDGE_MAKE_AVAILABLE_AGAIN); boolean outMakeAvailableAgain = b.getBoolean(PARAM_ACKNOWLEDGE_MAKE_AVAILABLE_AGAIN);
...@@ -137,7 +136,7 @@ public class DigitalGoodsConverterTest { ...@@ -137,7 +136,7 @@ public class DigitalGoodsConverterTest {
AcknowledgeResponse callback = (responseCode) -> state.set(responseCode); AcknowledgeResponse callback = (responseCode) -> state.set(responseCode);
TrustedWebActivityCallback convertedCallback = TrustedWebActivityCallback convertedCallback =
DigitalGoodsConverter.convertAcknowledgeCallback(callback); AcknowledgeConverter.convertCallback(callback);
Bundle args = new Bundle(); Bundle args = new Bundle();
int responseCode = 0; int responseCode = 0;
......
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