1 function initialize_EditorTests()
4 InspectorTest.createTestEditor = function(clientHeight, textEditorDelegate)
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);
15 function textWithSelection(text, selections)
17 if (!selections.length)
20 function lineWithCursor(line, column, cursorChar)
22 return line.substring(0, column) + cursorChar + line.substring(column);
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, ">");
36 return lines.join("\n");
39 InspectorTest.dumpTextWithSelection = function(textEditor, dumpWhiteSpaces)
41 var text = textWithSelection(textEditor.text(), textEditor.selections());
43 text = text.replace(/ /g, ".");
44 InspectorTest.addResult(text);
47 InspectorTest.setLineSelections = function(editor, selections)
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;
56 coords.push(new WebInspector.TextRange(selection.line, selection.from, selection.line, selection.to));
58 editor.setSelections(coords);
61 InspectorTest.typeIn = function(editor, typeText, callback)
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]) {
69 InspectorTest.fakeKeyEvent(editor, "enter", null, iterationCallback);
72 InspectorTest.fakeKeyEvent(editor, "leftArrow", null, iterationCallback);
75 InspectorTest.fakeKeyEvent(editor, "rightArrow", null, iterationCallback);
78 InspectorTest.fakeKeyEvent(editor, "upArrow", null, iterationCallback);
81 InspectorTest.fakeKeyEvent(editor, "downArrow", null, iterationCallback);
84 InspectorTest.fakeKeyEvent(editor, typeText[charIndex], null, iterationCallback);
98 function createCodeMirrorFakeEvent(eventType, code, charCode, modifiers)
100 function eventPreventDefault()
102 this._handled = true;
109 preventDefault: eventPreventDefault,
110 stopPropagation: function(){},
113 for (var i = 0; i < modifiers.length; ++i)
114 event[modifiers[i]] = true;
119 function fakeCodeMirrorKeyEvent(editor, eventType, code, charCode, modifiers)
121 var event = createCodeMirrorFakeEvent(eventType, code, charCode, modifiers);
124 editor._codeMirror.triggerOnKeyDown(event);
127 editor._codeMirror.triggerOnKeyPress(event);
130 editor._codeMirror.triggerOnKeyUp(event);
133 throw new Error("Unknown KeyEvent type");
135 return event._handled;
138 function fakeCodeMirrorInputEvent(editor, character)
140 if (typeof character === "string")
141 editor._codeMirror.display.input.value += character;
144 InspectorTest.fakeKeyEvent = function(editor, originalCode, modifiers, callback)
146 modifiers = modifiers || [];
149 if (originalCode === "'") {
152 } else if (originalCode === "\"") {
154 modifiers.push("shiftKey");
156 } else if (originalCode === "(") {
157 code = "9".charCodeAt(0);
158 modifiers.push("shiftKey");
159 charCode = originalCode.charCodeAt(0);
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)) {
168 if (fakeCodeMirrorKeyEvent(editor, "keypress", code, charCode, modifiers)) {
172 fakeCodeMirrorInputEvent(editor, originalCode);
173 fakeCodeMirrorKeyEvent(editor, "keyup", code, charCode, modifiers);
175 function callbackWrapper()
177 editor._codeMirror.off("inputRead", callbackWrapper);
180 editor._codeMirror.on("inputRead", callbackWrapper);