michael@0: /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: "use strict"; michael@0: michael@0: // Test the inplace-editor behavior. michael@0: // This test doesn't open the devtools, it just exercises the inplace-editor michael@0: // on test elements in the page michael@0: michael@0: let test = asyncTest(function*() { michael@0: yield addTab("data:text/html,inline editor tests"); michael@0: yield testReturnCommit(); michael@0: yield testBlurCommit(); michael@0: yield testAdvanceCharCommit(); michael@0: }); michael@0: michael@0: function testReturnCommit() { michael@0: info("Testing that pressing return commits the new value"); michael@0: let def = promise.defer(); michael@0: michael@0: createInplaceEditorAndClick({ michael@0: initial: "explicit initial", michael@0: start: function(editor) { michael@0: is(editor.input.value, "explicit initial", "Explicit initial value should be used."); michael@0: editor.input.value = "Test Value"; michael@0: EventUtils.sendKey("return"); michael@0: }, michael@0: done: onDone("Test Value", true, def) michael@0: }); michael@0: michael@0: return def.promise; michael@0: } michael@0: michael@0: function testBlurCommit() { michael@0: info("Testing that bluring the field commits the new value"); michael@0: let def = promise.defer(); michael@0: michael@0: createInplaceEditorAndClick({ michael@0: start: function(editor) { michael@0: is(editor.input.value, "Edit Me!", "textContent of the span used."); michael@0: editor.input.value = "Test Value"; michael@0: editor.input.blur(); michael@0: }, michael@0: done: onDone("Test Value", true, def) michael@0: }); michael@0: michael@0: return def.promise; michael@0: } michael@0: michael@0: function testAdvanceCharCommit() { michael@0: info("Testing that configured advanceChars commit the new value"); michael@0: let def = promise.defer(); michael@0: michael@0: createInplaceEditorAndClick({ michael@0: advanceChars: ":", michael@0: start: function(editor) { michael@0: let input = editor.input; michael@0: for each (let ch in "Test:") { michael@0: EventUtils.sendChar(ch); michael@0: } michael@0: }, michael@0: done: onDone("Test", true, def) michael@0: }); michael@0: michael@0: return def.promise; michael@0: } michael@0: michael@0: function testEscapeCancel() { michael@0: info("Testing that escape cancels the new value"); michael@0: let def = promise.defer(); michael@0: michael@0: createInplaceEditorAndClick({ michael@0: initial: "initial text", michael@0: start: function(editor) { michael@0: editor.input.value = "Test Value"; michael@0: EventUtils.sendKey("escape"); michael@0: }, michael@0: done: onDone("initial text", false, def) michael@0: }); michael@0: michael@0: return def.promise; michael@0: } michael@0: michael@0: function onDone(value, isCommit, def) { michael@0: return function(actualValue, actualCommit) { michael@0: info("Inplace-editor's done callback executed, checking its state"); michael@0: is(actualValue, value, "The value is correct"); michael@0: is(actualCommit, isCommit, "The commit boolean is correct"); michael@0: def.resolve(); michael@0: } michael@0: } michael@0: michael@0: function createInplaceEditorAndClick(options) { michael@0: clearBody(); michael@0: let span = options.element = createSpan(); michael@0: michael@0: info("Creating an inplace-editor field"); michael@0: editableField(options); michael@0: michael@0: info("Clicking on the inplace-editor field to turn to edit mode"); michael@0: span.click(); michael@0: } michael@0: michael@0: function clearBody() { michael@0: info("Clearing the page body"); michael@0: content.document.body.innerHTML = ""; michael@0: } michael@0: michael@0: function createSpan() { michael@0: info("Creating a new span element"); michael@0: let span = content.document.createElement("span"); michael@0: span.setAttribute("tabindex", "0"); michael@0: span.textContent = "Edit Me!"; michael@0: content.document.body.appendChild(span); michael@0: return span; michael@0: }