Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* vim: set ft=javascript ts=2 et sw=2 tw=80: */ |
michael@0 | 2 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 3 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 4 | |
michael@0 | 5 | "use strict"; |
michael@0 | 6 | |
michael@0 | 7 | // Test the inplace-editor behavior. |
michael@0 | 8 | // This test doesn't open the devtools, it just exercises the inplace-editor |
michael@0 | 9 | // on test elements in the page |
michael@0 | 10 | |
michael@0 | 11 | let test = asyncTest(function*() { |
michael@0 | 12 | yield addTab("data:text/html,inline editor tests"); |
michael@0 | 13 | yield testReturnCommit(); |
michael@0 | 14 | yield testBlurCommit(); |
michael@0 | 15 | yield testAdvanceCharCommit(); |
michael@0 | 16 | }); |
michael@0 | 17 | |
michael@0 | 18 | function testReturnCommit() { |
michael@0 | 19 | info("Testing that pressing return commits the new value"); |
michael@0 | 20 | let def = promise.defer(); |
michael@0 | 21 | |
michael@0 | 22 | createInplaceEditorAndClick({ |
michael@0 | 23 | initial: "explicit initial", |
michael@0 | 24 | start: function(editor) { |
michael@0 | 25 | is(editor.input.value, "explicit initial", "Explicit initial value should be used."); |
michael@0 | 26 | editor.input.value = "Test Value"; |
michael@0 | 27 | EventUtils.sendKey("return"); |
michael@0 | 28 | }, |
michael@0 | 29 | done: onDone("Test Value", true, def) |
michael@0 | 30 | }); |
michael@0 | 31 | |
michael@0 | 32 | return def.promise; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | function testBlurCommit() { |
michael@0 | 36 | info("Testing that bluring the field commits the new value"); |
michael@0 | 37 | let def = promise.defer(); |
michael@0 | 38 | |
michael@0 | 39 | createInplaceEditorAndClick({ |
michael@0 | 40 | start: function(editor) { |
michael@0 | 41 | is(editor.input.value, "Edit Me!", "textContent of the span used."); |
michael@0 | 42 | editor.input.value = "Test Value"; |
michael@0 | 43 | editor.input.blur(); |
michael@0 | 44 | }, |
michael@0 | 45 | done: onDone("Test Value", true, def) |
michael@0 | 46 | }); |
michael@0 | 47 | |
michael@0 | 48 | return def.promise; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | function testAdvanceCharCommit() { |
michael@0 | 52 | info("Testing that configured advanceChars commit the new value"); |
michael@0 | 53 | let def = promise.defer(); |
michael@0 | 54 | |
michael@0 | 55 | createInplaceEditorAndClick({ |
michael@0 | 56 | advanceChars: ":", |
michael@0 | 57 | start: function(editor) { |
michael@0 | 58 | let input = editor.input; |
michael@0 | 59 | for each (let ch in "Test:") { |
michael@0 | 60 | EventUtils.sendChar(ch); |
michael@0 | 61 | } |
michael@0 | 62 | }, |
michael@0 | 63 | done: onDone("Test", true, def) |
michael@0 | 64 | }); |
michael@0 | 65 | |
michael@0 | 66 | return def.promise; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | function testEscapeCancel() { |
michael@0 | 70 | info("Testing that escape cancels the new value"); |
michael@0 | 71 | let def = promise.defer(); |
michael@0 | 72 | |
michael@0 | 73 | createInplaceEditorAndClick({ |
michael@0 | 74 | initial: "initial text", |
michael@0 | 75 | start: function(editor) { |
michael@0 | 76 | editor.input.value = "Test Value"; |
michael@0 | 77 | EventUtils.sendKey("escape"); |
michael@0 | 78 | }, |
michael@0 | 79 | done: onDone("initial text", false, def) |
michael@0 | 80 | }); |
michael@0 | 81 | |
michael@0 | 82 | return def.promise; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | function onDone(value, isCommit, def) { |
michael@0 | 86 | return function(actualValue, actualCommit) { |
michael@0 | 87 | info("Inplace-editor's done callback executed, checking its state"); |
michael@0 | 88 | is(actualValue, value, "The value is correct"); |
michael@0 | 89 | is(actualCommit, isCommit, "The commit boolean is correct"); |
michael@0 | 90 | def.resolve(); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | function createInplaceEditorAndClick(options) { |
michael@0 | 95 | clearBody(); |
michael@0 | 96 | let span = options.element = createSpan(); |
michael@0 | 97 | |
michael@0 | 98 | info("Creating an inplace-editor field"); |
michael@0 | 99 | editableField(options); |
michael@0 | 100 | |
michael@0 | 101 | info("Clicking on the inplace-editor field to turn to edit mode"); |
michael@0 | 102 | span.click(); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | function clearBody() { |
michael@0 | 106 | info("Clearing the page body"); |
michael@0 | 107 | content.document.body.innerHTML = ""; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | function createSpan() { |
michael@0 | 111 | info("Creating a new span element"); |
michael@0 | 112 | let span = content.document.createElement("span"); |
michael@0 | 113 | span.setAttribute("tabindex", "0"); |
michael@0 | 114 | span.textContent = "Edit Me!"; |
michael@0 | 115 | content.document.body.appendChild(span); |
michael@0 | 116 | return span; |
michael@0 | 117 | } |