test_constructor.html (10748B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1345361 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for Bug 1345361</title> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 10 <script src="/tests/SimpleTest/SimpleTest.js"></script> 11 <script type="application/javascript"> 12 13 "use strict"; 14 SimpleTest.waitForExplicitFinish(); 15 16 var gUrl = SimpleTest.getTestFileURL('ConstructorChromeScript.js'); 17 var gScript = SpecialPowers.loadChromeScript(gUrl); 18 19 function testFailHandler(message) { 20 ok(false, message); 21 } 22 gScript.addMessageListener("test-fail", testFailHandler); 23 24 const simplestMethods = [{ 25 supportedMethods: "basic-card", 26 }]; 27 const simplestDetails = { 28 total: { 29 label: "Total", 30 amount: { 31 currency: "USD", 32 value: "1.00" 33 } 34 } 35 }; 36 37 const complexMethods = [{ 38 supportedMethods: "basic-card", 39 data: { 40 supportedNetworks: ['unionpay', 'visa', 'mastercard', 'amex', 'discover', 41 'diners', 'jcb', 'mir', 42 ], 43 }, 44 }]; 45 46 const nonBasicCardMethods = [{ 47 supportedMethods: "testing-payment-method", 48 data: { 49 paymentId: "P3892940", 50 paymentType: "prepaid", 51 }, 52 }]; 53 54 const complexDetails = { 55 id: "payment details", 56 total: { 57 label: "Total", 58 amount: { 59 currency: "USD", 60 value: "100.00" 61 } 62 }, 63 displayItems: [ 64 { 65 label: "First item", 66 amount: { 67 currency: "USD", 68 value: "60.00" 69 } 70 }, 71 { 72 label: "Second item", 73 amount: { 74 currency: "USD", 75 value: "40.00" 76 } 77 } 78 ], 79 modifiers: [ 80 { 81 supportedMethods: "basic-card", 82 total: { 83 label: "Discounted Total", 84 amount: { 85 currency: "USD", 86 value: "90.00" 87 } 88 }, 89 additionalDisplayItems: [ 90 { 91 label: "basic-card discount", 92 amount: { 93 currency: "USD", 94 value: "-10.00" 95 } 96 } 97 ], 98 data: { discountProgramParticipantId: "86328764873265", } 99 }, 100 ], 101 shippingOptions: [ 102 { 103 id: "NormalShipping", 104 label: "NormalShipping", 105 amount: { 106 currency: "USD", 107 value: "10.00" 108 }, 109 selected: true, 110 }, 111 { 112 id: "FastShipping", 113 label: "FastShipping", 114 amount: { 115 currency: "USD", 116 value: "30.00" 117 }, 118 selected: false, 119 }, 120 ], 121 }; 122 123 const complexOptions = { 124 requestPayerName: true, 125 requestPayerEmail: true, 126 requestPayerPhone: true, 127 requestShipping: true, 128 shippingType: "shipping" 129 }; 130 131 const duplicateShippingOptionsDetails = { 132 id: "duplicate shipping options details", 133 total: { 134 label: "Total", 135 amount: { 136 currency: "USD", 137 value: "1.00" 138 } 139 }, 140 shippingOptions: [ 141 { 142 id: "dupShipping", 143 label: "NormalShipping", 144 amount: { 145 currency: "USD", 146 value: "10.00" 147 }, 148 selected: true, 149 }, 150 { 151 id: "dupShipping", 152 label: "FastShipping", 153 amount: { 154 currency: "USD", 155 value: "30.00" 156 }, 157 selected: false, 158 }, 159 ], 160 }; 161 162 163 function testWithSimplestParameters() { 164 return new Promise((resolve, reject) => { 165 const payRequest = new PaymentRequest(simplestMethods, simplestDetails); 166 ok(payRequest, "PaymentRequest should be created"); 167 gScript.addMessageListener("check-complete", function checkCompleteHandler() { 168 gScript.removeMessageListener("check-complete", checkCompleteHandler); 169 resolve(); 170 }); 171 gScript.sendAsyncMessage("check-simplest-request"); 172 }); 173 } 174 175 function testWithComplexParameters() { 176 return new Promise((resolve, reject) => { 177 const payRequest = new PaymentRequest(complexMethods, complexDetails, complexOptions); 178 ok(payRequest, "PaymentRequest should be created"); 179 gScript.addMessageListener("check-complete", function checkCompleteHandler() { 180 gScript.removeMessageListener("check-complete", checkCompleteHandler); 181 resolve(); 182 }); 183 gScript.sendAsyncMessage("check-complex-request"); 184 }); 185 } 186 187 function testWithNonBasicCardMethods() { 188 return new Promise((resolve, reject) => { 189 const payRequest = new PaymentRequest(nonBasicCardMethods, simplestDetails); 190 ok(payRequest, "PaymentRequest should be created"); 191 gScript.addMessageListener("check-complete", function checkCompleteHandler() { 192 gScript.removeMessageListener("check-complete", checkCompleteHandler); 193 resolve(); 194 }); 195 gScript.sendAsyncMessage("check-nonbasiccard-request"); 196 }); 197 } 198 199 function testWithDuplicateShippingOptionsParameters() { 200 return new Promise((resolve, reject) => { 201 try { 202 const payRequest = new PaymentRequest(simplestMethods, 203 duplicateShippingOptionsDetails, 204 {requestShipping: true}); 205 ok(false, "Construction should fail with duplicate shippingOption Ids."); 206 resolve(); 207 } catch (e) { 208 is(e.name, "TypeError", "Expected 'TypeError' with duplicate shippingOption Ids."); 209 resolve(); 210 } 211 }); 212 } 213 214 function testShippingOptionAttribute() { 215 return new Promise((resolve, reject) => { 216 const details = { 217 total: { 218 label: "Total", 219 amount: { 220 currency: "USD", 221 value: "1.00", 222 }, 223 }, 224 shippingOptions: [ 225 { 226 id: "option1", 227 label: "option1", 228 amount: { 229 currency: "USD", 230 value: "1.00", 231 }, 232 selected: false, 233 }, 234 { 235 id: "option2", 236 label: "option2", 237 amount: { 238 currency: "USD", 239 value: "1.00", 240 }, 241 selected: false, 242 }, 243 ], 244 }; 245 const payRequest1 = new PaymentRequest(simplestMethods, 246 details, 247 {requestShipping: false}); 248 ok(payRequest1, "PaymentRequest should be created"); 249 is(payRequest1.shippingOption, null, 250 "request.shippingOption should be null in default, when options.requestShipping is false"); 251 details.shippingOptions[0].selected = true; 252 const payRequest2 = new PaymentRequest(simplestMethods, 253 details, 254 {requestShipping: false}); 255 ok(payRequest2, "PaymentRequest should be created"); 256 is(payRequest2.shippingOption, null, 257 "request.shippingOption should be null in default, when options.requestShipping is false"); 258 const payRequest3 = new PaymentRequest(simplestMethods, 259 details, 260 {requestShipping: true}); 261 ok(payRequest3, "PaymentRequest should be created"); 262 ok(payRequest3.shippingOption, 263 "request.shippingOption should not be null when both shoppingOtpion.selected and options.requestOptions are true"); 264 is(payRequest3.shippingOption, "option1", 265 "request.shippingOption should be 'option1'"); 266 details.shippingOptions[1].selected = true; 267 const payRequest4 = new PaymentRequest(simplestMethods, 268 details, 269 {requestShipping: true}); 270 ok(payRequest4, "PaymentRequest should be created"); 271 ok(payRequest4.shippingOption, 272 "request.shippingOption should not be null when both shoppingOtpion.selected and options.requestOptions are true"); 273 is(payRequest4.shippingOption, "option2", 274 "request.shippingOption should be 'option2' which is the last one selected."); 275 resolve(); 276 }); 277 } 278 279 function testMultipleRequests() { 280 return new Promise((resolve, reject) => { 281 const payRequest1 = new PaymentRequest(complexMethods, complexDetails, complexOptions); 282 const payRequest2 = new PaymentRequest(simplestMethods, simplestDetails); 283 ok(payRequest1, "PaymentRequest with complex parameters should be created"); 284 ok(payRequest2, "PaymentRequest with simplest parameters should be created"); 285 gScript.addMessageListener("check-complete", function checkCompleteHandler() { 286 gScript.removeMessageListener("check-complete", checkCompleteHandler); 287 resolve(); 288 }); 289 gScript.sendAsyncMessage("check-multiple-requests"); 290 }); 291 } 292 293 function testCrossOriginTopLevelPrincipal() { 294 return new Promise((resolve, reject) => { 295 var ifrr = document.createElement('iframe'); 296 297 window.addEventListener("message", function(event) { 298 is(event.data, "successful", 299 "Expected 'successful', but got '" + event.data + "'"); 300 gScript.addMessageListener("check-complete", function checkCompleteHandler() { 301 gScript.removeMessageListener("check-complete", checkCompleteHandler); 302 resolve(); 303 }); 304 gScript.sendAsyncMessage("check-cross-origin-top-level-principal"); 305 }); 306 307 ifrr.setAttribute('allow', 'payment'); 308 ifrr.src = "https://test1.example.com:443/tests/dom/payments/test/simple_payment_request.html"; 309 document.body.appendChild(ifrr); 310 }); 311 } 312 313 function teardown() { 314 gScript.addMessageListener("teardown-complete", function teardownCompleteHandler() { 315 gScript.removeMessageListener("teardown-complete", teardownCompleteHandler); 316 gScript.removeMessageListener("test-fail", testFailHandler) 317 gScript.destroy(); 318 SimpleTest.finish(); 319 }); 320 gScript.sendAsyncMessage("teardown"); 321 } 322 323 function runTests() { 324 testWithSimplestParameters() 325 .then(testWithComplexParameters) 326 .then(testWithNonBasicCardMethods) 327 .then(testWithDuplicateShippingOptionsParameters) 328 .then(testMultipleRequests) 329 .then(testCrossOriginTopLevelPrincipal) 330 .then(testShippingOptionAttribute) 331 .then(teardown) 332 .catch( e => { 333 ok(false, "Unexpected error: " + e.name); 334 SimpleTest.finish(); 335 }); 336 } 337 338 window.addEventListener('load', function() { 339 SpecialPowers.pushPrefEnv({ 340 'set': [ 341 ['dom.payments.request.enabled', true], 342 ] 343 }, runTests); 344 }); 345 346 </script> 347 </head> 348 <body> 349 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1345361">Mozilla Bug 1345361</a> 350 </body> 351 </html>