| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 // This file provides | 7 // This file provides |
| 8 // |spellcheck_test(sample, tester, expectedText, opt_title)| asynchronous test | 8 // |spellcheck_test(sample, tester, expectedText, opt_title)| asynchronous test |
| 9 // to W3C test harness for easier writing of spellchecker test cases. | 9 // to W3C test harness for easier writing of spellchecker test cases. |
| 10 // | 10 // |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 // prevent interference among them. If we call spellcheck_test while another | 329 // prevent interference among them. If we call spellcheck_test while another |
| 330 // test is running, the new test will be added into testQueue waiting for the | 330 // test is running, the new test will be added into testQueue waiting for the |
| 331 // completion of the previous test. | 331 // completion of the previous test. |
| 332 | 332 |
| 333 /** @type {boolean} */ | 333 /** @type {boolean} */ |
| 334 var spellcheckTestRunning = false; | 334 var spellcheckTestRunning = false; |
| 335 /** @type {!Array<!Object>} */ | 335 /** @type {!Array<!Object>} */ |
| 336 const testQueue = []; | 336 const testQueue = []; |
| 337 | 337 |
| 338 /** | 338 /** |
| 339 * @param {!Test} testObject |
| 339 * @param {!Sample|string} input | 340 * @param {!Sample|string} input |
| 340 * @param {function(!Document)|string} tester | 341 * @param {function(!Document)|string} tester |
| 341 * @param {string} expectedText | 342 * @param {string} expectedText |
| 342 * @param {Object} args | |
| 343 */ | 343 */ |
| 344 function invokeSpellcheckTest(input, tester, expectedText, args) { | 344 function invokeSpellcheckTest(testObject, input, tester, expectedText) { |
| 345 spellcheckTestRunning = true; | 345 spellcheckTestRunning = true; |
| 346 | 346 |
| 347 async_test(testObject => { | 347 testObject.step(() => { |
| 348 // TODO(xiaochengh): Merge the following part with |assert_selection|. | 348 // TODO(xiaochengh): Merge the following part with |assert_selection|. |
| 349 /** @type {!Sample} */ | 349 /** @type {!Sample} */ |
| 350 const sample = typeof(input) === 'string' ? new Sample(input) : input; | 350 const sample = typeof(input) === 'string' ? new Sample(input) : input; |
| 351 testObject.sample = sample; | 351 testObject.sample = sample; |
| 352 | 352 |
| 353 if (typeof(tester) === 'function') { | 353 if (typeof(tester) === 'function') { |
| 354 tester.call(window, sample.document); | 354 tester.call(window, sample.document); |
| 355 } else if (typeof(tester) === 'string') { | 355 } else if (typeof(tester) === 'string') { |
| 356 const strings = tester.split(/ (.+)/); | 356 const strings = tester.split(/ (.+)/); |
| 357 sample.document.execCommand(strings[0], false, strings[1]); | 357 sample.document.execCommand(strings[0], false, strings[1]); |
| 358 } else { | 358 } else { |
| 359 assert_unreached(`Invalid tester: ${tester}`); | 359 assert_unreached(`Invalid tester: ${tester}`); |
| 360 } | 360 } |
| 361 | 361 |
| 362 /** @type {number} */ | 362 /** @type {number} */ |
| 363 const kMaxRetry = 10; | 363 const kMaxRetry = 10; |
| 364 /** @type {number} */ | 364 /** @type {number} */ |
| 365 const kRetryInterval = 50; | 365 const kRetryInterval = 50; |
| 366 | 366 |
| 367 // TODO(xiaochengh): We should make SpellCheckRequester::didCheck trigger | 367 // TODO(xiaochengh): We should make SpellCheckRequester::didCheck trigger |
| 368 // something in JavaScript (e.g., a |Promise|), so that we can actively know | 368 // something in JavaScript (e.g., a |Promise|), so that we can actively know |
| 369 // the completion of spellchecking instead of passively waiting for markers | 369 // the completion of spellchecking instead of passively waiting for markers |
| 370 // to appear or disappear. | 370 // to appear or disappear. |
| 371 testObject.step_timeout( | 371 testObject.step_timeout( |
| 372 () => verifyMarkers(testObject, sample, expectedText, | 372 () => verifyMarkers(testObject, sample, expectedText, |
| 373 kMaxRetry, kRetryInterval), | 373 kMaxRetry, kRetryInterval), |
| 374 kRetryInterval); | 374 kRetryInterval); |
| 375 }, args[kTitle], args); | 375 }); |
| 376 } | 376 } |
| 377 | 377 |
| 378 add_result_callback(testObj => { | 378 add_result_callback(testObj => { |
| 379 if (!testObj.properties[kIsSpellcheckTest]) | 379 if (!testObj.properties[kIsSpellcheckTest]) |
| 380 return; | 380 return; |
| 381 |
| 382 /** @type {boolean} */ |
| 383 var shouldRemoveSample = false; |
| 384 if (testObj.status === testObj.PASS) { |
| 385 if (testObj.properties[kCallback]) |
| 386 testObj.properties[kCallback](testObj.sample); |
| 387 else |
| 388 shouldRemoveSample = true; |
| 389 } else { |
| 390 if (window.testRunner) |
| 391 shouldRemoveSample = true; |
| 392 } |
| 393 |
| 394 if (shouldRemoveSample) |
| 395 testObj.sample.remove(); |
| 396 |
| 397 // This is the earliest timing when a new spellcheck_test can be started. |
| 381 spellcheckTestRunning = false; | 398 spellcheckTestRunning = false; |
| 382 | 399 |
| 383 if (testObj.status === testObj.PASS) { | |
| 384 if (testObj.properties[kCallback]) { | |
| 385 testObj.properties[kCallback](testObj.sample); | |
| 386 } else { | |
| 387 testObj.sample.remove(); | |
| 388 } | |
| 389 } | |
| 390 | |
| 391 /** @type {Object} */ | 400 /** @type {Object} */ |
| 392 const next = testQueue.shift(); | 401 const next = testQueue.shift(); |
| 393 if (next === undefined) | 402 if (next === undefined) |
| 394 return; | 403 return; |
| 395 invokeSpellcheckTest(next.input, next.tester, | 404 invokeSpellcheckTest(next.testObject, next.input, |
| 396 next.expectedText, next.args); | 405 next.tester, next.expectedText); |
| 397 }); | 406 }); |
| 398 | 407 |
| 399 /** | 408 /** |
| 400 * @param {Object=} passedArgs | 409 * @param {Object=} passedArgs |
| 401 * @return {!Object} | 410 * @return {!Object} |
| 402 */ | 411 */ |
| 403 function getTestArguments(passedArgs) { | 412 function getTestArguments(passedArgs) { |
| 404 const args = {}; | 413 const args = {}; |
| 405 args[kIsSpellcheckTest] = true; | 414 args[kIsSpellcheckTest] = true; |
| 406 [kTitle, kCallback].forEach(key => args[key] = undefined); | 415 [kTitle, kCallback].forEach(key => args[key] = undefined); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 421 * @param {function(!Document)|string} tester | 430 * @param {function(!Document)|string} tester |
| 422 * @param {string} expectedText | 431 * @param {string} expectedText |
| 423 * @param {Object=} opt_args | 432 * @param {Object=} opt_args |
| 424 */ | 433 */ |
| 425 function spellcheckTest(input, tester, expectedText, opt_args) { | 434 function spellcheckTest(input, tester, expectedText, opt_args) { |
| 426 if (window.testRunner) | 435 if (window.testRunner) |
| 427 window.testRunner.setMockSpellCheckerEnabled(true); | 436 window.testRunner.setMockSpellCheckerEnabled(true); |
| 428 | 437 |
| 429 /** @type {!Object} */ | 438 /** @type {!Object} */ |
| 430 const args = getTestArguments(opt_args); | 439 const args = getTestArguments(opt_args); |
| 440 /** @type {!Test} */ |
| 441 const testObject = async_test(args[kTitle], args); |
| 431 | 442 |
| 432 if (spellcheckTestRunning) { | 443 if (spellcheckTestRunning) { |
| 433 testQueue.push({ | 444 testQueue.push({ |
| 434 input: input, tester: tester, | 445 testObject: testObject, input: input, |
| 435 expectedText: expectedText, args: args}); | 446 tester: tester, expectedText: expectedText}); |
| 436 return; | 447 return; |
| 437 } | 448 } |
| 438 | 449 |
| 439 invokeSpellcheckTest(input, tester, expectedText, args); | 450 invokeSpellcheckTest(testObject, input, tester, expectedText); |
| 440 } | 451 } |
| 441 | 452 |
| 442 // Export symbols | 453 // Export symbols |
| 443 window.spellcheck_test = spellcheckTest; | 454 window.spellcheck_test = spellcheckTest; |
| 444 })(); | 455 })(); |
| OLD | NEW |