Bug 952894 - Browser search and feed discovery tests. r=rnewman, a=lsblakk
authorMark Finkle <mfinkle@mozilla.com>
Sat, 28 Dec 2013 22:15:09 -0500
changeset 174564 de2c488ac3178015293818aef24999e0a78d713e
parent 174563 b71255e32a12433b7b65e77bb41b97d1db3f2d40
child 174565 e4a16e078e59659c03df91b0bbb3543b28bf7480
push id3224
push userlsblakk@mozilla.com
push dateTue, 04 Feb 2014 01:06:49 +0000
treeherdermozilla-beta@60c04d0987f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersrnewman, lsblakk
bugs952894
milestone28.0a2
Bug 952894 - Browser search and feed discovery tests. r=rnewman, a=lsblakk
mobile/android/base/tests/link_discovery.html
mobile/android/base/tests/robocop.ini
mobile/android/base/tests/testBrowserDiscovery.java
mobile/android/base/tests/testBrowserDiscovery.js
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/tests/link_discovery.html
@@ -0,0 +1,8 @@
+<!DOCTYPE HTML>
+<html>
+  <head id="linkparent">
+    <title>Autodiscovery Test</title>
+  </head>
+  <body>
+  </body>
+</html>
--- a/mobile/android/base/tests/robocop.ini
+++ b/mobile/android/base/tests/robocop.ini
@@ -57,16 +57,17 @@ skip-if = processor == "x86"
 # [testShareLink] # see bug 915897
 [testSystemPages]
 # disabled on x86 only; bug 907383
 skip-if = processor == "x86"
 # [testThumbnails] # see bug 813107
 # [testVkbOverlap] # see bug 907274
 
 # Using JavascriptTest
+[testBrowserDiscovery]
 [testDeviceSearchEngine]
 [testJNI]
 # [testMozPay] # see bug 945675
 [testOrderedBroadcast]
 [testSharedPreferences]
 [testUITelemetry]
 
 # Used for Talos, please don't use in mochitest
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/tests/testBrowserDiscovery.java
@@ -0,0 +1,10 @@
+package org.mozilla.gecko.tests;
+
+import org.mozilla.gecko.*;
+
+
+public class testBrowserDiscovery extends JavascriptTest {
+    public testBrowserDiscovery() {
+        super("testBrowserDiscovery.js");
+    }
+}
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/tests/testBrowserDiscovery.js
@@ -0,0 +1,151 @@
+// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*-
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+function ok(passed, text) {
+  do_report_result(passed, text, Components.stack.caller, false);
+}
+
+// We use a global variable to track the <browser> where the tests are happening
+let browser;
+
+function setHandlerFunc(handler, test) {
+  browser.addEventListener("DOMLinkAdded", function linkAdded(event) {
+    browser.removeEventListener("DOMLinkAdded", linkAdded, false);
+    Services.tm.mainThread.dispatch(handler.bind(this, test), Ci.nsIThread.DISPATCH_NORMAL);
+  }, false);
+}
+
+add_test(function setup_browser() {
+  let BrowserApp = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp;
+  do_register_cleanup(function cleanup() {
+    BrowserApp.closeTab(BrowserApp.getTabForBrowser(browser));
+  });
+
+  let url = "http://mochi.test:8888/tests/robocop/link_discovery.html";
+  browser = BrowserApp.addTab(url, { selected: true, parentId: BrowserApp.selectedTab.id }).browser;
+  browser.addEventListener("load", function startTests(event) {
+    browser.removeEventListener("load", startTests, true);
+    Services.tm.mainThread.dispatch(run_next_test, Ci.nsIThread.DISPATCH_NORMAL);
+  }, true);
+});
+
+let searchDiscoveryTests = [
+  { text: "rel search discovered" },
+  { rel: "SEARCH", text: "rel is case insensitive" },
+  { rel: "-search-", pass: false, text: "rel -search- not discovered" },
+  { rel: "foo bar baz search quux", text: "rel may contain additional rels separated by spaces" },
+  { href: "https://not.mozilla.com", text: "HTTPS ok" },
+  { href: "ftp://not.mozilla.com", text: "FTP ok" },
+  { href: "data:text/foo,foo", pass: false, text: "data URI not permitted" },
+  { href: "javascript:alert(0)", pass: false, text: "JS URI not permitted" },
+  { type: "APPLICATION/OPENSEARCHDESCRIPTION+XML", text: "type is case insensitve" },
+  { type: " application/opensearchdescription+xml ", text: "type may contain extra whitespace" },
+  { type: "application/opensearchdescription+xml; charset=utf-8", text: "type may have optional parameters (RFC2046)" },
+  { type: "aapplication/opensearchdescription+xml", pass: false, text: "type should not be loosely matched" },
+  { rel: "search search search", count: 1, text: "only one engine should be added" }
+];
+
+function execute_search_test(test) {
+  if (browser.engines) {
+    let matchCount = (!("count" in test) || browser.engines.length === test.count);
+    let matchTitle = (test.title == browser.engines[0].title);
+    ok(matchCount && matchTitle, test.text);
+    browser.engines = null;
+  } else {
+    ok(!test.pass, test.text);
+  }
+  run_next_test();
+}
+
+function prep_search_test(test) {
+  setHandlerFunc(execute_search_test, test);
+
+  let rel = test.rel || "search";
+  let href = test.href || "http://so.not.here.mozilla.com/search.xml";
+  let type = test.type || "application/opensearchdescription+xml";
+  let title = test.title;
+  if (!("pass" in test)) {
+    test.pass = true;
+  }
+
+  let head = browser.contentDocument.getElementById("linkparent");
+  let link = browser.contentDocument.createElement("link");
+  link.rel = rel;
+  link.href = href;
+  link.type = type;
+  link.title = title;
+  head.appendChild(link);
+}
+
+let feedDiscoveryTests = [
+  { text: "rel feed discovered" },
+  { rel: "ALTERNATE", text: "rel is case insensitive" },
+  { rel: "-alternate-", pass: false, text: "rel -alternate- not discovered" },
+  { rel: "foo bar baz alternate quux", text: "rel may contain additional rels separated by spaces" },
+  { href: "https://not.mozilla.com", text: "HTTPS ok" },
+  { href: "ftp://not.mozilla.com", text: "FTP ok" },
+  { href: "data:text/foo,foo", pass: false, text: "data URI not permitted" },
+  { href: "javascript:alert(0)", pass: false, text: "JS URI not permitted" },
+  { type: "application/rss+xml", text: "type can be RSS" },
+  { type: "aPPliCAtion/RSS+xml", text: "type is case insensitve" },
+  { type: " application/atom+xml ", text: "type may contain extra whitespace" },
+  { type: "application/atom+xml; charset=utf-8", text: "type may have optional parameters (RFC2046)" },
+  { type: "aapplication/atom+xml", pass: false, text: "type should not be loosely matched" },
+  { rel: "alternate alternate alternate", count: 1, text: "only one feed should be added" }
+];
+
+function execute_feed_test(test) {
+  if (browser.feeds) {
+    let matchCount = (!("count" in test) || browser.feeds.length === test.count);
+    let matchTitle = (test.title == browser.feeds[0].title);
+    ok(matchCount && matchTitle, test.text);
+    browser.feeds = null;
+  } else {
+    ok(!test.pass, test.text);
+  }
+  run_next_test();
+}
+
+function prep_feed_test(test) {
+  setHandlerFunc(execute_feed_test, test);
+
+  let rel = test.rel || "alternate";
+  let href = test.href || "http://so.not.here.mozilla.com/feed.xml";
+  let type = test.type || "application/atom+xml";
+  let title = test.title;
+  if (!("pass" in test)) {
+    test.pass = true;
+  }
+
+  let head = browser.contentDocument.getElementById("linkparent");
+  let link = browser.contentDocument.createElement("link");
+  link.rel = rel;
+  link.href = href;
+  link.type = type;
+  link.title = title;
+  head.appendChild(link);
+}
+
+let searchTest;
+while ((searchTest = searchDiscoveryTests.shift())) {
+  let title = searchTest.title || searchDiscoveryTests.length;
+  searchTest.title = title;
+  add_test(prep_search_test.bind(this, searchTest));
+}
+
+let feedTest;
+while ((feedTest = feedDiscoveryTests.shift())) {
+  let title = feedTest.title || feedDiscoveryTests.length;
+  feedTest.title = title;
+  add_test(prep_feed_test.bind(this, feedTest));
+}
+
+run_next_test();