| <!DOCTYPE html> |
| <!-- |
| Copyright (c) 2014 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. |
| --> |
| <link rel="import" href="/tracing/base/guid.html"> |
| <link rel="import" href="/tracing/base/statistics.html"> |
| <link rel="import" href="/tracing/base/unittest/test_case.html"> |
| <link rel="import" href="/tracing/base/unittest/constants.html"> |
| <script> |
| 'use strict'; |
| |
| tr.exportTo('tr.b.unittest', function() { |
| var TestCase = tr.b.unittest.TestCase; |
| var PerfTestCase = tr.b.unittest.PerfTestCase; |
| |
| var TestTypes = tr.b.unittest.TestTypes; |
| |
| function TestSuite(name, suiteConstructor) { |
| this.guid = tr.b.GUID.allocate(); |
| this.name_ = name; |
| this.tests_ = []; |
| this.testNames_ = {}; // For dupe checking. |
| |
| global.flakyTest = function(testCaseOrName, opt_testFn, opt_options) { |
| if (testCaseOrName instanceof TestCase) { |
| testCaseOrName.options['flaky'] = true; |
| test(testCaseOrName); |
| } else { |
| var options = opt_options || {}; |
| options['flaky'] = true; |
| test(testCaseOrName, opt_testFn, options); |
| } |
| }.bind(this); |
| |
| global.test = function(testCaseOrName, opt_testFn, opt_options) { |
| if (testCaseOrName instanceof TestCase) { |
| if (opt_testFn !== undefined) |
| throw new Error('opt_testFn cannot be given when giving a TestCase'); |
| if (opt_options !== undefined) |
| throw new Error('opt_options cannot be given when giving a TestCase'); |
| this.addTest(testCaseOrName); |
| return; |
| } |
| |
| var testName = testCaseOrName; |
| var testFn = opt_testFn; |
| var options = opt_options || {}; |
| if (testFn == undefined) |
| throw new Error('Must provide opt_testFn'); |
| |
| // If the test cares about DPI settings then we first push a test |
| // that fakes the DPI as the low or hi Dpi version, depending on what |
| // we're current using. |
| if (options.dpiAware) { |
| var defaultDevicePixelRatio = window.devicePixelRatio; |
| var dpi = defaultDevicePixelRatio > 1 ? 1 : 2; |
| |
| var testWrapper = function() { |
| window.devicePixelRatio = dpi; |
| try { |
| testFn.bind(this).call(); |
| } finally { |
| window.devicePixelRatio = defaultDevicePixelRatio; |
| } |
| }; |
| |
| var newName = name; |
| if (dpi === 1) { |
| newName += '_loDPI'; |
| testName += '_hiDPI'; |
| } else { |
| newName += '_hiDPI'; |
| testName += '_loDPI'; |
| } |
| |
| this.addTest(new TestCase(newName, |
| testWrapper, options || {})); |
| } |
| |
| this.addTest(new TestCase(testName, |
| testFn, options || {})); |
| }.bind(this); |
| |
| global.timedPerfTest = function(name, testFn, options) { |
| if (options === undefined || options.iterations === undefined) |
| throw new Error('timedPerfTest must have iteration option provided.'); |
| this.addTest(new PerfTestCase(name, testFn, options)); |
| }.bind(this); |
| |
| try { |
| suiteConstructor.call(); |
| } finally { |
| global.test = undefined; |
| global.timedPerfTest = undefined; |
| } |
| } |
| |
| TestSuite.prototype = { |
| __proto__: Object.prototype, |
| |
| get tests() { |
| return this.tests_; |
| }, |
| |
| addTest: function(test) { |
| if (test.suite !== undefined) |
| throw new Error('Test suite is already assigned'); |
| if (this.testNames_[test.name] !== undefined) |
| throw new Error('Test name already used'); |
| test.suite = this; |
| this.testNames_[test.name] = true; |
| this.tests_.push(test); |
| }, |
| |
| get name() { |
| return this.name_; |
| } |
| }; |
| |
| function testSuite(suiteConstructor) { |
| if (!global._currentSuiteLoader) |
| throw new Error('testSuites can only be defined during suite loading'); |
| global._currentSuiteLoader.constructAndRegisterTestSuite(suiteConstructor); |
| } |
| |
| return { |
| TestSuite: TestSuite, |
| testSuite: testSuite |
| }; |
| }); |
| </script> |