Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / src / tvcm / unittest / suite_loader.js
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.
4
5 'use strict';
6
7 tvcm.require('tvcm.iteration_helpers');
8 tvcm.require('tvcm.promise');
9 tvcm.require('tvcm.unittest.test_suite');
10
11 tvcm.exportTo('tvcm.unittest', function() {
12   var currentSuiteLoader_ = undefined;
13
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);
23             } else {
24               console.log('Failed to load ' + url);
25               resolver.reject();
26             }
27           }, 0);
28         }
29       };
30       req.send(null);
31     });
32   }
33
34   function TestLink(linkPath, title) {
35     this.linkPath = linkPath;
36     this.title = title;
37   }
38
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_ = {};
45
46     this.testSuites = [];
47     this.testLinks = [];
48
49     this.allSuitesLoadedPromise = new tvcm.Promise(function(r) {
50       this.allSuitesLoadedResolver_ = r;
51     }.bind(this));
52
53     if (opt_suiteNamesToLoad) {
54       this.beginLoadingModules_(opt_suiteNamesToLoad);
55     } else {
56       getAsync('/tvcm/json/tests').then(
57           function(data) {
58             var testMetadata = JSON.parse(data);
59             var testModuleNames = testMetadata.test_module_names;
60             this.beginLoadingModules_(testModuleNames, testMetadata);
61           }.bind(this),
62           this.loadingTestsFailed_.bind(this));
63     }
64   }
65
66   var loadedSuitesByName = {};
67
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'],
75                                            tl['title']));
76         }
77       }
78
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);
84           continue;
85         }
86         this.testSuites.push(loadedSuitesByName[name]);
87       }
88
89       for (var i = 0; i < moduleNamesThatNeedToBeLoaded.length; i++)
90         this.pendingSuiteNames_[moduleNamesThatNeedToBeLoaded[i]] = true;
91
92       // Start the loading.
93       if (moduleNamesThatNeedToBeLoaded.length > 0) {
94         tvcm.require(moduleNamesThatNeedToBeLoaded);
95         this.loadTimeout_ = window.setTimeout(
96             this.loadingTestsTimeout_.bind(this),
97             60 * 1000);
98       } else {
99         this.didLoadAllTests_();
100       }
101     },
102
103     loadingTestsFailed_: function() {
104       currentSuiteLoader_ = undefined;
105       this.allSuitesLoadedResolver_.reject(
106           new Error('/tvcm/json/tests failed to load'));
107     },
108
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_)));
115     },
116
117     get areAllSuitesLoaded() {
118       return tvcm.dictionaryLength(this.pendingSuiteNames_) === 0;
119     },
120
121     addTestSuite: function(suite) {
122       if (this.pendingSuiteNames_[suite.name] === undefined)
123         throw new Error('Did not expect to load ' + suite.name);
124
125       loadedSuitesByName[suite.name] = suite;
126       delete this.pendingSuiteNames_[suite.name];
127
128       this.testSuites.push(suite);
129       if (!this.areAllSuitesLoaded)
130         return;
131       this.didLoadAllTests_();
132     },
133
134     didLoadAllTests_: function() {
135       if (this.loadTimeout_) {
136         window.clearTimeout(this.loadTimeout_);
137         this.loadTimeout_ = undefined;
138       }
139
140       currentSuiteLoader_ = undefined;
141       this.allSuitesLoadedResolver_.resolve(this);
142     },
143
144     getAllTests: function() {
145       var tests = [];
146       this.testSuites.forEach(function(suite) {
147         tests.push.apply(tests, suite.tests);
148       });
149       return tests;
150     },
151
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)
158             return test;
159         }
160       }
161       throw new Error('Test ' + fullyQualifiedName + 'not found');
162     }
163   };
164
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));
170   }
171
172   return {
173     SuiteLoader: SuiteLoader,
174     testSuite: testSuite
175   };
176 });