1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/tests/testBrowserDiscovery.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +"use strict"; 1.10 + 1.11 +const { classes: Cc, interfaces: Ci, utils: Cu } = Components; 1.12 + 1.13 +Cu.import("resource://gre/modules/Services.jsm"); 1.14 + 1.15 +function ok(passed, text) { 1.16 + do_report_result(passed, text, Components.stack.caller, false); 1.17 +} 1.18 + 1.19 +// We use a global variable to track the <browser> where the tests are happening 1.20 +let browser; 1.21 + 1.22 +function setHandlerFunc(handler, test) { 1.23 + browser.addEventListener("DOMLinkAdded", function linkAdded(event) { 1.24 + browser.removeEventListener("DOMLinkAdded", linkAdded, false); 1.25 + Services.tm.mainThread.dispatch(handler.bind(this, test), Ci.nsIThread.DISPATCH_NORMAL); 1.26 + }, false); 1.27 +} 1.28 + 1.29 +add_test(function setup_browser() { 1.30 + let BrowserApp = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; 1.31 + do_register_cleanup(function cleanup() { 1.32 + BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser)); 1.33 + }); 1.34 + 1.35 + let url = "http://mochi.test:8888/tests/robocop/link_discovery.html"; 1.36 + browser = BrowserApp.addTab(url, { selected: true, parentId: BrowserApp.selectedTab.id }).browser; 1.37 + browser.addEventListener("load", function startTests(event) { 1.38 + browser.removeEventListener("load", startTests, true); 1.39 + Services.tm.mainThread.dispatch(run_next_test, Ci.nsIThread.DISPATCH_NORMAL); 1.40 + }, true); 1.41 +}); 1.42 + 1.43 +let searchDiscoveryTests = [ 1.44 + { text: "rel search discovered" }, 1.45 + { rel: "SEARCH", text: "rel is case insensitive" }, 1.46 + { rel: "-search-", pass: false, text: "rel -search- not discovered" }, 1.47 + { rel: "foo bar baz search quux", text: "rel may contain additional rels separated by spaces" }, 1.48 + { href: "https://not.mozilla.com", text: "HTTPS ok" }, 1.49 + { href: "ftp://not.mozilla.com", text: "FTP ok" }, 1.50 + { href: "data:text/foo,foo", pass: false, text: "data URI not permitted" }, 1.51 + { href: "javascript:alert(0)", pass: false, text: "JS URI not permitted" }, 1.52 + { type: "APPLICATION/OPENSEARCHDESCRIPTION+XML", text: "type is case insensitve" }, 1.53 + { type: " application/opensearchdescription+xml ", text: "type may contain extra whitespace" }, 1.54 + { type: "application/opensearchdescription+xml; charset=utf-8", text: "type may have optional parameters (RFC2046)" }, 1.55 + { type: "aapplication/opensearchdescription+xml", pass: false, text: "type should not be loosely matched" }, 1.56 + { rel: "search search search", count: 1, text: "only one engine should be added" } 1.57 +]; 1.58 + 1.59 +function execute_search_test(test) { 1.60 + if (browser.engines) { 1.61 + let matchCount = (!("count" in test) || browser.engines.length === test.count); 1.62 + let matchTitle = (test.title == browser.engines[0].title); 1.63 + ok(matchCount && matchTitle, test.text); 1.64 + browser.engines = null; 1.65 + } else { 1.66 + ok(!test.pass, test.text); 1.67 + } 1.68 + run_next_test(); 1.69 +} 1.70 + 1.71 +function prep_search_test(test) { 1.72 + setHandlerFunc(execute_search_test, test); 1.73 + 1.74 + let rel = test.rel || "search"; 1.75 + let href = test.href || "http://so.not.here.mozilla.com/search.xml"; 1.76 + let type = test.type || "application/opensearchdescription+xml"; 1.77 + let title = test.title; 1.78 + if (!("pass" in test)) { 1.79 + test.pass = true; 1.80 + } 1.81 + 1.82 + let head = browser.contentDocument.getElementById("linkparent"); 1.83 + let link = browser.contentDocument.createElement("link"); 1.84 + link.rel = rel; 1.85 + link.href = href; 1.86 + link.type = type; 1.87 + link.title = title; 1.88 + head.appendChild(link); 1.89 +} 1.90 + 1.91 +let feedDiscoveryTests = [ 1.92 + { text: "rel feed discovered" }, 1.93 + { rel: "ALTERNATE", text: "rel is case insensitive" }, 1.94 + { rel: "-alternate-", pass: false, text: "rel -alternate- not discovered" }, 1.95 + { rel: "foo bar baz alternate quux", text: "rel may contain additional rels separated by spaces" }, 1.96 + { href: "https://not.mozilla.com", text: "HTTPS ok" }, 1.97 + { href: "ftp://not.mozilla.com", text: "FTP ok" }, 1.98 + { href: "data:text/foo,foo", pass: false, text: "data URI not permitted" }, 1.99 + { href: "javascript:alert(0)", pass: false, text: "JS URI not permitted" }, 1.100 + { type: "application/rss+xml", text: "type can be RSS" }, 1.101 + { type: "aPPliCAtion/RSS+xml", text: "type is case insensitve" }, 1.102 + { type: " application/atom+xml ", text: "type may contain extra whitespace" }, 1.103 + { type: "application/atom+xml; charset=utf-8", text: "type may have optional parameters (RFC2046)" }, 1.104 + { type: "aapplication/atom+xml", pass: false, text: "type should not be loosely matched" }, 1.105 + { rel: "alternate alternate alternate", count: 1, text: "only one feed should be added" } 1.106 +]; 1.107 + 1.108 +function execute_feed_test(test) { 1.109 + if (browser.feeds) { 1.110 + let matchCount = (!("count" in test) || browser.feeds.length === test.count); 1.111 + let matchTitle = (test.title == browser.feeds[0].title); 1.112 + ok(matchCount && matchTitle, test.text); 1.113 + browser.feeds = null; 1.114 + } else { 1.115 + ok(!test.pass, test.text); 1.116 + } 1.117 + run_next_test(); 1.118 +} 1.119 + 1.120 +function prep_feed_test(test) { 1.121 + setHandlerFunc(execute_feed_test, test); 1.122 + 1.123 + let rel = test.rel || "alternate"; 1.124 + let href = test.href || "http://so.not.here.mozilla.com/feed.xml"; 1.125 + let type = test.type || "application/atom+xml"; 1.126 + let title = test.title; 1.127 + if (!("pass" in test)) { 1.128 + test.pass = true; 1.129 + } 1.130 + 1.131 + let head = browser.contentDocument.getElementById("linkparent"); 1.132 + let link = browser.contentDocument.createElement("link"); 1.133 + link.rel = rel; 1.134 + link.href = href; 1.135 + link.type = type; 1.136 + link.title = title; 1.137 + head.appendChild(link); 1.138 +} 1.139 + 1.140 +let searchTest; 1.141 +while ((searchTest = searchDiscoveryTests.shift())) { 1.142 + let title = searchTest.title || searchDiscoveryTests.length; 1.143 + searchTest.title = title; 1.144 + add_test(prep_search_test.bind(this, searchTest)); 1.145 +} 1.146 + 1.147 +let feedTest; 1.148 +while ((feedTest = feedDiscoveryTests.shift())) { 1.149 + let title = feedTest.title || feedDiscoveryTests.length; 1.150 + feedTest.title = title; 1.151 + add_test(prep_feed_test.bind(this, feedTest)); 1.152 +} 1.153 + 1.154 +run_next_test();