Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / inspector / editor / editor-test.js
1 function initialize_EditorTests()
2 {
3
4 InspectorTest.createTestEditor = function(clientHeight, textEditorDelegate)
5 {
6     runtime.loadModule("source_frame");
7     var textEditor = new WebInspector.CodeMirrorTextEditor("", textEditorDelegate || new WebInspector.TextEditorDelegate());
8     clientHeight = clientHeight || 100;
9     textEditor.element.style.height = clientHeight + "px";
10     textEditor.element.style.flex = "none";
11     textEditor.show(WebInspector.inspectorView.element);
12     return textEditor;
13 };
14
15 function textWithSelection(text, selections)
16 {
17     if (!selections.length)
18         return text;
19
20     function lineWithCursor(line, column, cursorChar)
21     {
22         return line.substring(0, column) + cursorChar + line.substring(column);
23     }
24
25     var lines = text.split("\n");
26     selections.sort(WebInspector.TextRange.comparator);
27     for (var i = selections.length - 1; i >= 0; --i) {
28         var selection = selections[i];
29         selection = selection.normalize();
30         var endCursorChar = selection.isEmpty() ? "|" : "<";
31         lines[selection.endLine] = lineWithCursor(lines[selection.endLine], selection.endColumn, endCursorChar);
32         if (!selection.isEmpty()) {
33             lines[selection.startLine] = lineWithCursor(lines[selection.startLine], selection.startColumn, ">");
34         }
35     }
36     return lines.join("\n");
37 }
38
39 InspectorTest.dumpTextWithSelection = function(textEditor, dumpWhiteSpaces)
40 {
41     var text = textWithSelection(textEditor.text(), textEditor.selections());
42     if (dumpWhiteSpaces)
43         text = text.replace(/ /g, ".");
44     InspectorTest.addResult(text);
45 }
46
47 InspectorTest.setLineSelections = function(editor, selections)
48 {
49     var coords = [];
50     for (var i = 0; i < selections.length; ++i) {
51         var selection = selections[i];
52         if (typeof selection.column === "number") {
53             selection.from = selection.column;
54             selection.to = selection.column;
55         }
56         coords.push(new WebInspector.TextRange(selection.line, selection.from, selection.line, selection.to));
57     }
58     editor.setSelections(coords);
59 }
60
61 InspectorTest.typeIn = function(editor, typeText, callback)
62 {
63     var noop = new Function();
64     for(var charIndex = 0; charIndex < typeText.length; ++charIndex) {
65         // As soon as the last key event was processed, the whole text was processed.
66         var iterationCallback = charIndex + 1 === typeText.length ? callback : noop;
67         switch (typeText[charIndex]) {
68         case "\n":
69             InspectorTest.fakeKeyEvent(editor, "enter", null, iterationCallback);
70             break;
71         case "L":
72             InspectorTest.fakeKeyEvent(editor, "leftArrow", null, iterationCallback);
73             break;
74         case "R":
75             InspectorTest.fakeKeyEvent(editor, "rightArrow", null, iterationCallback);
76             break;
77         case "U":
78             InspectorTest.fakeKeyEvent(editor, "upArrow", null, iterationCallback);
79             break;
80         case "D":
81             InspectorTest.fakeKeyEvent(editor, "downArrow", null, iterationCallback);
82             break;
83         default:
84             InspectorTest.fakeKeyEvent(editor, typeText[charIndex], null, iterationCallback);
85         }
86     }
87 }
88
89 var eventCodes = {
90     enter: 13,
91     home: 36,
92     leftArrow: 37,
93     upArrow: 38,
94     rightArrow: 39,
95     downArrow: 40
96 };
97
98 function createCodeMirrorFakeEvent(eventType, code, charCode, modifiers)
99 {
100     function eventPreventDefault()
101     {
102         this._handled = true;
103     }
104     var event = {
105         _handled: false,
106         type: eventType,
107         keyCode: code,
108         charCode: charCode,
109         preventDefault: eventPreventDefault,
110         stopPropagation: function(){},
111     };
112     if (modifiers) {
113         for (var i = 0; i < modifiers.length; ++i)
114             event[modifiers[i]] = true;
115     }
116     return event;
117 }
118
119 function fakeCodeMirrorKeyEvent(editor, eventType, code, charCode, modifiers)
120 {
121     var event = createCodeMirrorFakeEvent(eventType, code, charCode, modifiers);
122     switch(eventType) {
123     case "keydown":
124         editor._codeMirror.triggerOnKeyDown(event);
125         break;
126     case "keypress":
127         editor._codeMirror.triggerOnKeyPress(event);
128         break;
129     case "keyup":
130         editor._codeMirror.triggerOnKeyUp(event);
131         break;
132     default:
133         throw new Error("Unknown KeyEvent type");
134     }
135     return event._handled;
136 }
137
138 function fakeCodeMirrorInputEvent(editor, character)
139 {
140     if (typeof character === "string")
141         editor._codeMirror.display.input.value += character;
142 }
143
144 InspectorTest.fakeKeyEvent = function(editor, originalCode, modifiers, callback)
145 {
146     modifiers = modifiers || [];
147     var code;
148     var charCode;
149     if (originalCode === "'") {
150         code = 222;
151         charCode = 0;
152     } else if (originalCode === "\"") {
153         code = 222;
154         modifiers.push("shiftKey");
155         charCode = 0;
156     } else if (originalCode === "(") {
157         code = "9".charCodeAt(0);
158         modifiers.push("shiftKey");
159         charCode = originalCode.charCodeAt(0);
160     }
161     var code = code || eventCodes[originalCode] || originalCode;
162     if (typeof code === "string")
163         code = code.charCodeAt(0);
164     if (fakeCodeMirrorKeyEvent(editor, "keydown", code, charCode, modifiers)) {
165         callback();
166         return;
167     }
168     if (fakeCodeMirrorKeyEvent(editor, "keypress", code, charCode, modifiers)) {
169         callback();
170         return;
171     }
172     fakeCodeMirrorInputEvent(editor, originalCode);
173     fakeCodeMirrorKeyEvent(editor, "keyup", code, charCode, modifiers);
174
175     function callbackWrapper()
176     {
177         editor._codeMirror.off("inputRead", callbackWrapper);
178         callback();
179     }
180     editor._codeMirror.on("inputRead", callbackWrapper);
181 }
182
183 }