Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 'use strict'; | |
| 6 | |
| 7 // This file provides | |
| 8 // |spellcheck_test(sample, tester, expectedMarkers, opt_title)| asynchronous | |
| 9 // test to W3C test harness for easier writing of editing test cases. | |
| 10 // | |
| 11 // |sample| is an HTML fragment text which is inserted as |innerHTML|. It should | |
| 12 // have at least one focus boundary point marker "|" and at most one anchor | |
| 13 // boundary point marker "^" indicating the initial selection. | |
| 14 // | |
| 15 // |tester| is either name with parameter of execCommand or function taking | |
| 16 // one parameter |Document|. | |
| 17 // | |
| 18 // |expectedMarkers| is either a |Marker| or a |Marker| array, where each | |
| 19 // |Marker| is an |Object| with the following properties: | |
| 20 // - |location| and |length| are integers indicating the range of the marked | |
| 21 // text. It must hold that |location >= 0| and |length > 0|. | |
| 22 // - |type| is an optional string indicating the marker type. When present, it | |
| 23 // must be equal to either "spelling" or "grammer". When absent, it is | |
| 24 // regarded as "spelling". | |
| 25 // - |description| is an optional string indicating the description of a marker. | |
| 26 // | |
| 27 // |opt_title| is an optional string giving the title of the test case. | |
| 28 // | |
| 29 // Examples: | |
| 30 // | |
| 31 // spellcheck_test( | |
| 32 // '<div contentEditable>|</div>', | |
| 33 // 'insertText wellcome.', | |
| 34 // spellingMarker(0, 8, 'welcome'), // 'wellcome' | |
| 35 // 'Mark misspellings and give replacement suggestions after typing.'); | |
| 36 // | |
| 37 // spellcheck_test( | |
| 38 // '<div contentEditable>|</div>', | |
| 39 // 'insertText You has the right.', | |
| 40 // grammarMarker(4, 3), // 'has' | |
| 41 // 'Mark ungrammatical phrases after typing.'); | |
| 42 | |
| 43 (function() { | |
| 44 const Sample = window.Sample; | |
| 45 | |
| 46 /** @type {string} */ | |
| 47 const kSpelling = 'spelling'; | |
| 48 /** @type {string} */ | |
| 49 const kGrammar = 'grammar'; | |
| 50 | |
| 51 class Marker { | |
| 52 /** | |
| 53 * @public | |
| 54 * @param {number} location | |
| 55 * @param {number} length | |
| 56 * @param {string=} opt_type | |
| 57 * @param {string=} opt_description | |
| 58 */ | |
| 59 constructor(location, length, opt_type, opt_description) { | |
| 60 /** @type {number} */ | |
| 61 this.location_ = location; | |
| 62 /** @type {number} */ | |
| 63 this.length_ = length; | |
| 64 /** @type {string} */ | |
| 65 this.type_ = opt_type || 'spelling'; | |
| 66 /** @type {?string} */ | |
| 67 this.description_ = opt_description ? opt_description : null; | |
|
yosin_UTC9
2016/10/24 07:28:18
Let's have const kDontCareDescription = null; or j
| |
| 68 } | |
| 69 | |
| 70 /** @return {number} */ | |
| 71 get location() { return this.location_; } | |
| 72 | |
| 73 /** @return {number} */ | |
| 74 get length() { return this.length_; } | |
| 75 | |
| 76 /** @return {string} */ | |
| 77 get type() { return this.type_; } | |
| 78 | |
| 79 /** @return {?string} */ | |
| 80 get description() { return this.description_; } | |
| 81 | |
| 82 /** | |
| 83 * @public | |
| 84 */ | |
| 85 assertValid() { | |
| 86 // TODO(xiaochengh): Add proper assert descriptions when needed. | |
| 87 assert_true(Number.isInteger(this.location_)); | |
| 88 assert_greater_than_equal(this.location_, 0); | |
| 89 assert_true(Number.isInteger(this.length_)); | |
| 90 assert_greater_than(this.length_, 0); | |
| 91 assert_true(this.type_ === kSpelling || this.type_ === kGrammar); | |
| 92 if (this.description_ === null) | |
| 93 return; | |
| 94 assert_true(typeof this.description_ === 'string'); | |
| 95 } | |
| 96 | |
| 97 /** | |
| 98 * @public | |
| 99 * @param {!Marker} expected | |
| 100 */ | |
| 101 assertMatch(expected) { | |
| 102 assert_equals(this.location, expected.location, | |
| 103 'Marker locations mismatch.'); | |
| 104 assert_equals(this.length, expected.length, 'Marker lengths mismatch.'); | |
| 105 assert_equals(this.type, expected.type, 'Marker types mismatch.'); | |
| 106 if (expected.description === null) | |
| 107 return; | |
| 108 assert_equals(this.description, expected.description, | |
| 109 'Marker descriptions mismatch'); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 /** | |
| 114 * @param {number} location | |
| 115 * @param {number} length | |
| 116 * @param {string=} opt_description | |
| 117 * @return {!Marker} | |
| 118 */ | |
| 119 function spellingMarker(location, length, opt_description) { | |
| 120 return new Marker(location, length, kSpelling, opt_description); | |
| 121 } | |
| 122 | |
| 123 /** | |
| 124 * @param {number} location | |
| 125 * @param {number} length | |
| 126 * @param {string=} opt_description | |
| 127 * @return {!Marker} | |
| 128 */ | |
| 129 function grammarMarker(location, length, opt_description) { | |
| 130 return new Marker(location, length, kGrammar, opt_description); | |
| 131 } | |
| 132 | |
| 133 /** | |
| 134 * @param {!Marker} marker1 | |
| 135 * @param {!Marker} marker2 | |
| 136 * @return {number} | |
| 137 */ | |
| 138 function markerComparison(marker1, marker2) { | |
| 139 return marker1.location - marker2.location; | |
| 140 } | |
| 141 | |
| 142 /** | |
| 143 * @param {!Array<!Marker>} expectedMarkers | |
| 144 */ | |
| 145 function checkExpectedMarkers(expectedMarkers) { | |
| 146 expectedMarkers.forEach(marker => marker.assertValid()); | |
| 147 expectedMarkers.sort(markerComparison); | |
| 148 expectedMarkers.reduce((lastMarker, currentMarker) => { | |
| 149 assert_less_than( | |
| 150 lastMarker.location + lastMarker.length, currentMarker.location, | |
| 151 'Marker ranges should be disjoint.'); | |
| 152 return currentMarker; | |
| 153 }); | |
| 154 } | |
| 155 | |
| 156 /** | |
| 157 * @param {!Node} node | |
| 158 * @param {string} type | |
| 159 * @param {!Array<!Marker>} markers | |
| 160 */ | |
| 161 function extractMarkersOfType(node, type, markers) { | |
| 162 /** @type {!HTMLBodyElement} */ | |
| 163 const body = node.ownerDocument.body; | |
| 164 /** @type {number} */ | |
| 165 const markerCount = window.internals.markerCountForNode(node, type); | |
| 166 for (var i = 0; i < markerCount; ++i) { | |
| 167 /** @type {!Range} */ | |
| 168 const markerRange = window.internals.markerRangeForNode(node, type, i); | |
| 169 /** @type {string} */ | |
| 170 const description = window.internals.markerDescriptionForNode(node, type, i) ; | |
| 171 /** @type {number} */ | |
| 172 const location = window.internals.locationFromRange(body, markerRange); | |
| 173 /** @type {number} */ | |
| 174 const length = window.internals.lengthFromRange(body, markerRange); | |
| 175 | |
| 176 markers.push(new Marker(location, length, type, description)); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 /** | |
| 181 * @param {!Node} node | |
| 182 * @param {!Array<!Marker>} markers | |
| 183 */ | |
| 184 function extractAllMarkersRecursivelyTo(node, markers) { | |
| 185 extractMarkersOfType(node, kSpelling, markers); | |
| 186 extractMarkersOfType(node, kGrammar, markers); | |
| 187 node.childNodes.forEach( | |
| 188 child => extractAllMarkersRecursivelyTo(child, markers)); | |
| 189 } | |
| 190 | |
| 191 /** | |
| 192 * @param {!Document} doc | |
| 193 * @return {!Array<!Marker>} | |
| 194 */ | |
| 195 function extractAllMarkers(doc) { | |
| 196 /** @type {!Array<!Marker>} */ | |
| 197 const markers = []; | |
| 198 extractAllMarkersRecursivelyTo(doc.body, markers); | |
| 199 markers.sort(markerComparison); | |
| 200 return markers; | |
| 201 } | |
| 202 | |
| 203 /** | |
| 204 * @param {!Test} testObject | |
| 205 * @param {!Sample} sample, | |
| 206 * @param {!Array<!Marker>} expectedMarkers | |
| 207 * @param {number} remainingRetry | |
| 208 * @param {number} retryInterval | |
| 209 */ | |
| 210 function verifyMarkers( | |
| 211 testObject, sample, expectedMarkers, remainingRetry, retryInterval) { | |
| 212 assert_not_equals( | |
| 213 window.internals, undefined, | |
| 214 'window.internals is required for running automated spellcheck tests.'); | |
| 215 | |
| 216 /** @type {!Array<!Marker>} */ | |
| 217 const actualMarkers = extractAllMarkers(sample.document); | |
| 218 try { | |
| 219 assert_equals(actualMarkers.length, expectedMarkers.length, | |
| 220 'Number of markers mismatch.'); | |
| 221 actualMarkers.forEach( | |
| 222 (marker, index) => marker.assertMatch(expectedMarkers[index])); | |
| 223 testObject.done(); | |
| 224 sample.remove(); | |
| 225 } catch (error) { | |
| 226 if (remainingRetry <= 0) | |
| 227 throw error; | |
| 228 | |
| 229 // Force invoking idle time spellchecker in case it has not been run yet. | |
| 230 if (window.testRunner) | |
| 231 window.testRunner.runIdleTasks(() => {}); | |
| 232 | |
| 233 // TODO(xiaochengh): We should make SpellCheckRequester::didCheck trigger | |
| 234 // something in JavaScript (e.g., a |Promise|), so that we can actively | |
| 235 // know the completion of spellchecking instead of passively waiting for | |
| 236 // markers to appear or disappear. | |
| 237 testObject.step_timeout( | |
| 238 () => verifyMarkers(testObject, sample, expectedMarkers, | |
| 239 remainingRetry - 1, retryInterval), | |
| 240 retryInterval); | |
| 241 } | |
| 242 } | |
| 243 | |
| 244 // Spellchecker gets triggered not only by text and selection change, but also | |
| 245 // by focus change. For example, misspelling markers in <INPUT> disappear when | |
| 246 // the window loses focus, even though the selection does not change. | |
| 247 // Therefore, we disallow spellcheck tests from running simultaneously to | |
| 248 // prevent interference among them. If we call spellcheck_test while another | |
| 249 // test is running, the new test will be added into testQueue waiting for the | |
| 250 // completion of the previous test. | |
| 251 | |
| 252 /** @type {boolean} */ | |
| 253 var spellcheckTestRunning = false; | |
| 254 /** @type {!Array<!Object>} */ | |
| 255 const testQueue = []; | |
| 256 | |
| 257 /** | |
| 258 * @param {string} inputText | |
| 259 * @param {function(!Document)|string} tester | |
| 260 * @param {!Marker|!Array<!Marker>} expectedMarkers | |
| 261 * @param {string=} opt_title | |
| 262 */ | |
| 263 function invokeSpellcheckTest(inputText, tester, expectedMarkers, opt_title) { | |
| 264 spellcheckTestRunning = true; | |
| 265 | |
| 266 /** @type {!Test} */ | |
| 267 const testObject = async_test(opt_title, {isSpellcheckTest: true}); | |
| 268 | |
| 269 if (!(expectedMarkers instanceof Array)) | |
| 270 expectedMarkers = [expectedMarkers] | |
| 271 testObject.step(() => checkExpectedMarkers(expectedMarkers)); | |
| 272 | |
| 273 if (window.testRunner) | |
| 274 window.testRunner.setMockSpellCheckerEnabled(true); | |
| 275 | |
| 276 /** @type {!Sample} */ | |
| 277 const sample = new Sample(inputText); | |
| 278 if (typeof(tester) === 'function') { | |
| 279 tester.call(window, sample.document); | |
| 280 } else if (typeof(tester) === 'string') { | |
| 281 const strings = tester.split(/ (.+)/); | |
| 282 sample.document.execCommand(strings[0], false, strings[1]); | |
| 283 } else { | |
| 284 testObject.step(() => assert_unreached(`Invalid tester: ${tester}`)); | |
| 285 } | |
| 286 | |
| 287 /** @type {number} */ | |
| 288 const kMaxRetry = 10; | |
| 289 /** @type {number} */ | |
| 290 const kRetryInterval = 50; | |
| 291 | |
| 292 // TODO(xiaochengh): We should make SpellCheckRequester::didCheck trigger | |
| 293 // something in JavaScript (e.g., a |Promise|), so that we can actively know | |
| 294 // the completion of spellchecking instead of passively waiting for markers to | |
| 295 // appear or disappear. | |
| 296 testObject.step_timeout( | |
| 297 () => verifyMarkers(testObject, sample, expectedMarkers, | |
| 298 kMaxRetry, kRetryInterval), | |
| 299 kRetryInterval); | |
| 300 } | |
| 301 | |
| 302 add_result_callback(testObj => { | |
| 303 if (!testObj.properties.isSpellcheckTest) | |
| 304 return; | |
| 305 spellcheckTestRunning = false; | |
| 306 /** @type {Object} */ | |
| 307 const args = testQueue.shift(); | |
| 308 if (args === undefined) | |
| 309 return; | |
| 310 invokeSpellcheckTest(args.inputText, args.tester, | |
| 311 args.expectedMarkers, args.opt_title); | |
| 312 }); | |
| 313 | |
| 314 /** | |
| 315 * @param {string} inputText | |
| 316 * @param {function(!Document)|string} tester | |
| 317 * @param {!Marker|!Array<!Marker>} expectedMarkers | |
| 318 * @param {string=} opt_title | |
| 319 */ | |
| 320 function spellcheckTest(inputText, tester, expectedMarkers, opt_title) { | |
| 321 if (spellcheckTestRunning) { | |
| 322 testQueue.push({ | |
| 323 inputText: inputText, tester: tester, | |
| 324 expectedMarkers: expectedMarkers, opt_title: opt_title}); | |
| 325 return; | |
| 326 } | |
| 327 | |
| 328 invokeSpellcheckTest(inputText, tester, expectedMarkers, opt_title); | |
| 329 } | |
| 330 | |
| 331 // Export symbols | |
| 332 window.Marker = Marker; | |
| 333 window.spellingMarker = spellingMarker; | |
| 334 window.grammarMarker = grammarMarker; | |
| 335 window.spellcheck_test = spellcheckTest; | |
| 336 })(); | |
| OLD | NEW |