summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/browser/browser_search_discovery.js
blob: 94c198776c9218d1fd2c62e4da7d78acad26f534 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/* eslint-disable mozilla/no-arbitrary-setTimeout */

// Bug 1588193 - BrowserTestUtils.waitForContentEvent now resolves slightly
// earlier than before, so it no longer suffices to only wait for a single event
// tick before checking if browser.engines has been updated. Instead we use a 1s
// timeout, which may cause the test to take more time.
requestLongerTimeout(2);

add_task(async function () {
  let url =
    "http://mochi.test:8888/browser/browser/components/search/test/browser/discovery.html";
  info("Test search discovery");
  await BrowserTestUtils.withNewTab(url, searchDiscovery);
});

let searchDiscoveryTests = [
  { text: "rel search discovered" },
  { rel: "SEARCH", text: "rel is case insensitive" },
  { rel: "-search-", pass: false, text: "rel -search- not discovered" },
  {
    rel: "foo bar baz search quux",
    text: "rel may contain additional rels separated by spaces",
  },
  { href: "https://not.mozilla.com", text: "HTTPS ok" },
  { href: "data:text/foo,foo", pass: false, text: "data URI not permitted" },
  { href: "javascript:alert(0)", pass: false, text: "JS URI not permitted" },
  {
    type: "APPLICATION/OPENSEARCHDESCRIPTION+XML",
    text: "type is case insensitve",
  },
  {
    type: " application/opensearchdescription+xml ",
    text: "type may contain extra whitespace",
  },
  {
    type: "application/opensearchdescription+xml; charset=utf-8",
    text: "type may have optional parameters (RFC2046)",
  },
  {
    type: "aapplication/opensearchdescription+xml",
    pass: false,
    text: "type should not be loosely matched",
  },
  {
    rel: "search search search",
    count: 1,
    text: "only one engine should be added",
  },
];

async function searchDiscovery() {
  let browser = gBrowser.selectedBrowser;

  for (let testCase of searchDiscoveryTests) {
    if (testCase.pass == undefined) {
      testCase.pass = true;
    }
    testCase.title = testCase.title || searchDiscoveryTests.indexOf(testCase);

    let promiseLinkAdded = BrowserTestUtils.waitForContentEvent(
      gBrowser.selectedBrowser,
      "DOMLinkAdded",
      false,
      null,
      true
    );

    await SpecialPowers.spawn(gBrowser.selectedBrowser, [testCase], test => {
      let doc = content.document;
      let head = doc.getElementById("linkparent");
      let link = doc.createElement("link");
      link.rel = test.rel || "search";
      link.href = test.href || "http://so.not.here.mozilla.com/search.xml";
      link.type = test.type || "application/opensearchdescription+xml";
      link.title = test.title;
      head.appendChild(link);
    });

    await promiseLinkAdded;
    await new Promise(resolve => setTimeout(resolve, 1000));

    if (browser.engines) {
      info(`Found ${browser.engines.length} engines`);
      info(`First engine title: ${browser.engines[0].title}`);
      let hasEngine = testCase.count
        ? browser.engines[0].title == testCase.title &&
          browser.engines.length == testCase.count
        : browser.engines[0].title == testCase.title;
      ok(hasEngine, testCase.text);
      browser.engines = null;
    } else {
      ok(!testCase.pass, testCase.text);
    }
  }

  info("Test multiple engines with the same title");
  let promiseLinkAdded = BrowserTestUtils.waitForContentEvent(
    gBrowser.selectedBrowser,
    "DOMLinkAdded",
    false,
    e => e.target.href == "http://second.mozilla.com/search.xml",
    true
  );
  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    let doc = content.document;
    let head = doc.getElementById("linkparent");
    let link = doc.createElement("link");
    link.rel = "search";
    link.href = "http://first.mozilla.com/search.xml";
    link.type = "application/opensearchdescription+xml";
    link.title = "Test Engine";
    let link2 = link.cloneNode(false);
    link2.href = "http://second.mozilla.com/search.xml";
    head.appendChild(link);
    head.appendChild(link2);
  });

  await promiseLinkAdded;
  await new Promise(resolve => setTimeout(resolve, 1000));

  ok(browser.engines, "has engines");
  is(browser.engines.length, 1, "only one engine");
  is(
    browser.engines[0].uri,
    "http://first.mozilla.com/search.xml",
    "first engine wins"
  );
  browser.engines = null;
}