Skip to content

Commit

Permalink
Add support for test variants in .any.js tests.
Browse files Browse the repository at this point in the history
Fixes #7210.
  • Loading branch information
Ms2ger committed Apr 24, 2018
1 parent 5e8ee10 commit 3084706
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 16 deletions.
7 changes: 7 additions & 0 deletions docs/_writing-tests/testharness.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ can be used to include both the global and a local `utils.js` in a test.

Use `// META: timeout=long` at the beginning of the resource.

### Specifying test variants in auto-generated boilerplate tests

Use `// META: variant=url-suffix` at the beginning of the resource. For example,

// META: variant=
// META: variant=?wss


[general guidelines]: {{ site.baseurl }}{% link _writing-tests/general-guidelines.md %}
[testharness-api]: {{ site.baseurl }}{% link _writing-tests/testharness-api.md %}
Expand Down
38 changes: 26 additions & 12 deletions tools/manifest/sourcefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,18 @@ def variant_nodes(self):
@cached_property
def test_variants(self):
rv = []
for element in self.variant_nodes:
if "content" in element.attrib:
variant = element.attrib["content"]
assert variant == "" or variant[0] in ["#", "?"]
rv.append(variant)
if self.ext == ".js":
for (key, value) in self.script_metadata:
if key == b"variant":
rv.append(value.decode("utf-8"))
else:
for element in self.variant_nodes:
if "content" in element.attrib:
variant = element.attrib["content"]
rv.append(variant)

for variant in rv:
assert variant == "" or variant[0] in ["#", "?"], variant

if not rv:
rv = [""]
Expand Down Expand Up @@ -598,20 +605,27 @@ def manifest_items(self):
break

tests = [
TestharnessTest(self, global_variant_url(self.url, suffix), timeout=self.timeout)
TestharnessTest(self, global_variant_url(self.url, suffix) + variant, timeout=self.timeout)
for suffix in sorted(global_suffixes(globals))
for variant in self.test_variants
]
rv = TestharnessTest.item_type, tests

elif self.name_is_worker:
rv = (TestharnessTest.item_type,
[TestharnessTest(self, replace_end(self.url, ".worker.js", ".worker.html"),
timeout=self.timeout)])
test_url = replace_end(self.url, ".worker.js", ".worker.html")
tests = [
TestharnessTest(self, test_url + variant, timeout=self.timeout)
for variant in self.test_variants
]
rv = TestharnessTest.item_type, tests

elif self.name_is_window:
rv = (TestharnessTest.item_type,
[TestharnessTest(self, replace_end(self.url, ".window.js", ".window.html"),
timeout=self.timeout)])
test_url = replace_end(self.url, ".window.js", ".window.html")
tests = [
TestharnessTest(self, test_url + variant, timeout=self.timeout)
for variant in self.test_variants
]
rv = TestharnessTest.item_type, tests

elif self.name_is_webdriver:
rv = WebdriverSpecTest.item_type, [WebdriverSpecTest(self, self.url,
Expand Down
98 changes: 98 additions & 0 deletions tools/manifest/tests/test_sourcefile.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,66 @@ def test_window_long_timeout():
assert item.timeout == "long"


def test_worker_with_variants():
contents = b"""// META: variant=
// META: variant=?wss
test()"""

s = create("html/test.worker.js", contents=contents)
assert not s.name_is_non_test
assert not s.name_is_manual
assert not s.name_is_visual
assert not s.name_is_multi_global
assert s.name_is_worker
assert not s.name_is_window
assert not s.name_is_reference

assert not s.content_is_testharness

item_type, items = s.manifest_items()
assert item_type == "testharness"

expected_urls = [
"/html/test.worker.html" + suffix
for suffix in ["", "?wss"]
]
assert len(items) == len(expected_urls)

for item, url in zip(items, expected_urls):
assert item.url == url
assert item.timeout is None


def test_window_with_variants():
contents = b"""// META: variant=
// META: variant=?wss
test()"""

s = create("html/test.window.js", contents=contents)
assert not s.name_is_non_test
assert not s.name_is_manual
assert not s.name_is_visual
assert not s.name_is_multi_global
assert not s.name_is_worker
assert s.name_is_window
assert not s.name_is_reference

assert not s.content_is_testharness

item_type, items = s.manifest_items()
assert item_type == "testharness"

expected_urls = [
"/html/test.window.html" + suffix
for suffix in ["", "?wss"]
]
assert len(items) == len(expected_urls)

for item, url in zip(items, expected_urls):
assert item.url == url
assert item.timeout is None


def test_python_long_timeout():
contents = b"""# META: timeout=long
Expand Down Expand Up @@ -319,6 +379,44 @@ def test_multi_global_with_custom_globals(input, expected):
assert item.timeout is None


def test_multi_global_with_variants():
contents = b"""// META: global=window,worker
// META: variant=
// META: variant=?wss
test()"""

s = create("html/test.any.js", contents=contents)
assert not s.name_is_non_test
assert not s.name_is_manual
assert not s.name_is_visual
assert s.name_is_multi_global
assert not s.name_is_worker
assert not s.name_is_reference

assert not s.content_is_testharness

item_type, items = s.manifest_items()
assert item_type == "testharness"

urls = {
"dedicatedworker": "/html/test.any.worker.html",
"serviceworker": "/html/test.https.any.serviceworker.html",
"sharedworker": "/html/test.any.sharedworker.html",
"window": "/html/test.any.html",
}

expected_urls = sorted(
urls[ty] + suffix
for ty in ["dedicatedworker", "serviceworker", "sharedworker", "window"]
for suffix in ["", "?wss"]
)
assert len(items) == len(expected_urls)

for item, url in zip(items, expected_urls):
assert item.url == url
assert item.timeout is None


@pytest.mark.parametrize("input,expected", [
(b"""//META: foo=bar\n""", [(b"foo", b"bar")]),
(b"""// META: foo=bar\n""", [(b"foo", b"bar")]),
Expand Down
11 changes: 7 additions & 4 deletions tools/serve/serve.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ def handle_request(self, request, response):
self.check_exposure(request)

path = self._get_path(request.url_parts.path, True)
query = request.url_parts.query
if query:
query = "?" + query
meta = "\n".join(self._get_meta(request))
response.content = self.wrapper % {"meta": meta, "path": path}
response.content = self.wrapper % {"meta": meta, "path": path, "query": query}
wrap_pipeline(path, request, response)

def _get_path(self, path, resource_path):
Expand Down Expand Up @@ -171,7 +174,7 @@ class WorkersHandler(HtmlWrapperHandler):
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
fetch_tests_from_worker(new Worker("%(path)s"));
fetch_tests_from_worker(new Worker("%(path)s%(query)s"));
</script>
"""

Expand Down Expand Up @@ -217,7 +220,7 @@ class SharedWorkersHandler(HtmlWrapperHandler):
<script src="/resources/testharnessreport.js"></script>
<div id=log></div>
<script>
fetch_tests_from_worker(new SharedWorker("%(path)s"));
fetch_tests_from_worker(new SharedWorker("%(path)s%(query)s"));
</script>
"""

Expand All @@ -236,7 +239,7 @@ class ServiceWorkersHandler(HtmlWrapperHandler):
const scope = 'does/not/exist';
let reg = await navigator.serviceWorker.getRegistration(scope);
if (reg) await reg.unregister();
reg = await navigator.serviceWorker.register("%(path)s", {scope});
reg = await navigator.serviceWorker.register("%(path)s%(query)s", {scope});
fetch_tests_from_worker(reg.installing);
})();
</script>
Expand Down

0 comments on commit 3084706

Please sign in to comment.