• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 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/**
6 * @fileoverview
7 * Integration module for QUnit tests running in browser tests.
8 * Specifically it:
9 * - Sets QUnit.autostart to false, so that the browser test can hook the test
10 *   results callback before the test starts.
11 * - Implements a text-based test reporter to report test results back to the
12 *   browser test.
13 */
14
15(function(QUnit, automationController, exports) {
16
17'use strict';
18
19var TestReporter = function() {
20  this.errorMessage_ = '';
21  this.failedTestsCount_ = 0;
22  this.failedAssertions_ = [];
23};
24
25TestReporter.prototype.init = function(qunit) {
26  qunit.testDone(this.onTestDone_.bind(this));
27  qunit.log(this.onAssertion_.bind(this));
28};
29
30TestReporter.prototype.onTestDone_ = function(details) {
31  if (this.failedAssertions_.length > 0) {
32    this.errorMessage_ += '  ' + details.module + '.' + details.name + '\n';
33    this.errorMessage_ += this.failedAssertions_.map(
34        function(assertion, index){
35          return '    ' + (index + 1) + '. ' + assertion.message + '\n' +
36                 '    ' + assertion.source;
37        }).join('\n');
38    this.failedAssertions_ = [];
39    this.failedTestsCount_++;
40  }
41};
42
43TestReporter.prototype.onAssertion_ = function(details) {
44  if (!details.result) {
45    this.failedAssertions_.push(details);
46  }
47};
48
49TestReporter.prototype.getErrorMessage = function(){
50  var errorMessage = '';
51  if (this.failedTestsCount_ > 0) {
52    var test = (this.failedTestsCount_ > 1) ? 'tests' : 'test';
53    errorMessage = this.failedTestsCount_  + ' ' + test + ' failed:\n';
54    errorMessage += this.errorMessage_;
55  }
56  return errorMessage;
57};
58
59var BrowserTestHarness = function(qunit, domAutomationController, reporter) {
60  this.qunit_ = qunit;
61  this.automationController_ = domAutomationController;
62  this.reporter_ = reporter;
63};
64
65BrowserTestHarness.prototype.init = function() {
66  this.qunit_.config.autostart = false;
67};
68
69BrowserTestHarness.prototype.run = function() {
70  this.reporter_.init(this.qunit_);
71  this.qunit_.start();
72  this.qunit_.done(function(details){
73    this.automationController_.send(JSON.stringify({
74      passed: details.passed == details.total,
75      errorMessage: this.reporter_.getErrorMessage()
76    }));
77  }.bind(this));
78};
79
80// The browser test runs chrome with the flag --dom-automation, which creates
81// the window.domAutomationController object.  This allows the test suite to
82// JS-encoded data back to the browser test.
83if (automationController) {
84  if (!QUnit) {
85    console.error('browser_test_harness.js must be included after QUnit.js.');
86    return;
87  }
88
89  var testHarness = new BrowserTestHarness(
90      QUnit,
91      automationController,
92      new TestReporter());
93  testHarness.init();
94  exports.browserTestHarness = testHarness;
95}
96
97})(window.QUnit, window.domAutomationController, window);