1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/common/tests/unit/test_async_chain.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,30 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +Cu.import("resource://services-common/async.js"); 1.8 + 1.9 +function run_test() { 1.10 + _("Chain a few async methods, making sure the 'this' object is correct."); 1.11 + 1.12 + let methods = { 1.13 + save: function(x, callback) { 1.14 + this.x = x; 1.15 + callback(x); 1.16 + }, 1.17 + addX: function(x, callback) { 1.18 + callback(x + this.x); 1.19 + }, 1.20 + double: function(x, callback) { 1.21 + callback(x * 2); 1.22 + }, 1.23 + neg: function(x, callback) { 1.24 + callback(-x); 1.25 + } 1.26 + }; 1.27 + methods.chain = Async.chain; 1.28 + 1.29 + // ((1 + 1 + 1) * (-1) + 1) * 2 + 1 = -3 1.30 + methods.chain(methods.save, methods.addX, methods.addX, methods.neg, 1.31 + methods.addX, methods.double, methods.addX, methods.save)(1); 1.32 + do_check_eq(methods.x, -3); 1.33 +}