1 function initialize_EditorTests()
4 InspectorTest.createTestEditor = function(clientHeight, textEditorDelegate)
6 var textEditor = new WebInspector.CodeMirrorTextEditor("", textEditorDelegate || new WebInspector.TextEditorDelegate());
7 clientHeight = clientHeight || 100;
8 textEditor.element.style.height = clientHeight + "px";
9 textEditor.element.style.flex = "none";
10 textEditor.show(WebInspector.inspectorView.element);
14 function textWithSelection(text, selections)
16 if (!selections.length)
19 function lineWithCursor(line, column, cursorChar)
21 return line.substring(0, column) + cursorChar + line.substring(column);
24 var lines = text.split("\n");
25 selections.sort(WebInspector.TextRange.comparator);
26 for (var i = selections.length - 1; i >= 0; --i) {
27 var selection = selections[i];
28 selection = selection.normalize();
29 var endCursorChar = selection.isEmpty() ? "|" : "<";
30 lines[selection.endLine] = lineWithCursor(lines[selection.endLine], selection.endColumn, endCursorChar);
31 if (!selection.isEmpty()) {
32 lines[selection.startLine] = lineWithCursor(lines[selection.startLine], selection.startColumn, ">");
35 return lines.join("\n");
38 InspectorTest.dumpTextWithSelection = function(textEditor, dumpWhiteSpaces)
40 var text = textWithSelection(textEditor.text(), textEditor.selections());
42 text = text.replace(/ /g, ".");
43 InspectorTest.addResult(text);
46 InspectorTest.setLineSelections = function(editor, selections)
49 for (var i = 0; i < selections.length; ++i) {
50 var selection = selections[i];
51 if (typeof selection.column === "number") {
52 selection.from = selection.column;
53 selection.to = selection.column;
55 coords.push(new WebInspector.TextRange(selection.line, selection.from, selection.line, selection.to));
57 editor.setSelections(coords);
60 InspectorTest.typeIn = function(editor, typeText, callback)
62 callback = callback || new Function();
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);
183 InspectorTest.dumpSelectionStats = function(textEditor)
185 var listHashMap = {};
187 var selections = textEditor.selections();
188 for (var i = 0; i < selections.length; ++i) {
189 var selection = selections[i];
190 var text = textEditor.copyRange(selection);
191 if (!listHashMap[text]) {
192 listHashMap[text] = 1;
193 sortedKeys.push(text);
198 for (var i = 0; i < sortedKeys.length; ++i) {
199 var keyName = sortedKeys[i];
201 keyName = "<Empty string>";
203 keyName = "'" + keyName + "'";
204 InspectorTest.addResult(keyName + ": " + listHashMap[sortedKeys[i]]);