1 // Copyright (c) 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 tvcm.require('tvcm.iteration_helpers');
8 tvcm.require('tvcm.promise');
9 tvcm.require('tvcm.unittest.test_suite');
11 tvcm.exportTo('tvcm.unittest', function() {
12 var currentSuiteLoader_ = undefined;
14 function getAsync(url, cb) {
15 return new tvcm.Promise(function(resolver) {
16 var req = new XMLHttpRequest();
17 req.open('GET', url, true);
18 req.onreadystatechange = function(aEvt) {
19 if (req.readyState == 4) {
20 window.setTimeout(function() {
21 if (req.status == 200) {
22 resolver.fulfill(req.responseText);
24 console.log('Failed to load ' + url);
34 function TestLink(linkPath, title) {
35 this.linkPath = linkPath;
39 function SuiteLoader(opt_suiteNamesToLoad) {
40 if (currentSuiteLoader_)
41 throw new Error('Cannot have more than one SuiteLoader active at once');
42 currentSuiteLoader_ = this;
43 this.numPendingSuites_ = {};
44 this.pendingSuiteNames_ = {};
49 this.allSuitesLoadedPromise = new tvcm.Promise(function(r) {
50 this.allSuitesLoadedResolver_ = r;
53 if (opt_suiteNamesToLoad) {
54 this.beginLoadingModules_(opt_suiteNamesToLoad);
56 getAsync('/tvcm/json/tests').then(
58 var testMetadata = JSON.parse(data);
59 var testModuleNames = testMetadata.test_module_names;
60 this.beginLoadingModules_(testModuleNames, testMetadata);
62 this.loadingTestsFailed_.bind(this));
66 var loadedSuitesByName = {};
68 SuiteLoader.prototype = {
69 beginLoadingModules_: function(testModuleNames, opt_testMetadata) {
70 if (opt_testMetadata) {
71 var testMetadata = opt_testMetadata;
72 for (var i = 0; i < testMetadata.test_links.length; i++) {
73 var tl = testMetadata.test_links[i];
74 this.testLinks.push(new TestLink(tl['path'],
79 var moduleNamesThatNeedToBeLoaded = [];
80 for (var i = 0; i < testModuleNames.length; i++) {
81 var name = testModuleNames[i];
82 if (loadedSuitesByName[name] === undefined) {
83 moduleNamesThatNeedToBeLoaded.push(name);
86 this.testSuites.push(loadedSuitesByName[name]);
89 for (var i = 0; i < moduleNamesThatNeedToBeLoaded.length; i++)
90 this.pendingSuiteNames_[moduleNamesThatNeedToBeLoaded[i]] = true;
93 if (moduleNamesThatNeedToBeLoaded.length > 0) {
94 tvcm.require(moduleNamesThatNeedToBeLoaded);
95 this.loadTimeout_ = window.setTimeout(
96 this.loadingTestsTimeout_.bind(this),
99 this.didLoadAllTests_();
103 loadingTestsFailed_: function() {
104 currentSuiteLoader_ = undefined;
105 this.allSuitesLoadedResolver_.reject(
106 new Error('/tvcm/json/tests failed to load'));
109 loadingTestsTimeout_: function() {
110 currentSuiteLoader_ = undefined;
111 this.loadingTestsTimeout_ = undefined;
112 this.allSuitesLoadedResolver_.reject(
113 new Error('Timed out waiting for %s to define suites: ' +
114 tvcm.dictionaryKeys(this.pendingSuiteNames_)));
117 get areAllSuitesLoaded() {
118 return tvcm.dictionaryLength(this.pendingSuiteNames_) === 0;
121 addTestSuite: function(suite) {
122 if (this.pendingSuiteNames_[suite.name] === undefined)
123 throw new Error('Did not expect to load ' + suite.name);
125 loadedSuitesByName[suite.name] = suite;
126 delete this.pendingSuiteNames_[suite.name];
128 this.testSuites.push(suite);
129 if (!this.areAllSuitesLoaded)
131 this.didLoadAllTests_();
134 didLoadAllTests_: function() {
135 if (this.loadTimeout_) {
136 window.clearTimeout(this.loadTimeout_);
137 this.loadTimeout_ = undefined;
140 currentSuiteLoader_ = undefined;
141 this.allSuitesLoadedResolver_.resolve(this);
144 getAllTests: function() {
146 this.testSuites.forEach(function(suite) {
147 tests.push.apply(tests, suite.tests);
152 findTestWithFullyQualifiedName: function(fullyQualifiedName) {
153 for (var i = 0; i < this.testSuites.length; i++) {
154 var suite = this.testSuites[i];
155 for (var j = 0; j < suite.tests.length; j++) {
156 var test = suite.tests[j];
157 if (test.fullyQualifiedName == fullyQualifiedName)
161 throw new Error('Test ' + fullyQualifiedName + 'not found');
165 function testSuite(name, suiteConstructor) {
166 if (currentSuiteLoader_ === undefined)
167 throw new Error('Cannot define testSuites when no SuiteLoader exists.');
168 currentSuiteLoader_.addTestSuite(new tvcm.unittest.TestSuite(
169 currentSuiteLoader_, name, suiteConstructor));
173 SuiteLoader: SuiteLoader,