test_requestShipping.html (4405B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1436903 5 --> 6 <head> 7 <meta charset="utf-8"> 8 <title>Test for Bug 1436903</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('RequestShippingChromeScript.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 defaultMethods = [{ 25 supportedMethods: "basic-card", 26 data: { 27 supportedNetworks: ['unionpay', 'visa', 'mastercard', 'amex', 'discover', 28 'diners', 'jcb', 'mir', 29 ], 30 }, 31 }, { 32 supportedMethods: "testing-payment-method", 33 }]; 34 const defaultDetails = { 35 id: "test payment", 36 total: { 37 label: "Total", 38 amount: { 39 currency: "USD", 40 value: "1.00" 41 } 42 }, 43 shippingOptions: [ 44 { 45 id: "NormalShipping", 46 label: "NormalShipping", 47 amount: { 48 currency: "USD", 49 value: "10.00" 50 }, 51 selected: false, 52 }, 53 { 54 id: "FastShipping", 55 label: "FastShipping", 56 amount: { 57 currency: "USD", 58 value: "30.00" 59 }, 60 selected: false, 61 }, 62 ], 63 }; 64 65 const defaultOptions = { 66 requestPayerName: true, 67 requestPayerEmail: false, 68 requestPayerPhone: false, 69 requestShipping: false, 70 shippingType: "shipping" 71 }; 72 73 const updatedOptionDetails = { 74 total: { 75 label: "Total", 76 amount: { 77 currency: "USD", 78 value: "1.00" 79 } 80 }, 81 shippingOptions: [ 82 { 83 id: "NormalShipping", 84 label: "NormalShipping", 85 amount: { 86 currency: "USD", 87 value: "10.00" 88 }, 89 selected: false, 90 }, 91 { 92 id: "FastShipping", 93 label: "FastShipping", 94 amount: { 95 currency: "USD", 96 value: "30.00" 97 }, 98 selected: true, 99 }, 100 ], 101 }; 102 103 const nonSupportedMethods = [{ 104 supportedMethods: "nonsupported-method", 105 }]; 106 107 108 function updateWithShippingAddress() { 109 return new Promise((resolve, reject) => { 110 resolve(defaultDetails); 111 }); 112 } 113 114 function updateWithShippingOption() { 115 return new Promise((resolve, reject) => { 116 resolve(updatedOptionDetails); 117 }); 118 } 119 120 function testShow() { 121 gScript.sendAsyncMessage("set-normal-ui-service"); 122 return new Promise((resolve, reject) => { 123 const payRequest = new PaymentRequest(defaultMethods, defaultDetails, defaultOptions); 124 125 payRequest.addEventListener("shippingaddresschange", event => { 126 event.updateWith(updateWithShippingAddress()); 127 }); 128 payRequest.addEventListener("shippingoptionchange", event => { 129 event.updateWith(updateWithShippingOption()); 130 }); 131 132 const handler = SpecialPowers.getDOMWindowUtils(window).setHandlingUserInput(true); 133 payRequest.show().then(response => { 134 response.complete("success").then(() =>{ 135 resolve(); 136 }).catch(e => { 137 ok(false, "Unexpected error: " + e.name); 138 resolve(); 139 }); 140 }).catch( e => { 141 ok(false, "Unexpected error: " + e.name); 142 resolve(); 143 }).finally(handler.destruct); 144 }); 145 } 146 147 function teardown() { 148 ok(true, "Mandatory assert"); 149 gScript.addMessageListener("teardown-complete", function teardownCompleteHandler() { 150 gScript.removeMessageListener("teardown-complete", teardownCompleteHandler); 151 gScript.removeMessageListener("test-fail", testFailHandler) 152 gScript.destroy(); 153 SimpleTest.finish(); 154 }); 155 gScript.sendAsyncMessage("teardown"); 156 } 157 158 function runTests() { 159 testShow() 160 .then(teardown) 161 .catch( e => { 162 ok(false, "Unexpected error: " + e.name); 163 SimpleTest.finish(); 164 }); 165 } 166 167 window.addEventListener('load', function() { 168 SpecialPowers.pushPrefEnv({ 169 'set': [ 170 ['dom.payments.request.enabled', true], 171 ] 172 }, runTests); 173 }); 174 175 </script> 176 </head> 177 <body> 178 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1436903">Mozilla Bug 1436903</a> 179 </body> 180 </html>