mobile/android/base/tests/testBrowserDiscovery.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 "use strict";
     8 const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
    10 Cu.import("resource://gre/modules/Services.jsm");
    12 function ok(passed, text) {
    13   do_report_result(passed, text, Components.stack.caller, false);
    14 }
    16 // We use a global variable to track the <browser> where the tests are happening
    17 let browser;
    19 function setHandlerFunc(handler, test) {
    20   browser.addEventListener("DOMLinkAdded", function linkAdded(event) {
    21     browser.removeEventListener("DOMLinkAdded", linkAdded, false);
    22     Services.tm.mainThread.dispatch(handler.bind(this, test), Ci.nsIThread.DISPATCH_NORMAL);
    23   }, false);
    24 }
    26 add_test(function setup_browser() {
    27   let BrowserApp = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp;
    28   do_register_cleanup(function cleanup() {
    29     BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser));
    30   });
    32   let url = "http://mochi.test:8888/tests/robocop/link_discovery.html";
    33   browser = BrowserApp.addTab(url, { selected: true, parentId: BrowserApp.selectedTab.id }).browser;
    34   browser.addEventListener("load", function startTests(event) {
    35     browser.removeEventListener("load", startTests, true);
    36     Services.tm.mainThread.dispatch(run_next_test, Ci.nsIThread.DISPATCH_NORMAL);
    37   }, true);
    38 });
    40 let searchDiscoveryTests = [
    41   { text: "rel search discovered" },
    42   { rel: "SEARCH", text: "rel is case insensitive" },
    43   { rel: "-search-", pass: false, text: "rel -search- not discovered" },
    44   { rel: "foo bar baz search quux", text: "rel may contain additional rels separated by spaces" },
    45   { href: "https://not.mozilla.com", text: "HTTPS ok" },
    46   { href: "ftp://not.mozilla.com", text: "FTP ok" },
    47   { href: "data:text/foo,foo", pass: false, text: "data URI not permitted" },
    48   { href: "javascript:alert(0)", pass: false, text: "JS URI not permitted" },
    49   { type: "APPLICATION/OPENSEARCHDESCRIPTION+XML", text: "type is case insensitve" },
    50   { type: " application/opensearchdescription+xml ", text: "type may contain extra whitespace" },
    51   { type: "application/opensearchdescription+xml; charset=utf-8", text: "type may have optional parameters (RFC2046)" },
    52   { type: "aapplication/opensearchdescription+xml", pass: false, text: "type should not be loosely matched" },
    53   { rel: "search search search", count: 1, text: "only one engine should be added" }
    54 ];
    56 function execute_search_test(test) {
    57   if (browser.engines) {
    58     let matchCount = (!("count" in test) || browser.engines.length === test.count);
    59     let matchTitle = (test.title == browser.engines[0].title);
    60     ok(matchCount && matchTitle, test.text);
    61     browser.engines = null;
    62   } else {
    63     ok(!test.pass, test.text);
    64   }
    65   run_next_test();
    66 }
    68 function prep_search_test(test) {
    69   setHandlerFunc(execute_search_test, test);
    71   let rel = test.rel || "search";
    72   let href = test.href || "http://so.not.here.mozilla.com/search.xml";
    73   let type = test.type || "application/opensearchdescription+xml";
    74   let title = test.title;
    75   if (!("pass" in test)) {
    76     test.pass = true;
    77   }
    79   let head = browser.contentDocument.getElementById("linkparent");
    80   let link = browser.contentDocument.createElement("link");
    81   link.rel = rel;
    82   link.href = href;
    83   link.type = type;
    84   link.title = title;
    85   head.appendChild(link);
    86 }
    88 let feedDiscoveryTests = [
    89   { text: "rel feed discovered" },
    90   { rel: "ALTERNATE", text: "rel is case insensitive" },
    91   { rel: "-alternate-", pass: false, text: "rel -alternate- not discovered" },
    92   { rel: "foo bar baz alternate quux", text: "rel may contain additional rels separated by spaces" },
    93   { href: "https://not.mozilla.com", text: "HTTPS ok" },
    94   { href: "ftp://not.mozilla.com", text: "FTP ok" },
    95   { href: "data:text/foo,foo", pass: false, text: "data URI not permitted" },
    96   { href: "javascript:alert(0)", pass: false, text: "JS URI not permitted" },
    97   { type: "application/rss+xml", text: "type can be RSS" },
    98   { type: "aPPliCAtion/RSS+xml", text: "type is case insensitve" },
    99   { type: " application/atom+xml ", text: "type may contain extra whitespace" },
   100   { type: "application/atom+xml; charset=utf-8", text: "type may have optional parameters (RFC2046)" },
   101   { type: "aapplication/atom+xml", pass: false, text: "type should not be loosely matched" },
   102   { rel: "alternate alternate alternate", count: 1, text: "only one feed should be added" }
   103 ];
   105 function execute_feed_test(test) {
   106   if (browser.feeds) {
   107     let matchCount = (!("count" in test) || browser.feeds.length === test.count);
   108     let matchTitle = (test.title == browser.feeds[0].title);
   109     ok(matchCount && matchTitle, test.text);
   110     browser.feeds = null;
   111   } else {
   112     ok(!test.pass, test.text);
   113   }
   114   run_next_test();
   115 }
   117 function prep_feed_test(test) {
   118   setHandlerFunc(execute_feed_test, test);
   120   let rel = test.rel || "alternate";
   121   let href = test.href || "http://so.not.here.mozilla.com/feed.xml";
   122   let type = test.type || "application/atom+xml";
   123   let title = test.title;
   124   if (!("pass" in test)) {
   125     test.pass = true;
   126   }
   128   let head = browser.contentDocument.getElementById("linkparent");
   129   let link = browser.contentDocument.createElement("link");
   130   link.rel = rel;
   131   link.href = href;
   132   link.type = type;
   133   link.title = title;
   134   head.appendChild(link);
   135 }
   137 let searchTest;
   138 while ((searchTest = searchDiscoveryTests.shift())) {
   139   let title = searchTest.title || searchDiscoveryTests.length;
   140   searchTest.title = title;
   141   add_test(prep_search_test.bind(this, searchTest));
   142 }
   144 let feedTest;
   145 while ((feedTest = feedDiscoveryTests.shift())) {
   146   let title = feedTest.title || feedDiscoveryTests.length;
   147   feedTest.title = title;
   148   add_test(prep_feed_test.bind(this, feedTest));
   149 }
   151 run_next_test();

mercurial