browser_inplace-editor-01.js (5544B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 /* import-globals-from helper_inplace_editor.js */ 4 5 "use strict"; 6 7 loadHelperScript("helper_inplace_editor.js"); 8 9 // Test the inplace-editor behavior. 10 11 add_task(async function () { 12 await addTab("data:text/html;charset=utf-8,inline editor tests"); 13 const { host, doc } = await createHost(); 14 15 await testMultipleInitialization(doc); 16 await testReturnCommit(doc); 17 await testBlurCommit(doc); 18 await testAdvanceCharCommit(doc); 19 await testAdvanceCharsFunction(doc); 20 await testEscapeCancel(doc); 21 await testInputAttributes(doc); 22 23 host.destroy(); 24 gBrowser.removeCurrentTab(); 25 }); 26 27 function testMultipleInitialization(doc) { 28 doc.body.innerHTML = ""; 29 const options = {}; 30 const span = (options.element = createSpan(doc)); 31 32 info("Creating multiple inplace-editor fields"); 33 editableField(options); 34 editableField(options); 35 36 info("Clicking on the inplace-editor field to turn to edit mode"); 37 span.click(); 38 39 is(span.style.display, "none", "The original <span> is hidden"); 40 is(doc.querySelectorAll("input").length, 1, "Only one <input>"); 41 is( 42 doc.querySelectorAll("span").length, 43 2, 44 "Correct number of <span> elements" 45 ); 46 is( 47 doc.querySelectorAll("span.autosizer").length, 48 1, 49 "There is an autosizer element" 50 ); 51 } 52 53 function testReturnCommit(doc) { 54 info("Testing that pressing return commits the new value"); 55 return new Promise(resolve => { 56 createInplaceEditorAndClick( 57 { 58 initial: "explicit initial", 59 start(editor) { 60 is( 61 editor.input.value, 62 "explicit initial", 63 "Explicit initial value should be used." 64 ); 65 editor.input.value = "Test Value"; 66 EventUtils.sendKey("return"); 67 }, 68 done: onDone("Test Value", true, resolve), 69 }, 70 doc 71 ); 72 }); 73 } 74 75 function testBlurCommit(doc) { 76 info("Testing that bluring the field commits the new value"); 77 return new Promise(resolve => { 78 createInplaceEditorAndClick( 79 { 80 start(editor) { 81 is(editor.input.value, "Edit Me!", "textContent of the span used."); 82 editor.input.value = "Test Value"; 83 editor.input.blur(); 84 }, 85 done: onDone("Test Value", true, resolve), 86 }, 87 doc, 88 "Edit Me!" 89 ); 90 }); 91 } 92 93 function testAdvanceCharCommit(doc) { 94 info("Testing that configured advanceChars commit the new value"); 95 return new Promise(resolve => { 96 createInplaceEditorAndClick( 97 { 98 advanceChars: ":", 99 start() { 100 EventUtils.sendString("Test:"); 101 }, 102 done: onDone("Test", true, resolve), 103 }, 104 doc 105 ); 106 }); 107 } 108 109 function testAdvanceCharsFunction(doc) { 110 info("Testing advanceChars as a function"); 111 return new Promise(resolve => { 112 let firstTime = true; 113 114 createInplaceEditorAndClick( 115 { 116 initial: "", 117 advanceChars(charCode, text) { 118 if (charCode !== KeyboardEvent.DOM_VK_COLON) { 119 return false; 120 } 121 if (firstTime) { 122 firstTime = false; 123 return false; 124 } 125 126 // Just to make sure we check it somehow. 127 return !!text.length; 128 }, 129 start() { 130 for (const ch of ":Test:") { 131 EventUtils.sendChar(ch); 132 } 133 }, 134 done: onDone(":Test", true, resolve), 135 }, 136 doc 137 ); 138 }); 139 } 140 141 function testEscapeCancel(doc) { 142 info("Testing that escape cancels the new value"); 143 return new Promise(resolve => { 144 createInplaceEditorAndClick( 145 { 146 initial: "initial text", 147 start(editor) { 148 editor.input.value = "Test Value"; 149 EventUtils.sendKey("escape"); 150 }, 151 done: onDone("initial text", false, resolve), 152 }, 153 doc 154 ); 155 }); 156 } 157 158 function testInputAttributes(doc) { 159 info("Testing that inputAriaLabel works as expected"); 160 doc.body.innerHTML = ""; 161 162 let element = createSpan(doc); 163 editableField({ 164 element, 165 inputAriaLabel: "TEST_ARIA_LABEL", 166 }); 167 168 info("Clicking on the inplace-editor field to turn to edit mode"); 169 element.click(); 170 let input = doc.querySelector("input"); 171 is( 172 input.getAttribute("aria-label"), 173 "TEST_ARIA_LABEL", 174 "Input has expected aria-label" 175 ); 176 177 info("Testing that inputAriaLabelledBy works as expected"); 178 doc.body.innerHTML = ""; 179 element = createSpan(doc); 180 editableField({ 181 element, 182 inputAriaLabelledBy: "TEST_ARIA_LABELLED_BY", 183 }); 184 185 info("Clicking on the inplace-editor field to turn to edit mode"); 186 element.click(); 187 input = doc.querySelector("input"); 188 is( 189 input.getAttribute("aria-labelledby"), 190 "TEST_ARIA_LABELLED_BY", 191 "Input has expected aria-labelledby" 192 ); 193 194 info("Testing that inputClass works as expected"); 195 doc.body.innerHTML = ""; 196 element = createSpan(doc); 197 editableField({ 198 element, 199 inputClass: "TEST_INPUT_CLASS", 200 }); 201 202 info("Clicking on the inplace-editor field to turn to edit mode"); 203 element.click(); 204 input = doc.querySelector("input"); 205 ok( 206 input.classList.contains("TEST_INPUT_CLASS"), 207 "Input has expected TEST_INPUT_CLASS class" 208 ); 209 } 210 211 function onDone(value, isCommit, resolve) { 212 return function (actualValue, actualCommit) { 213 info("Inplace-editor's done callback executed, checking its state"); 214 is(actualValue, value, "The value is correct"); 215 is(actualCommit, isCommit, "The commit boolean is correct"); 216 resolve(); 217 }; 218 }