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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial