Bug 1454813: Part 2a - Remove spawn_task support in plain/chrome mochitests. r=florian

MozReview-Commit-ID: DbGBt6tH6Vo

--HG--
extra : rebase_source : c99351319eefb8e6454e80e0d354ef650e672ddf
This commit is contained in:
Kris Maglione 2018-04-17 16:01:10 -07:00
Родитель f1f4e87948
Коммит 2744972833
16 изменённых файлов: 33 добавлений и 528 удалений

Просмотреть файл

@ -28,7 +28,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=1022869
waitForLoad().then(function() {
// Cookies are set up, disallow third-party cookies and start the test.
SpecialPowers.pushPrefEnv({ set: [[ 'network.cookie.cookieBehavior', 1 ]] },
() => { spawn_task(continueTest); });
() => { continueTest(); });
}).catch((e) => { ok(false, `Got exception: ${e}`) });
}

Просмотреть файл

@ -13,8 +13,8 @@ SimpleTest.requestFlakyTimeout("Legacy test, possibly no good reason");
var winUtils = SpecialPowers.getDOMWindowUtils(window);
function* setup() {
yield SpecialPowers.pushPrefEnv({set: [["general.smoothScroll", false]]});
async function setup() {
await SpecialPowers.pushPrefEnv({set: [["general.smoothScroll", false]]});
winUtils.advanceTimeAndRefresh(100);
}
@ -205,8 +205,8 @@ async function testRunner() {
}
}
spawn_task(setup)
.then(() => spawn_task(testRunner))
.then(() => spawn_task(cleanup))
setup()
.then(() => testRunner())
.then(() => cleanup())
.catch(err => ok(false, err));
</script>

Просмотреть файл

@ -286,8 +286,7 @@ var sleep = function (timeoutMs) {
// __testMediaQueriesInPictureElements(resisting)__.
// Test to see if media queries are properly spoofed in picture elements
// when we are resisting fingerprinting. A generator function
// to be used with SpawnTask.js.
// when we are resisting fingerprinting.
var testMediaQueriesInPictureElements = async function(resisting) {
let picture = document.createElementNS(HTML_NS, "picture");
for (let [key, offVal, onVal] of expected_values) {
@ -327,8 +326,7 @@ var pushPref = function (key, value) {
};
// __test(isContent)__.
// Run all tests. A generator function to be used
// with SpawnTask.js.
// Run all tests.
var test = async function(isContent) {
for (prefValue of [false, true]) {
await pushPref("privacy.resistFingerprinting", prefValue);

Просмотреть файл

@ -22,8 +22,8 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=418986
<script type="text/javascript">
// Run all tests now.
window.onload = function () {
add_task(function* () {
yield test(false);
add_task(async function() {
await test(false);
});
};
</script>

Просмотреть файл

@ -3,7 +3,6 @@ skip-if = os == 'android'
support-files = test-dir/test-file
[test_sample.xul]
[test_sanityAddTask.xul]
[test_sanityEventUtils.xul]
[test_sanityPluginUtils.html]
[test_sanityException.xul]
@ -12,7 +11,6 @@ support-files = test-dir/test-file
fail-if = true
[test_sanityManifest_pf.xul]
fail-if = true
[test_sanitySpawnTask.xul]
[test_chromeGetTestFile.xul]
[test_tasks_skip.xul]
[test_tasks_skipall.xul]

Просмотреть файл

@ -1,43 +0,0 @@
<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
type="text/css"?>
<window title="Test spawnTawk function"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"/>
<script type="application/javascript">
<![CDATA[
// Check that we can 'add_task' a few times and all tasks run asynchronously before test finishes.
add_task(async function() {
var x = await Promise.resolve(1);
is(x, 1, "task yields Promise value as expected");
});
add_task(function* () {
var x = yield [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)];
is(x.join(""), "123", "task yields Promise value as expected");
});
add_task(function* () {
var x = yield (function* () {
return 3;
}());
is(x, 3, "task yields generator function return value as expected");
});
]]>
</script>
<body xmlns="http://www.w3.org/1999/xhtml" >
</body>
</window>

Просмотреть файл

@ -1,70 +0,0 @@
<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
type="text/css"?>
<window title="Test spawnTawk function"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"/>
<script type="application/javascript">
<![CDATA[
SimpleTest.waitForExplicitFinish();
var externalGeneratorFunction = function* () {
return 8;
};
var nestedFunction = function* () {
return yield function* () {
return yield function* () {
return yield function* () {
return yield Promise.resolve(9);
}();
}();
}();
}
var variousTests = function* () {
var val1 = yield [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)];
is(val1.join(""), "123", "Array of promises -> Promise.all");
var val2 = yield Promise.resolve(2);
is(val2, 2, "Resolved promise yields value.");
var val3 = yield function* () { return 3; };
is(val3, 3, "Generator functions are spawned.");
//var val4 = yield function () { return 4; };
//is(val4, 4, "Plain functions run and return.");
var val5 = yield (function* () { return 5; }());
is(val5, 5, "Generators are spawned.");
try {
var val6 = yield Promise.reject(Error("error6"));
ok(false, "Shouldn't reach this line.");
} catch (error) {
is(error.message, "error6", "Rejected promise throws error.");
}
try {
var val7 = yield function* () { throw Error("error7"); };
ok(false, "Shouldn't reach this line.");
} catch (error) {
is(error.message, "error7", "Thrown error propagates.");
}
var val8 = yield externalGeneratorFunction();
is(val8, 8, "External generator also spawned.");
var val9 = yield nestedFunction();
is(val9, 9, "Nested generator functions work.");
return 10;
};
spawn_task(variousTests).then(function(result) {
is(result, 10, "spawn_task(...) returns promise");
SimpleTest.finish();
});
]]>
</script>
<body xmlns="http://www.w3.org/1999/xhtml" >
</body>
</window>

Просмотреть файл

@ -1,7 +1,6 @@
[DEFAULT]
[test_TestsRunningAfterSimpleTestFinish.html]
skip-if = true #depends on fix for bug 1048446
[test_add_task.html]
[test_createFiles.html]
[test_importInMainProcess.html]
support-files = importtesting_chromescript.js
@ -40,5 +39,4 @@ fail-if = true
[test_sanity_manifest_pf.html]
skip-if = toolkit == 'android' # we use the old manifest style on android
fail-if = true
[test_spawn_task.html]
[test_sanity_waitForCondition.html]

Просмотреть файл

@ -1,38 +0,0 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for mochitest add_task, found in SpawnTask.js</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=">Mozilla Bug 1187701</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script class="testbody" type="text/javascript">
// Check that we can 'add_task' a few times and all tasks run asynchronously before test finishes.
add_task(async function() {
var x = await Promise.resolve(1);
is(x, 1, "task yields Promise value as expected");
});
add_task(function* () {
var x = yield [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)];
is(x.join(""), "123", "task yields Promise value as expected");
});
add_task(function* () {
var x = yield (function* () {
return 3;
}());
is(x, 3, "task yields generator function return value as expected");
});
</script>
</pre>
</body>
</html>

Просмотреть файл

@ -1,73 +0,0 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Test for mochitest SpawnTask.js sanity</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=">Mozilla Bug 1078657</a>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for sanity **/
SimpleTest.waitForExplicitFinish();
var externalGeneratorFunction = function* () {
return 8;
};
var nestedFunction = function* () {
return yield function* () {
return yield function* () {
return yield function* () {
return yield Promise.resolve(9);
}();
}();
}();
}
var variousTests = function* () {
var val1 = yield [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)];
is(val1.join(""), "123", "Array of promises -> Promise.all");
var val2 = yield Promise.resolve(2);
is(val2, 2, "Resolved promise yields value.");
var val3 = yield function* () { return 3; };
is(val3, 3, "Generator functions are spawned.");
//var val4 = yield function () { return 4; };
//is(val4, 4, "Plain functions run and return.");
var val5 = yield (function* () { return 5; }());
is(val5, 5, "Generators are spawned.");
try {
var val6 = yield Promise.reject(Error("error6"));
ok(false, "Shouldn't reach this line.");
} catch (error) {
is(error.message, "error6", "Rejected promise throws error.");
}
try {
var val7 = yield function* () { throw Error("error7"); };
ok(false, "Shouldn't reach this line.");
} catch (error) {
is(error.message, "error7", "Thrown error propagates.");
}
var val8 = yield externalGeneratorFunction();
is(val8, 8, "External generator also spawned.");
var val9 = yield nestedFunction();
is(val9, 9, "Nested generator functions work.");
return 10;
};
spawn_task(variousTests).then(function(result) {
is(result, 10, "spawn_task(...) returns promise");
SimpleTest.finish();
});
</script>
</pre>
</body>
</html>

Просмотреть файл

@ -1,24 +0,0 @@
LICENSE for SpawnTask.js (the co library):
(The MIT License)
Copyright (c) 2014 TJ Holowaychuk &lt;tj@vision-media.ca&gt;
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Просмотреть файл

@ -1,248 +1,3 @@
// # SpawnTask.js
// Directly copied from the "co" library by TJ Holowaychuk.
// See https://github.com/tj/co/tree/4.6.0
// For use with mochitest-plain and mochitest-chrome.
// spawn_task(generatorFunction):
// Expose only the `co` function, which is very similar to Task.spawn in Task.jsm.
// We call this function spawn_task to make its purpose more plain, and to
// reduce the chance of name collisions.
var spawn_task = (function () {
/**
* slice() reference.
*/
var slice = Array.prototype.slice;
/**
* Wrap the given generator `fn` into a
* function that returns a promise.
* This is a separate function so that
* every `co()` call doesn't create a new,
* unnecessary closure.
*
* @param {GeneratorFunction} fn
* @return {Function}
* @api public
*/
co.wrap = function (fn) {
createPromise.__generatorFunction__ = fn;
return createPromise;
function createPromise() {
return co.call(this, fn.apply(this, arguments));
}
};
/**
* Execute the generator function or a generator
* and return a promise.
*
* @param {Function} fn
* @return {Promise}
* @api public
*/
function co(gen) {
var ctx = this;
var args = slice.call(arguments, 1)
// we wrap everything in a promise to avoid promise chaining,
// which leads to memory leak errors.
// see https://github.com/tj/co/issues/180
return new Promise(function(resolve, reject) {
if (typeof gen === 'function') gen = gen.apply(ctx, args);
if (!gen || typeof gen.next !== 'function') return resolve(gen);
onFulfilled();
/**
* @param {Mixed} res
* @return {Promise}
* @api private
*/
function onFulfilled(res) {
var ret;
try {
ret = gen.next(res);
} catch (e) {
return reject(e);
}
next(ret);
}
/**
* @param {Error} err
* @return {Promise}
* @api private
*/
function onRejected(err) {
var ret;
try {
ret = gen.throw(err);
} catch (e) {
return reject(e);
}
next(ret);
}
/**
* Get the next value in the generator,
* return a promise.
*
* @param {Object} ret
* @return {Promise}
* @api private
*/
function next(ret) {
if (ret.done) return resolve(ret.value);
var value = toPromise.call(ctx, ret.value);
if (value && isPromise(value)) return value.then(onFulfilled, onRejected);
return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, '
+ 'but the following object was passed: "' + String(ret.value) + '"'));
}
});
}
/**
* Convert a `yield`ed value into a promise.
*
* @param {Mixed} obj
* @return {Promise}
* @api private
*/
function toPromise(obj) {
if (!obj) return obj;
if (isPromise(obj)) return obj;
if (isGeneratorFunction(obj) || isGenerator(obj)) return co.call(this, obj);
if ('function' == typeof obj) return thunkToPromise.call(this, obj);
if (Array.isArray(obj)) return arrayToPromise.call(this, obj);
if (isObject(obj)) return objectToPromise.call(this, obj);
return obj;
}
/**
* Convert a thunk to a promise.
*
* @param {Function}
* @return {Promise}
* @api private
*/
function thunkToPromise(fn) {
var ctx = this;
return new Promise(function (resolve, reject) {
fn.call(ctx, function (err, res) {
if (err) return reject(err);
if (arguments.length > 2) res = slice.call(arguments, 1);
resolve(res);
});
});
}
/**
* Convert an array of "yieldables" to a promise.
* Uses `Promise.all()` internally.
*
* @param {Array} obj
* @return {Promise}
* @api private
*/
function arrayToPromise(obj) {
return Promise.all(obj.map(toPromise, this));
}
/**
* Convert an object of "yieldables" to a promise.
* Uses `Promise.all()` internally.
*
* @param {Object} obj
* @return {Promise}
* @api private
*/
function objectToPromise(obj){
var results = new obj.constructor();
var keys = Object.keys(obj);
var promises = [];
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var promise = toPromise.call(this, obj[key]);
if (promise && isPromise(promise)) defer(promise, key);
else results[key] = obj[key];
}
return Promise.all(promises).then(function () {
return results;
});
function defer(promise, key) {
// predefine the key in the result
results[key] = undefined;
promises.push(promise.then(function (res) {
results[key] = res;
}));
}
}
/**
* Check if `obj` is a promise.
*
* @param {Object} obj
* @return {Boolean}
* @api private
*/
function isPromise(obj) {
return 'function' == typeof obj.then;
}
/**
* Check if `obj` is a generator.
*
* @param {Mixed} obj
* @return {Boolean}
* @api private
*/
function isGenerator(obj) {
return 'function' == typeof obj.next && 'function' == typeof obj.throw;
}
/**
* Check if `obj` is a generator function.
*
* @param {Mixed} obj
* @return {Boolean}
* @api private
*/
function isGeneratorFunction(obj) {
var constructor = obj.constructor;
if (!constructor) return false;
if ('GeneratorFunction' === constructor.name || 'GeneratorFunction' === constructor.displayName) return true;
return isGenerator(constructor.prototype);
}
/**
* Check for plain object.
*
* @param {Mixed} val
* @return {Boolean}
* @api private
*/
function isObject(val) {
return Object == val.constructor;
}
return co;
})();
// add_task(generatorFunction):
// Call `add_task(generatorFunction)` for each separate
// asynchronous task in a mochitest. Tasks are run consecutively.
@ -254,6 +9,10 @@ var add_task = (function () {
var task_list = [];
var run_only_this_task = null;
function isGenerator(value) {
return value && typeof value === "object" && typeof value.next === "function";
}
// The "add_task" function
return function (generatorFunction) {
if (task_list.length === 0) {
@ -269,7 +28,7 @@ var add_task = (function () {
// Use setTimeout to ensure the master task runs after the client
// script finishes.
setTimeout(function () {
spawn_task(function* () {
(async () => {
// Allow for a task to be skipped; we need only use the structured logger
// for this, whilst deactivating log buffering to ensure that messages
// are always printed to stdout.
@ -295,7 +54,10 @@ var add_task = (function () {
continue;
}
info("SpawnTask.js | Entering test " + name);
yield task();
let result = await task();
if (isGenerator(result)) {
ok(false, "Task returned a generator");
}
info("SpawnTask.js | Leaving test " + name);
}
} catch (ex) {
@ -308,7 +70,7 @@ var add_task = (function () {
}
// All tasks are finished.
SimpleTest.finish();
});
})();
});
}
generatorFunction.skip = () => generatorFunction.__skipMe = true;

Просмотреть файл

@ -324,8 +324,8 @@
false);
// Chain testStructure to runTests's promise.
spawn_task(testStructure(mm, isPrivate)).then(deferred.resolve)
.catch((e) => { info(`caught failing test ${e}`); });
testStructure(mm, isPrivate).then(deferred.resolve)
.catch((e) => { info(`caught failing test ${e}`); });
});
document.body.appendChild(iframe);

Просмотреть файл

@ -2,8 +2,7 @@
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<window title="Test disableglobalhistory attribute on remote browsers"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
onload="run_test();">
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript"
@ -28,16 +27,15 @@
});
}
function run_test() {
spawn_task(function*() {
yield expectUseGlobalHistory("inprocess_disabled", false);
yield expectUseGlobalHistory("inprocess_enabled", true);
add_task(async function() {
await expectUseGlobalHistory("inprocess_disabled", false);
await expectUseGlobalHistory("inprocess_enabled", true);
yield expectUseGlobalHistory("remote_disabled", false);
yield expectUseGlobalHistory("remote_enabled", true);
window.opener.done();
});
};
await expectUseGlobalHistory("remote_disabled", false);
await expectUseGlobalHistory("remote_enabled", true);
window.opener.done();
ok(true);
});
</script>
</window>

Просмотреть файл

@ -19,8 +19,7 @@
function done() {
w.close();
SimpleTest.finish();
}
</script>
</window>
</window>

Просмотреть файл

@ -41,7 +41,7 @@ function setupFormHistory(aCallback) {
{ op: "remove" },
{ op: "add", fieldname: "field1", value: "Sec" },
], () => {
spawn_task(aCallback);
aCallback();
});
}