test_bug1086684.js (2699B)
1 "use strict"; 2 3 const { XPCShellContentUtils } = ChromeUtils.importESModule( 4 "resource://testing-common/XPCShellContentUtils.sys.mjs" 5 ); 6 7 XPCShellContentUtils.init(this); 8 9 const childFramePath = "/file_bug1086684.html"; 10 const childFrameURL = "http://example.com" + childFramePath; 11 12 const childFrameContents = `<!DOCTYPE html> 13 <html> 14 <head> 15 <meta charset="UTF-8"> 16 </head> 17 <body> 18 <div id="content"> 19 <input type="file" id="f"> 20 </div> 21 </body> 22 </html>`; 23 24 const server = XPCShellContentUtils.createHttpServer({ 25 hosts: ["example.com"], 26 }); 27 server.registerPathHandler(childFramePath, (request, response) => { 28 response.write(childFrameContents); 29 }); 30 31 function childFrameScript() { 32 /* eslint-env mozilla/frame-script */ 33 "use strict"; 34 35 let { MockFilePicker } = ChromeUtils.importESModule( 36 "resource://testing-common/MockFilePicker.sys.mjs" 37 ); 38 39 function parentReady(message) { 40 MockFilePicker.init(content.browsingContext); 41 MockFilePicker.setFiles([message.data.file]); 42 MockFilePicker.returnValue = MockFilePicker.returnOK; 43 44 let input = content.document.getElementById("f"); 45 input.addEventListener("change", () => { 46 MockFilePicker.cleanup(); 47 let value = input.value; 48 message.target.sendAsyncMessage("testBug1086684:childDone", { value }); 49 }); 50 51 // Activate the page to allow opening the file picker. 52 content.document.notifyUserGestureActivation(); 53 54 input.focus(); 55 input.click(); 56 } 57 58 addMessageListener("testBug1086684:parentReady", function (message) { 59 parentReady(message); 60 }); 61 } 62 63 add_task(async function () { 64 Services.prefs.setBoolPref("dom.security.https_first", false); 65 let page = await XPCShellContentUtils.loadContentPage(childFrameURL, { 66 remote: true, 67 }); 68 69 page.loadFrameScript(childFrameScript); 70 71 await new Promise(resolve => { 72 let test; 73 function* testStructure(mm) { 74 let value; 75 76 function testDone(msg) { 77 test.next(msg.data.value); 78 } 79 80 mm.addMessageListener("testBug1086684:childDone", testDone); 81 82 let blob = new Blob([]); 83 let file = new File([blob], "helloworld.txt", { type: "text/plain" }); 84 85 mm.sendAsyncMessage("testBug1086684:parentReady", { file }); 86 value = yield; 87 88 // Note that the "helloworld.txt" passed in above doesn't affect the 89 // 'value' getter. Because we're mocking a file using a blob, we ask the 90 // blob for its path, which is the empty string. 91 equal(value, "", "got the right answer and didn't crash"); 92 93 resolve(); 94 } 95 96 test = testStructure(page.browser.messageManager); 97 test.next(); 98 }); 99 100 await page.close(); 101 Services.prefs.clearUserPref("dom.security.https_first"); 102 });