Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/ce/ve.ce.TestRunner.js

raw symbols SCIP
1 /*!
2 * VisualEditor IME-like testing
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Text offset from start or end of string
9 *
10 * The offsets are measured in text node code unit boundaries. That is, there is an
11 * implicit start and end position in each text node, so there are eight positions in
12 * "he<b>ll</b>o". This will not always agree exactly with a real browser's cursor
13 * positions, because of grapheme clustering and positioning around tags, but is a
14 * useful test shorthand.
15 *
16 * @class
17 * @constructor
18 * @param {string} direction (forward counts from string start, backward from end)
19 * @param {number} offset Offset in text node code units
20 */
21 ve.ce.TestOffset = function VeCeTestOffset( direction, offset ) {
22 if ( direction !== 'forward' && direction !== 'backward' ) {
23 throw new Error( 'Invalid direction "' + direction + '"' );
24 }
25 this.direction = direction;
26 this.offset = offset;
27 this.lastText = null;
28 this.lastSel = null;
29 };
30
31 /**
32 * @typedef {Object} OffsetInfo
33 * @memberof ve.ce.TestOffset
34 * @property {number} [consumed] The number of code units consumed (if n out of range)
35 * @property {Node} [node] The node containing the offset (if n in range)
36 * @property {number} [offset] The offset in code units / child elements (if n in range)
37 * @property {string} [slice] String representation of the offset position (if n in range)
38 */
39
40 /**
41 * Calculate the offset from each end of a particular HTML string
42 *
43 * @param {Node} node The DOM node with respect to which the offset is resolved
44 * @return {ve.ce.TestOffset.OffsetInfo} Offset information
45 */
46 ve.ce.TestOffset.prototype.resolve = function ( node ) {
47 const reversed = ( this.direction !== 'forward' );
48 return ve.ce.TestOffset.static.findTextOffset( node, this.offset, reversed );
49 };
50
51 /* Static Methods */
52 ve.ce.TestOffset.static = {};
53
54 /**
55 * Find text by offset in the given node.
56 * Returns the same as #resolve.
57 *
58 * @private
59 * @static
60 * @param {Node} node
61 * @param {number} n Offset
62 * @param {boolean} reversed
63 * @return {ve.ce.TestOffset.OffsetInfo} Offset information
64 */
65 ve.ce.TestOffset.static.findTextOffset = function ( node, n, reversed ) {
66 if ( node.nodeType === node.TEXT_NODE ) {
67 // Test length >= n because one more boundaries than code units
68 if ( node.textContent.length >= n ) {
69 const offset = reversed ? node.textContent.length - n : n;
70 const slice = node.textContent.slice( 0, offset ) + '|' +
71 node.textContent.slice( offset );
72 return { node: node, offset: offset, slice: slice };
73 } else {
74 return { consumed: node.textContent.length + 1 };
75 }
76 }
77 if ( node.nodeType !== node.ELEMENT_NODE ) {
78 return { consumed: 0 };
79 }
80 // node is an ELEMENT_NODE below here
81 // TODO consecutive text nodes will cause an extra phantom boundary.
82 // In realistic usage, this can't always be avoided, because normalize() will
83 // close an IME.
84 const childNodes = Array.prototype.slice.call( node.childNodes );
85
86 if ( childNodes.length === 0 ) {
87 if ( n === 0 ) {
88 return { node: node, offset: 0, slice: '|' };
89 }
90 return { consumed: 0 };
91 }
92
93 if ( reversed ) {
94 childNodes.reverse();
95 }
96 let consumed = 0;
97 for ( let i = 0, len = childNodes.length; i < len; i++ ) {
98 const childNode = node.childNodes[ i ];
99 const found = ve.ce.TestOffset.static.findTextOffset( childNode, n - consumed, reversed );
100 if ( found.node ) {
101 return found;
102 }
103 consumed += found.consumed;
104 // Extra boundary after element, if not followed by a text node
105 if ( childNode.nodeType === node.ELEMENT_NODE ) {
106 if ( i + 1 === len || childNodes[ i + 1 ].nodeType !== node.TEXT_NODE ) {
107 consumed += 1;
108 if ( consumed === n ) {
109 // TODO: create a reasonable 'slice' string
110 return { node: node, offset: i + 1, slice: 'XXX' };
111 }
112 }
113 }
114 }
115 return { consumed: consumed };
116 };
117
118 /**
119 * IME-like CE test class
120 *
121 * @class
122 * @constructor
123 * @param {ve.ce.Surface} surface The UI Surface
124 */
125 ve.ce.TestRunner = function VeCeTestRunner( surface ) {
126 this.view = surface;
127 this.model = surface.getModel();
128 this.doc = surface.getElementDocument();
129 this.nativeSelection = surface.nativeSelection;
130
131 // TODO: The code assumes that the document consists of exactly one paragraph
132 this.lastText = this.getParagraph().textContent;
133
134 // Turn off SurfaceObserver setTimeouts
135 surface.surfaceObserver.pollInterval = null;
136
137 // Take control of eventSequencer 'setTimeouts'
138 ve.test.utils.hijackEventSequencerTimeouts( this.view.eventSequencer );
139 };
140
141 /* Methods */
142
143 /**
144 * Get the paragraph node in which testing occurs
145 *
146 * TODO: The code assumes that the document consists of exactly one paragraph
147 *
148 * @return {Node} The paragraph node
149 */
150 ve.ce.TestRunner.prototype.getParagraph = function () {
151 const p = this.view.$element.find( '.ve-ce-attachedRootNode > p' )[ 0 ];
152 if ( p === undefined ) {
153 if ( this.view.$element.find( '.ve-ce-attachedRootNode' )[ 0 ] === undefined ) {
154 throw new Error( 'no CE div' );
155 }
156 throw new Error( 'CE div but no p' );
157 }
158 return p;
159 };
160
161 /**
162 * Run any pending postponed calls
163 *
164 * Exceptions thrown will leave postponed calls in an inconsistent state
165 */
166 ve.ce.TestRunner.prototype.endLoop = function () {
167 this.view.eventSequencer.endLoop();
168 };
169
170 /**
171 * Send an event to the ce.Surface eventSequencer
172 *
173 * @param {string} eventName DOM event name (such as 'keydown')
174 * @param {Object} ev Fake event object with any necessary properties
175 */
176 ve.ce.TestRunner.prototype.sendEvent = function ( eventName, ev ) {
177 // Ensure ev has an originalEvent property.
178 ev.originalEvent = ev.originalEvent || {};
179 this.view.eventSequencer.onEvent( eventName, ev );
180 };
181
182 /**
183 * Change the text
184 *
185 * TODO: it should be possible to add markup
186 *
187 * @param {string} text The new text
188 */
189 ve.ce.TestRunner.prototype.changeText = function ( text ) {
190 // TODO: This method doesn't handle arbitrary text changes in a paragraph
191 // with non-text nodes. It just works for the main cases that are important
192 // in the existing IME tests.
193
194 // Remove all descendant text nodes
195 // This may clobber the selection, so the test had better call changeSel next.
196 const paragraph = this.getParagraph();
197 $( paragraph ).find( '*' ).addBack().contents().each( function () {
198 if ( this.nodeType === Node.TEXT_NODE ) {
199 this.parentNode.removeChild( this );
200 }
201 } );
202
203 const range = document.createRange();
204 if ( text === '' ) {
205 range.setStart( paragraph, 0 );
206 } else {
207 // Insert the text at the start of the paragraph, and put the cursor after
208 // the insertion, to ensure consistency across test environments.
209 // See T176453
210 const textNode = document.createTextNode( text );
211 paragraph.insertBefore( textNode, paragraph.firstChild );
212 range.setStart( textNode, text.length );
213 }
214 this.nativeSelection.removeAllRanges();
215 this.nativeSelection.addRange( range );
216 this.lastText = text;
217 };
218
219 /**
220 * @typedef {Object} PlainRange
221 * @property {Node} startNode The node at the start of the selection
222 * @property {number} startOffset The start offset within the node
223 * @property {Node} endNode The node at the endof the selection
224 * @property {number} endOffset The endoffset within the node
225 */
226
227 /**
228 * Select text by offset in concatenated text nodes
229 *
230 * @param {ve.ce.TestOffset|number} start The start offset
231 * @param {ve.ce.TestOffset|number} end The end offset
232 * @return {PlainRange} Selected range
233 */
234 ve.ce.TestRunner.prototype.changeSel = function ( start, end ) {
235 if ( typeof start === 'number' ) {
236 start = new ve.ce.TestOffset( 'forward', start );
237 }
238 if ( typeof end === 'number' ) {
239 end = new ve.ce.TestOffset( 'forward', end );
240 }
241
242 const foundStart = start.resolve( this.getParagraph() );
243 const foundEnd = start.resolve( this.getParagraph() );
244 if ( !foundStart.node ) {
245 throw new Error( 'Bad start offset: ' + start.offset );
246 }
247 if ( !foundEnd.node ) {
248 throw new Error( 'Bad end offset: ', end.offset );
249 }
250
251 const nativeRange = this.doc.createRange();
252 nativeRange.setStart( foundStart.node, foundStart.offset );
253 nativeRange.setEnd( foundEnd.node, foundEnd.offset );
254 this.nativeSelection.removeAllRanges();
255 this.getParagraph().focus();
256 this.nativeSelection.addRange( nativeRange, false );
257 this.lastSel = [ start, end ];
258
259 return {
260 startNode: foundStart.node,
261 endNode: foundEnd.node,
262 startOffset: foundStart.offset,
263 endOffset: foundEnd.offset
264 };
265 };
266
267 /**
268 * Call assert.equal to check the IME test has updated the DM correctly
269 *
270 * @param {Object} assert The QUnit assertion object
271 * @param {string} testName The name of the test scenario
272 * @param {number} sequence The sequence number in the test scenario
273 */
274 ve.ce.TestRunner.prototype.testEqual = function ( assert, testName, sequence ) {
275 const comment = testName + ' seq=' + sequence + ': "' + this.lastText + '"';
276 assert.strictEqual( this.model.getDocument().data.getText( false ), this.lastText, comment );
277 };
278
279 /**
280 * Call assert.notStrictEqual to check the IME test has not updated the DM correctly
281 *
282 * @param {Object} assert The QUnit assertion object
283 * @param {string} testName The name of the test scenario
284 * @param {number} sequence The sequence number in the test scenario
285 */
286 ve.ce.TestRunner.prototype.testNotEqual = function ( assert, testName, sequence ) {
287 const comment = testName + ' seq=' + sequence + ': "' + this.lastText + '"';
288 assert.notStrictEqual( this.model.getDocument().data.getText( false ), this.lastText, comment );
289 };
290
291 ve.ce.TestRunner.prototype.ok = function ( assert, testName, sequence ) {
292 const comment = testName + ' seq=' + sequence + ': "' + this.lastText + '"';
293 assert.true( true, comment );
294 };
295
296 ve.ce.TestRunner.prototype.failDied = function ( assert, testName, sequence, ex ) {
297 const comment = testName + ' seq=' + sequence + ': "' + this.lastText +
298 '" ex=' + ex + ' stack=<' + ex.stack + '>';
299 assert.true( false, comment );
300 };