Commit 98d5858a authored by Mohamad Ahmadi's avatar Mohamad Ahmadi Committed by Commit Bot

[PR] Convert numbers->strings to match Desktop/Android validation behavior

Bug: 785429
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I23c008943b60e89b9b58265cdad3293b3dda2bb5
Reviewed-on: https://chromium-review.googlesource.com/793983Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Commit-Queue: Moe Ahmadi <mahmadi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#519803}
parent 09d0298d
...@@ -180,15 +180,13 @@ var SerializedPaymentResponse; ...@@ -180,15 +180,13 @@ var SerializedPaymentResponse;
*/ */
__gCrWeb['paymentRequestManager'].validatePaymentCurrencyAmount = function( __gCrWeb['paymentRequestManager'].validatePaymentCurrencyAmount = function(
amount, amountName) { amount, amountName) {
if (typeof amount.value !== 'string') var value = String(amount.value);
throw new TypeError(amountName + ' value must be a string'); if (value > __gCrWeb['paymentRequestManager'].MAX_STRING_LENGTH) {
if (amount.value.length >
__gCrWeb['paymentRequestManager'].MAX_STRING_LENGTH) {
throw new TypeError( throw new TypeError(
amountName + ' value cannot be longer than ' + amountName + ' value cannot be longer than ' +
__gCrWeb['paymentRequestManager'].MAX_STRING_LENGTH + ' characters'); __gCrWeb['paymentRequestManager'].MAX_STRING_LENGTH + ' characters');
} }
if (!/^-?[0-9]+(\.[0-9]+)?$/.test(amount.value)) { if (!/^-?[0-9]+(\.[0-9]+)?$/.test(value)) {
throw new TypeError( throw new TypeError(
amountName + ' value is not a valid decimal monetary value'); amountName + ' value is not a valid decimal monetary value');
} }
...@@ -402,7 +400,7 @@ var SerializedPaymentResponse; ...@@ -402,7 +400,7 @@ var SerializedPaymentResponse;
if (typeof modifiers[i].total !== 'undefined') { if (typeof modifiers[i].total !== 'undefined') {
__gCrWeb['paymentRequestManager'].validatePaymentItem( __gCrWeb['paymentRequestManager'].validatePaymentItem(
modifiers[i].total, 'Modifier total'); modifiers[i].total, 'Modifier total');
if (modifiers[i].total.amount.value[0] == '-') if (String(modifiers[i].total.amount.value)[0] == '-')
throw new TypeError('Modifier total value should be non-negative'); throw new TypeError('Modifier total value should be non-negative');
} }
...@@ -531,7 +529,7 @@ var SerializedPaymentResponse; ...@@ -531,7 +529,7 @@ var SerializedPaymentResponse;
throw new TypeError('Total is missing.'); throw new TypeError('Total is missing.');
__gCrWeb['paymentRequestManager'].validatePaymentItem( __gCrWeb['paymentRequestManager'].validatePaymentItem(
details.total, 'Total'); details.total, 'Total');
if (details.total.amount.value[0] == '-') if (String(details.total.amount.value)[0] == '-')
throw new TypeError('Total value should be non-negative'); throw new TypeError('Total value should be non-negative');
return __gCrWeb['paymentRequestManager'].validatePaymentDetailsBase( return __gCrWeb['paymentRequestManager'].validatePaymentDetailsBase(
...@@ -552,7 +550,7 @@ var SerializedPaymentResponse; ...@@ -552,7 +550,7 @@ var SerializedPaymentResponse;
if (details.total) { if (details.total) {
__gCrWeb['paymentRequestManager'].validatePaymentItem( __gCrWeb['paymentRequestManager'].validatePaymentItem(
details.total, 'Total'); details.total, 'Total');
if (details.total.amount.value[0] == '-') if (String(details.total.amount.value)[0] == '-')
throw new TypeError('Total value should be non-negative'); throw new TypeError('Total value should be non-negative');
} }
......
...@@ -15,17 +15,41 @@ const defaultAmount = Object.freeze({ ...@@ -15,17 +15,41 @@ const defaultAmount = Object.freeze({
currency: "USD", currency: "USD",
value: "1.0", value: "1.0",
}); });
const defaultNumberAmount = Object.freeze({
currency: "USD",
value: 1.0,
});
const defaultTotal = Object.freeze({ const defaultTotal = Object.freeze({
label: "Default Total", label: "Default Total",
amount: defaultAmount, amount: defaultAmount,
}); });
const defaultNumberTotal = Object.freeze({
label: "Default Number Total",
amount: defaultNumberAmount,
});
const defaultDetails = Object.freeze({ const defaultDetails = Object.freeze({
total: defaultTotal, total: defaultTotal,
displayItems: [
{
label: "Default Display Item",
amount: defaultAmount,
},
],
});
const defaultNumberDetails = Object.freeze({
total: defaultNumberTotal,
displayItems: [
{
label: "Default Display Item",
amount: defaultNumberAmount,
},
],
}); });
// Avoid false positives, this should always pass // Avoid false positives, this should always pass
function smokeTest() { function smokeTest() {
new PaymentRequest(defaultMethods, defaultDetails); new PaymentRequest(defaultMethods, defaultDetails);
new PaymentRequest(defaultMethods, defaultNumberDetails);
} }
test(() => { test(() => {
smokeTest(); smokeTest();
...@@ -177,6 +201,7 @@ const invalidTotalAmounts = invalidAmounts.concat([ ...@@ -177,6 +201,7 @@ const invalidTotalAmounts = invalidAmounts.concat([
"-1.0", "-1.0",
"-1.00", "-1.00",
"-1000.000", "-1000.000",
-10,
]); ]);
test(() => { test(() => {
smokeTest(); smokeTest();
......
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