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

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: Changing js-test.js loading order 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 // testharness.js has the higher priority.
4 var TESTHARNESS = true;
5 var JSTEST = false;
6
7 (function () {
8 // Selected properies from testharness.js
9 var testharnessProperties = [
10 'test', 'async_test', 'promise_test', 'promise_rejects',
11 'generate_tests', 'setup', 'done', 'assert_true', 'assert_false'
12 ];
13
14 // Selected properties from js-test.js
15 var jsTestProperties = [
16 'isJsTest', 'testPassed', 'testFailed', 'gc', 'finishJSTest'
17 ];
18
19 // Check if testharness.js is properly loaded and set up a flag for it.
20 for (var name in testharnessProperties) {
21 if (!self.hasOwnProperty(testharnessProperties[name])) {
22 TESTHARNESS = false;
23 break;
24 }
25 }
26
27 // Immediately return here because testharness.js is ready.
28 if (TESTHARNESS)
29 return;
30
31 // Because testharness.js is not loaded, let us assume that js-test.js might
32 // be in use. Check if js-test.js is properly loaded and set up a flag for
33 // it.
34 JSTEST = true;
35 for (var name in jsTestProperties) {
36 if (!self.hasOwnProperty(jsTestProperties[name])) {
37 JSTEST = false;
38 break;
39 }
40 }
41
42 // If both are not loaded at all, throw here.
43 if (!JSTEST)
44 throw new Error('Cannot proceed. No test infrastructure is loaded.');
45 })();
46
47
48
1 function writeString(s, a, offset) { 49 function writeString(s, a, offset) {
2 for (var i = 0; i < s.length; ++i) { 50 for (var i = 0; i < s.length; ++i) {
3 a[offset + i] = s.charCodeAt(i); 51 a[offset + i] = s.charCodeAt(i);
4 } 52 }
5 } 53 }
6 54
7 function writeInt16(n, a, offset) { 55 function writeInt16(n, a, offset) {
8 n = Math.floor(n); 56 n = Math.floor(n);
9 57
10 var b1 = n & 255; 58 var b1 = n & 255;
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 var Audit = (function () { 342 var Audit = (function () {
295 343
296 'use strict'; 344 'use strict';
297 345
298 function Tasks() { 346 function Tasks() {
299 this.tasks = {}; 347 this.tasks = {};
300 this.queue = []; 348 this.queue = [];
301 this.currentTask = 0; 349 this.currentTask = 0;
302 } 350 }
303 351
352 // This is to prime the task runner for the testharness.js async operation.
353 Tasks.prototype._initialize = function () {
354 if (TESTHARNESS) {
355 setup(new Function(), {
356 explicit_done: true
357 });
358 }
359 };
360
361 // Finalize the task runner by notifying testharness and testRunner that
362 // all the task is completed.
363 Tasks.prototype._finalize = function () {
364 if (TESTHARNESS) {
365 // From testharness.js
366 done();
367 }
368 };
369
304 Tasks.prototype.defineTask = function (taskName, taskFunc) { 370 Tasks.prototype.defineTask = function (taskName, taskFunc) {
305 // Check if there is a task defined with the same name. If found, do 371 // Check if there is a task defined with the same name. If found, do
306 // not add the task to the roster. 372 // not add the task to the roster.
307 if (this.tasks.hasOwnProperty(taskName)) { 373 if (this.tasks.hasOwnProperty(taskName)) {
308 debug('>> Audit.defineTask:: Duplicate task definition. ("' + taskNa me + '")'); 374 debug('>> Audit.defineTask:: Duplicate task definition. ("' + taskNa me + '")');
309 return; 375 return;
310 } 376 }
311 377
312 this.tasks[taskName] = taskFunc; 378 this.tasks[taskName] = taskFunc;
313 379
314 // Push the task name in the order of definition. 380 // Push the task name in the order of definition.
315 this.queue.push(taskName); 381 this.queue.push(taskName);
316 }; 382 };
317 383
318 // If arguments are given, only run the requested tasks. Check for any 384 // If arguments are given, only run the requested tasks. Check for any
319 // undefined or duplicate task in the requested task arguments. If there 385 // undefined or duplicate task in the requested task arguments. If there
320 // is no argument, run all the defined tasks. 386 // is no argument, run all the defined tasks.
321 Tasks.prototype.runTasks = function () { 387 Tasks.prototype.runTasks = function () {
322 388
389 this._initialize();
390
323 if (arguments.length > 0) { 391 if (arguments.length > 0) {
324 392
325 // Reset task queue and refill it with the with the given arguments, 393 // Reset task queue and refill it with the with the given arguments,
326 // in argument order. 394 // in argument order.
327 this.queue = []; 395 this.queue = [];
328 396
329 for (var i = 0; i < arguments.length; i++) { 397 for (var i = 0; i < arguments.length; i++) {
330 if (!this.tasks.hasOwnProperty(arguments[i])) 398 if (!this.tasks.hasOwnProperty(arguments[i]))
331 debug('>> Audit.runTasks:: Ignoring undefined task. ("' + ar guments[i] + '")'); 399 debug('>> Audit.runTasks:: Ignoring undefined task. ("' + ar guments[i] + '")');
332 else if (this.queue.indexOf(arguments[i]) > -1) 400 else if (this.queue.indexOf(arguments[i]) > -1)
333 debug('>> Audit.runTasks:: Ignoring duplicate task request. ("' + arguments[i] + '")'); 401 debug('>> Audit.runTasks:: Ignoring duplicate task request. ("' + arguments[i] + '")');
334 else 402 else
335 this.queue.push(arguments[i]); 403 this.queue.push(arguments[i]);
336 } 404 }
337 } 405 }
338 406
339 if (this.queue.length === 0) { 407 if (this.queue.length === 0) {
340 debug('>> Audit.runTasks:: No task to run.'); 408 debug('>> Audit.runTasks:: No task to run.');
341 return; 409 return;
342 } 410 }
343 411
344 // done() callback from each task. Increase the task index and call the 412 // taskDone() callback from each task. Increase the task index and call
345 // next task. Note that explicit signaling by done() in each task 413 // the next task. Note that explicit signaling by taskDone() in each
346 // is needed because some of tests run asynchronously. 414 // task is needed because some of tests run asynchronously.
347 var done = function () { 415 var taskDone = function () {
348 if (this.currentTask !== this.queue.length - 1) { 416 if (this.currentTask !== this.queue.length - 1) {
349 ++this.currentTask; 417 ++this.currentTask;
350 // debug('>> Audit.runTasks: ' + this.queue[this.currentTask]); 418 // debug('>> Audit.runTasks: ' + this.queue[this.currentTask]);
351 this.tasks[this.queue[this.currentTask]](done); 419 this.tasks[this.queue[this.currentTask]](taskDone);
420 } else {
421 this._finalize();
352 } 422 }
353 return; 423 return;
354 }.bind(this); 424 }.bind(this);
355 425
356 // Start the first task. 426 // Start the first task.
357 // debug('>> Audit.runTasks: ' + this.queue[this.currentTask]); 427 // debug('>> Audit.runTasks: ' + this.queue[this.currentTask]);
358 this.tasks[this.queue[this.currentTask]](done); 428 this.tasks[this.queue[this.currentTask]](taskDone);
359 }; 429 };
360 430
361 return { 431 return {
362 createTaskRunner: function () { 432 createTaskRunner: function () {
363 return new Tasks(); 433 return new Tasks();
364 } 434 }
365 }; 435 };
366 436
367 })(); 437 })();
368 438
(...skipping 23 matching lines...) Expand all
392 'use strict'; 462 'use strict';
393 463
394 // ShouldModel internal class. For the exposed (factory) method, it is the 464 // ShouldModel internal class. For the exposed (factory) method, it is the
395 // return value of this closure. 465 // return value of this closure.
396 function ShouldModel(desc, target, opts) { 466 function ShouldModel(desc, target, opts) {
397 this.desc = desc; 467 this.desc = desc;
398 this.target = target; 468 this.target = target;
399 469
400 // Check if the target contains any NaN value. 470 // Check if the target contains any NaN value.
401 this._checkNaN(this.target, 'ACTUAL'); 471 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 472
409 // |_testPassed| and |_testFailed| set this appropriately. 473 // |_testPassed| and |_testFailed| set this appropriately.
410 this._success = false; 474 this._success = false;
411 475
412 // If the number of errors is greater than this, the rest of error 476 // 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 477 // messages are suppressed. the value is fairly arbitrary, but shouldn't
414 // be too small or too large. 478 // be too small or too large.
415 this.NUM_ERRORS_LOG = opts.numberOfErrorLog; 479 this.NUM_ERRORS_LOG = opts.numberOfErrorLog;
416 480
417 // If the number of array elements is greater than this, the rest of 481 // If the number of array elements is greater than this, the rest of
418 // elements will be omitted. 482 // elements will be omitted.
419 this.NUM_ARRAY_LOG = opts.numberOfArrayLog; 483 this.NUM_ARRAY_LOG = opts.numberOfArrayLog;
420 484
421 // If true, verbose output for the failure case is printed, for methods where this makes 485 // If true, verbose output for the failure case is printed, for methods where this makes
422 // sense. 486 // sense.
423 this.verbose = !opts.brief; 487 this.verbose = !opts.brief;
424 488
425 // If set, this is the precision with which numbers will be printed. 489 // If set, this is the precision with which numbers will be printed.
426 this.PRINT_PRECISION = opts.precision; 490 this.PRINT_PRECISION = opts.precision;
427 } 491 }
428 492
429 // Internal methods starting with a underscore. 493 // Internal methods starting with a underscore.
430 ShouldModel.prototype._testPassed = function (msg) { 494 ShouldModel.prototype._testPassed = function (msg) {
431 testPassed(this.desc + ' ' + msg + '.');
432 this._success = true; 495 this._success = true;
496 if (TESTHARNESS) {
497 // Using testharness.js
498 test(function () {
499 assert_true(true);
500 }, this.desc + ' ' + msg + '.');
501 } else {
502 // Using js-test.js
503 testPassed(this.desc + ' ' + msg + '.');
504 }
433 }; 505 };
434 506
435 ShouldModel.prototype._testFailed = function (msg) { 507 ShouldModel.prototype._testFailed = function (msg) {
436 testFailed(this.desc + ' ' + msg + '.');
437 this._success = false; 508 this._success = false;
509 var that = this;
510 if (TESTHARNESS) {
511 test(function () {
512 assert_true(false, that.desc + ' ' + msg + '.');
513 }, this.desc);
514 } else {
515 testFailed(this.desc + ' ' + msg + '.');
516 }
438 }; 517 };
439 518
440 ShouldModel.prototype._isArray = function (arg) { 519 ShouldModel.prototype._isArray = function (arg) {
441 return arg instanceof Array || arg instanceof Float32Array || arg instance of Uint8Array || 520 return arg instanceof Array || arg instanceof Float32Array || arg instance of Uint8Array ||
442 arg instanceof Uint16Array || arg instanceof Uint32Array || arg instance of Int8Array || 521 arg instanceof Uint16Array || arg instanceof Uint32Array || arg instance of Int8Array ||
443 arg instanceof Int16Array || arg instanceof Int32Array || arg instanceof Uint8ClampedArray || 522 arg instanceof Int16Array || arg instanceof Int32Array || arg instanceof Uint8ClampedArray ||
444 arg instanceof Float64Array; 523 arg instanceof Float64Array;
445 }; 524 };
446 525
447 ShouldModel.prototype._assert = function (expression, reason, value) { 526 ShouldModel.prototype._assert = function (expression, reason, value) {
448 if (expression) 527 if (expression)
449 return; 528 return;
450 529
451 var failureMessage = 'Assertion failed: ' + reason + ' ' + this.desc +'. '; 530 var failureMessage = 'Assertion failed: ' + reason + ' ' + this.desc +'. ';
452 if (arguments.length >= 3) 531 if (arguments.length >= 3)
453 failureMessage += ": " + value; 532 failureMessage += ": " + value;
454 testFailed(failureMessage); 533
534 if (TESTHARNESS) {
535 test(function () {
536 assert_true(false, reason + ' (' + value + ')');
537 }, this.desc)
538 } else {
539 testFailed(failureMessage);
540 }
541
455 throw failureMessage; 542 throw failureMessage;
456 }; 543 };
457 544
458 // Check the expected value if it is a NaN (Number) or has NaN(s) in 545 // 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 546 // its content (Array or Float32Array). Returns a string depends on the
460 // result of check. 547 // result of check.
461 ShouldModel.prototype._checkNaN = function (value, label) { 548 ShouldModel.prototype._checkNaN = function (value, label) {
462 var failureMessage = 'NaN found in ' + label + ' while testing "' + 549 var failureMessage = 'NaN found in ' + label + ' while testing "' +
463 this.desc + '"'; 550 this.desc + '"';
464 551
465 // Checking a single variable first. 552 // Checking a single variable first.
466 if (Number.isNaN(value)) { 553 if (Number.isNaN(value)) {
467 testFailed(failureMessage); 554 if (TESTHARNESS) {
555 test(function () {
556 assert_true(false, failureMessage);
557 }, this.desc)
558 } else {
559 testFailed(failureMessage);
560 }
561
468 throw failureMessage; 562 throw failureMessage;
469 } 563 }
470 564
471 // If the value is not a NaN nor array, we can assume it is safe. 565 // If the value is not a NaN nor array, we can assume it is safe.
472 if (!this._isArray(value)) 566 if (!this._isArray(value))
473 return; 567 return;
474 568
475 // Otherwise, check the array array. 569 // Otherwise, check the array array.
476 var indices = []; 570 var indices = [];
477 for (var i = 0; i < value.length; i++) { 571 for (var i = 0; i < value.length; i++) {
478 if (Number.isNaN(value[i])) 572 if (Number.isNaN(value[i]))
479 indices.push(i); 573 indices.push(i);
480 } 574 }
481 575
482 if (indices.length === 0) 576 if (indices.length === 0)
483 return; 577 return;
484 578
485 var failureDetail = ' (' + indices.length + ' instances total)\n'; 579 var failureDetail = ' (' + indices.length + ' instances total)\n';
486 for (var n = 0; n < indices.length; n++) { 580 for (var n = 0; n < indices.length; n++) {
487 failureDetail += ' >> [' + indices[n] + '] = NaN\n'; 581 failureDetail += ' >> [' + indices[n] + '] = NaN\n';
488 if (n >= this.NUM_ERRORS_LOG) { 582 if (n >= this.NUM_ERRORS_LOG) {
489 failureDetail += ' and ' + (indices.length - n) + 583 failureDetail += ' and ' + (indices.length - n) +
490 ' more NaNs...'; 584 ' more NaNs...';
491 break; 585 break;
492 } 586 }
493 } 587 }
494 588
495 testFailed(failureMessage + failureDetail); 589 if (TESTHARNESS) {
590 test(function () {
591 assert_true(false, failureMessage + failureDetail);
592 }, this.desc)
593 } else {
594 testFailed(failureMessage + failureDetail);
595 }
596
496 throw failureMessage; 597 throw failureMessage;
497 }; 598 };
498 599
499 // Check if |target| is equal to |value|. 600 // Check if |target| is equal to |value|.
500 // 601 //
501 // Example: 602 // Example:
502 // Should('Zero', 0).beEqualTo(0); 603 // Should('Zero', 0).beEqualTo(0);
503 // Result: 604 // Result:
504 // "PASS Zero is equal to 0." 605 // "PASS Zero is equal to 0."
505 ShouldModel.prototype.beEqualTo = function (value) { 606 ShouldModel.prototype.beEqualTo = function (value) {
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after
1058 if (opts.hasOwnProperty('brief')) 1159 if (opts.hasOwnProperty('brief'))
1059 _opts.brief = opts.brief; 1160 _opts.brief = opts.brief;
1060 if (opts.hasOwnProperty('precision')) 1161 if (opts.hasOwnProperty('precision'))
1061 _opts.precision = opts.precision; 1162 _opts.precision = opts.precision;
1062 } 1163 }
1063 1164
1064 return new ShouldModel(desc, target, _opts); 1165 return new ShouldModel(desc, target, _opts);
1065 }; 1166 };
1066 1167
1067 })(); 1168 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698