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.raf');
9 tvcm.exportTo('tvcm.unittest', function() {
10 var realTvcmOnAnimationFrameError;
11 var realWindowOnError;
13 function installGlobalTestHooks(runner) {
14 realTvcmOnAnimationFrameError = tvcm.onAnimationFrameError;
15 tvcm.onAnimationFrameError = function(error) {
16 runner.results.addErrorForCurrentTest(error);
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);
28 tvcm.unittest.addHTMLOutputForCurrentTest = function(element) {
29 runner.results.addHTMLOutputForCurrentTest(element);
32 global.sessionStorage.clear();
33 tvcm.Settings.setAlternativeStorageInstance(global.sessionStorage);
34 tvcm.KeyEventManager.resetInstanceForUnitTesting();
37 function uninstallGlobalTestHooks() {
38 window.onerror = realWindowOnError;
39 realWindowOnError = undefined;
41 tvcm.onAnimationFrameError = realTvcmOnAnimationFrameError;
42 realTvcmOnAnimationFrameError = undefined;
44 tvcm.unittest.addHTMLOutputForCurrentTest = undefined;
48 function TestRunner(results, testCases) {
49 this.results_ = results;
50 this.testCases_ = testCases;
51 this.pendingTestCases_ = [];
53 this.runOneTestCaseScheduled_ = false;
55 this.runCompletedPromise = undefined;
56 this.runCompletedResolver_ = undefined;
58 this.currentTestCase_ = undefined;
61 TestRunner.prototype = {
62 __proto__: Object.prototype,
64 beginRunning: function() {
65 if (this.pendingTestCases_.length)
66 throw new Error('Tests still running!');
68 this.runCompletedPromise = new tvcm.Promise(function(resolver) {
69 this.runCompletedResolver_ = resolver;
72 this.pendingTestCases_ = this.testCases_.slice(0);
74 this.scheduleRunOneTestCase_();
76 return this.runCompletedPromise;
79 beginToStopRunning: function() {
80 if (!this.runCompletedResolver_)
81 throw new Error('Still running');
82 this.pendingTestCases_ = [];
83 return this.runCompletedPromise;
87 return this.testCases_;
94 scheduleRunOneTestCase_: function() {
95 if (this.runOneTestCaseScheduled_)
97 this.runOneTestCaseScheduled_ = true;
98 tvcm.requestIdleCallback(this.runOneTestCase_, this);
101 runOneTestCase_: function() {
102 this.runOneTestCaseScheduled_ = false;
104 if (this.pendingTestCases_.length == 0) {
105 this.didFinishRunningAllTests_();
109 this.currentTestCase_ = this.pendingTestCases_.splice(0, 1)[0];
111 this.results_.willRunTest(this.currentTestCase_);
112 if (!this.setUpCurrentTestCase_()) {
113 this.results_.didCurrentTestEnd();
114 this.currentTestCase_ = undefined;
115 this.scheduleRunOneTestCase_();
119 this.runCurrentTestCase_().then(
120 function pass(result) {
121 this.tearDownCurrentTestCase_(true);
123 this.results_.setReturnValueFromCurrentTest(result);
124 this.results_.didCurrentTestEnd();
125 this.currentTestCase_ = undefined;
126 this.scheduleRunOneTestCase_();
128 function fail(error) {
129 this.results_.addErrorForCurrentTest(error);
130 this.tearDownCurrentTestCase_(false);
131 this.results_.didCurrentTestEnd();
132 this.currentTestCase_ = undefined;
133 this.scheduleRunOneTestCase_();
137 setUpCurrentTestCase_: function() {
138 // Try setting it up. Return true if succeeded.
139 installGlobalTestHooks(this);
141 if (this.currentTestCase_.options_.setUp)
142 this.currentTestCase_.options_.setUp.call(this.currentTestCase_);
144 this.results_.addErrorForCurrentTest(error);
150 runCurrentTestCase_: function() {
151 return new Promise(function(runTestCaseResolver) {
153 var maybePromise = this.currentTestCase_.run();
155 runTestCaseResolver.reject(error);
159 if (maybePromise !== undefined && maybePromise.then) {
162 runTestCaseResolver.fulfill(result);
165 runTestCaseResolver.reject(error);
168 runTestCaseResolver.fulfill(maybePromise);
173 tearDownCurrentTestCase_: function() {
175 if (this.currentTestCase_.tearDown)
176 this.currentTestCase_.tearDown.call(this.currentTestCase_);
178 this.results_.addErrorForCurrentTest(error);
181 uninstallGlobalTestHooks();
184 didFinishRunningAllTests_: function() {
185 this.results.didRunTests();
186 this.runCompletedResolver_.resolve();
187 this.runCompletedResolver_ = undefined;
192 TestRunner: TestRunner