Path: blob/trunk/javascript/webdriver/test/testutil.js
2868 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617goog.provide('webdriver.test.testutil');18goog.provide('webdriver.test.testutil.StubError');1920goog.require('goog.array');21goog.require('goog.debug.Error');22goog.require('goog.string');23goog.require('goog.testing.recordFunction');24goog.require('webdriver.stacktrace');25262728/**29* A custom error used for testing.30* @param {string=} opt_msg The error message to use.31* @constructor32* @extends {goog.debug.Error}33* @final34*/35webdriver.test.testutil.StubError = function(opt_msg) {36webdriver.test.testutil.StubError.base(this, 'constructor', opt_msg);37};38goog.inherits(webdriver.test.testutil.StubError, goog.debug.Error);394041/** @override */42webdriver.test.testutil.StubError.prototype.name = 'StubError';434445/** @type {Array.<!string>} */46webdriver.test.testutil.messages = [];4748webdriver.test.testutil.getStackTrace = function() {49return webdriver.stacktrace.get();50};5152webdriver.test.testutil.throwStubError = function() {53throw new webdriver.test.testutil.StubError;54};5556webdriver.test.testutil.assertIsStubError = function(error) {57assertTrue(error + ' is not an instanceof StubError',58error instanceof webdriver.test.testutil.StubError);59};606162/**63* Asserts the contents of the {@link webdriver.test.testutil.messages} array64* are as expected.65* @param {...*} var_args The expected contents.66*/67webdriver.test.testutil.assertMessages = function(var_args) {68var args = Array.prototype.slice.call(arguments, 0);69assertArrayEquals(args, webdriver.test.testutil.messages);70};717273/**74* Wraps a call to {@link webdriver.test.testutil.assertMessages} so it can75* be passed as a callback.76* @param {...*} var_args The expected contents.77* @return {!Function} The wrapped function.78*/79webdriver.test.testutil.assertingMessages = function(var_args) {80var args = goog.array.slice(arguments, 0);81return function() {82return webdriver.test.testutil.assertMessages.apply(null, args);83};84};858687/**88* Asserts an object is a promise.89* @param {*} obj The object to check.90*/91webdriver.test.testutil.assertIsPromise = function(obj) {92assertTrue('Value is not a promise: ' + goog.typeOf(obj),93webdriver.promise.isPromise(obj));94};959697/**98* Asserts an object is not a promise.99* @param {*} obj The object to check.100*/101webdriver.test.testutil.assertNotPromise = function(obj) {102assertFalse(webdriver.promise.isPromise(obj));103};104105/**106* Wraps a function. The wrapped function will have several utility functions:107* <ul>108* <li>assertCalled: Asserts that the function was called.109* <li>assertNotCalled: Asserts that the function was not called.110* </ul>111* @param {Function=} opt_fn The function to wrap; defaults to112* goog.nullFunction.113* @return {!Function} The wrapped function.114* @see goog.testing.recordFunction115*/116webdriver.test.testutil.callbackHelper = function(opt_fn) {117var callback = goog.testing.recordFunction(opt_fn);118119callback.getExpectedCallCountMessage = function(n, opt_prefix, opt_noJoin) {120var message = [];121if (opt_prefix) message.push(opt_prefix);122123var calls = callback.getCalls();124message.push(125'Expected to be called ' + n + ' times.',126' was called ' + calls.length + ' times:');127goog.array.forEach(calls, function(call) {128var e = call.getError();129if (e) {130throw e;131}132});133return opt_noJoin ? message : message.join('\n');134};135136callback.assertCalled = function(opt_message) {137assertEquals(callback.getExpectedCallCountMessage(1, opt_message),1381, callback.getCallCount());139};140141callback.assertNotCalled = function(opt_message) {142assertEquals(callback.getExpectedCallCountMessage(0, opt_message),1430, callback.getCallCount());144};145146return callback;147};148149150/**151* Creates a utility for managing a pair of callbacks, capable of asserting only152* one of the pair was ever called.153*154* @param {Function=} opt_callback The callback to manage.155* @param {Function=} opt_errback The errback to manage.156*/157webdriver.test.testutil.callbackPair = function(opt_callback, opt_errback) {158var pair = {159callback: webdriver.test.testutil.callbackHelper(opt_callback),160errback: webdriver.test.testutil.callbackHelper(opt_errback)161};162163/** @param {string=} opt_message Optional failure message. */164pair.assertEither = function(opt_message) {165if (!pair.callback.getCallCount() &&166!pair.errback.getCallCount()) {167var message = ['Neither callback nor errback has been called'];168if (opt_message) goog.array.insertAt(message, opt_message);169fail(message.join('\n'));170}171};172173/** @param {string=} opt_message Optional failure message. */174pair.assertNeither = function(opt_message) {175var message = (opt_message || '') + 'Did not expect callback or errback';176pair.callback.assertNotCalled(message);177pair.errback.assertNotCalled(message);178};179180/** @param {string=} opt_message Optional failure message. */181pair.assertCallback = function(opt_message) {182var message = opt_message ? (opt_message + ': ') : '';183pair.errback.assertNotCalled(message + 'Expected callback, not errback');184pair.callback.assertCalled(message + 'Callback not called');185};186187/** @param {string=} opt_message Optional failure message. */188pair.assertErrback = function(opt_message) {189var message = opt_message ? (opt_message + ': ') : '';190pair.callback.assertNotCalled(message + 'Expected errback, not callback');191pair.errback.assertCalled(message + 'Errback not called');192};193194pair.reset = function() {195pair.callback.reset();196pair.errback.reset();197};198199return pair;200};201202203webdriver.test.testutil.assertObjectEquals = function(expected, actual) {204assertObjectEquals(205'Expected: ' + JSON.stringify(expected) + '\n' +206'Actual: ' + JSON.stringify(actual),207expected, actual);208};209210211