Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: third_party/WebKit/LayoutTests/webaudio/resources/audio-testing.js

Issue 2212023002: Modifying audio-testing.js to support testharness.js. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Initial patch Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* global self */
2
3 // Check if testharness is properly included and set up a flag for it.
4 var TESTHARNESS = true;
5 var testharnessFunctions = [
6 'test', 'async_test', 'promise_test', 'promise_rejects', 'generate_tests',
7 'setup', 'done', 'assert_true', 'assert_false'
8 ];
9
10 for (var func in testharnessFunctions) {
11 if (!self.hasOwnProperty(testharnessFunctions[func])) {
12 TESTHARNESS = false;
13 break;
14 }
15 }
16
17
1 function writeString(s, a, offset) { 18 function writeString(s, a, offset) {
2 for (var i = 0; i < s.length; ++i) { 19 for (var i = 0; i < s.length; ++i) {
3 a[offset + i] = s.charCodeAt(i); 20 a[offset + i] = s.charCodeAt(i);
4 } 21 }
5 } 22 }
6 23
7 function writeInt16(n, a, offset) { 24 function writeInt16(n, a, offset) {
8 n = Math.floor(n); 25 n = Math.floor(n);
9 26
10 var b1 = n & 255; 27 var b1 = n & 255;
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 var Audit = (function () { 311 var Audit = (function () {
295 312
296 'use strict'; 313 'use strict';
297 314
298 function Tasks() { 315 function Tasks() {
299 this.tasks = {}; 316 this.tasks = {};
300 this.queue = []; 317 this.queue = [];
301 this.currentTask = 0; 318 this.currentTask = 0;
302 } 319 }
303 320
321 // This is to prime the task runner for the testharness.js async operation.
322 Tasks.prototype._initialize = function () {
323 if (TESTHARNESS) {
324 setup(new Function(), {
325 explicit_done: true
326 });
327 } else {
328 // For js-test.js
329 window.jsTestIsAsync = true;
Raymond Toy 2016/08/04 18:07:08 Are you sure we always want to set this? Thus far
hongchan 2016/08/04 18:31:37 If the value is already set, this doesn't do anyth
330 }
331
332 return;
333 };
334
335 // Finalize the task runner by notifying testharness and testRunner that
336 // all the task is completed.
337 Tasks.prototype._finalize = function () {
338 if (TESTHARNESS) {
339 // From testharness.js
340 done();
341
342 } else {
343 // For js-test.js
344 finishJSTest();
Raymond Toy 2016/08/04 18:07:08 Won't this break existing tests? I've always been
hongchan 2016/08/04 18:31:37 I've tested this against our existing test suites.
345 }
346
347 return;
348 };
349
304 Tasks.prototype.defineTask = function (taskName, taskFunc) { 350 Tasks.prototype.defineTask = function (taskName, taskFunc) {
305 // Check if there is a task defined with the same name. If found, do 351 // Check if there is a task defined with the same name. If found, do
306 // not add the task to the roster. 352 // not add the task to the roster.
307 if (this.tasks.hasOwnProperty(taskName)) { 353 if (this.tasks.hasOwnProperty(taskName)) {
308 debug('>> Audit.defineTask:: Duplicate task definition. ("' + taskNa me + '")'); 354 debug('>> Audit.defineTask:: Duplicate task definition. ("' + taskNa me + '")');
309 return; 355 return;
310 } 356 }
311 357
312 this.tasks[taskName] = taskFunc; 358 this.tasks[taskName] = taskFunc;
313 359
314 // Push the task name in the order of definition. 360 // Push the task name in the order of definition.
315 this.queue.push(taskName); 361 this.queue.push(taskName);
316 }; 362 };
317 363
318 // If arguments are given, only run the requested tasks. Check for any 364 // If arguments are given, only run the requested tasks. Check for any
319 // undefined or duplicate task in the requested task arguments. If there 365 // undefined or duplicate task in the requested task arguments. If there
320 // is no argument, run all the defined tasks. 366 // is no argument, run all the defined tasks.
321 Tasks.prototype.runTasks = function () { 367 Tasks.prototype.runTasks = function () {
322 368
369 this._initialize();
370
323 if (arguments.length > 0) { 371 if (arguments.length > 0) {
324 372
325 // Reset task queue and refill it with the with the given arguments, 373 // Reset task queue and refill it with the with the given arguments,
326 // in argument order. 374 // in argument order.
327 this.queue = []; 375 this.queue = [];
328 376
329 for (var i = 0; i < arguments.length; i++) { 377 for (var i = 0; i < arguments.length; i++) {
330 if (!this.tasks.hasOwnProperty(arguments[i])) 378 if (!this.tasks.hasOwnProperty(arguments[i]))
331 debug('>> Audit.runTasks:: Ignoring undefined task. ("' + ar guments[i] + '")'); 379 debug('>> Audit.runTasks:: Ignoring undefined task. ("' + ar guments[i] + '")');
332 else if (this.queue.indexOf(arguments[i]) > -1) 380 else if (this.queue.indexOf(arguments[i]) > -1)
333 debug('>> Audit.runTasks:: Ignoring duplicate task request. ("' + arguments[i] + '")'); 381 debug('>> Audit.runTasks:: Ignoring duplicate task request. ("' + arguments[i] + '")');
334 else 382 else
335 this.queue.push(arguments[i]); 383 this.queue.push(arguments[i]);
336 } 384 }
337 } 385 }
338 386
339 if (this.queue.length === 0) { 387 if (this.queue.length === 0) {
340 debug('>> Audit.runTasks:: No task to run.'); 388 debug('>> Audit.runTasks:: No task to run.');
341 return; 389 return;
342 } 390 }
343 391
344 // done() callback from each task. Increase the task index and call the 392 // taskDone() callback from each task. Increase the task index and call
345 // next task. Note that explicit signaling by done() in each task 393 // the next task. Note that explicit signaling by taskDone() in each
346 // is needed because some of tests run asynchronously. 394 // task is needed because some of tests run asynchronously.
347 var done = function () { 395 var taskDone = function () {
348 if (this.currentTask !== this.queue.length - 1) { 396 if (this.currentTask !== this.queue.length - 1) {
349 ++this.currentTask; 397 ++this.currentTask;
350 // debug('>> Audit.runTasks: ' + this.queue[this.currentTask]); 398 // debug('>> Audit.runTasks: ' + this.queue[this.currentTask]);
351 this.tasks[this.queue[this.currentTask]](done); 399 this.tasks[this.queue[this.currentTask]](taskDone);
400 } else {
401 this._finalize();
352 } 402 }
353 return; 403 return;
354 }.bind(this); 404 }.bind(this);
355 405
356 // Start the first task. 406 // Start the first task.
357 // debug('>> Audit.runTasks: ' + this.queue[this.currentTask]); 407 // debug('>> Audit.runTasks: ' + this.queue[this.currentTask]);
358 this.tasks[this.queue[this.currentTask]](done); 408 this.tasks[this.queue[this.currentTask]](taskDone);
359 }; 409 };
360 410
361 return { 411 return {
362 createTaskRunner: function () { 412 createTaskRunner: function () {
363 return new Tasks(); 413 return new Tasks();
364 } 414 }
365 }; 415 };
366 416
367 })(); 417 })();
368 418
(...skipping 23 matching lines...) Expand all
392 'use strict'; 442 'use strict';
393 443
394 // ShouldModel internal class. For the exposed (factory) method, it is the 444 // ShouldModel internal class. For the exposed (factory) method, it is the
395 // return value of this closure. 445 // return value of this closure.
396 function ShouldModel(desc, target, opts) { 446 function ShouldModel(desc, target, opts) {
397 this.desc = desc; 447 this.desc = desc;
398 this.target = target; 448 this.target = target;
399 449
400 // Check if the target contains any NaN value. 450 // Check if the target contains any NaN value.
401 this._checkNaN(this.target, 'ACTUAL'); 451 this._checkNaN(this.target, 'ACTUAL');
402 // if (resultNaNCheck.length > 0) {
403 // var failureMessage = 'NaN found while testing the target (' + lab el + ')';
404 // testFailed(failureMessage + ': "' + this.desc + '" \n' +
405 // resultNaNCheck);
406 // throw failureMessage;
407 // }
408 452
409 // |_testPassed| and |_testFailed| set this appropriately. 453 // |_testPassed| and |_testFailed| set this appropriately.
410 this._success = false; 454 this._success = false;
411 455
412 // If the number of errors is greater than this, the rest of error 456 // If the number of errors is greater than this, the rest of error
413 // messages are suppressed. the value is fairly arbitrary, but shouldn't 457 // messages are suppressed. the value is fairly arbitrary, but shouldn't
414 // be too small or too large. 458 // be too small or too large.
415 this.NUM_ERRORS_LOG = opts.numberOfErrorLog; 459 this.NUM_ERRORS_LOG = opts.numberOfErrorLog;
416 460
417 // If the number of array elements is greater than this, the rest of 461 // If the number of array elements is greater than this, the rest of
418 // elements will be omitted. 462 // elements will be omitted.
419 this.NUM_ARRAY_LOG = opts.numberOfArrayLog; 463 this.NUM_ARRAY_LOG = opts.numberOfArrayLog;
420 464
421 // If true, verbose output for the failure case is printed, for methods where this makes 465 // If true, verbose output for the failure case is printed, for methods where this makes
422 // sense. 466 // sense.
423 this.verbose = !opts.brief; 467 this.verbose = !opts.brief;
424 468
425 // If set, this is the precision with which numbers will be printed. 469 // If set, this is the precision with which numbers will be printed.
426 this.PRINT_PRECISION = opts.precision; 470 this.PRINT_PRECISION = opts.precision;
427 } 471 }
428 472
429 // Internal methods starting with a underscore. 473 // Internal methods starting with a underscore.
430 ShouldModel.prototype._testPassed = function (msg) { 474 ShouldModel.prototype._testPassed = function (msg) {
431 testPassed(this.desc + ' ' + msg + '.');
432 this._success = true; 475 this._success = true;
476 if (TESTHARNESS) {
477 // Using testharness.js
478 test(function () {
479 assert_true(true);
Raymond Toy 2016/08/04 18:07:08 This is different from what you showed me this mor
hongchan 2016/08/04 18:31:37 When the assert passes, we should print out the me
480 }, this.desc + ' ' + msg + '.');
481 } else {
482 // Using js-test.js
483 testPassed(this.desc + ' ' + msg + '.');
484 }
433 }; 485 };
434 486
435 ShouldModel.prototype._testFailed = function (msg) { 487 ShouldModel.prototype._testFailed = function (msg) {
436 testFailed(this.desc + ' ' + msg + '.');
437 this._success = false; 488 this._success = false;
489 var that = this;
490 if (TESTHARNESS) {
491 test(function () {
492 assert_true(false, that.desc + ' ' + msg + '.');
Raymond Toy 2016/08/04 18:07:08 Why is this different from line 479?
hongchan 2016/08/04 18:31:37 On the other hand, this description message gets p
493 }, this.desc);
494 } else {
495 testFailed(this.desc + ' ' + msg + '.');
496 }
438 }; 497 };
439 498
440 ShouldModel.prototype._isArray = function (arg) { 499 ShouldModel.prototype._isArray = function (arg) {
441 return arg instanceof Array || arg instanceof Float32Array || arg instance of Uint8Array || 500 return arg instanceof Array || arg instanceof Float32Array || arg instance of Uint8Array ||
442 arg instanceof Uint16Array || arg instanceof Uint32Array || arg instance of Int8Array || 501 arg instanceof Uint16Array || arg instanceof Uint32Array || arg instance of Int8Array ||
443 arg instanceof Int16Array || arg instanceof Int32Array || arg instanceof Uint8ClampedArray || 502 arg instanceof Int16Array || arg instanceof Int32Array || arg instanceof Uint8ClampedArray ||
444 arg instanceof Float64Array; 503 arg instanceof Float64Array;
445 }; 504 };
446 505
447 ShouldModel.prototype._assert = function (expression, reason, value) { 506 ShouldModel.prototype._assert = function (expression, reason, value) {
448 if (expression) 507 if (expression)
449 return; 508 return;
450 509
451 var failureMessage = 'Assertion failed: ' + reason + ' ' + this.desc +'. '; 510 var failureMessage = 'Assertion failed: ' + reason + ' ' + this.desc +'. ';
452 if (arguments.length >= 3) 511 if (arguments.length >= 3)
453 failureMessage += ": " + value; 512 failureMessage += ": " + value;
454 testFailed(failureMessage); 513
514 if (TESTHARNESS) {
515 test(function () {
516 assert_true(false, reason + ' (' + value + ')');
Raymond Toy 2016/08/04 18:07:08 Hard to tell, but why is this different from just
hongchan 2016/08/04 18:31:37 When assert_true fails it prints out the message s
517 }, this.desc)
518 } else {
519 testFailed(failureMessage);
520 }
521
455 throw failureMessage; 522 throw failureMessage;
456 }; 523 };
457 524
458 // Check the expected value if it is a NaN (Number) or has NaN(s) in 525 // Check the expected value if it is a NaN (Number) or has NaN(s) in
459 // its content (Array or Float32Array). Returns a string depends on the 526 // its content (Array or Float32Array). Returns a string depends on the
460 // result of check. 527 // result of check.
461 ShouldModel.prototype._checkNaN = function (value, label) { 528 ShouldModel.prototype._checkNaN = function (value, label) {
462 var failureMessage = 'NaN found in ' + label + ' while testing "' + 529 var failureMessage = 'NaN found in ' + label + ' while testing "' +
463 this.desc + '"'; 530 this.desc + '"';
464 531
465 // Checking a single variable first. 532 // Checking a single variable first.
466 if (Number.isNaN(value)) { 533 if (Number.isNaN(value)) {
467 testFailed(failureMessage); 534 if (TESTHARNESS) {
535 test(function () {
536 assert_true(false, failureMessage);
537 }, this.desc)
538 } else {
539 testFailed(failureMessage);
540 }
541
468 throw failureMessage; 542 throw failureMessage;
469 } 543 }
470 544
471 // If the value is not a NaN nor array, we can assume it is safe. 545 // If the value is not a NaN nor array, we can assume it is safe.
472 if (!this._isArray(value)) 546 if (!this._isArray(value))
473 return; 547 return;
474 548
475 // Otherwise, check the array array. 549 // Otherwise, check the array array.
476 var indices = []; 550 var indices = [];
477 for (var i = 0; i < value.length; i++) { 551 for (var i = 0; i < value.length; i++) {
478 if (Number.isNaN(value[i])) 552 if (Number.isNaN(value[i]))
479 indices.push(i); 553 indices.push(i);
480 } 554 }
481 555
482 if (indices.length === 0) 556 if (indices.length === 0)
483 return; 557 return;
484 558
485 var failureDetail = ' (' + indices.length + ' instances total)\n'; 559 var failureDetail = ' (' + indices.length + ' instances total)\n';
486 for (var n = 0; n < indices.length; n++) { 560 for (var n = 0; n < indices.length; n++) {
487 failureDetail += ' >> [' + indices[n] + '] = NaN\n'; 561 failureDetail += ' >> [' + indices[n] + '] = NaN\n';
488 if (n >= this.NUM_ERRORS_LOG) { 562 if (n >= this.NUM_ERRORS_LOG) {
489 failureDetail += ' and ' + (indices.length - n) + 563 failureDetail += ' and ' + (indices.length - n) +
490 ' more NaNs...'; 564 ' more NaNs...';
491 break; 565 break;
492 } 566 }
493 } 567 }
494 568
495 testFailed(failureMessage + failureDetail); 569 if (TESTHARNESS) {
570 test(function () {
571 assert_true(false, failureMessage + failureDetail);
572 }, this.desc)
573 } else {
574 testFailed(failureMessage + failureDetail);
575 }
576
496 throw failureMessage; 577 throw failureMessage;
497 }; 578 };
498 579
499 // Check if |target| is equal to |value|. 580 // Check if |target| is equal to |value|.
500 // 581 //
501 // Example: 582 // Example:
502 // Should('Zero', 0).beEqualTo(0); 583 // Should('Zero', 0).beEqualTo(0);
503 // Result: 584 // Result:
504 // "PASS Zero is equal to 0." 585 // "PASS Zero is equal to 0."
505 ShouldModel.prototype.beEqualTo = function (value) { 586 ShouldModel.prototype.beEqualTo = function (value) {
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after
1058 if (opts.hasOwnProperty('brief')) 1139 if (opts.hasOwnProperty('brief'))
1059 _opts.brief = opts.brief; 1140 _opts.brief = opts.brief;
1060 if (opts.hasOwnProperty('precision')) 1141 if (opts.hasOwnProperty('precision'))
1061 _opts.precision = opts.precision; 1142 _opts.precision = opts.precision;
1062 } 1143 }
1063 1144
1064 return new ShouldModel(desc, target, _opts); 1145 return new ShouldModel(desc, target, _opts);
1065 }; 1146 };
1066 1147
1067 })(); 1148 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698