summaryrefslogtreecommitdiffstats
path: root/mobile/android/tests/browser/robocop/testBrowserDiscovery.js
blob: 3b3421dc24e197f439ead6122ff801602ffbd762 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

var { classes: Cc, interfaces: Ci, utils: Cu } = Components;

Cu.import("resource://gre/modules/Services.jsm");

// We use a global variable to track the <browser> where the tests are happening
var browser;

function setHandlerFunc(handler, test) {
  browser.addEventListener("DOMLinkAdded", function linkAdded(event) {
    browser.removeEventListener("DOMLinkAdded", linkAdded, false);
    Services.tm.mainThread.dispatch(handler.bind(this, test), Ci.nsIThread.DISPATCH_NORMAL);
  }, false);
}

add_test(function setup_browser() {
  let BrowserApp = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp;
  do_register_cleanup(function cleanup() {
    BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser));
  });

  let url = "http://mochi.test:8888/tests/robocop/link_discovery.html";
  browser = BrowserApp.addTab(url, { selected: true, parentId: BrowserApp.selectedTab.id }).browser;
  browser.addEventListener("load", function startTests(event) {
    browser.removeEventListener("load", startTests, true);
    Services.tm.mainThread.dispatch(run_next_test, Ci.nsIThread.DISPATCH_NORMAL);
  }, true);
});

var 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: "ftp://not.mozilla.com", text: "FTP 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" }
];

function execute_search_test(test) {
  if (browser.engines) {
    let matchCount = (!("count" in test) || browser.engines.length === test.count);
    let matchTitle = (test.title == browser.engines[0].title);
    ok(matchCount && matchTitle, test.text);
    browser.engines = null;
  } else {
    ok(!test.pass, test.text);
  }
  run_next_test();
}

function prep_search_test(test) {
  // Syncrhonously load the search service.
  Services.search.getVisibleEngines();

  setHandlerFunc(execute_search_test, test);

  let rel = test.rel || "search";
  let href = test.href || "http://so.not.here.mozilla.com/search.xml";
  let type = test.type || "application/opensearchdescription+xml";
  let title = test.title;
  if (!("pass" in test)) {
    test.pass = true;
  }

  let head = browser.contentDocument.getElementById("linkparent");
  let link = browser.contentDocument.createElement("link");
  link.rel = rel;
  link.href = href;
  link.type = type;
  link.title = title;
  head.appendChild(link);
}

var feedDiscoveryTests = [
  { text: "rel feed discovered" },
  { rel: "ALTERNATE", text: "rel is case insensitive" },
  { rel: "-alternate-", pass: false, text: "rel -alternate- not discovered" },
  { rel: "foo bar baz alternate quux", text: "rel may contain additional rels separated by spaces" },
  { href: "https://not.mozilla.com", text: "HTTPS ok" },
  { href: "ftp://not.mozilla.com", text: "FTP 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/rss+xml", text: "type can be RSS" },
  { type: "aPPliCAtion/RSS+xml", text: "type is case insensitve" },
  { type: " application/atom+xml ", text: "type may contain extra whitespace" },
  { type: "application/atom+xml; charset=utf-8", text: "type may have optional parameters (RFC2046)" },
  { type: "aapplication/atom+xml", pass: false, text: "type should not be loosely matched" },
  { rel: "alternate alternate alternate", count: 1, text: "only one feed should be added" }
];

function execute_feed_test(test) {
  if (browser.feeds) {
    let matchCount = (!("count" in test) || browser.feeds.length === test.count);
    let matchTitle = (test.title == browser.feeds[0].title);
    ok(matchCount && matchTitle, test.text);
    browser.feeds = null;
  } else {
    ok(!test.pass, test.text);
  }
  run_next_test();
}

function prep_feed_test(test) {
  setHandlerFunc(execute_feed_test, test);

  let rel = test.rel || "alternate";
  let href = test.href || "http://so.not.here.mozilla.com/feed.xml";
  let type = test.type || "application/atom+xml";
  let title = test.title;
  if (!("pass" in test)) {
    test.pass = true;
  }

  let head = browser.contentDocument.getElementById("linkparent");
  let link = browser.contentDocument.createElement("link");
  link.rel = rel;
  link.href = href;
  link.type = type;
  link.title = title;
  head.appendChild(link);
}

var searchTest;
while ((searchTest = searchDiscoveryTests.shift())) {
  let title = searchTest.title || searchDiscoveryTests.length;
  searchTest.title = title;
  add_test(prep_search_test.bind(this, searchTest));
}

var feedTest;
while ((feedTest = feedDiscoveryTests.shift())) {
  let title = feedTest.title || feedDiscoveryTests.length;
  feedTest.title = title;
  add_test(prep_feed_test.bind(this, feedTest));
}

run_next_test();