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