browser_input_file_picker_opening_notification.js (2684B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const TEST_URL = 7 "https://example.org/document-builder.sjs?html=<input type='file'>"; 8 9 const MockFilePicker = SpecialPowers.MockFilePicker; 10 MockFilePicker.init(window.browsingContext); 11 12 add_task(async _ => { 13 registerCleanupFunction(() => MockFilePicker.cleanup()); 14 15 await testPickerOpeningNotification(gBrowser, { method: "input.click()" }); 16 await testPickerOpeningNotification(gBrowser, { 17 method: "input.showPicker()", 18 }); 19 }); 20 21 async function testPickerOpeningNotification(gBrowser, { method }) { 22 info("Test the input-file-picker-opening notification for method=" + method); 23 await BrowserTestUtils.withNewTab( 24 { 25 gBrowser, 26 url: TEST_URL, 27 }, 28 async function (browser) { 29 const onPickerCanceled = new Promise(resolve => { 30 MockFilePicker.showCallback = function () { 31 resolve(); 32 return Ci.nsIFilePicker.returnCancel; 33 }; 34 }); 35 await SpecialPowers.spawn(browser, [method], async function (_method) { 36 const fileInputElement = content.document.querySelector("input"); 37 38 const waitForPickerOpeningNotification = () => { 39 return new Promise(resolve => { 40 Services.obs.addObserver(function onPickerOpening(subject) { 41 info( 42 "Received a file-input-picker-opening notification for subject: " + 43 subject.outerHTML 44 ); 45 is( 46 subject, 47 fileInputElement, 48 "Notification received for the expected input element" 49 ); 50 51 Services.obs.removeObserver( 52 onPickerOpening, 53 "file-input-picker-opening" 54 ); 55 resolve(); 56 }, "file-input-picker-opening"); 57 }); 58 }; 59 60 let onPickerOpeningNotification = waitForPickerOpeningNotification(); 61 content.document.notifyUserGestureActivation(); 62 63 info("Trigger the file picker to be displayed via " + _method); 64 if (_method == "input.click()") { 65 fileInputElement.click(); 66 } else if (_method == "input.showPicker()") { 67 fileInputElement.showPicker(); 68 } else { 69 throw new Error( 70 "Unsupported method for testPickerOpeningNotification: " + _method 71 ); 72 } 73 info("Wait for the mock file picker showCallback to cancel the picker"); 74 await onPickerOpeningNotification; 75 }); 76 77 info("Wait for the file-input-picker-opening notification"); 78 await onPickerCanceled; 79 } 80 ); 81 }