browser_discovery.js
Go to the documentation of this file.
2 var browser;
3 
4 function doc() browser.contentDocument;
5 
6 function setHandlerFunc(aResultFunc) {
7  DOMLinkHandler.handleEvent = function (event) {
8  gBrowserHandler.call(DOMLinkHandler, event);
9  aResultFunc();
10  }
11 }
12 
13 function test() {
14  gBrowserHandler = DOMLinkHandler.handleEvent;
15  ok(gBrowserHandler, "found browser handler");
16 
18 
19  gBrowser.selectedTab = gBrowser.addTab();
20  browser = gBrowser.selectedBrowser;
21  browser.addEventListener("load", function (event) {
22  event.currentTarget.removeEventListener("load", arguments.callee, true);
23  iconDiscovery();
24  }, true);
25  content.location = "chrome://mochikit/content/browser/browser/base/content/test/discovery.html";
26 }
27 
29  { text: "rel icon discovered" },
30  { rel: "abcdefg icon qwerty", text: "rel may contain additional rels separated by spaces" },
31  { rel: "ICON", text: "rel is case insensitive" },
32  { rel: "shortcut-icon", pass: false, text: "rel shortcut-icon not discovered" },
33  { href: "moz.png", text: "relative href works" },
34  { href: "notthere.png", text: "404'd icon is removed properly" },
35  { href: "data:image/x-icon,%00", type: "image/x-icon", text: "data: URIs work" },
36  { type: "image/png; charset=utf-8", text: "type may have optional parameters (RFC2046)" }
37 ];
38 
40  var test = iconDiscoveryTests[0];
41  var head = doc().getElementById("linkparent");
42  var hasSrc = gProxyFavIcon.hasAttribute("src");
43  if (test.pass)
44  ok(hasSrc, test.text);
45  else
46  ok(!hasSrc, test.text);
47 
48  head.removeChild(head.getElementsByTagName('link')[0]);
49  iconDiscoveryTests.shift();
50  iconDiscovery(); // Run the next test.
51 }
52 
53 function iconDiscovery() {
55  if (iconDiscoveryTests.length) {
56  gProxyFavIcon.removeAttribute("src");
57 
58  var test = iconDiscoveryTests[0];
59  var head = doc().getElementById("linkparent");
60  var link = doc().createElement("link");
61 
62  var rel = test.rel || "icon";
63  var href = test.href || "chrome://mochikit/content/browser/browser/base/content/test/moz.png";
64  var type = test.type || "image/png";
65  if (test.pass == undefined)
66  test.pass = true;
67 
68  link.rel = rel;
69  link.href = href;
70  link.type = type;
71  head.appendChild(link);
72  } else {
74  }
75 }
76 
78  { text: "rel search discovered" },
79  { rel: "SEARCH", text: "rel is case insensitive" },
80  { rel: "-search-", pass: false, text: "rel -search- not discovered" },
81  { rel: "foo bar baz search quux", text: "rel may contain additional rels separated by spaces" },
82  { href: "https://not.mozilla.com", text: "HTTPS ok" },
83  { href: "ftp://not.mozilla.com", text: "FTP ok" },
84  { href: "data:text/foo,foo", pass: false, text: "data URI not permitted" },
85  { href: "javascript:alert(0)", pass: false, text: "JS URI not permitted" },
86  { type: "APPLICATION/OPENSEARCHDESCRIPTION+XML", text: "type is case insensitve" },
87  { type: " application/opensearchdescription+xml ", text: "type may contain extra whitespace" },
88  { type: "application/opensearchdescription+xml; charset=utf-8", text: "type may have optional parameters (RFC2046)" },
89  { type: "aapplication/opensearchdescription+xml", pass: false, text: "type should not be loosely matched" },
90  { rel: "search search search", count: 1, text: "only one engine should be added" }
91 ];
92 
94  var test = searchDiscoveryTests[0];
95  var title = test.title || searchDiscoveryTests.length;
96  if (browser.engines) {
97  var hasEngine = (test.count) ? (browser.engines[0].title == title &&
98  browser.engines.length == test.count) :
99  (browser.engines[0].title == title);
100  ok(hasEngine, test.text);
101  browser.engines = null;
102  }
103  else
104  ok(!test.pass, test.text);
105 
106  searchDiscoveryTests.shift();
107  searchDiscovery(); // Run the next test.
108 }
109 
110 // This handler is called twice, once for each added link element.
111 // Only want to check once the second link element has been added.
112 var ranOnce = false;
114  if (!ranOnce) {
115  ranOnce = true;
116  return;
117  }
118  ok(browser.engines, "has engines");
119  is(browser.engines.length, 1, "only one engine");
120  is(browser.engines[0].uri, "http://first.mozilla.com/search.xml", "first engine wins");
121 
122  gBrowser.removeCurrentTab();
123 
124  // Reset the default link handler
125  DOMLinkHandler.handleEvent = gBrowserHandler;
126 
127  finish();
128 }
129 
130 function searchDiscovery() {
131  var head = doc().getElementById("linkparent");
132 
133  if (searchDiscoveryTests.length) {
135  var test = searchDiscoveryTests[0];
136  var link = doc().createElement("link");
137 
138  var rel = test.rel || "search";
139  var href = test.href || "http://so.not.here.mozilla.com/search.xml";
140  var type = test.type || "application/opensearchdescription+xml";
141  var title = test.title || searchDiscoveryTests.length;
142  if (test.pass == undefined)
143  test.pass = true;
144 
145  link.rel = rel;
146  link.href = href;
147  link.type = type;
148  link.title = title;
149  head.appendChild(link);
150  } else {
152  // Test multiple engines with the same title
153  var link = doc().createElement("link");
154  link.rel = "search";
155  link.href = "http://first.mozilla.com/search.xml";
156  link.type = "application/opensearchdescription+xml";
157  link.title = "Test Engine";
158  var link2 = link.cloneNode(false);
159  link2.href = "http://second.mozilla.com/search.xml";
160 
161  head.appendChild(link);
162  head.appendChild(link2);
163  }
164 }
function doc() browser.contentDocument
function setHandlerFunc(aResultFunc)
var event
var gBrowserHandler
function runIconDiscoveryTest()
var count
Definition: test_bug7406.js:32
var searchDiscoveryTests
Element Properties href
waitForExplicitFinish()
function searchDiscovery()
var ranOnce
return null
Definition: FeedWriter.js:1143
var iconDiscoveryTests
return!aWindow arguments!aWindow arguments[0]
var gProxyFavIcon
Definition: browser.js:76
function runMultipleEnginesTestAndFinalize()
function test()
var browser
function runSearchDiscoveryTest()
function iconDiscovery()