Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / inspector / editor / editor-test.js
blobd357e7b018d975615a914b36f44f88d2a296301b
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);
11     return textEditor;
14 function textWithSelection(text, selections)
16     if (!selections.length)
17         return text;
19     function lineWithCursor(line, column, cursorChar)
20     {
21         return line.substring(0, column) + cursorChar + line.substring(column);
22     }
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, ">");
33         }
34     }
35     return lines.join("\n");
38 InspectorTest.dumpTextWithSelection = function(textEditor, dumpWhiteSpaces)
40     var text = textWithSelection(textEditor.text(), textEditor.selections());
41     if (dumpWhiteSpaces)
42         text = text.replace(/ /g, ".");
43     InspectorTest.addResult(text);
46 InspectorTest.setLineSelections = function(editor, selections)
48     var coords = [];
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;
54         }
55         coords.push(new WebInspector.TextRange(selection.line, selection.from, selection.line, selection.to));
56     }
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]) {
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     }
89 var eventCodes = {
90     enter: 13,
91     home: 36,
92     leftArrow: 37,
93     upArrow: 38,
94     rightArrow: 39,
95     downArrow: 40
98 function createCodeMirrorFakeEvent(eventType, code, charCode, modifiers)
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;
119 function fakeCodeMirrorKeyEvent(editor, eventType, code, charCode, modifiers)
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;
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 || [];
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 = 34;
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);
175     function callbackWrapper()
176     {
177         editor._codeMirror.off("inputRead", callbackWrapper);
178         callback();
179     }
180     editor._codeMirror.on("inputRead", callbackWrapper);
183 InspectorTest.dumpSelectionStats = function(textEditor)
185     var listHashMap = {};
186     var sortedKeys = [];
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);
194         } else {
195             ++listHashMap[text];
196         }
197     }
198     for (var i = 0; i < sortedKeys.length; ++i) {
199         var keyName = sortedKeys[i];
200         if (!keyName.length)
201             keyName = "<Empty string>";
202         else
203             keyName = "'" + keyName + "'";
204         InspectorTest.addResult(keyName + ": " + listHashMap[sortedKeys[i]]);
205     }