2017-06-23 12:15:58 +03:00
<!DOCTYPE HTML>
< html >
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1345366
-->
< head >
< meta charset = "utf-8" >
< title > Test for Bug 1345366< / title >
< link rel = "stylesheet" type = "text/css" href = "/tests/SimpleTest/test.css" / >
< script type = "application/javascript" src = "/tests/SimpleTest/SimpleTest.js" > < / script >
< script type = "application/javascript" >
"use strict";
SimpleTest.waitForExplicitFinish();
var gUrl = SimpleTest.getTestFileURL('ShowPaymentChromeScript.js');
var gScript = SpecialPowers.loadChromeScript(gUrl);
function testFailHandler(message) {
ok(false, message);
}
gScript.addMessageListener("test-fail", testFailHandler);
const defaultMethods = [{
2017-06-30 20:17:12 +03:00
supportedMethods: "basic-card",
2017-06-23 12:15:58 +03:00
data: {
supportedNetworks: ['unionpay', 'visa', 'mastercard', 'amex', 'discover',
'diners', 'jcb', 'mir',
],
supportedTypes: ['prepaid', 'debit', 'credit'],
},
2017-07-24 12:17:03 +03:00
}, {
supportedMethods: "testing-payment-method",
2017-06-23 12:15:58 +03:00
}];
const defaultDetails = {
id: "test payment",
total: {
label: "Total",
amount: {
currency: "USD",
value: "1.00"
}
},
shippingOptions: [
{
id: "NormalShipping",
label: "NormalShipping",
amount: {
currency: "USD",
value: "10.00"
},
selected: false,
},
{
id: "FastShipping",
label: "FastShipping",
amount: {
currency: "USD",
value: "30.00"
},
selected: false,
},
],
};
const defaultOptions = {
requestPayerName: true,
requestPayerEmail: false,
reqeustPayerPhone: false,
requestShipping: true,
shippingType: "shipping"
};
const updatedOptionDetails = {
total: {
label: "Total",
amount: {
currency: "USD",
value: "1.00"
}
},
shippingOptions: [
{
id: "NormalShipping",
label: "NormalShipping",
amount: {
currency: "USD",
value: "10.00"
},
selected: false,
},
{
id: "FastShipping",
label: "FastShipping",
amount: {
currency: "USD",
value: "30.00"
},
selected: true,
},
],
};
const updatedErrorDetails = {
total: {
label: "Total",
amount: {
currency: "USD",
value: "1.00"
}
},
shippingOptions: [
{
id: "NormalShipping",
label: "NormalShipping",
amount: {
currency: "USD",
value: "10.00"
},
selected: false,
},
{
id: "FastShipping",
label: "FastShipping",
amount: {
currency: "USD",
value: "30.00"
},
selected: false,
},
],
error: "Update with Error",
};
2017-08-21 06:35:58 +03:00
const nonSupportedMethods = [{
supportedMethods: "nonsupported-method",
}];
2017-06-23 12:15:58 +03:00
function updateWithShippingAddress() {
return new Promise((resolve, reject) => {
resolve(defaultDetails);
});
}
function updateWithShippingOption() {
return new Promise((resolve, reject) => {
resolve(updatedOptionDetails);
});
}
function updateWithError() {
return new Promise((resolve, reject) => {
resolve(updatedErrorDetails);
});
}
function testShow() {
gScript.sendAsyncMessage("set-normal-ui-service");
return new Promise((resolve, reject) => {
const payRequest = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
payRequest.addEventListener("shippingaddresschange", event => {
event.updateWith(updateWithShippingAddress());
});
payRequest.addEventListener("shippingoptionchange", event => {
event.updateWith(updateWithShippingOption());
});
payRequest.show().then(response => {
is(response.requestId, "test payment", "response.requestId should be 'test payment'.");
2017-07-24 12:17:03 +03:00
is(response.methodName, "testing-payment-method", "response.methodName should be 'testing-payment-method'.");
is(response.details.paymentToken, "6880281f-0df3-4b8e-916f-66575e2457c1", "response.details.paymentToken should be '6880281f-0df3-4b8e-916f-66575e2457c1'.");
2017-06-23 12:15:58 +03:00
is(response.shippingAddress.country, "USA", "response.shippingAddress.country should be 'USA'.");
is(response.shippingAddress.addressLine.length, 1, "response.shippingAddress.addressLine.length should be 1.");
is(response.shippingAddress.addressLine[0], "Easton Ave", "response.shippingAddress.addressLine[0] should be 'Easton Ave'.");
is(response.shippingAddress.region, "CA", "response.shippingAddress.region should be 'CA'.");
is(response.shippingAddress.city, "San Bruno", "response.shippingAddress.city should be 'San Bruno'.");
is(response.shippingAddress.dependentLocality, "", "response.shippingAddress.dependentLocality should be empty.");
is(response.shippingAddress.postalCode, "94066", "response.shippingAddress.postalCode should be '94066'.");
is(response.shippingAddress.sortingCode, "123456", "response.shippingAddress.sortingCode should be '123456'.");
is(response.shippingAddress.organization, "", "response.shippingAddress.organization should be empty.");
is(response.shippingAddress.recipient, "Bill A. Pacheco", "response.shippingAddress.recipient should be 'Bill A. Pacheco'.");
is(response.shippingAddress.phone, "+1-434-441-3879", "response.shippingAddress.phone should be '+1-434-441-3879'.");
is(response.shippingOption, "FastShipping", "response.shippingOption should be 'FastShipping'.");
is(response.payerName, "Bill A. Pacheco", "response.payerName should be 'Bill A. Pacheco'.");
ok(!response.payerEamil, "response.payerEmail should be empty");
ok(!response.payerPhone, "response.payerPhone should be empty");
response.complete("success").then(() =>{
resolve();
}).catch(e => {
ok(false, "Unexpected error: " + e.name);
resolve();
});
}).catch( e => {
ok(false, "Unexpected error: " + e.name);
resolve();
});
});
}
2017-08-21 06:35:58 +03:00
function testCannotMakePaymentShow() {
return new Promise((resolve, reject) => {
const payRequest = new PaymentRequest(nonSupportedMethods, defaultDetails);
payRequest.canMakePayment().then(result => {
ok(!result, "canMakePayment() should return false, but got " + result + ".");
payRequest.show().then( () => {
ok(false, "Should be rejected with 'NotSupportedError', but got resolved");
resolve();
}).catch( e => {
is(e.name, "NotSupportedError", "Should be rejected with 'NotSupportedError', but got " + e.name + ".");
resolve();
});
});
});
}
2017-06-23 12:15:58 +03:00
function testRejectShow() {
gScript.sendAsyncMessage("set-reject-ui-service");
return new Promise((resolve, reject) => {
const payRequest = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
payRequest.show().then((result) => {
ok(false, "Should be rejected with 'AbortError', but got resolved");
resolve();
}, (result) => {
is(result.name, "AbortError", "Should be rejected with 'AbortError', but got " + result.name + ".");
resolve();
}).catch(e => {
ok(false, "Unexpected error: " + e.name);
resolve();
});
});
}
function testUpdateWithError() {
gScript.sendAsyncMessage("set-update-with-error-ui-service");
return new Promise((resolve, reject) => {
const payRequest = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions);
payRequest.addEventListener("shippingaddresschange", event => {
event.updateWith(updateWithError());
});
payRequest.addEventListener("shippingoptionchange", event => {
event.updateWith(updateWithError());
});
payRequest.show().then((result) => {
ok(false, "Should be rejected with 'AbortError', but got resolved");
resolve();
}, (result) => {
is(result.name, "AbortError", "Should be rejected with 'AbortError', but got " + result.name + ".");
resolve();
}).catch(e => {
ok(false, "Unexpected error: " + e.name);
resolve();
});
});
}
2017-07-24 12:17:03 +03:00
function testNullDetailsResponse() {
return new Promise((resolve, reject) => {
2017-08-21 06:35:58 +03:00
gScript.addMessageListener("test-init-data-and-response-complete",
2017-07-24 12:17:03 +03:00
function nullDetailsResponseCompleteHandler() {
2017-08-21 06:35:58 +03:00
gScript.removeMessageListener("test-init-data-and-response-complete",
2017-07-24 12:17:03 +03:00
nullDetailsResponseCompleteHandler);
resolve();
});
2017-08-21 06:35:58 +03:00
gScript.sendAsyncMessage("test-init-data-and-response");
2017-07-24 12:17:03 +03:00
});
}
2017-06-23 12:15:58 +03:00
function teardown() {
gScript.addMessageListener("teardown-complete", function teardownCompleteHandler() {
gScript.removeMessageListener("teardown-complete", teardownCompleteHandler);
gScript.removeMessageListener("test-fail", testFailHandler)
gScript.destroy();
SimpleTest.finish();
});
gScript.sendAsyncMessage("teardown");
}
function runTests() {
2017-08-21 06:35:58 +03:00
testCannotMakePaymentShow()
.then(testRejectShow)
2017-06-23 12:15:58 +03:00
.then(testShow)
.then(testUpdateWithError)
2017-07-24 12:17:03 +03:00
.then(testNullDetailsResponse)
2017-06-23 12:15:58 +03:00
.then(teardown)
.catch( e => {
ok(false, "Unexpected error: " + e.name);
SimpleTest.finish();
});
}
window.addEventListener('load', function() {
SpecialPowers.pushPrefEnv({
'set': [
['dom.payments.request.enabled', true],
]
}, runTests);
});
< / script >
< / head >
< body >
< a target = "_blank" href = "https://bugzilla.mozilla.org/show_bug.cgi?id=1345366" > Mozilla Bug 1345366< / a >
< / body >
< / html >