toolkit/devtools/tests/unit/test_async-utils.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: js; js-indent-level: 2; -*- */
michael@0 2 /* Any copyright is dedicated to the Public Domain.
michael@0 3 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 4
michael@0 5 // Test async-utils.js
michael@0 6
michael@0 7 const {Task} = Cu.import("resource://gre/modules/Task.jsm", {});
michael@0 8 // |const| will not work because
michael@0 9 // it will make the Promise object immutable before assigning.
michael@0 10 // Using Object.defineProperty() instead.
michael@0 11 Object.defineProperty(this, "Promise", {
michael@0 12 value: Cu.import("resource://gre/modules/Promise.jsm", {}).Promise,
michael@0 13 writable: false, configurable: false
michael@0 14 });
michael@0 15 const {require} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools;
michael@0 16 const {async, asyncOnce, promiseInvoke, promiseCall} = require("devtools/async-utils");
michael@0 17
michael@0 18 function run_test() {
michael@0 19 do_test_pending();
michael@0 20 Task.spawn(function*() {
michael@0 21 for (let helper of [async, asyncOnce]) {
michael@0 22 yield test_async_args(helper);
michael@0 23 yield test_async_return(helper);
michael@0 24 yield test_async_throw(helper);
michael@0 25 }
michael@0 26 yield test_async_once();
michael@0 27 yield test_async_invoke();
michael@0 28 do_test_finished();
michael@0 29 }).then(null, error => {
michael@0 30 do_throw(error);
michael@0 31 });
michael@0 32 }
michael@0 33
michael@0 34 // Test that arguments are correctly passed through to the async function.
michael@0 35 function test_async_args(async) {
michael@0 36 let obj = {
michael@0 37 method: async(function*(a, b) {
michael@0 38 do_check_eq(this, obj);
michael@0 39 do_check_eq(a, "foo");
michael@0 40 do_check_eq(b, "bar");
michael@0 41 })
michael@0 42 };
michael@0 43
michael@0 44 return obj.method("foo", "bar");
michael@0 45 }
michael@0 46
michael@0 47 // Test that the return value from the async function is resolution value of
michael@0 48 // the promise.
michael@0 49 function test_async_return(async) {
michael@0 50 let obj = {
michael@0 51 method: async(function*(a, b) {
michael@0 52 return a + b;
michael@0 53 })
michael@0 54 };
michael@0 55
michael@0 56 return obj.method("foo", "bar").then(ret => {
michael@0 57 do_check_eq(ret, "foobar");
michael@0 58 });
michael@0 59 }
michael@0 60
michael@0 61 // Test that the throwing from an async function rejects the promise.
michael@0 62 function test_async_throw(async) {
michael@0 63 let obj = {
michael@0 64 method: async(function*() {
michael@0 65 throw "boom";
michael@0 66 })
michael@0 67 };
michael@0 68
michael@0 69 return obj.method().then(null, error => {
michael@0 70 do_check_eq(error, "boom");
michael@0 71 });
michael@0 72 }
michael@0 73
michael@0 74 // Test that asyncOnce only runs the async function once per instance and
michael@0 75 // returns the same promise for that instance.
michael@0 76 function test_async_once() {
michael@0 77 let counter = 0;
michael@0 78
michael@0 79 function Foo() {}
michael@0 80 Foo.prototype = {
michael@0 81 ran: false,
michael@0 82 method: asyncOnce(function*() {
michael@0 83 yield Promise.resolve();
michael@0 84 if (this.ran) {
michael@0 85 do_throw("asyncOnce function unexpectedly ran twice on the same object");
michael@0 86 }
michael@0 87 this.ran = true;
michael@0 88 return counter++;
michael@0 89 })
michael@0 90 };
michael@0 91
michael@0 92 let foo1 = new Foo();
michael@0 93 let foo2 = new Foo();
michael@0 94 let p1 = foo1.method();
michael@0 95 let p2 = foo2.method();
michael@0 96
michael@0 97 do_check_neq(p1, p2);
michael@0 98
michael@0 99 let p3 = foo1.method();
michael@0 100 do_check_eq(p1, p3);
michael@0 101 do_check_false(foo1.ran);
michael@0 102
michael@0 103 let p4 = foo2.method();
michael@0 104 do_check_eq(p2, p4);
michael@0 105 do_check_false(foo2.ran);
michael@0 106
michael@0 107 return p1.then(ret => {
michael@0 108 do_check_true(foo1.ran);
michael@0 109 do_check_eq(ret, 0);
michael@0 110 return p2;
michael@0 111 }).then(ret => {
michael@0 112 do_check_true(foo2.ran);
michael@0 113 do_check_eq(ret, 1);
michael@0 114 });
michael@0 115 }
michael@0 116
michael@0 117 // Test invoke and call.
michael@0 118 function test_async_invoke() {
michael@0 119 return Task.spawn(function*() {
michael@0 120 function func(a, b, expectedThis, callback) {
michael@0 121 "use strict";
michael@0 122 do_check_eq(a, "foo");
michael@0 123 do_check_eq(b, "bar");
michael@0 124 do_check_eq(this, expectedThis);
michael@0 125 callback(a + b);
michael@0 126 }
michael@0 127
michael@0 128 // Test call.
michael@0 129 let callResult = yield promiseCall(func, "foo", "bar", undefined);
michael@0 130 do_check_eq(callResult, "foobar");
michael@0 131
michael@0 132
michael@0 133 // Test invoke.
michael@0 134 let obj = { method: func };
michael@0 135 let invokeResult = yield promiseInvoke(obj, obj.method, "foo", "bar", obj);
michael@0 136 do_check_eq(invokeResult, "foobar");
michael@0 137
michael@0 138
michael@0 139 // Test passing multiple values to the callback.
michael@0 140 function multipleResults(callback) {
michael@0 141 callback("foo", "bar");
michael@0 142 }
michael@0 143
michael@0 144 let results = yield promiseCall(multipleResults);
michael@0 145 do_check_eq(results.length, 2);
michael@0 146 do_check_eq(results[0], "foo");
michael@0 147 do_check_eq(results[1], "bar");
michael@0 148
michael@0 149
michael@0 150 // Test throwing from the function.
michael@0 151 function thrower() {
michael@0 152 throw "boom";
michael@0 153 }
michael@0 154
michael@0 155 yield promiseCall(thrower).then(null, error => {
michael@0 156 do_check_eq(error, "boom");
michael@0 157 });
michael@0 158 });
michael@0 159 }

mercurial