1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
base.requireStylesheet('ui.trace_viewer');
base.requireStylesheet('base.unittest');
base.require('base.key_event_manager');
base.require('base.promise');
base.require('base.settings');
base.require('base.unittest.test_error');
base.require('base.unittest.assertions');
base.exportTo('base.unittest', function() {
var TestStatus = {
FAILED: 0,
PASSED: 1,
PENDING: 2
};
var TestTypes = {
UNITTEST: 0,
PERFTEST: 1
};
var showCondensed_ = false;
var testType_ = TestTypes.UNITTEST;
function showCondensed(val) {
showCondensed_ = val;
}
function testType(val) {
if (val === 'perf')
testType_ = TestTypes.PERFTEST;
else
testType_ = TestTypes.UNITTEST;
}
function logWarningMessage(message) {
var messagesEl = document.querySelector('#messages');
messagesEl.setAttribute('hasMessages', true);
var li = document.createElement('li');
li.innerText = message;
var list = document.querySelector('#message-list');
list.appendChild(li);
}
function TestRunner(tests) {
this.suites_ = [];
this.suiteNames_ = {};
this.tests_ = tests || [];
this.moduleCount_ = 0;
this.stats_ = {
tests: 0,
failures: 0,
exceptions: [],
duration: 0.0
};
}
TestRunner.prototype = {
__proto__: Object.prototype,
run: function() {
this.clear_(document.querySelector('#test-results'));
this.clear_(document.querySelector('#exception-list'));
this.updateStats_();
this.runSuites_();
},
addSuite: function(suite) {
if (this.suiteNames_[suite.name] === true)
logWarningMessage('Duplicate test suite name detected: ' + suite.name);
this.suites_.push(suite);
this.suiteNames_[suite.name] = true;
},
get suiteCount() {
return this.suites_.length;
},
clear_: function(el) {
while (el.firstChild)
el.removeChild(el.firstChild);
},
runSuites_: function(opt_idx) {
var idx = opt_idx || 0;
var suiteCount = this.suites_.length;
if (idx >= suiteCount) {
var harness = document.querySelector('#test-results');
harness.appendChild(document.createElement('br'));
harness.appendChild(document.createTextNode('Test Run Complete'));
return;
}
var suite = this.suites_[idx];
suite.showLongResults = (suiteCount === 1);
suite.displayInfo();
return suite.runTests(this.tests_).then(function(ignored) {
this.stats_.duration += suite.duration;
this.stats_.tests += suite.testCount;
this.stats_.failures += suite.failureCount;
this.updateStats_();
return this.runSuites_(idx + 1);
}.bind(this));
},
onAnimationFrameError: function(e, opt_stack) {
if (e.message)
console.error(e.message, e.stack);
else
console.error(e);
var exception = {e: e, stack: opt_stack};
this.stats_.exceptions.push(exception);
this.appendException(exception);
this.updateStats_();
},
updateStats_: function() {
var statEl = document.querySelector('#stats');
statEl.innerHTML =
this.suites_.length + ' suites, ' +
'<span class="passed">' + this.stats_.tests + '</span> tests, ' +
'<span class="failed">' + this.stats_.failures +
'</span> failures, ' +
'<span class="exception">' + this.stats_.exceptions.length +
'</span> exceptions,' +
' in ' + this.stats_.duration.toFixed(2) + 'ms.';
},
appendException: function(exc) {
var exceptionsEl = document.querySelector('#exceptions');
exceptionsEl.setAttribute('hasExceptions', this.stats_.exceptions.length);
var excEl = document.createElement('li');
excEl.innerHTML = exc.e + '<pre>' + exc.stack + '</pre>';
var exceptionsEl = document.querySelector('#exception-list');
exceptionsEl.appendChild(excEl);
}
};
function TestSuite(name, suite) {
this.name_ = name;
this.tests_ = [];
this.testNames_ = {};
this.failures_ = [];
this.showLongResults = false;
this.duration_ = 0.0;
this.resultsEl_ = undefined;
global.setupOnce = function(fn) { this.setupOnceFn_ = fn; }.bind(this);
global.setup = function(fn) { this.setupFn_ = fn; }.bind(this);
global.teardown = function(fn) { this.teardownFn_ = fn; }.bind(this);
global.test = function(name, test, options) {
options = options || {};
if (this.testNames_[name] === true)
logWarningMessage('Duplicate test name detected: ' + name);
var testName = name;
// If the test cares about DPI settings then we first push a test
// that fakes the DPI as the low or hi Dpi version, depending on what
// we're current using.
if (options.dpiAware) {
var defaultDevicePixelRatio = window.devicePixelRatio;
var dpi = defaultDevicePixelRatio > 1 ? 1 : 2;
var testWrapper = function() {
window.devicePixelRatio = dpi;
test.bind(this).call();
window.devicePixelRatio = defaultDevicePixelRatio;
};
var newName = name;
if (dpi === 1) {
newName += '_loDPI';
testName += '_hiDPI';
} else {
newName += '_hiDPI';
testName += '_loDPI';
}
this.tests_.push(new Test(newName, testWrapper, options || {}));
}
this.tests_.push(new Test(testName, test, options || {}));
this.testNames_[name] = true;
}.bind(this);
global.perfTest = function(name, test, options) {
if (this.testNames_[name] === true)
logWarningMessage('Duplicate test name detected: ' + name);
this.tests_.push(new PerfTest(name, test, options || {}));
this.testNames_[name] = true;
}.bind(this);
global.timedPerfTest = function(name, test, options) {
if (options === undefined || options.iterations === undefined)
throw new Error('timedPerfTest must have iteration option provided.');
name += '_' + options.iterations;
if (this.testNames_[name] === true)
logWarningMessage('Duplicate test name detected: ' + name);
options.results = options.results || TimingTestResult;
var testWrapper = function(results) {
results.testCount = options.iterations;
for (var i = 0; i < options.iterations; ++i) {
var start = window.performance.now();
test.bind(this).call();
results.add(window.performance.now() - start);
}
};
this.tests_.push(new PerfTest(name, testWrapper, options));
this.testNames_[name] = true;
}.bind(this);
suite.call();
global.setupOnce = undefined;
global.setup = undefined;
global.teardown = undefined;
global.test = undefined;
global.perfTest = undefined;
global.timedPerfTest = undefined;
}
TestSuite.prototype = {
__proto__: Object.prototype,
get name() {
return this.name_;
},
get results() {
return (this.failureCount > 0) ? TestStatus.FAILED : TestStatus.PASSED;
},
get testCount() {
return this.tests_.length;
},
get failureCount() {
return this.failures.length;
},
get failures() {
return this.failures_;
},
get duration() {
return this.duration_;
},
displayInfo: function() {
this.resultsEl_ = document.createElement('div');
this.resultsEl_.className = 'test-result';
var resultsPanel = document.querySelector('#test-results');
resultsPanel.appendChild(this.resultsEl_);
if (this.showLongResults) {
this.resultsEl_.innerText = this.name;
} else {
var link = '/src/tests.html?suite=';
link += this.name.replace(/\./g, '/');
link += '&type=' + (testType_ === TestTypes.PERFTEST ? 'perf' : 'unit');
var suiteInfo = document.createElement('a');
suiteInfo.href = link;
suiteInfo.innerText = this.name;
this.resultsEl_.appendChild(suiteInfo);
}
var statusEl = document.createElement('span');
statusEl.classList.add('results');
statusEl.classList.add('pending');
statusEl.innerText = 'pending';
this.resultsEl_.appendChild(statusEl);
},
runTests: function(testsToRun) {
this.testsToRun_ = testsToRun;
if (this.setupOnceFn_ !== undefined)
this.setupOnceFn_.bind(this).call();
var remainingTests;
if (testsToRun.length) {
remainingTests = this.tests_.reduce(function(remainingTests, test) {
if (this.testsToRun_.indexOf(test.name) !== -1)
remainingTests.push(test);
return remainingTests;
}.bind(this), []);
} else {
remainingTests = this.tests_.slice(0);
}
return this.runRemainingTests_(remainingTests).then(
function resolve(ignored) {
this.duration_ = this.tests_.reduce(function(total, test) {
return total += test.duration;
}, 0);
this.outputResults();
}.bind(this),
function reject(e) {
console.error(e);
this.outputResults();
}.bind(this)
);
},
runRemainingTests_: function(remainingTests) {
if (!remainingTests.length)
return base.Promise.resolve();
var test = remainingTests.pop();
// Clear settings storage before each test.
global.sessionStorage.clear();
base.Settings.setAlternativeStorageInstance(global.sessionStorage);
base.onAnimationFrameError =
testRunners[testType_].onAnimationFrameError.bind(
testRunners[testType_]);
base.KeyEventManager.resetInstanceForUnitTesting();
var testWorkAreaEl_ = document.createElement('div');
this.resultsEl_.appendChild(testWorkAreaEl_);
test.workArea = testWorkAreaEl_;
if (this.setupFn_ !== undefined)
this.setupFn_.bind(test).call();
var suite = this;
return test.run.then(function(ignore) {
test.status = TestStatus.PASSED;
suite.testTearDown_(test);
return suite.runRemainingTests_(remainingTests);
}, function(error) {
var stack = error && error.stack ? error.stack : '';
console.error("Rejected, cause: \'" + error + "\'", stack);
test.status = TestStatus.FAILED;
test.failure = error;
suite.failures_.push({
error: test.failure,
test: test.name
});
suite.testTearDown_(test);
return suite.runRemainingTests_(remainingTests);
});
},
testTearDown_: function(test) {
this.resultsEl_.removeChild(test.workArea);
if (this.teardownFn_ !== undefined)
this.teardownFn_.bind(test).call();
},
outputResults: function() {
if ((this.results === TestStatus.PASSED) && showCondensed_ &&
!this.showLongResults) {
var parent = this.resultsEl_.parentNode;
parent.removeChild(this.resultsEl_);
this.resultsEl_ = undefined;
parent.appendChild(document.createTextNode('.'));
return;
}
var status = this.resultsEl_.querySelector('.results');
status.classList.remove('pending');
if (this.results === TestStatus.PASSED) {
status.innerText = 'passed';
status.classList.add('passed');
} else {
status.innerText = 'FAILED';
status.classList.add('failed');
}
status.innerText += ' (' + this.duration_.toFixed(2) + 'ms)';
var child = this.showLongResults ? this.outputLongResults() :
this.outputShortResults();
if (child !== undefined)
this.resultsEl_.appendChild(child);
},
outputShortResults: function() {
if (this.results === TestStatus.PASSED)
return undefined;
var parent = document.createElement('div');
var failureList = this.failures;
for (var i = 0; i < failureList.length; ++i) {
var fail = failureList[i];
var preEl = document.createElement('pre');
preEl.className = 'failure';
preEl.innerText = 'Test: ' + fail.test + '\n' + fail.error.stack;
parent.appendChild(preEl);
}
return parent;
},
outputLongResults: function() {
var parent = document.createElement('div');
this.tests_.forEach(function(test) {
if (this.testsToRun_.length !== 0 &&
this.testsToRun_.indexOf(test.name) === -1)
return;
// Construct an individual result div.
var testEl = document.createElement('div');
testEl.className = 'individual-result';
var link = '/src/tests.html?suite=';
link += this.name.replace(/\./g, '/');
link += '&test=' + test.name.replace(/\./g, '/');
link += '&type=' +
(testType_ === TestTypes.PERFTEST ? 'perf' : 'unit');
var suiteInfo = document.createElement('a');
suiteInfo.href = link;
suiteInfo.innerText = test.name;
testEl.appendChild(suiteInfo);
parent.appendChild(testEl);
var resultEl = document.createElement('span');
resultEl.classList.add('results');
testEl.appendChild(resultEl);
if (test.status === TestStatus.PASSED) {
resultEl.classList.add('passed');
resultEl.innerText =
'passed (' + test.output() + ')';
} else if (test.status === TestStatus.PENDING) {
resultEl.classList.add('failed');
resultEl.innerText = 'PENDING...TIMEOUT';
} else {
resultEl.classList.add('failed');
resultEl.innerText = 'FAILED';
var preEl = document.createElement('pre');
preEl.className = 'failure';
preEl.innerText = test.failure.stack || test.failure;
testEl.appendChild(preEl);
}
if (test.hasAppendedContent)
testEl.appendChild(test.appendedContent);
}.bind(this));
return parent;
},
toString: function() {
return this.name_;
}
};
function Test(name, test, options) {
this.name_ = name;
this.test_ = test;
this.isPerfTest_ = false;
this.options_ = options;
this.failure_ = undefined;
this.duration_ = 0;
this.status_ = TestStatus.PENDING;
this.appendedContent_ = undefined;
}
Test.prototype = {
__proto__: Object.prototype,
get run() {
var test = this;
return new base.Promise(function(r) {
var startTime = window.performance.now();
try {
var maybePromise = test.test_();
if (maybePromise) {
// An async test may not have completed.
maybePromise.then(function(ignored) {
test.duration = window.performance.now() - startTime;
r.resolve();
}, r.reject);
} else {
test.duration = window.performance.now() - startTime;
r.resolve();
}
} catch (e) {
test.duration = window.performance.now() - startTime;
r.reject(e);
}
});
},
get failure() {
return this.failure_;
},
set failure(val) {
this.failure_ = val;
},
get name() {
return this.name_;
},
get isPerfTest() {
return this.isPerfTest_;
},
get testRuns() {
return this.testRuns_;
},
get status() {
return this.status_;
},
set status(val) {
this.status_ = val;
},
get duration() {
return this.duration_;
},
get options() {
return this.options_;
},
set duration(duration) {
this.duration_ = duration;
},
get hasAppendedContent() {
return (this.appendedContent_ !== undefined);
},
get appendedContent() {
return this.appendedContent_;
},
get workArea() {
return this.testWorkArea_;
},
set workArea(workArea) {
this.testWorkArea_ = workArea;
},
addHTMLOutput: function(element) {
this.testWorkArea_.appendChild(element);
this.appendedContent_ = element;
},
toString: function() {
return this.name_;
},
output: function() {
return this.duration_.toFixed(2) + 'ms';
}
};
function PerfTest(name, test, options) {
Test.apply(this, arguments);
this.isPerfTest_ = true;
var resultObject = options.results || TestResult;
this.results_ = new resultObject();
}
PerfTest.prototype = {
__proto__: Test.prototype,
run: function() {
try {
this.test_.call(this, this.results_);
this.status_ = TestStatus.PASSED;
} catch (e) {
console.error(e, e.stack);
this.failure_ = e;
}
},
output: function() {
return this.results_.output();
}
};
var testRunners = {};
var totalSuiteCount_ = 0;
function allSuitesLoaded_() {
return (testRunners[TestTypes.UNITTEST].suiteCount +
testRunners[TestTypes.PERFTEST].suiteCount) >= totalSuiteCount_;
}
function testSuite(name, suite) {
testRunners[TestTypes.UNITTEST].addSuite(new TestSuite(name, suite));
if (allSuitesLoaded_())
runSuites();
}
function perfTestSuite(name, suite) {
testRunners[TestTypes.PERFTEST].addSuite(new TestSuite(name, suite));
if (allSuitesLoaded_())
runSuites();
}
function Suites(suitePaths, tests) {
// Assume one suite per file.
totalSuiteCount_ = suitePaths.length;
testRunners[TestTypes.UNITTEST] = new TestRunner(tests);
testRunners[TestTypes.PERFTEST] = new TestRunner(tests);
var modules = [];
suitePaths.forEach(function(path) {
var moduleName = path.slice(5, path.length - 3);
moduleName = moduleName.replace(/\//g, '.');
modules.push(moduleName);
});
base.require(modules);
}
function runSuites() {
testRunners[testType_].run();
}
function TestResult() {
this.results_ = [];
}
TestResult.prototype = {
add: function(result) {
this.results_.push(result);
},
output: function() {
return this.results_.join(', ');
}
};
function TimingTestResult() {
TestResult.apply(this, arguments);
this.runCount_ = 0;
}
TimingTestResult.prototype = {
__proto__: TestResult.prototype,
set testCount(runs) {
this.runCount_ = runs;
},
output: function() {
var totalTime = 0.0;
this.results_.forEach(function(t) { totalTime += t; });
return totalTime.toFixed(2) + 'ms) ' + this.runCount_ + ' runs, ' +
'avg ' + (totalTime / this.runCount_).toFixed(2) + 'ms/run';
}
};
return {
showCondensed: showCondensed,
testType: testType,
testSuite: testSuite,
perfTestSuite: perfTestSuite,
runSuites: runSuites,
Suites: Suites,
TestSuite_: TestSuite
};
});
|