mobile/android/base/tests/testBrowserDiscovery.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.

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

mercurial