OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 * Creates wrappers for callbacks and calls testDone() when all callbacks |
| 7 * have been invoked. |
| 8 * @param {testing.Test} fixture |
| 9 */ |
| 10 function CallbackHelper(fixture) { |
| 11 /** @type {Object} fixture */ |
| 12 this.fixture_ = fixture; |
| 13 /** @type {number} */ |
| 14 this.pendingCallbacks_ = 0; |
| 15 } |
| 16 |
| 17 CallbackHelper.prototype = { |
| 18 /** |
| 19 * @param {Function=} opt_callback |
| 20 * @return {Function} |
| 21 */ |
| 22 wrap: function(opt_callback) { |
| 23 var callback = opt_callback || function() {}; |
| 24 var savedArgs = new SaveMockArguments(); |
| 25 var lastCall = null; |
| 26 var completionAction = callFunctionWithSavedArgs(savedArgs, function() { |
| 27 if (lastCall) { |
| 28 throw new Error('Called more than once, first call here: ' + lastCall); |
| 29 } else { |
| 30 lastCall = new Error().stack; |
| 31 } |
| 32 callback.apply(this.fixture_, arguments); |
| 33 if (--this.pendingCallbacks_ <= 0) |
| 34 testDone(); |
| 35 }.bind(this)); |
| 36 // runAllActionsAsync catches exceptions and puts them in the test |
| 37 // framework's list of errors and fails the test. |
| 38 var runAll = runAllActionsAsync(WhenTestDone.ASSERT, completionAction); |
| 39 ++this.pendingCallbacks_; |
| 40 return function() { |
| 41 savedArgs.arguments = Array.prototype.slice.call(arguments); |
| 42 runAll.invoke(); |
| 43 } |
| 44 } |
| 45 }; |
OLD | NEW |