forked from Polymer/web-component-tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.js
1903 lines (1685 loc) · 61.1 KB
/
browser.js
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
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function () { 'use strict';
var util_js = {
get whenFrameworksReady () { return whenFrameworksReady; },
get pluralizedStat () { return pluralizedStat; },
get loadScript () { return loadScript; },
get loadStyle () { return loadStyle; },
get debug () { return debug; },
get parseUrl () { return parseUrl; },
get expandUrl () { return expandUrl; },
get getParams () { return getParams; },
get mergeParams () { return mergeParams; },
get getParam () { return getParam; },
get paramsToQuery () { return paramsToQuery; },
get basePath () { return basePath; },
get relativeLocation () { return relativeLocation; },
get cleanLocation () { return cleanLocation; },
get parallel () { return parallel; },
get scriptPrefix () { return scriptPrefix; }
};
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
function whenFrameworksReady(callback) {
debug('whenFrameworksReady');
var done = function() {
debug('whenFrameworksReady done');
callback();
};
function componentsReady() {
// handle Polymer 0.5 readiness
if (window.Polymer && Polymer.whenReady) {
Polymer.whenReady(done);
} else {
done();
}
}
// All our supported framework configurations depend on imports.
if (window.WebComponents) {
if (WebComponents.whenReady) {
WebComponents.whenReady(function() {
debug('WebComponents Ready');
componentsReady();
});
} else {
whenWebComponentsReady(componentsReady);
}
} else if (window.HTMLImports) {
HTMLImports.whenReady(function() {
debug('HTMLImports Ready');
componentsReady();
});
} else {
done();
}
}
function whenWebComponentsReady(cb) {
var after = function after() {
window.removeEventListener('WebComponentsReady', after);
debug('WebComponentsReady');
cb();
};
window.addEventListener('WebComponentsReady', after);
}
/**
* @param {number} count
* @param {string} kind
* @return {string} '<count> <kind> tests' or '<count> <kind> test'.
*/
function pluralizedStat(count, kind) {
if (count === 1) {
return count + ' ' + kind + ' test';
} else {
return count + ' ' + kind + ' tests';
}
}
/**
* @param {string} path The URI of the script to load.
* @param {function} done
*/
function loadScript(path, done) {
var script = document.createElement('script');
script.src = path;
if (done) {
script.onload = done.bind(null, null);
script.onerror = done.bind(null, 'Failed to load script ' + script.src);
}
document.head.appendChild(script);
}
/**
* @param {string} path The URI of the stylesheet to load.
* @param {function} done
*/
function loadStyle(path, done) {
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = path;
if (done) {
link.onload = done.bind(null, null);
link.onerror = done.bind(null, 'Failed to load stylesheet ' + link.href);
}
document.head.appendChild(link);
}
/**
* @param {...*} var_args Logs values to the console when the `debug`
* configuration option is true.
*/
function debug(var_args) {
if (!config_js.get('verbose')) return;
var args = [window.location.pathname];
args.push.apply(args, arguments);
(console.debug || console.log).apply(console, args);
}
// URL Processing
/**
* @param {string} url
* @return {{base: string, params: string}}
*/
function parseUrl(url) {
var parts = url.match(/^(.*?)(?:\?(.*))?$/);
return {
base: parts[1],
params: this.getParams(parts[2] || ''),
};
}
/**
* Expands a URL that may or may not be relative to `base`.
*
* @param {string} url
* @param {string} base
* @return {string}
*/
function expandUrl(url, base) {
if (!base) return url;
if (url.match(/^(\/|https?:\/\/)/)) return url;
if (base.substr(base.length - 1) !== '/') {
base = base + '/';
}
return base + url;
}
/**
* @param {string=} opt_query A query string to parse.
* @return {!Object<string, !Array<string>>} All params on the URL's query.
*/
function getParams(opt_query) {
var query = typeof opt_query === 'string' ? opt_query : window.location.search;
if (query.substring(0, 1) === '?') {
query = query.substring(1);
}
// python's SimpleHTTPServer tacks a `/` on the end of query strings :(
if (query.slice(-1) === '/') {
query = query.substring(0, query.length - 1);
}
if (query === '') return {};
var result = {};
query.split('&').forEach(function(part) {
var pair = part.split('=');
if (pair.length !== 2) {
console.warn('Invalid URL query part:', part);
return;
}
var key = decodeURIComponent(pair[0]);
var value = decodeURIComponent(pair[1]);
if (!result[key]) {
result[key] = [];
}
result[key].push(value);
});
return result;
}
/**
* Merges params from `source` into `target` (mutating `target`).
*
* @param {!Object<string, !Array<string>>} target
* @param {!Object<string, !Array<string>>} source
*/
function mergeParams(target, source) {
Object.keys(source).forEach(function(key) {
if (!(key in target)) {
target[key] = [];
}
target[key] = target[key].concat(source[key]);
});
}
/**
* @param {string} param The param to return a value for.
* @return {?string} The first value for `param`, if found.
*/
function getParam(param) {
var params = getParams();
return params[param] ? params[param][0] : null;
}
/**
* @param {!Object<string, !Array<string>>} params
* @return {string} `params` encoded as a URI query.
*/
function paramsToQuery(params) {
var pairs = [];
Object.keys(params).forEach(function(key) {
params[key].forEach(function(value) {
pairs.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
});
});
return '?' + pairs.join('&');
}
/**
* @param {!Location|string} location
* @return {string}
*/
function basePath(location) {
return (location.pathname || location).match(/^.*\//)[0];
}
/**
* @param {!Location|string} location
* @param {string} basePath
* @return {string}
*/
function relativeLocation(location, basePath) {
var path = location.pathname || location;
if (path.indexOf(basePath) === 0) {
path = path.substring(basePath.length);
}
return path;
}
/**
* @param {!Location|string} location
* @return {string}
*/
function cleanLocation(location) {
var path = location.pathname || location;
if (path.slice(-11) === '/index.html') {
path = path.slice(0, path.length - 10);
}
return path;
}
/**
* Like `async.parallelLimit`, but our own so that we don't force a dependency
* on downstream code.
*
* @param {!Array<function(function(*))>} runners Runners that call their given
* Node-style callback when done.
* @param {number|function(*)} limit Maximum number of concurrent runners.
* (optional).
* @param {?function(*)} done Callback that should be triggered once all runners
* have completed, or encountered an error.
*/
function parallel(runners, limit, done) {
if (typeof limit !== 'number') {
done = limit;
limit = 0;
}
if (!runners.length) return done();
var called = false;
var total = runners.length;
var numActive = 0;
var numDone = 0;
function runnerDone(error) {
if (called) return;
numDone = numDone + 1;
numActive = numActive - 1;
if (error || numDone >= total) {
called = true;
done(error);
} else {
runOne();
}
}
function runOne() {
if (limit && numActive >= limit) return;
if (!runners.length) return;
numActive = numActive + 1;
runners.shift()(runnerDone);
}
runners.forEach(runOne);
}
/**
* Finds the directory that a loaded script is hosted on.
*
* @param {string} filename
* @return {string?}
*/
function scriptPrefix(filename) {
var scripts = document.querySelectorAll('script[src*="' + filename + '"]');
if (scripts.length !== 1) return null;
var script = scripts[0].src;
return script.substring(0, script.indexOf(filename));
}
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
function ChildRunner(url, parentScope) {
var urlBits = util_js.parseUrl(url);
util_js.mergeParams(
urlBits.params, util_js.getParams(parentScope.location.search));
delete urlBits.params.cli_browser_id;
this.url = urlBits.base + util_js.paramsToQuery(urlBits.params);
this.parentScope = parentScope;
this.state = 'initializing';
}
// ChildRunners get a pretty generous load timeout by default.
ChildRunner.loadTimeout = 60000;
// We can't maintain properties on iframe elements in Firefox/Safari/???, so we
// track childRunners by URL.
ChildRunner._byUrl = {};
/**
* @return {ChildRunner} The `ChildRunner` that was registered for this window.
*/
ChildRunner.current = function() {
return ChildRunner.get(window);
};
/**
* @param {!Window} target A window to find the ChildRunner of.
* @param {boolean} traversal Whether this is a traversal from a child window.
* @return {ChildRunner} The `ChildRunner` that was registered for `target`.
*/
ChildRunner.get = function(target, traversal) {
var childRunner = ChildRunner._byUrl[target.location.href];
if (childRunner) return childRunner;
if (window.parent === window) { // Top window.
if (traversal) {
console.warn('Subsuite loaded but was never registered. This most likely is due to wonky history behavior. Reloading...');
window.location.reload();
}
return null;
}
// Otherwise, traverse.
return window.parent.WCT._ChildRunner.get(target, true);
};
/**
* Loads and runs the subsuite.
*
* @param {function} done Node-style callback.
*/
ChildRunner.prototype.run = function(done) {
util_js.debug('ChildRunner#run', this.url);
this.state = 'loading';
this.onRunComplete = done;
this.iframe = document.createElement('iframe');
this.iframe.src = this.url;
this.iframe.classList.add('subsuite');
var container = document.getElementById('subsuites');
if (!container) {
container = document.createElement('div');
container.id = 'subsuites';
document.body.appendChild(container);
}
container.appendChild(this.iframe);
// let the iframe expand the URL for us.
this.url = this.iframe.src;
ChildRunner._byUrl[this.url] = this;
this.timeoutId = setTimeout(
this.loaded.bind(this, new Error('Timed out loading ' + this.url)), ChildRunner.loadTimeout);
this.iframe.addEventListener('error',
this.loaded.bind(this, new Error('Failed to load document ' + this.url)));
this.iframe.contentWindow.addEventListener('DOMContentLoaded', this.loaded.bind(this, null));
};
/**
* Called when the sub suite's iframe has loaded (or errored during load).
*
* @param {*} error The error that occured, if any.
*/
ChildRunner.prototype.loaded = function(error) {
util_js.debug('ChildRunner#loaded', this.url, error);
// Not all targets have WCT loaded (compatiblity mode)
if (this.iframe.contentWindow.WCT) {
this.share = this.iframe.contentWindow.WCT.share;
}
if (error) {
this.signalRunComplete(error);
this.done();
}
};
/**
* Called in mocha/run.js when all dependencies have loaded, and the child is
* ready to start running tests
*
* @param {*} error The error that occured, if any.
*/
ChildRunner.prototype.ready = function(error) {
util_js.debug('ChildRunner#ready', this.url, error);
if (this.timeoutId) {
clearTimeout(this.timeoutId);
}
if (error) {
this.signalRunComplete(error);
this.done();
}
};
/** Called when the sub suite's tests are complete, so that it can clean up. */
ChildRunner.prototype.done = function done() {
util_js.debug('ChildRunner#done', this.url, arguments);
// make sure to clear that timeout
this.ready();
this.signalRunComplete();
if (!this.iframe) return;
// Be safe and avoid potential browser crashes when logic attempts to interact
// with the removed iframe.
setTimeout(function() {
this.iframe.parentNode.removeChild(this.iframe);
this.iframe = null;
}.bind(this), 1);
};
ChildRunner.prototype.signalRunComplete = function signalRunComplete(error) {
if (!this.onRunComplete) return;
this.state = 'complete';
this.onRunComplete(error);
this.onRunComplete = null;
};
var config_js = {
get _config () { return _config; },
get setup () { return setup; },
get get () { return get; }
};
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
var _config = {
/**
* `.js` scripts to be loaded (synchronously) before WCT starts in earnest.
*
* Paths are relative to `scriptPrefix`.
*/
environmentScripts: [
// https://github.com/PolymerLabs/stacky/issues/2
'stacky/lib/parsing.js',
'stacky/lib/formatting.js',
'stacky/lib/normalization.js',
'async/lib/async.js',
'lodash/lodash.js',
'mocha/mocha.js',
'chai/chai.js',
'sinonjs/sinon.js',
'sinon-chai/lib/sinon-chai.js',
'accessibility-developer-tools/dist/js/axs_testing.js',
'web-component-tester/runtime-helpers/a11ySuite.js'
],
/** Absolute root for client scripts. Detected in `setup()` if not set. */
root: null,
/** By default, we wait for any web component frameworks to load. */
waitForFrameworks: true,
/** Alternate callback for waiting for tests.
* `this` for the callback will be the window currently running tests.
*/
waitFor: null,
/** How many `.html` suites that can be concurrently loaded & run. */
numConcurrentSuites: 1,
/** Whether `console.error` should be treated as a test failure. */
trackConsoleError: true,
/** Configuration passed to mocha.setup. */
mochaOptions: {
timeout: 10 * 1000
},
/** Whether WCT should emit (extremely verbose) debugging log messages. */
verbose: false,
};
/**
* Merges initial `options` into WCT's global configuration.
*
* @param {Object} options The options to merge. See `browser/config.js` for a
* reference.
*/
function setup(options) {
var childRunner = ChildRunner.current();
if (childRunner) {
_deepMerge(_config, childRunner.parentScope.WCT._config);
// But do not force the mocha UI
delete _config.mochaOptions.ui;
}
if (options && typeof options === 'object') {
_deepMerge(_config, options);
}
if (!_config.root) {
// Sibling dependencies.
var root = util_js.scriptPrefix('browser.js');
_config.root = util_js.basePath(root.substr(0, root.length - 1));
if (!_config.root) {
throw new Error('Unable to detect root URL for WCT sources. Please set WCT.root before including browser.js');
}
}
}
/**
* Retrieves a configuration value.
*
* @param {string} key
* @return {*}
*/
function get(key) {
return _config[key];
}
// Internal
function _deepMerge(target, source) {
Object.keys(source).forEach(function(key) {
if (target[key] !== null && typeof target[key] === 'object' && !Array.isArray(target[key])) {
_deepMerge(target[key], source[key]);
} else {
target[key] = source[key];
}
});
}
var suites_js = {
get htmlSuites () { return suites_js__htmlSuites; },
get jsSuites () { return suites_js__jsSuites; },
get loadSuites () { return loadSuites; },
get activeChildSuites () { return activeChildSuites; },
get loadJsSuites () { return loadJsSuites; },
get runSuites () { return runSuites; }
};
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
var suites_js__htmlSuites = [];
var suites_js__jsSuites = [];
// We process grep ourselves to avoid loading suites that will be filtered.
var GREP = util_js.getParam('grep');
/**
* Loads suites of tests, supporting both `.js` and `.html` files.
*
* @param {!Array.<string>} files The files to load.
*/
function loadSuites(files) {
files.forEach(function(file) {
if (/\.js(\?.*)?$/.test(file)) {
suites_js__jsSuites.push(file);
} else if (/\.html(\?.*)?$/.test(file)) {
suites_js__htmlSuites.push(file);
} else {
throw new Error('Unknown resource type: ' + file);
}
});
}
/**
* @return {!Array.<string>} The child suites that should be loaded, ignoring
* those that would not match `GREP`.
*/
function activeChildSuites() {
var subsuites = suites_js__htmlSuites;
if (GREP) {
var cleanSubsuites = [];
for (var i = 0, subsuite; subsuite = subsuites[i]; i++) {
if (GREP.indexOf(util_js.cleanLocation(subsuite)) !== -1) {
cleanSubsuites.push(subsuite);
}
}
subsuites = cleanSubsuites;
}
return subsuites;
}
/**
* Loads all `.js` sources requested by the current suite.
*
* @param {!MultiReporter} reporter
* @param {function} done
*/
function loadJsSuites(reporter, done) {
util_js.debug('loadJsSuites', suites_js__jsSuites);
var loaders = suites_js__jsSuites.map(function(file) {
// We only support `.js` dependencies for now.
return util_js.loadScript.bind(util_js, file);
});
util_js.parallel(loaders, done);
}
/**
* @param {!MultiReporter} reporter
* @param {!Array.<string>} childSuites
* @param {function} done
*/
function runSuites(reporter, childSuites, done) {
util_js.debug('runSuites');
var suiteRunners = [
// Run the local tests (if any) first, not stopping on error;
_runMocha.bind(null, reporter),
];
// As well as any sub suites. Again, don't stop on error.
childSuites.forEach(function(file) {
suiteRunners.push(function(next) {
var childRunner = new ChildRunner(file, window);
reporter.emit('childRunner start', childRunner);
childRunner.run(function(error) {
reporter.emit('childRunner end', childRunner);
if (error) reporter.emitOutOfBandTest(file, error);
next();
});
});
});
util_js.parallel(suiteRunners, config_js.get('numConcurrentSuites'), function(error) {
reporter.done();
done(error);
});
}
/**
* Kicks off a mocha run, waiting for frameworks to load if necessary.
*
* @param {!MultiReporter} reporter Where to send Mocha's events.
* @param {function} done A callback fired, _no error is passed_.
*/
function _runMocha(reporter, done, waited) {
if (config_js.get('waitForFrameworks') && !waited) {
var waitFor = (config_js.get('waitFor') || util_js.whenFrameworksReady).bind(window);
waitFor(_runMocha.bind(null, reporter, done, true));
return;
}
util_js.debug('_runMocha');
var mocha = window.mocha;
var Mocha = window.Mocha;
mocha.reporter(reporter.childReporter(window.location));
mocha.suite.title = reporter.suiteTitle(window.location);
mocha.grep(GREP);
// We can't use `mocha.run` because it bashes over grep, invert, and friends.
// See https://github.com/visionmedia/mocha/blob/master/support/tail.js#L137
var runner = Mocha.prototype.run.call(mocha, function(error) {
if (document.getElementById('mocha')) {
Mocha.utils.highlightTags('code');
}
done(); // We ignore the Mocha failure count.
});
// Mocha's default `onerror` handling strips the stack (to support really old
// browsers). We upgrade this to get better stacks for async errors.
//
// TODO(nevir): Can we expand support to other browsers?
if (navigator.userAgent.match(/chrome/i)) {
window.onerror = null;
window.addEventListener('error', function(event) {
if (!event.error) return;
if (event.error.ignore) return;
runner.uncaught(event.error);
});
}
}
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
var ConsoleReporter__console = window.console;
var FONT = ';font: normal 13px "Roboto", "Helvetica Neue", "Helvetica", sans-serif;';
var STYLES = {
plain: FONT,
suite: 'color: #5c6bc0' + FONT,
test: FONT,
passing: 'color: #259b24' + FONT,
pending: 'color: #e65100' + FONT,
failing: 'color: #c41411' + FONT,
stack: 'color: #c41411',
results: FONT + 'font-size: 16px',
};
// I don't think we can feature detect this one...
var userAgent = navigator.userAgent.toLowerCase();
var CAN_STYLE_LOG = userAgent.match('firefox') || userAgent.match('webkit');
var CAN_STYLE_GROUP = userAgent.match('webkit');
// Track the indent for faked `console.group`
var logIndent = '';
function log(text, style) {
text = text.split('\n').map(function(l) { return logIndent + l; }).join('\n');
if (CAN_STYLE_LOG) {
ConsoleReporter__console.log('%c' + text, STYLES[style] || STYLES.plain);
} else {
ConsoleReporter__console.log(text);
}
}
function logGroup(text, style) {
if (CAN_STYLE_GROUP) {
ConsoleReporter__console.group('%c' + text, STYLES[style] || STYLES.plain);
} else if (ConsoleReporter__console.group) {
ConsoleReporter__console.group(text);
} else {
logIndent = logIndent + ' ';
log(text, style);
}
}
function logGroupEnd() {
if (ConsoleReporter__console.groupEnd) {
ConsoleReporter__console.groupEnd();
} else {
logIndent = logIndent.substr(0, logIndent.length - 2);
}
}
function logException(error) {
log(error.stack || error.message || error, 'stack');
}
/**
* A Mocha reporter that logs results out to the web `console`.
*
* @param {!Mocha.Runner} runner The runner that is being reported on.
*/
function Console(runner) {
Mocha.reporters.Base.call(this, runner);
runner.on('suite', function(suite) {
if (suite.root) return;
logGroup(suite.title, 'suite');
}.bind(this));
runner.on('suite end', function(suite) {
if (suite.root) return;
logGroupEnd();
}.bind(this));
runner.on('test', function(test) {
logGroup(test.title, 'test');
}.bind(this));
runner.on('pending', function(test) {
logGroup(test.title, 'pending');
}.bind(this));
runner.on('fail', function(test, error) {
logException(error);
}.bind(this));
runner.on('test end', function(test) {
logGroupEnd();
}.bind(this));
runner.on('end', this.logSummary.bind(this));
}
/** Prints out a final summary of test results. */
Console.prototype.logSummary = function logSummary() {
logGroup('Test Results', 'results');
if (this.stats.failures > 0) {
log(util_js.pluralizedStat(this.stats.failures, 'failing'), 'failing');
}
if (this.stats.pending > 0) {
log(util_js.pluralizedStat(this.stats.pending, 'pending'), 'pending');
}
log(util_js.pluralizedStat(this.stats.passes, 'passing'));
if (!this.stats.failures) {
log('test suite passed', 'passing');
}
log('Evaluated ' + this.stats.tests + ' tests in ' + this.stats.duration + 'ms.');
logGroupEnd();
};
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
/**
* WCT-specific behavior on top of Mocha's default HTML reporter.
*
* @param {!Mocha.Runner} runner The runner that is being reported on.
*/
function HTML(runner) {
var output = document.createElement('div');
output.id = 'mocha';
document.body.appendChild(output);
runner.on('suite', function(test) {
this.total = runner.total;
}.bind(this));
Mocha.reporters.HTML.call(this, runner);
}
// Woo! What a hack. This just saves us from adding a bunch of complexity around
// style loading.
var style = document.createElement('style');
style.textContent = 'html, body {' +
' position: relative;' +
' height: 100%;' +
' width: 100%;' +
' min-width: 900px;' +
'}' +
'#mocha, #subsuites {' +
' height: 100%;' +
' position: absolute;' +
' top: 0;' +
'}' +
'#mocha {' +
' box-sizing: border-box;' +
' margin: 0 !important;' +
' overflow-y: auto;' +
' padding: 60px 20px;' +
' right: 0;' +
' left: 500px;' +
'}' +
'#subsuites {' +
' -ms-flex-direction: column;' +
' -webkit-flex-direction: column;' +
' display: -ms-flexbox;' +
' display: -webkit-flex;' +
' display: flex;' +
' flex-direction: column;' +
' left: 0;' +
' width: 500px;' +
'}' +
'#subsuites .subsuite {' +
' border: 0;' +
' width: 100%;' +
' height: 100%;' +
'}' +
'#mocha .test.pass .duration {' +
' color: #555 !important;' +
'}';
document.head.appendChild(style);
/**
* @license
* Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/
var STACKY_CONFIG = {
indent: ' ',
locationStrip: [
/^https?:\/\/[^\/]+/,
/\?.*$/,
],
filter: function(line) {
return line.location.match(/\/web-component-tester\/[^\/]+(\?.*)?$/);
},
};
// https://github.com/visionmedia/mocha/blob/master/lib/runner.js#L36-46
var MOCHA_EVENTS = [
'start',
'end',
'suite',
'suite end',
'test',
'test end',
'hook',
'hook end',
'pass',
'fail',
'pending',
];
// Until a suite has loaded, we assume this many tests in it.
var ESTIMATED_TESTS_PER_SUITE = 3;
/**
* A Mocha-like reporter that combines the output of multiple Mocha suites.
*
* @param {number} numSuites The number of suites that will be run, in order to
* estimate the total number of tests that will be performed.
* @param {!Array.<!Mocha.reporters.Base>} reporters The set of reporters that
* should receive the unified event stream.
* @param {MultiReporter} parent The parent reporter, if present.
*/
function MultiReporter(numSuites, reporters, parent) {
this.reporters = reporters.map(function(reporter) {
return new reporter(this);
}.bind(this));
this.parent = parent;
this.basePath = parent && parent.basePath || util_js.basePath(window.location);
this.total = numSuites * ESTIMATED_TESTS_PER_SUITE;
// Mocha reporters assume a stream of events, so we have to be careful to only
// report on one runner at a time...
this.currentRunner = null;
// ...while we buffer events for any other active runners.
this.pendingEvents = [];
this.emit('start');
}
/**
* @param {!Location|string} location The location this reporter represents.
* @return {!Mocha.reporters.Base} A reporter-like "class" for each child suite
* that should be passed to `mocha.run`.
*/
MultiReporter.prototype.childReporter = function childReporter(location) {
var name = this.suiteTitle(location);
// The reporter is used as a constructor, so we can't depend on `this` being
// properly bound.
var self = this;
function reporter(runner) {
runner.name = name;
self.bindChildRunner(runner);
}