browser/components/search/test/browser_bing_behavior.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 /* Any copyright is dedicated to the Public Domain.
     2  * http://creativecommons.org/publicdomain/zero/1.0/ */
     4 /*
     5  * Test Bing search plugin URLs
     6  */
     8 "use strict";
    10 const BROWSER_SEARCH_PREF = "browser.search.";
    12 let runtime = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime);
    13 // Custom search parameters
    14 const PC_PARAM_VALUE = runtime.isOfficialBranding ? "MOZI" : null;
    16 function test() {
    17   let engine = Services.search.getEngineByName("Bing");
    18   ok(engine, "Bing is installed");
    20   let previouslySelectedEngine = Services.search.currentEngine;
    21   Services.search.currentEngine = engine;
    23   let base = "http://www.bing.com/search?q=foo";
    24   if (typeof(PC_PARAM_VALUE) == "string")
    25     base += "&pc=" + PC_PARAM_VALUE;
    27   let url;
    29   // Test search URLs (including purposes).
    30   url = engine.getSubmission("foo").uri.spec;
    31   is(url, base, "Check search URL for 'foo'");
    33   waitForExplicitFinish();
    35   var gCurrTest;
    36   var gTests = [
    37     {
    38       name: "context menu search",
    39       searchURL: base + "&form=MOZCON",
    40       run: function () {
    41         // Simulate a contextmenu search
    42         // FIXME: This is a bit "low-level"...
    43         BrowserSearch.loadSearch("foo", false, "contextmenu");
    44       }
    45     },
    46     {
    47       name: "keyword search",
    48       searchURL: base + "&form=MOZLBR",
    49       run: function () {
    50         gURLBar.value = "? foo";
    51         gURLBar.focus();
    52         EventUtils.synthesizeKey("VK_RETURN", {});
    53       }
    54     },
    55     {
    56       name: "search bar search",
    57       searchURL: base + "&form=MOZSBR",
    58       run: function () {
    59         let sb = BrowserSearch.searchBar;
    60         sb.focus();
    61         sb.value = "foo";
    62         registerCleanupFunction(function () {
    63           sb.value = "";
    64         });
    65         EventUtils.synthesizeKey("VK_RETURN", {});
    66       }
    67     },
    68     {
    69       name: "new tab search",
    70       searchURL: base + "&form=MOZTSB",
    71       run: function () {
    72         function doSearch(doc) {
    73           // Re-add the listener, and perform a search
    74           gBrowser.addProgressListener(listener);
    75           doc.getElementById("newtab-search-text").value = "foo";
    76           doc.getElementById("newtab-search-submit").click();
    77         }
    79         // load about:newtab, but remove the listener first so it doesn't
    80         // get in the way
    81         gBrowser.removeProgressListener(listener);
    82         gBrowser.loadURI("about:newtab");
    83         info("Waiting for about:newtab load");
    84         tab.linkedBrowser.addEventListener("load", function load(event) {
    85           if (event.originalTarget != tab.linkedBrowser.contentDocument ||
    86               event.target.location.href == "about:blank") {
    87             info("skipping spurious load event");
    88             return;
    89           }
    90           tab.linkedBrowser.removeEventListener("load", load, true);
    92           // Observe page setup
    93           let win = gBrowser.contentWindow;
    94           if (win.gSearch.currentEngineName ==
    95               Services.search.currentEngine.name) {
    96             doSearch(win.document);
    97           }
    98           else {
    99             info("Waiting for newtab search init");
   100             win.addEventListener("ContentSearchService", function done(event) {
   101               info("Got newtab search event " + event.detail.type);
   102               if (event.detail.type == "State") {
   103                 win.removeEventListener("ContentSearchService", done);
   104                 // Let gSearch respond to the event before continuing.
   105                 executeSoon(() => doSearch(win.document));
   106               }
   107             });
   108           }
   109         }, true);
   110       }
   111     },
   112     {
   113       name: "home page search",
   114       searchURL: base + "&form=MOZSPG",
   115       run: function () {
   116         // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB)
   117         ignoreAllUncaughtExceptions(true);
   119         // load about:home, but remove the listener first so it doesn't
   120         // get in the way
   121         gBrowser.removeProgressListener(listener);
   122         gBrowser.loadURI("about:home");
   123         info("Waiting for about:home load");
   124         tab.linkedBrowser.addEventListener("load", function load(event) {
   125           if (event.originalTarget != tab.linkedBrowser.contentDocument ||
   126               event.target.location.href == "about:blank") {
   127             info("skipping spurious load event");
   128             return;
   129           }
   130           tab.linkedBrowser.removeEventListener("load", load, true);
   132           // Observe page setup
   133           let doc = gBrowser.contentDocument;
   134           let mutationObserver = new MutationObserver(function (mutations) {
   135             for (let mutation of mutations) {
   136               if (mutation.attributeName == "searchEngineName") {
   137                 // Re-add the listener, and perform a search
   138                 gBrowser.addProgressListener(listener);
   139                 doc.getElementById("searchText").value = "foo";
   140                 doc.getElementById("searchSubmit").click();
   141               }
   142             }
   143           });
   144           mutationObserver.observe(doc.documentElement, { attributes: true });
   145         }, true);
   146       }
   147     }
   148   ];
   150   function nextTest() {
   151     // Make sure we listen again for uncaught exceptions in the next test or cleanup.
   152     ignoreAllUncaughtExceptions(false);
   154     if (gTests.length) {
   155       gCurrTest = gTests.shift();
   156       info("Running : " + gCurrTest.name);
   157       executeSoon(gCurrTest.run);
   158     } else {
   159       finish();
   160     }
   161   }
   163   let tab = gBrowser.selectedTab = gBrowser.addTab();
   165   let listener = {
   166     onStateChange: function onStateChange(webProgress, req, flags, status) {
   167       info("onStateChange");
   168       // Only care about top-level document starts
   169       let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT |
   170                      Ci.nsIWebProgressListener.STATE_START;
   171       if (!(flags & docStart) || !webProgress.isTopLevel)
   172         return;
   174       info("received document start");
   176       ok(req instanceof Ci.nsIChannel, "req is a channel");
   177       is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded");
   178       info("Actual URI: " + req.URI.spec);
   180       req.cancel(Components.results.NS_ERROR_FAILURE);
   182       executeSoon(nextTest);
   183     }
   184   }
   186   registerCleanupFunction(function () {
   187     gBrowser.removeProgressListener(listener);
   188     gBrowser.removeTab(tab);
   189     Services.search.currentEngine = previouslySelectedEngine;
   190   });
   192   tab.linkedBrowser.addEventListener("load", function load() {
   193     tab.linkedBrowser.removeEventListener("load", load, true);
   194     gBrowser.addProgressListener(listener);
   195     nextTest();
   196   }, true);
   197 }

mercurial