browser/base/content/test/general/browser_discovery.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/general/browser_discovery.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,159 @@
     1.4 +var browser;
     1.5 +
     1.6 +function doc() browser.contentDocument;
     1.7 +
     1.8 +function setHandlerFunc(aResultFunc) {
     1.9 +  gBrowser.addEventListener("DOMLinkAdded", function (event) {
    1.10 +    gBrowser.removeEventListener("DOMLinkAdded", arguments.callee, false);
    1.11 +    executeSoon(aResultFunc);
    1.12 +  }, false);
    1.13 +}
    1.14 +
    1.15 +function test() {
    1.16 +  waitForExplicitFinish();
    1.17 +
    1.18 +  gBrowser.selectedTab = gBrowser.addTab();
    1.19 +  browser = gBrowser.selectedBrowser;
    1.20 +  browser.addEventListener("load", function (event) {
    1.21 +    event.currentTarget.removeEventListener("load", arguments.callee, true);
    1.22 +    iconDiscovery();
    1.23 +  }, true);
    1.24 +  var rootDir = getRootDirectory(gTestPath);
    1.25 +  content.location = rootDir + "discovery.html";
    1.26 +}
    1.27 +
    1.28 +var iconDiscoveryTests = [
    1.29 +  { text: "rel icon discovered" },
    1.30 +  { rel: "abcdefg icon qwerty", text: "rel may contain additional rels separated by spaces" },
    1.31 +  { rel: "ICON", text: "rel is case insensitive" },
    1.32 +  { rel: "shortcut-icon", pass: false, text: "rel shortcut-icon not discovered" },
    1.33 +  { href: "moz.png", text: "relative href works" },
    1.34 +  { href: "notthere.png", text: "404'd icon is removed properly" },
    1.35 +  { href: "data:image/x-icon,%00", type: "image/x-icon", text: "data: URIs work" },
    1.36 +  { type: "image/png; charset=utf-8", text: "type may have optional parameters (RFC2046)" }
    1.37 +];
    1.38 +
    1.39 +function runIconDiscoveryTest() {
    1.40 +  var test = iconDiscoveryTests[0];
    1.41 +  var head = doc().getElementById("linkparent");
    1.42 +  var hasSrc = gBrowser.getIcon() != null;
    1.43 +  if (test.pass)
    1.44 +    ok(hasSrc, test.text);
    1.45 +  else
    1.46 +    ok(!hasSrc, test.text);
    1.47 +
    1.48 +  head.removeChild(head.getElementsByTagName('link')[0]);
    1.49 +  iconDiscoveryTests.shift();
    1.50 +  iconDiscovery(); // Run the next test.
    1.51 +}
    1.52 +
    1.53 +function iconDiscovery() {
    1.54 +  if (iconDiscoveryTests.length) {
    1.55 +    setHandlerFunc(runIconDiscoveryTest);
    1.56 +    gBrowser.setIcon(gBrowser.selectedTab, null);
    1.57 +
    1.58 +    var test = iconDiscoveryTests[0];
    1.59 +    var head = doc().getElementById("linkparent");
    1.60 +    var link = doc().createElement("link");
    1.61 +
    1.62 +    var rootDir = getRootDirectory(gTestPath);
    1.63 +    var rel = test.rel || "icon";
    1.64 +    var href = test.href || rootDir + "moz.png";
    1.65 +    var type = test.type || "image/png";
    1.66 +    if (test.pass == undefined)
    1.67 +      test.pass = true;
    1.68 +
    1.69 +    link.rel = rel;
    1.70 +    link.href = href;
    1.71 +    link.type = type;
    1.72 +    head.appendChild(link);
    1.73 +  } else {
    1.74 +    searchDiscovery();
    1.75 +  }
    1.76 +}
    1.77 +
    1.78 +var searchDiscoveryTests = [
    1.79 +  { text: "rel search discovered" },
    1.80 +  { rel: "SEARCH", text: "rel is case insensitive" },
    1.81 +  { rel: "-search-", pass: false, text: "rel -search- not discovered" },
    1.82 +  { rel: "foo bar baz search quux", text: "rel may contain additional rels separated by spaces" },
    1.83 +  { href: "https://not.mozilla.com", text: "HTTPS ok" },
    1.84 +  { href: "ftp://not.mozilla.com", text: "FTP ok" },
    1.85 +  { href: "data:text/foo,foo", pass: false, text: "data URI not permitted" },
    1.86 +  { href: "javascript:alert(0)", pass: false, text: "JS URI not permitted" },
    1.87 +  { type: "APPLICATION/OPENSEARCHDESCRIPTION+XML", text: "type is case insensitve" },
    1.88 +  { type: " application/opensearchdescription+xml ", text: "type may contain extra whitespace" },
    1.89 +  { type: "application/opensearchdescription+xml; charset=utf-8", text: "type may have optional parameters (RFC2046)" },
    1.90 +  { type: "aapplication/opensearchdescription+xml", pass: false, text: "type should not be loosely matched" },
    1.91 +  { rel: "search search search", count: 1, text: "only one engine should be added" }
    1.92 +];
    1.93 +
    1.94 +function runSearchDiscoveryTest() {
    1.95 +  var test = searchDiscoveryTests[0];
    1.96 +  var title = test.title || searchDiscoveryTests.length;
    1.97 +  if (browser.engines) {
    1.98 +    var hasEngine = (test.count) ? (browser.engines[0].title == title &&
    1.99 +                                    browser.engines.length == test.count) :
   1.100 +                                   (browser.engines[0].title == title);
   1.101 +    ok(hasEngine, test.text);
   1.102 +    browser.engines = null;
   1.103 +  }
   1.104 +  else
   1.105 +    ok(!test.pass, test.text);
   1.106 +
   1.107 +  searchDiscoveryTests.shift();
   1.108 +  searchDiscovery(); // Run the next test.
   1.109 +}
   1.110 +
   1.111 +// This handler is called twice, once for each added link element.
   1.112 +// Only want to check once the second link element has been added.
   1.113 +var ranOnce = false;
   1.114 +function runMultipleEnginesTestAndFinalize() {
   1.115 +  if (!ranOnce) {
   1.116 +    ranOnce = true;
   1.117 +    return;
   1.118 +  }
   1.119 +  ok(browser.engines, "has engines");
   1.120 +  is(browser.engines.length, 1, "only one engine");
   1.121 +  is(browser.engines[0].uri, "http://first.mozilla.com/search.xml", "first engine wins");
   1.122 +
   1.123 +  gBrowser.removeCurrentTab();
   1.124 +  finish();
   1.125 +}
   1.126 +
   1.127 +function searchDiscovery() {
   1.128 +  var head = doc().getElementById("linkparent");
   1.129 +
   1.130 +  if (searchDiscoveryTests.length) {
   1.131 +    setHandlerFunc(runSearchDiscoveryTest);
   1.132 +    var test = searchDiscoveryTests[0];
   1.133 +    var link = doc().createElement("link");
   1.134 +
   1.135 +    var rel = test.rel || "search";
   1.136 +    var href = test.href || "http://so.not.here.mozilla.com/search.xml";
   1.137 +    var type = test.type || "application/opensearchdescription+xml";
   1.138 +    var title = test.title || searchDiscoveryTests.length;
   1.139 +    if (test.pass == undefined)
   1.140 +      test.pass = true;
   1.141 +
   1.142 +    link.rel = rel;
   1.143 +    link.href = href;
   1.144 +    link.type = type;
   1.145 +    link.title = title;
   1.146 +    head.appendChild(link);
   1.147 +  } else {
   1.148 +    setHandlerFunc(runMultipleEnginesTestAndFinalize);
   1.149 +    setHandlerFunc(runMultipleEnginesTestAndFinalize);
   1.150 +    // Test multiple engines with the same title
   1.151 +    var link = doc().createElement("link");
   1.152 +    link.rel = "search";
   1.153 +    link.href = "http://first.mozilla.com/search.xml";
   1.154 +    link.type = "application/opensearchdescription+xml";
   1.155 +    link.title = "Test Engine";
   1.156 +    var link2 = link.cloneNode(false);
   1.157 +    link2.href = "http://second.mozilla.com/search.xml";
   1.158 +
   1.159 +    head.appendChild(link);
   1.160 +    head.appendChild(link2);
   1.161 +  }
   1.162 +}

mercurial