browser_file_input.js (3150B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 /* import-globals-from ../../mochitest/name.js */ 8 loadScripts({ name: "name.js", dir: MOCHITESTS_DIR }); 9 10 addAccessibleTask( 11 ` 12 <input type="file" id="noName"> 13 <input type="file" id="ariaLabel" aria-label="ariaLabel"> 14 <label>wrappingLabel <input type="file" id="wrappingLabel"></label> 15 <label for="labelFor">labelFor</label> <input type="file" id="labelFor"> 16 <input type="file" id="title" title="title"> 17 `, 18 async function (browser, docAcc) { 19 const browseButton = "Browse…"; 20 const noFileSuffix = `${browseButton} No file selected.`; 21 const noName = findAccessibleChildByID(docAcc, "noName"); 22 testName(noName, noFileSuffix); 23 const ariaLabel = findAccessibleChildByID(docAcc, "ariaLabel"); 24 testName(ariaLabel, `ariaLabel ${noFileSuffix}`); 25 const wrappingLabel = findAccessibleChildByID(docAcc, "wrappingLabel"); 26 testName(wrappingLabel, `wrappingLabel ${noFileSuffix}`); 27 const labelFor = findAccessibleChildByID(docAcc, "labelFor"); 28 testName(labelFor, `labelFor ${noFileSuffix}`); 29 const title = findAccessibleChildByID(docAcc, "title"); 30 testName(title, noFileSuffix); 31 testDescr(title, "title"); 32 33 // Test that the name of the button changes correctly when a file is chosen. 34 function chooseFile(id) { 35 return invokeContentTask(browser, [id], contentId => { 36 const MockFilePicker = content.SpecialPowers.MockFilePicker; 37 MockFilePicker.init(content.browsingContext); 38 MockFilePicker.useBlobFile(); 39 MockFilePicker.returnValue = MockFilePicker.returnOK; 40 const input = content.document.getElementById(contentId); 41 const inputReceived = new Promise(resolve => 42 input.addEventListener( 43 "input", 44 event => { 45 MockFilePicker.cleanup(); 46 resolve(event.target.files[0].name); 47 }, 48 { once: true } 49 ) 50 ); 51 52 // Activate the page to allow opening the file picker. 53 content.SpecialPowers.wrap( 54 content.document 55 ).notifyUserGestureActivation(); 56 57 input.click(); 58 59 return inputReceived; 60 }); 61 } 62 63 info("noName: Choosing file"); 64 let nameChanged = waitForEvent(EVENT_NAME_CHANGE, "noName"); 65 const fn = await chooseFile("noName"); 66 // e.g. "Browse…helloworld.txt" 67 const withFileSuffix = `${browseButton} ${fn}`; 68 await nameChanged; 69 testName(noName, withFileSuffix); 70 71 info("ariaLabel: Choosing file"); 72 nameChanged = waitForEvent(EVENT_NAME_CHANGE, "ariaLabel"); 73 await chooseFile("ariaLabel"); 74 await nameChanged; 75 testName(ariaLabel, `ariaLabel ${withFileSuffix}`); 76 77 info("wrappingLabel: Choosing file"); 78 nameChanged = waitForEvent(EVENT_NAME_CHANGE, "wrappingLabel"); 79 await chooseFile("wrappingLabel"); 80 await nameChanged; 81 testName(wrappingLabel, `wrappingLabel ${withFileSuffix}`); 82 }, 83 { topLevel: true, chrome: true } 84 );