test_extractLabelStrings.js (2368B)
1 "use strict"; 2 3 var { LabelUtils } = ChromeUtils.importESModule( 4 "resource://gre/modules/shared/LabelUtils.sys.mjs" 5 ); 6 7 const TESTCASES = [ 8 { 9 description: "A label element contains one input element.", 10 document: `<label id="typeA"> label type A 11 <!-- This comment should not be extracted. --> 12 <input type="text"> 13 <script>FOO</script> 14 <noscript>FOO</noscript> 15 <option>FOO</option> 16 <style>FOO</style> 17 </label>`, 18 inputId: "typeA", 19 expectedStrings: ["label type A"], 20 }, 21 { 22 description: "A label element with inner div contains one input element.", 23 document: `<label id="typeB"> label type B 24 <!-- This comment should not be extracted. --> 25 <script>FOO</script> 26 <noscript>FOO</noscript> 27 <option>FOO</option> 28 <style>FOO</style> 29 <div> inner div 30 <input type="text"> 31 </div> 32 </label>`, 33 inputId: "typeB", 34 expectedStrings: ["label type B", "inner div"], 35 }, 36 { 37 description: 38 "A label element with inner prefix/postfix strings contains span elements.", 39 document: `<label id="typeC"> label type C 40 <!-- This comment should not be extracted. --> 41 <script>FOO</script> 42 <noscript>FOO</noscript> 43 <option>FOO</option> 44 <style>FOO</style> 45 <div> inner div prefix 46 <span>test C-1 </span> 47 <span> test C-2</span> 48 inner div postfix 49 </div> 50 </label>`, 51 inputId: "typeC", 52 expectedStrings: [ 53 "label type C", 54 "inner div prefix", 55 "test C-1", 56 "test C-2", 57 "inner div postfix", 58 ], 59 }, 60 ]; 61 62 TESTCASES.forEach(testcase => { 63 add_task(async function () { 64 info("Starting testcase: " + testcase.description); 65 LabelUtils._labelStrings = new WeakMap(); 66 67 let doc = MockDocument.createTestDocument( 68 "http://localhost:8080/test/", 69 testcase.document 70 ); 71 72 let element = doc.getElementById(testcase.inputId); 73 let strings = LabelUtils.extractLabelStrings(element); 74 75 Assert.deepEqual(strings, testcase.expectedStrings); 76 }); 77 });