Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / src / tvcm / unittest / test_runner.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.raf');
8
9 tvcm.exportTo('tvcm.unittest', function() {
10   var realTvcmOnAnimationFrameError;
11   var realWindowOnError;
12   var realWindowHistoryPushState;
13
14   function installGlobalTestHooks(runner) {
15     realTvcmOnAnimationFrameError = tvcm.onAnimationFrameError;
16     tvcm.onAnimationFrameError = function(error) {
17       runner.results.addErrorForCurrentTest(error);
18     }
19
20     realWindowOnError = window.onerror;
21     window.onerror = function(errorMsg, url, lineNumber) {
22       runner.results.addErrorForCurrentTest(
23           errorMsg + ' at ' + url + ':' + lineNumber);
24       if (realWindowOnError)
25         return realWindowOnError(errorMsg, url, lineNumber);
26       return false;
27     }
28
29     realWindowHistoryPushState = window.history.pushState;
30     window.history.pushState = function() {
31     };
32
33     tvcm.unittest.addHTMLOutputForCurrentTest = function(element) {
34       runner.results.addHTMLOutputForCurrentTest(element);
35     }
36
37     global.sessionStorage.clear();
38     tvcm.Settings.setAlternativeStorageInstance(global.sessionStorage);
39     tvcm.KeyEventManager.resetInstanceForUnitTesting();
40   }
41
42   function uninstallGlobalTestHooks() {
43     window.onerror = realWindowOnError;
44     realWindowOnError = undefined;
45
46     tvcm.onAnimationFrameError = realTvcmOnAnimationFrameError;
47     realTvcmOnAnimationFrameError = undefined;
48
49     realWindowHistoryPushState = undefined;
50     window.history.pushState = realWindowHistoryPushState;
51
52
53     tvcm.unittest.addHTMLOutputForCurrentTest = undefined;
54   }
55
56
57   function TestRunner(results, testCases) {
58     this.results_ = results;
59     this.testCases_ = testCases;
60     this.pendingTestCases_ = [];
61
62     this.runOneTestCaseScheduled_ = false;
63
64     this.runCompletedPromise = undefined;
65     this.runCompletedResolver_ = undefined;
66
67     this.currentTestCase_ = undefined;
68   }
69
70   TestRunner.prototype = {
71     __proto__: Object.prototype,
72
73     beginRunning: function() {
74       if (this.pendingTestCases_.length)
75         throw new Error('Tests still running!');
76
77       this.runCompletedPromise = new tvcm.Promise(function(resolver) {
78         this.runCompletedResolver_ = resolver;
79       }.bind(this));
80
81       this.pendingTestCases_ = this.testCases_.slice(0);
82
83       this.scheduleRunOneTestCase_();
84
85       return this.runCompletedPromise;
86     },
87
88     beginToStopRunning: function() {
89       if (!this.runCompletedResolver_)
90         throw new Error('Still running');
91       this.pendingTestCases_ = [];
92       return this.runCompletedPromise;
93     },
94
95     get testCases() {
96       return this.testCases_;
97     },
98
99     get results() {
100       return this.results_;
101     },
102
103     scheduleRunOneTestCase_: function() {
104       if (this.runOneTestCaseScheduled_)
105         return;
106       this.runOneTestCaseScheduled_ = true;
107       tvcm.requestIdleCallback(this.runOneTestCase_, this);
108     },
109
110     runOneTestCase_: function() {
111       this.runOneTestCaseScheduled_ = false;
112
113       if (this.pendingTestCases_.length == 0) {
114         this.didFinishRunningAllTests_();
115         return;
116       }
117
118       this.currentTestCase_ = this.pendingTestCases_.splice(0, 1)[0];
119
120       this.results_.willRunTest(this.currentTestCase_);
121       if (!this.setUpCurrentTestCase_()) {
122         this.results_.didCurrentTestEnd();
123         this.currentTestCase_ = undefined;
124         this.scheduleRunOneTestCase_();
125         return;
126       }
127
128       this.runCurrentTestCase_().then(
129           function pass(result) {
130             this.tearDownCurrentTestCase_(true);
131             if (result)
132               this.results_.setReturnValueFromCurrentTest(result);
133             this.results_.didCurrentTestEnd();
134             this.currentTestCase_ = undefined;
135             this.scheduleRunOneTestCase_();
136           }.bind(this),
137           function fail(error) {
138             this.results_.addErrorForCurrentTest(error);
139             this.tearDownCurrentTestCase_(false);
140             this.results_.didCurrentTestEnd();
141             this.currentTestCase_ = undefined;
142             this.scheduleRunOneTestCase_();
143           }.bind(this));
144     },
145
146     setUpCurrentTestCase_: function() {
147       // Try setting it up. Return true if succeeded.
148       installGlobalTestHooks(this);
149       try {
150         if (this.currentTestCase_.options_.setUp)
151           this.currentTestCase_.options_.setUp.call(this.currentTestCase_);
152       } catch (error) {
153         this.results_.addErrorForCurrentTest(error);
154         return false;
155       }
156       return true;
157     },
158
159     runCurrentTestCase_: function() {
160       return new Promise(function(runTestCaseResolver) {
161         try {
162           var maybePromise = this.currentTestCase_.run();
163         } catch (error) {
164           runTestCaseResolver.reject(error);
165           return;
166         }
167
168         if (maybePromise !== undefined && maybePromise.then) {
169           maybePromise.then(
170               function(result) {
171                 runTestCaseResolver.fulfill(result);
172               },
173               function(error) {
174                 runTestCaseResolver.reject(error);
175               });
176         } else {
177           runTestCaseResolver.fulfill(maybePromise);
178         }
179       }.bind(this));
180     },
181
182     tearDownCurrentTestCase_: function() {
183       try {
184         if (this.currentTestCase_.tearDown)
185           this.currentTestCase_.tearDown.call(this.currentTestCase_);
186       } catch (error) {
187         this.results_.addErrorForCurrentTest(error);
188       }
189
190       uninstallGlobalTestHooks();
191     },
192
193     didFinishRunningAllTests_: function() {
194       this.results.didRunTests();
195       this.runCompletedResolver_.resolve();
196       this.runCompletedResolver_ = undefined;
197     }
198   };
199
200   return {
201     TestRunner: TestRunner
202   };
203 });