Skip to content

Commit

Permalink
Adding BaseWorkerHandler and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald committed Feb 23, 2021
1 parent 568429f commit 1e08d95
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
13 changes: 12 additions & 1 deletion docs/writing-tests/testharness.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ It is possible to customize the set of scopes with a metadata comment, such as
// ==> would only run in the window and service worker scope
// META: global=dedicatedworker
// ==> would run in the default dedicated worker scope
// META: global=dedicatedworker-module
// ==> would run in the dedicated worker scope as a module
// META: global=worker
// ==> would run in the dedicated, shared, and service worker scopes
```
Expand All @@ -149,8 +151,13 @@ are:

* `window` (default): to be run at <code><var>x</var>.any.html</code>
* `dedicatedworker` (default): to be run at <code><var>x</var>.any.worker.html</code>
* `serviceworker`: to be run at <code><var>x</var>.any.serviceworker.html</code> (`.https` is implied)
* `dedicatedworker-module` to be run at <code><var>x</var>.any.worker-module.html</code>
* `serviceworker`: to be run at <code><var>x</var>.any.serviceworker.html</code> (`.https` is
implied)
* `serviceworker-module`: to be run at <code><var>x</var>.any.serviceworker-module.html</code>
(`.https` is implied)
* `sharedworker`: to be run at <code><var>x</var>.any.sharedworker.html</code>
* `sharedworker-module`: to be run at <code><var>x</var>.any.sharedworker-module.html</code>
* `jsshell`: to be run in a JavaScript shell, without access to the DOM
(currently only supported in SpiderMonkey, and skipped in wptrunner)
* `worker`: shorthand for the dedicated, shared, and service worker scopes
Expand Down Expand Up @@ -183,6 +190,10 @@ Use `// META: script=link/to/resource.js` at the beginning of the resource. For

can be used to include both the global and a local `utils.js` in a test.

In window environments, the script will be included using a classic `<script>` tag. In classic
worker environments, the script will be imported using `importScripts()`. In module worker
environments, the script will be imported using a static `import`.

### Specifying a timeout of long

Use `// META: timeout=long` at the beginning of the resource.
Expand Down
35 changes: 19 additions & 16 deletions tools/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,23 @@ class ServiceWorkerModulesHandler(HtmlWrapperHandler):
"""


class AnyWorkerHandler(WrapperHandler):
class BaseWorkerHandler(WrapperHandler):
headers = [('Content-Type', 'text/javascript')]

def _meta_replacement(self, key, value):
return None

def _script_replacement(self, key, value):
if key == "script":
attribute = value.replace("\\", "\\\\").replace('"', '\\"')
return self._create_script_import(attribute)
if key == "title":
value = value.replace("\\", "\\\\").replace('"', '\\"')
return 'self.META_TITLE = "%s";' % value
return None


class ClassicWorkerHandler(BaseWorkerHandler):
path_replace = [(".any.worker.js", ".any.js")]
wrapper = """%(meta)s
self.GLOBAL = {
Expand All @@ -366,23 +381,11 @@ class AnyWorkerHandler(WrapperHandler):
done();
"""

def _meta_replacement(self, key, value):
return None

def _create_script_import(self, attribute):
return 'importScripts("%s")' % attribute

def _script_replacement(self, key, value):
if key == "script":
attribute = value.replace("\\", "\\\\").replace('"', '\\"')
return self._create_script_import(attribute)
if key == "title":
value = value.replace("\\", "\\\\").replace('"', '\\"')
return 'self.META_TITLE = "%s";' % value
return None


class AnyWorkerModuleHandler(AnyWorkerHandler):
class ModuleWorkerHandler(BaseWorkerHandler):
path_replace = [(".any.worker-module.js", ".any.js")]
wrapper = """%(meta)s
self.GLOBAL = {
Expand Down Expand Up @@ -451,8 +454,8 @@ def add_mount_point(self, url_base, path):
("GET", "*.any.sharedworker-module.html", SharedWorkerModulesHandler),
("GET", "*.any.serviceworker.html", ServiceWorkersHandler),
("GET", "*.any.serviceworker-module.html", ServiceWorkerModulesHandler),
("GET", "*.any.worker.js", AnyWorkerHandler),
("GET", "*.any.worker-module.js", AnyWorkerModuleHandler),
("GET", "*.any.worker.js", ClassicWorkerHandler),
("GET", "*.any.worker-module.js", ModuleWorkerHandler),
("GET", "*.asis", handlers.AsIsHandler),
("GET", "/.well-known/origin-policy", handlers.PythonScriptHandler),
("*", "*.py", handlers.PythonScriptHandler),
Expand Down

0 comments on commit 1e08d95

Please sign in to comment.