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