blob: a693414fc8065288f58445d0516ccbd2a74cee99 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright 2017 The Chromium Authors. All
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5/**
6 * @fileoverview using private properties isn't a Closure violation in tests.
7 * @suppress {accessControls}
8 */
9
10SourcesTestRunner.createTestEditor = function(clientHeight, textEditorDelegate) {
11 const textEditor =
12 new SourceFrame.SourcesTextEditor(textEditorDelegate || new SourceFrame.SourcesTextEditorDelegate());
13 clientHeight = clientHeight || 100;
14 textEditor.element.style.height = clientHeight + 'px';
15 textEditor.element.style.flex = 'none';
16 textEditor.show(UI.inspectorView.element);
17 return textEditor;
18};
19
20function textWithSelection(text, selections) {
21 if (!selections.length)
22 return text;
23
24 function lineWithCursor(line, column, cursorChar) {
25 return line.substring(0, column) + cursorChar + line.substring(column);
26 }
27
28 const lines = text.split('\n');
29 selections.sort(TextUtils.TextRange.comparator);
30
31 for (let i = selections.length - 1; i >= 0; --i) {
32 let selection = selections[i];
33 selection = selection.normalize();
34 const endCursorChar = (selection.isEmpty() ? '|' : '<');
35 lines[selection.endLine] = lineWithCursor(lines[selection.endLine], selection.endColumn, endCursorChar);
36
37 if (!selection.isEmpty())
38 lines[selection.startLine] = lineWithCursor(lines[selection.startLine], selection.startColumn, '>');
39 }
40
41 return lines.join('\n');
42}
43
44SourcesTestRunner.dumpTextWithSelection = function(textEditor, dumpWhiteSpaces) {
45 let text = textWithSelection(textEditor.text(), textEditor.selections());
46
47 if (dumpWhiteSpaces)
48 text = text.replace(/ /g, '.');
49
50 TestRunner.addResult(text);
51};
52
53SourcesTestRunner.setLineSelections = function(editor, selections) {
54 const coords = [];
55
56 for (let i = 0; i < selections.length; ++i) {
57 const selection = selections[i];
58
59 if (typeof selection.column === 'number') {
60 selection.from = selection.column;
61 selection.to = selection.column;
62 }
63
64 coords.push(new TextUtils.TextRange(selection.line, selection.from, selection.line, selection.to));
65 }
66
67 editor.setSelections(coords);
68};
69
70SourcesTestRunner.typeIn = function(editor, typeText, callback) {
71 callback = callback || new Function();
72 const noop = new Function();
73
74 for (let charIndex = 0; charIndex < typeText.length; ++charIndex) {
75 const iterationCallback = (charIndex + 1 === typeText.length ? callback : noop);
76
77 switch (typeText[charIndex]) {
78 case '\n':
79 SourcesTestRunner.fakeKeyEvent(editor, 'Enter', null, iterationCallback);
80 break;
81 case 'L':
82 SourcesTestRunner.fakeKeyEvent(editor, 'ArrowLeft', null, iterationCallback);
83 break;
84 case 'R':
85 SourcesTestRunner.fakeKeyEvent(editor, 'ArrowRight', null, iterationCallback);
86 break;
87 case 'U':
88 SourcesTestRunner.fakeKeyEvent(editor, 'ArrowUp', null, iterationCallback);
89 break;
90 case 'D':
91 SourcesTestRunner.fakeKeyEvent(editor, 'ArrowDown', null, iterationCallback);
92 break;
93 default:
94 SourcesTestRunner.fakeKeyEvent(editor, typeText[charIndex], null, iterationCallback);
95 }
96 }
97};
98
99const eventCodes = {
100 Enter: 13,
101 Home: 36,
102 ArrowLeft: 37,
103 ArrowUp: 38,
104 ArrowRight: 39,
105 ArrowDown: 40
106};
107
108function createCodeMirrorFakeEvent(editor, eventType, code, charCode, modifiers) {
109 function eventPreventDefault() {
110 this._handled = true;
111 }
112
113 const event = {
114 _handled: false,
115 type: eventType,
116 keyCode: code,
117 charCode: charCode,
118 preventDefault: eventPreventDefault,
119 stopPropagation: function() {},
120 target: editor._codeMirror.display.input.textarea
121 };
122
123 if (modifiers) {
124 for (let i = 0; i < modifiers.length; ++i)
125 event[modifiers[i]] = true;
126 }
127
128 return event;
129}
130
131function fakeCodeMirrorKeyEvent(editor, eventType, code, charCode, modifiers) {
132 const event = createCodeMirrorFakeEvent(editor, eventType, code, charCode, modifiers);
133
134 switch (eventType) {
135 case 'keydown':
136 editor._codeMirror.triggerOnKeyDown(event);
137 break;
138 case 'keypress':
139 editor._codeMirror.triggerOnKeyPress(event);
140 break;
141 case 'keyup':
142 editor._codeMirror.triggerOnKeyUp(event);
143 break;
144 default:
145 throw new Error('Unknown KeyEvent type');
146 }
147
148 return event._handled;
149}
150
151function fakeCodeMirrorInputEvent(editor, character) {
Joel Einbinderf5ea74e2018-06-05 02:14:19152 if (typeof character !== 'string')
153 return;
154 const input = editor._codeMirror.display.input;
155 const value = input.textarea.value;
156 const newValue =
157 value.substring(0, input.textarea.selectionStart) + character + value.substring(input.textarea.selectionEnd);
158 const caretPosition = input.textarea.selectionStart + character.length;
159 input.textarea.value = newValue;
160 input.textarea.setSelectionRange(caretPosition, caretPosition);
161 input.poll();
Blink Reformat4c46d092018-04-07 15:32:37162}
163
164SourcesTestRunner.fakeKeyEvent = function(editor, originalCode, modifiers, callback) {
165 modifiers = modifiers || [];
166 let code;
167 let charCode;
168
169 if (originalCode === '\'') {
170 code = 222;
171 charCode = 0;
172 } else if (originalCode === '"') {
173 code = 222;
174 modifiers.push('shiftKey');
175 charCode = 34;
176 } else if (originalCode === '(') {
177 code = '9'.charCodeAt(0);
178 modifiers.push('shiftKey');
179 charCode = originalCode.charCodeAt(0);
180 }
181
182 code = code || eventCodes[originalCode] || originalCode;
183
184 if (typeof code === 'string')
185 code = code.charCodeAt(0);
186
187 if (fakeCodeMirrorKeyEvent(editor, 'keydown', code, charCode, modifiers)) {
188 callback();
189 return;
190 }
191
192 if (fakeCodeMirrorKeyEvent(editor, 'keypress', code, charCode, modifiers)) {
193 callback();
194 return;
195 }
196
Joel Einbinderf5ea74e2018-06-05 02:14:19197 const inputReadPromise = new Promise(x => editor._codeMirror.on('inputRead', x));
Blink Reformat4c46d092018-04-07 15:32:37198 fakeCodeMirrorInputEvent(editor, originalCode);
199 fakeCodeMirrorKeyEvent(editor, 'keyup', code, charCode, modifiers);
Joel Einbinderf5ea74e2018-06-05 02:14:19200 inputReadPromise.then(callback);
Blink Reformat4c46d092018-04-07 15:32:37201};
202
203SourcesTestRunner.dumpSelectionStats = function(textEditor) {
204 const listHashMap = {};
205 const sortedKeys = [];
206 const selections = textEditor.selections();
207
208 for (let i = 0; i < selections.length; ++i) {
209 const selection = selections[i];
210 const text = textEditor.text(selection);
211
212 if (!listHashMap[text]) {
213 listHashMap[text] = 1;
214 sortedKeys.push(text);
215 } else {
216 ++listHashMap[text];
217 }
218 }
219
220 for (let i = 0; i < sortedKeys.length; ++i) {
221 let keyName = sortedKeys[i];
222
223 if (!keyName.length)
224 keyName = '<Empty string>';
225 else
226 keyName = '\'' + keyName + '\'';
227
228 TestRunner.addResult(keyName + ': ' + listHashMap[sortedKeys[i]]);
229 }
230};