test_file_blob_upload.html (4350B)
1 <!-- 2 Any copyright is dedicated to the Public Domain. 3 http://creativecommons.org/publicdomain/zero/1.0/ 4 --> 5 <!DOCTYPE HTML> 6 <html> 7 <head> 8 <title>Bug 1203680 - Test interception of file blob uploads</title> 9 <script src="/tests/SimpleTest/SimpleTest.js"></script> 10 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 11 </head> 12 <body> 13 <p id="display"></p> 14 <div id="content" style="display: none"></div> 15 <pre id="test"></pre> 16 <script src="utils.js"></script> 17 <script class="testbody" type="text/javascript"> 18 var registration; 19 var iframe; 20 function start() { 21 return navigator.serviceWorker.register("empty.js", 22 { scope: "./sw_clients/" }) 23 .then((swr) => { 24 registration = swr 25 return waitForState(swr.installing, 'activated', swr); 26 }); 27 } 28 29 function unregister() { 30 if (iframe) { 31 iframe.remove(); 32 iframe = null; 33 } 34 35 return registration.unregister().then(function(result) { 36 ok(result, "Unregister should return true."); 37 }, function(e) { 38 ok(false, "Unregistering the SW failed with " + e + "\n"); 39 }); 40 } 41 42 function withFrame() { 43 var content = document.getElementById("content"); 44 ok(content, "Parent exists."); 45 46 iframe = document.createElement("iframe"); 47 iframe.setAttribute('src', "sw_clients/file_blob_upload_frame.html"); 48 content.appendChild(iframe); 49 50 return new Promise(function(resolve, reject) { 51 window.addEventListener('message', function(evt) { 52 if (evt.data.status === 'READY') { 53 resolve(); 54 } else { 55 reject(evt.data.result); 56 } 57 }, {once: true}); 58 }); 59 } 60 61 function postBlob(body) { 62 return new Promise(function(resolve, reject) { 63 window.addEventListener('message', function(evt) { 64 if (evt.data.status === 'OK') { 65 is(JSON.stringify(body), JSON.stringify(evt.data.result), 66 'body echoed back correctly'); 67 resolve(); 68 } else { 69 reject(evt.data.result); 70 } 71 }, {once: true}); 72 73 iframe.contentWindow.postMessage({ type: 'TEST', body }, '*'); 74 }); 75 } 76 77 function generateMessage(length) { 78 79 var lorem = 80 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis egestas ' 81 'vehicula tortor eget ultrices. Sed et luctus est. Nunc eu orci ligula. ' 82 'In vel ornare eros, eget lacinia diam. Praesent vel metus mattis, ' 83 'cursus nulla sit amet, rhoncus diam. Aliquam nulla tortor, aliquet et ' 84 'viverra non, dignissim vel tellus. Praesent sed ex in dolor aliquet ' 85 'aliquet. In at facilisis sem, et aliquet eros. Maecenas feugiat nisl ' 86 'quis elit blandit posuere. Duis viverra odio sed eros consectetur, ' 87 'viverra mattis ligula volutpat.'; 88 89 var result = ''; 90 91 while (result.length < length) { 92 var remaining = length - result.length; 93 if (remaining < lorem.length) { 94 result += lorem.slice(0, remaining); 95 } else { 96 result += lorem; 97 } 98 } 99 100 return result; 101 } 102 103 var smallBody = generateMessage(64); 104 var mediumBody = generateMessage(1024); 105 106 // TODO: Test large bodies over the default pipe size. Currently stalls 107 // due to bug 1134372. 108 //var largeBody = generateMessage(100 * 1024); 109 110 function runTest() { 111 start() 112 .then(withFrame) 113 .then(function() { 114 return postBlob({ hops: 0, message: smallBody }); 115 }) 116 .then(function() { 117 return postBlob({ hops: 1, message: smallBody }); 118 }) 119 .then(function() { 120 return postBlob({ hops: 10, message: smallBody }); 121 }) 122 .then(function() { 123 return postBlob({ hops: 0, message: mediumBody }); 124 }) 125 .then(function() { 126 return postBlob({ hops: 1, message: mediumBody }); 127 }) 128 .then(function() { 129 return postBlob({ hops: 10, message: mediumBody }); 130 }) 131 .then(unregister) 132 .catch(function(e) { 133 ok(false, "Some test failed with error " + e); 134 }).then(SimpleTest.finish); 135 } 136 137 SimpleTest.waitForExplicitFinish(); 138 SpecialPowers.pushPrefEnv({"set": [ 139 ["dom.serviceWorkers.exemptFromPerDomainMax", true], 140 ["dom.serviceWorkers.enabled", true], 141 ["dom.serviceWorkers.testing.enabled", true] 142 ]}, runTest); 143 </script> 144 </pre> 145 </body> 146 </html>