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.
6 * Creates wrappers for callbacks and calls testDone() when all callbacks
8 * @param {testing.Test} fixture
10 function CallbackHelper(fixture
) {
11 /** @type {Object} fixture */
12 this.fixture_
= fixture
;
14 this.pendingCallbacks_
= 0;
17 CallbackHelper
.prototype = {
19 * @param {Function=} opt_callback
22 wrap: function(opt_callback
) {
23 var callback
= opt_callback
|| function() {};
24 var savedArgs
= new SaveMockArguments();
26 var completionAction
= callFunctionWithSavedArgs(savedArgs
, function() {
28 throw new Error('Called more than once, first call here: ' + lastCall
);
30 lastCall
= new Error().stack
;
32 callback
.apply(this.fixture_
, arguments
);
33 if (--this.pendingCallbacks_
<= 0)
34 CallbackHelper
.testDone_();
36 // runAllActionsAsync catches exceptions and puts them in the test
37 // framework's list of errors and fails the test if appropriate.
38 var runAll
= runAllActionsAsync(WhenTestDone
.ASSERT
, completionAction
);
39 ++this.pendingCallbacks_
;
41 savedArgs
.arguments
= Array
.prototype.slice
.call(arguments
);
50 CallbackHelper
.testDone_
= this.testDone
;
51 // Remove testDone for public use since direclty using it conflicts with
52 // this callback helper.