gecko-dev/toolkit/modules/tests/xpcshell/test_PromiseUtils.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

103 lines
4.1 KiB
JavaScript

/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
/* eslint-disable mozilla/no-arbitrary-setTimeout */
"use strict";
const {PromiseUtils} = ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
const {setTimeout} = ChromeUtils.import("resource://gre/modules/Timer.jsm");
const {PromiseTestUtils} = ChromeUtils.import("resource://testing-common/PromiseTestUtils.jsm");
// Tests for PromiseUtils.jsm
// Tests for PromiseUtils.defer()
/* Tests for checking the resolve method of the Deferred object
* returned by PromiseUtils.defer() */
add_task(async function test_resolve_string() {
let def = PromiseUtils.defer();
let expected = "The promise is resolved " + Math.random();
def.resolve(expected);
let result = await def.promise;
Assert.equal(result, expected, "def.resolve() resolves the promise");
});
/* Test for the case when undefined is passed to the resolve method
* of the Deferred object */
add_task(async function test_resolve_undefined() {
let def = PromiseUtils.defer();
def.resolve();
let result = await def.promise;
Assert.equal(result, undefined, "resolve works with undefined as well");
});
/* Test when a pending promise is passed to the resolve method
* of the Deferred object */
add_task(async function test_resolve_pending_promise() {
let def = PromiseUtils.defer();
let expected = 100 + Math.random();
let p = new Promise((resolve, reject) => {
setTimeout(() => resolve(expected), 100);
});
def.resolve(p);
let result = await def.promise;
Assert.equal(result, expected, "def.promise assumed the state of the passed promise");
});
/* Test when a resovled promise is passed
* to the resolve method of the Deferred object */
add_task(async function test_resolve_resolved_promise() {
let def = PromiseUtils.defer();
let expected = "Yeah resolved" + Math.random();
let p = new Promise((resolve, reject) => resolve(expected));
def.resolve(p);
let result = await def.promise;
Assert.equal(result, expected, "Resolved promise is passed to the resolve method");
});
/* Test for the case when a rejected promise is
* passed to the resolve method */
add_task(async function test_resolve_rejected_promise() {
let def = PromiseUtils.defer();
let p = new Promise((resolve, reject) => reject(new Error("There its an rejection")));
def.resolve(p);
await Assert.rejects(def.promise, /There its an rejection/, "Settled rejection promise passed to the resolve method");
});
/* Test for the checking the reject method of
* the Deferred object returned by PromiseUtils.defer() */
add_task(async function test_reject_Error() {
let def = PromiseUtils.defer();
def.reject(new Error("This one rejects"));
await Assert.rejects(def.promise, /This one rejects/, "reject method with Error for rejection");
});
/* Test for the case when a pending Promise is passed to
* the reject method of Deferred object */
add_task(async function test_reject_pending_promise() {
let def = PromiseUtils.defer();
let p = new Promise((resolve, reject) => {
setTimeout(() => resolve(100), 100);
});
def.reject(p);
await Assert.rejects(def.promise, Promise, "Rejection with a pending promise uses the passed promise itself as the reason of rejection");
});
/* Test for the case when a resolved Promise
* is passed to the reject method */
add_task(async function test_reject_resolved_promise() {
let def = PromiseUtils.defer();
let p = new Promise((resolve, reject) => resolve("This resolved"));
def.reject(p);
await Assert.rejects(def.promise, Promise, "Rejection with a resolved promise uses the passed promise itself as the reason of rejection");
});
/* Test for the case when a rejected Promise is
* passed to the reject method */
add_task(async function test_reject_resolved_promise() {
PromiseTestUtils.expectUncaughtRejection(/This one rejects/);
let def = PromiseUtils.defer();
let p = new Promise((resolve, reject) => reject(new Error("This one rejects")));
def.reject(p);
await Assert.rejects(def.promise, Promise, "Rejection with a rejected promise uses the passed promise itself as the reason of rejection");
});