Skip to content

Commit

Permalink
Use a simpler and more reliable way to access the global scope in tes…
Browse files Browse the repository at this point in the history
…tharness.js.

In particular, this should work in JS shells.
  • Loading branch information
Ms2ger authored and jgraham committed Apr 26, 2018
1 parent 504f623 commit 62c85b4
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions resources/testharness.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ policies and contribution forms [3].
/* Documentation: http://web-platform-tests.org/writing-tests/testharness-api.html
* (../docs/_writing-tests/testharness-api.md) */

(function ()
(function (global_scope)
{
var debug = false;
// default timeout is 10 seconds, test can override if needed
Expand Down Expand Up @@ -48,9 +48,6 @@ policies and contribution forms [3].
*
* // Should return the test harness timeout duration in milliseconds.
* float test_timeout();
*
* // Should return the global scope object.
* object global_scope();
* };
*/

Expand Down Expand Up @@ -248,10 +245,6 @@ policies and contribution forms [3].
return settings.harness_timeout.normal;
};

WindowTestEnvironment.prototype.global_scope = function() {
return window;
};

/*
* Base TestEnvironment implementation for a generic web worker.
*
Expand Down Expand Up @@ -344,10 +337,6 @@ policies and contribution forms [3].
return null;
};

WorkerTestEnvironment.prototype.global_scope = function() {
return self;
};

/*
* Dedicated web workers.
* https://html.spec.whatwg.org/multipage/workers.html#dedicatedworkerglobalscope
Expand Down Expand Up @@ -463,23 +452,23 @@ policies and contribution forms [3].
};

function create_test_environment() {
if ('document' in self) {
if ('document' in global_scope) {
return new WindowTestEnvironment();
}
if ('DedicatedWorkerGlobalScope' in self &&
self instanceof DedicatedWorkerGlobalScope) {
if ('DedicatedWorkerGlobalScope' in global_scope &&
global_scope instanceof DedicatedWorkerGlobalScope) {
return new DedicatedWorkerTestEnvironment();
}
if ('SharedWorkerGlobalScope' in self &&
self instanceof SharedWorkerGlobalScope) {
if ('SharedWorkerGlobalScope' in global_scope &&
global_scope instanceof SharedWorkerGlobalScope) {
return new SharedWorkerTestEnvironment();
}
if ('ServiceWorkerGlobalScope' in self &&
self instanceof ServiceWorkerGlobalScope) {
if ('ServiceWorkerGlobalScope' in global_scope &&
global_scope instanceof ServiceWorkerGlobalScope) {
return new ServiceWorkerTestEnvironment();
}
if ('WorkerGlobalScope' in self &&
self instanceof WorkerGlobalScope) {
if ('WorkerGlobalScope' in global_scope &&
global_scope instanceof WorkerGlobalScope) {
return new DedicatedWorkerTestEnvironment();
}

Expand All @@ -489,13 +478,13 @@ policies and contribution forms [3].
var test_environment = create_test_environment();

function is_shared_worker(worker) {
return 'SharedWorker' in self && worker instanceof SharedWorker;
return 'SharedWorker' in global_scope && worker instanceof SharedWorker;
}

function is_service_worker(worker) {
// The worker object may be from another execution context,
// so do not use instanceof here.
return 'ServiceWorker' in self &&
return 'ServiceWorker' in global_scope &&
Object.prototype.toString.call(worker) == '[object ServiceWorker]';
}

Expand Down Expand Up @@ -2824,7 +2813,7 @@ policies and contribution forms [3].
function expose(object, name)
{
var components = name.split(".");
var target = test_environment.global_scope();
var target = global_scope;
for (var i = 0; i < components.length - 1; i++) {
if (!(components[i] in target)) {
target[components[i]] = {};
Expand All @@ -2846,7 +2835,7 @@ policies and contribution forms [3].
/** Returns the 'src' URL of the first <script> tag in the page to include the file 'testharness.js'. */
function get_script_url()
{
if (!('document' in self)) {
if (!('document' in global_scope)) {
return undefined;
}

Expand Down Expand Up @@ -2954,5 +2943,5 @@ policies and contribution forms [3].

test_environment.on_tests_ready();

})();
})(this);
// vim: set expandtab shiftwidth=4 tabstop=4:

0 comments on commit 62c85b4

Please sign in to comment.