test_shippingOptions.html (8498B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1440041 5 https://bugzilla.mozilla.org/show_bug.cgi?id=1443914 6 --> 7 <head> 8 <meta charset="utf-8"> 9 <title>Test for shippingOptions related bugs</title> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 11 <script src="/tests/SimpleTest/SimpleTest.js"></script> 12 <script type="application/javascript" src="./DefaultData.js"></script> 13 <script type="application/javascript"> 14 15 "use strict"; 16 SimpleTest.waitForExplicitFinish(); 17 18 var gUrl = SimpleTest.getTestFileURL('ShippingOptionsChromeScript.js'); 19 var gScript = SpecialPowers.loadChromeScript(gUrl); 20 21 function testFailHandler(message) { 22 ok(false, message); 23 } 24 function testPassHandler(message) { 25 ok(true, message); 26 } 27 gScript.addMessageListener("test-fail", testFailHandler); 28 gScript.addMessageListener("test-pass", testPassHandler); 29 30 let shippingOptions = [{ 31 id: "NormalShipping", 32 label: "NormalShipping", 33 amount: { 34 currency: "USD", 35 value: "10.00", 36 }, 37 selected: true, 38 },{ 39 id: "FastShipping", 40 label: "FastShipping", 41 amount: { 42 currency: "USD", 43 value: "5.00", 44 }, 45 selected: false, 46 }] 47 48 // testing function main body 49 function testShippingOptionsTemplate(initDetails, 50 optionUpdateDetails, 51 expectedRequestOption, 52 expectedOptionChangeOption, 53 expectedResponseOption) { 54 const expectedResults = {requestResult: expectedRequestOption, 55 changeOptionResult: expectedOptionChangeOption, 56 responseResult: expectedResponseOption,}; 57 gScript.sendAsyncMessage("set-expected-results", expectedResults); 58 return new Promise((resolve, reject) => { 59 const request = new PaymentRequest(defaultMethods, initDetails, defaultOptions); 60 const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true); 61 is(request.shippingOption, expectedRequestOption, 62 "request.shippingOption should be " + expectedRequestOption + 63 " after created, but got " + request.shippingOption + "."); 64 if (optionUpdateDetails) { 65 request.addEventListener("shippingoptionchange", event => { 66 is(request.shippingOption, expectedOptionChangeOption, 67 "request.shippingOption should be " + expectedOptionChangeOption + 68 " in shippingoptionchange event, but got " + request.shippingOption + "."); 69 event.updateWith(optionUpdateDetails); 70 }); 71 } 72 request.show().then(response => { 73 is(response.shippingOption, expectedResponseOption, 74 "response.shippingOption should be " + expectedResponseOption + 75 ", but got " + response.shippingOption + "."); 76 response.complete("success").then(() => { 77 resolve(); 78 }).catch(error => { 79 ok(false, "Unexpected error: " + error.name); 80 resolve(); 81 }) 82 }, response => { 83 }).catch(error => { 84 ok(false, "Unexpected error: " + error.name); 85 resolve(); 86 }).finally(handler.destruct); 87 }); 88 } 89 90 // test no selected shipping option in default 91 function testNoSelectedShippingOptions() { 92 return testShippingOptionsTemplate(defaultDetails, // initial details 93 null, // update details for optionchange 94 null, // expected request.shippintOption after create 95 null, // expected request.shippingOption after optionchange 96 null); // expected response.shippingOption 97 } 98 99 // test select one shipping option in default 100 function testSelectedOneShippingOption() { 101 let details = Object.assign({}, defaultDetails); 102 details.shippingOptions = shippingOptions; 103 details.shippingOptions[0].selected = true; 104 details.shippingOptions[1].selected = false; 105 const expectedOption = details.shippingOptions[0].id; 106 return testShippingOptionsTemplate(details, // initial details 107 null, // update details for optionchange 108 expectedOption, // expected request.shippintOption after create 109 null, // expected request.shippingOption after optionchange 110 expectedOption); // expected response.shippingOption 111 } 112 113 // test select multiple shipping options in default 114 function testMultiSelectedShippingOptions() { 115 let details = Object.assign({}, defaultDetails); 116 details.shippingOptions = shippingOptions; 117 details.shippingOptions[0].selected = true; 118 details.shippingOptions[1].selected = true; 119 const expectedOption = details.shippingOptions[1].id; 120 return testShippingOptionsTemplate(details, // initial details 121 null, // update details for optionchange 122 expectedOption, // expected request.shippintOption after create 123 null, // expected request.shippingOption after optionchange 124 expectedOption); // expected response.shippingOption 125 } 126 127 // test no selected shipping option in default, but selected by user 128 function testSelectedByUser() { 129 let updateDetails = Object.assign({}, defaultDetails); 130 updateDetails.shippingOptions = shippingOptions; 131 updateDetails.shippingOptions[0].selected = true; 132 updateDetails.shippingOptions[1].selected = false; 133 const expectedOption = updateDetails.shippingOptions[0].id; 134 return testShippingOptionsTemplate(defaultDetails, // initial details 135 updateDetails, // update details for optionchange 136 null, // expected request.shippintOption after create 137 expectedOption, // expected request.shippingOption after optionchange 138 expectedOption); // expected response.shippingOption 139 } 140 141 // test no selected shipping option in default, but selected by user then updated 142 // by merchant to the other. 143 function testUpdateSelectedByMerchant() { 144 let updateDetails = Object.assign({}, defaultDetails); 145 updateDetails.shippingOptions = shippingOptions; 146 updateDetails.shippingOptions[0].selected = false; 147 updateDetails.shippingOptions[1].selected = true; 148 const expectedOption = updateDetails.shippingOptions[0].id; 149 const expectedResponse = updateDetails.shippingOptions[1].id; 150 return testShippingOptionsTemplate(defaultDetails, // initial details 151 updateDetails, // update details for optionchange 152 null, // expected request.shippintOption after create 153 expectedOption, // expected request.shippingOption after optionchange 154 expectedResponse);// expected response.shippingOption 155 } 156 157 // test update shipping options to null 158 function testUpdateShippingOptionsToNull() { 159 let updateDetails = Object.assign({}, defaultDetails); 160 delete updateDetails.shippingOptions; 161 const expectedOption = defaultDetails.shippingOptions[0].id; 162 return testShippingOptionsTemplate(defaultDetails, // initial details 163 updateDetails, // update details for optionchange 164 null, // expected request.shippintOption after create 165 expectedOption, // expected request.shippingOption after optionchange 166 null); // expected response.shippingOption 167 } 168 169 function teardown() { 170 gScript.addMessageListener("teardown-complete", function teardownCompleteHandler() { 171 gScript.removeMessageListener("teardown-complete", teardownCompleteHandler); 172 gScript.removeMessageListener("test-fail", testFailHandler); 173 gScript.removeMessageListener("test-pass", testPassHandler); 174 gScript.destroy(); 175 SimpleTest.finish(); 176 }); 177 gScript.sendAsyncMessage("teardown"); 178 } 179 180 function runTests() { 181 testNoSelectedShippingOptions() 182 .then(testSelectedOneShippingOption) 183 .then(testMultiSelectedShippingOptions) 184 .then(testSelectedByUser) 185 .then(testUpdateSelectedByMerchant) 186 .then(testUpdateShippingOptionsToNull) 187 .then(teardown) 188 .catch( e => { 189 ok(false, "Unexpected error: " + e.name); 190 SimpleTest.finish(); 191 }); 192 } 193 194 window.addEventListener('load', function() { 195 SpecialPowers.pushPrefEnv({ 196 'set': [ 197 ['dom.payments.request.enabled', true], 198 ] 199 }, runTests); 200 }); 201 202 </script> 203 </head> 204 <body> 205 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1440041">Mozilla Bug 1440041</a> 206 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1443914">Mozilla Bug 1443914</a> 207 </body> 208 </html>