browser/components/search/test/browser_bing_behavior.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/search/test/browser_bing_behavior.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,197 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +/*
     1.8 + * Test Bing search plugin URLs
     1.9 + */
    1.10 +
    1.11 +"use strict";
    1.12 +
    1.13 +const BROWSER_SEARCH_PREF = "browser.search.";
    1.14 +
    1.15 +let runtime = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime);
    1.16 +// Custom search parameters
    1.17 +const PC_PARAM_VALUE = runtime.isOfficialBranding ? "MOZI" : null;
    1.18 +
    1.19 +function test() {
    1.20 +  let engine = Services.search.getEngineByName("Bing");
    1.21 +  ok(engine, "Bing is installed");
    1.22 +
    1.23 +  let previouslySelectedEngine = Services.search.currentEngine;
    1.24 +  Services.search.currentEngine = engine;
    1.25 +
    1.26 +  let base = "http://www.bing.com/search?q=foo";
    1.27 +  if (typeof(PC_PARAM_VALUE) == "string")
    1.28 +    base += "&pc=" + PC_PARAM_VALUE;
    1.29 +
    1.30 +  let url;
    1.31 +
    1.32 +  // Test search URLs (including purposes).
    1.33 +  url = engine.getSubmission("foo").uri.spec;
    1.34 +  is(url, base, "Check search URL for 'foo'");
    1.35 +
    1.36 +  waitForExplicitFinish();
    1.37 +
    1.38 +  var gCurrTest;
    1.39 +  var gTests = [
    1.40 +    {
    1.41 +      name: "context menu search",
    1.42 +      searchURL: base + "&form=MOZCON",
    1.43 +      run: function () {
    1.44 +        // Simulate a contextmenu search
    1.45 +        // FIXME: This is a bit "low-level"...
    1.46 +        BrowserSearch.loadSearch("foo", false, "contextmenu");
    1.47 +      }
    1.48 +    },
    1.49 +    {
    1.50 +      name: "keyword search",
    1.51 +      searchURL: base + "&form=MOZLBR",
    1.52 +      run: function () {
    1.53 +        gURLBar.value = "? foo";
    1.54 +        gURLBar.focus();
    1.55 +        EventUtils.synthesizeKey("VK_RETURN", {});
    1.56 +      }
    1.57 +    },
    1.58 +    {
    1.59 +      name: "search bar search",
    1.60 +      searchURL: base + "&form=MOZSBR",
    1.61 +      run: function () {
    1.62 +        let sb = BrowserSearch.searchBar;
    1.63 +        sb.focus();
    1.64 +        sb.value = "foo";
    1.65 +        registerCleanupFunction(function () {
    1.66 +          sb.value = "";
    1.67 +        });
    1.68 +        EventUtils.synthesizeKey("VK_RETURN", {});
    1.69 +      }
    1.70 +    },
    1.71 +    {
    1.72 +      name: "new tab search",
    1.73 +      searchURL: base + "&form=MOZTSB",
    1.74 +      run: function () {
    1.75 +        function doSearch(doc) {
    1.76 +          // Re-add the listener, and perform a search
    1.77 +          gBrowser.addProgressListener(listener);
    1.78 +          doc.getElementById("newtab-search-text").value = "foo";
    1.79 +          doc.getElementById("newtab-search-submit").click();
    1.80 +        }
    1.81 +
    1.82 +        // load about:newtab, but remove the listener first so it doesn't
    1.83 +        // get in the way
    1.84 +        gBrowser.removeProgressListener(listener);
    1.85 +        gBrowser.loadURI("about:newtab");
    1.86 +        info("Waiting for about:newtab load");
    1.87 +        tab.linkedBrowser.addEventListener("load", function load(event) {
    1.88 +          if (event.originalTarget != tab.linkedBrowser.contentDocument ||
    1.89 +              event.target.location.href == "about:blank") {
    1.90 +            info("skipping spurious load event");
    1.91 +            return;
    1.92 +          }
    1.93 +          tab.linkedBrowser.removeEventListener("load", load, true);
    1.94 +
    1.95 +          // Observe page setup
    1.96 +          let win = gBrowser.contentWindow;
    1.97 +          if (win.gSearch.currentEngineName ==
    1.98 +              Services.search.currentEngine.name) {
    1.99 +            doSearch(win.document);
   1.100 +          }
   1.101 +          else {
   1.102 +            info("Waiting for newtab search init");
   1.103 +            win.addEventListener("ContentSearchService", function done(event) {
   1.104 +              info("Got newtab search event " + event.detail.type);
   1.105 +              if (event.detail.type == "State") {
   1.106 +                win.removeEventListener("ContentSearchService", done);
   1.107 +                // Let gSearch respond to the event before continuing.
   1.108 +                executeSoon(() => doSearch(win.document));
   1.109 +              }
   1.110 +            });
   1.111 +          }
   1.112 +        }, true);
   1.113 +      }
   1.114 +    },
   1.115 +    {
   1.116 +      name: "home page search",
   1.117 +      searchURL: base + "&form=MOZSPG",
   1.118 +      run: function () {
   1.119 +        // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB)
   1.120 +        ignoreAllUncaughtExceptions(true);
   1.121 +
   1.122 +        // load about:home, but remove the listener first so it doesn't
   1.123 +        // get in the way
   1.124 +        gBrowser.removeProgressListener(listener);
   1.125 +        gBrowser.loadURI("about:home");
   1.126 +        info("Waiting for about:home load");
   1.127 +        tab.linkedBrowser.addEventListener("load", function load(event) {
   1.128 +          if (event.originalTarget != tab.linkedBrowser.contentDocument ||
   1.129 +              event.target.location.href == "about:blank") {
   1.130 +            info("skipping spurious load event");
   1.131 +            return;
   1.132 +          }
   1.133 +          tab.linkedBrowser.removeEventListener("load", load, true);
   1.134 +
   1.135 +          // Observe page setup
   1.136 +          let doc = gBrowser.contentDocument;
   1.137 +          let mutationObserver = new MutationObserver(function (mutations) {
   1.138 +            for (let mutation of mutations) {
   1.139 +              if (mutation.attributeName == "searchEngineName") {
   1.140 +                // Re-add the listener, and perform a search
   1.141 +                gBrowser.addProgressListener(listener);
   1.142 +                doc.getElementById("searchText").value = "foo";
   1.143 +                doc.getElementById("searchSubmit").click();
   1.144 +              }
   1.145 +            }
   1.146 +          });
   1.147 +          mutationObserver.observe(doc.documentElement, { attributes: true });
   1.148 +        }, true);
   1.149 +      }
   1.150 +    }
   1.151 +  ];
   1.152 +
   1.153 +  function nextTest() {
   1.154 +    // Make sure we listen again for uncaught exceptions in the next test or cleanup.
   1.155 +    ignoreAllUncaughtExceptions(false);
   1.156 +
   1.157 +    if (gTests.length) {
   1.158 +      gCurrTest = gTests.shift();
   1.159 +      info("Running : " + gCurrTest.name);
   1.160 +      executeSoon(gCurrTest.run);
   1.161 +    } else {
   1.162 +      finish();
   1.163 +    }
   1.164 +  }
   1.165 +
   1.166 +  let tab = gBrowser.selectedTab = gBrowser.addTab();
   1.167 +
   1.168 +  let listener = {
   1.169 +    onStateChange: function onStateChange(webProgress, req, flags, status) {
   1.170 +      info("onStateChange");
   1.171 +      // Only care about top-level document starts
   1.172 +      let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT |
   1.173 +                     Ci.nsIWebProgressListener.STATE_START;
   1.174 +      if (!(flags & docStart) || !webProgress.isTopLevel)
   1.175 +        return;
   1.176 +
   1.177 +      info("received document start");
   1.178 +
   1.179 +      ok(req instanceof Ci.nsIChannel, "req is a channel");
   1.180 +      is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded");
   1.181 +      info("Actual URI: " + req.URI.spec);
   1.182 +
   1.183 +      req.cancel(Components.results.NS_ERROR_FAILURE);
   1.184 +
   1.185 +      executeSoon(nextTest);
   1.186 +    }
   1.187 +  }
   1.188 +
   1.189 +  registerCleanupFunction(function () {
   1.190 +    gBrowser.removeProgressListener(listener);
   1.191 +    gBrowser.removeTab(tab);
   1.192 +    Services.search.currentEngine = previouslySelectedEngine;
   1.193 +  });
   1.194 +
   1.195 +  tab.linkedBrowser.addEventListener("load", function load() {
   1.196 +    tab.linkedBrowser.removeEventListener("load", load, true);
   1.197 +    gBrowser.addProgressListener(listener);
   1.198 +    nextTest();
   1.199 +  }, true);
   1.200 +}

mercurial