summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/test/data/webui/mocha_adapter.js62
-rw-r--r--chrome/test/data/webui/polymer_browser_test_base.js78
2 files changed, 140 insertions, 0 deletions
diff --git a/chrome/test/data/webui/mocha_adapter.js b/chrome/test/data/webui/mocha_adapter.js
new file mode 100644
index 0000000..8f85410
--- /dev/null
+++ b/chrome/test/data/webui/mocha_adapter.js
@@ -0,0 +1,62 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * @fileoverview A mocha adapter for BrowserTests. To use, include mocha.js and
+ * mocha_adapter.js in a WebUIBrowserTest's extraLibraries array.
+ */
+
+/**
+ * Initializes a mocha reporter for the BrowserTest framework, which registers
+ * event listeners on the given Runner.
+ * @constructor
+ * @param {Runner} runner Runs the tests and provides hooks for test results
+ * (see Runner.prototype in mocha.js).
+ */
+function BrowserTestReporter(runner) {
+ var passes = 0;
+ var failures = 0;
+
+ // Increment passes for each passed test.
+ runner.on('pass', function(test) {
+ passes++;
+ });
+
+ // Report failures. Mocha only catches "assert" failures, because "expect"
+ // failures are caught by test_api.js.
+ runner.on('fail', function(test, err) {
+ failures++;
+ var message = 'Mocha test failed: ' + test.fullTitle() + '\n';
+
+ // Remove unhelpful mocha lines from stack trace.
+ var stack = err.stack.split('\n');
+ for (var i = 0; i < stack.length; i++) {
+ if (stack[i].indexOf('mocha.js:') == -1)
+ message += stack[i] + '\n';
+ }
+
+ console.error(message);
+ });
+
+ // Report the results to the test API.
+ runner.on('end', function() {
+ if (failures == 0) {
+ testDone();
+ return;
+ }
+ testDone([
+ false,
+ 'Test Errors: ' + failures + '/' + (passes + failures) +
+ ' tests had failed assertions.'
+ ]);
+ });
+}
+
+// Configure mocha.
+mocha.setup({
+ // Use TDD interface instead of BDD.
+ ui: 'tdd',
+ // Use custom reporter to interface with BrowserTests.
+ reporter: BrowserTestReporter,
+});
diff --git a/chrome/test/data/webui/polymer_browser_test_base.js b/chrome/test/data/webui/polymer_browser_test_base.js
new file mode 100644
index 0000000..e628eda
--- /dev/null
+++ b/chrome/test/data/webui/polymer_browser_test_base.js
@@ -0,0 +1,78 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * @fileoverview Framework for running JavaScript tests of Polymer elements.
+ */
+
+/**
+ * Test fixture for Polymer element testing.
+ * @constructor
+ * @extends testing.Test
+ */
+function PolymerTest() {
+}
+
+PolymerTest.prototype = {
+ __proto__: testing.Test.prototype,
+
+ /**
+ * Navigate to a WebUI to satisfy BrowserTest conditions. Override to load a
+ * more useful WebUI.
+ * @override
+ */
+ browsePreload: 'chrome://chrome-urls/',
+
+ /**
+ * The mocha adapter assumes all tests are async.
+ * @override
+ * @final
+ */
+ isAsync: true,
+
+ /**
+ * @override
+ * @final
+ */
+ extraLibraries: ['../../../../third_party/mocha/mocha.js',
+ 'mocha_adapter.js'],
+
+ /** @override */
+ setUp: function() {
+ testing.Test.prototype.setUp.call(this);
+
+ // Import Polymer before running tests.
+ suiteSetup(function(done) {
+ var link = document.createElement('link');
+ link.rel = 'import';
+ link.onload = function() {
+ done();
+ };
+ link.onerror = function() {
+ done(new Error('Failed to load Polymer!'));
+ };
+ link.href = 'chrome://resources/polymer/v0_8/polymer/polymer.html';
+ document.head.appendChild(link);
+ });
+ },
+};
+
+/**
+ * Imports the HTML file, then calls |done| on success or throws an error.
+ * @param {String} href The URL to load.
+ * @param {Function} done The done callback.
+ */
+PolymerTest.importHref = function(href, done) {
+ Polymer.Base.importHref(
+ href,
+ function() { done(); },
+ function() { throw new Error('Failed to load ' + href); });
+};
+
+/**
+ * Removes all content from the body.
+ */
+PolymerTest.clearBody = function() {
+ document.body.innerHTML = '';
+};