summaryrefslogtreecommitdiffstats
path: root/dom/manifest/test/browser_ManifestFinder_browserHasManifestLink.js
blob: 5ec663962e72a57b58b96f5abb341662ecd0377d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Used by JSHint:
/*global Cu, BrowserTestUtils, ok, add_task, gBrowser */
"use strict";
const { ManifestFinder } = Cu.import("resource://gre/modules/ManifestFinder.jsm", {});
const defaultURL = new URL("http://example.org/browser/dom/manifest/test/resource.sjs");
defaultURL.searchParams.set("Content-Type", "text/html; charset=utf-8");

const tests = [{
  body: `
    <link rel="manifesto" href='${defaultURL}?body={"name":"fail"}'>
    <link rel="foo bar manifest bar test" href='${defaultURL}?body={"name":"value"}'>
    <link rel="manifest" href='${defaultURL}?body={"name":"fail"}'>
  `,
  run(result) {
    ok(result, "Document has a web manifest.");
  },
}, {
  body: `
    <link rel="amanifista" href='${defaultURL}?body={"name":"fail"}'>
    <link rel="foo bar manifesto bar test" href='${defaultURL}?body={"name":"pass-1"}'>
    <link rel="manifesto" href='${defaultURL}?body={"name":"fail"}'>`,
  run(result) {
    ok(!result, "Document does not have a web manifest.");
  },
}, {
  body: `
    <link rel="manifest" href="">
    <link rel="manifest" href='${defaultURL}?body={"name":"fail"}'>`,
  run(result) {
    ok(!result, "Manifest link is has empty href.");
  },
}, {
  body: `
    <link rel="manifest">
    <link rel="manifest" href='${defaultURL}?body={"name":"fail"}'>`,
  run(result) {
    ok(!result, "Manifest link is missing.");
  },
}];

function makeTestURL({ body }) {
  const url = new URL(defaultURL);
  url.searchParams.set("body", encodeURIComponent(body));
  return url.href;
}

/**
 * Test basic API error conditions
 */
add_task(function*() {
  const expected = "Invalid types should throw a TypeError.";
  for (let invalidValue of [undefined, null, 1, {}, "test"]) {
    try {
      yield ManifestFinder.contentManifestLink(invalidValue);
      ok(false, expected);
    } catch (e) {
      is(e.name, "TypeError", expected);
    }
    try {
      yield ManifestFinder.browserManifestLink(invalidValue);
      ok(false, expected);
    } catch (e) {
      is(e.name, "TypeError", expected);
    }
  }
});

add_task(function*() {
  const runningTests = tests
    .map(
      test => ({
        gBrowser,
        test,
        url: makeTestURL(test),
      })
    )
    .map(
      tabOptions => BrowserTestUtils.withNewTab(tabOptions, function*(browser) {
        const result = yield ManifestFinder.browserHasManifestLink(browser);
        tabOptions.test.run(result);
      })
    );
  yield Promise.all(runningTests);
});