Commit c622f14e authored by Evelyn Hung's avatar Evelyn Hung
Browse files

Bug 1355451 - Speculative connect to websites on mousedown on awesomebar item. r=mak

We preconnect to an explicit URL or the url in moz-action:remotetab on
the popup.

MozReview-Commit-ID: Itgie3BMehn

--HG--
extra : rebase_source : de70c39a166ad5aec4b64efd469e4a10f7297ac3
extra : source : ec99e3ba4e7a9ccb960937ac1eb39947a1060cc6
parent 8b042b8f
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -115,6 +115,7 @@ support-files =
support-files =
  searchSuggestionEngine2.xml
  searchSuggestionEngine.sjs
[browser_urlbar_search_speculative_connect_mousedown.js]
[browser_urlbar_stop_pending.js]
support-files =
  slow-page.sjs
+6 −14
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@
// This test ensures that we setup a speculative network
// connection for autoFilled values.

let {HttpServer} = Cu.import("resource://testing-common/httpd.js", {});
let gHttpServer = null;
let gScheme = "http";
let gHost = "localhost"; // 'localhost' by default.
@@ -16,21 +15,13 @@ let gPrivateWin = null;
let gIsSpeculativeConnected = false;

add_task(async function setup() {
  if (!gHttpServer) {
    gHttpServer = new HttpServer();
    try {
      gHttpServer.start(gPort);
  gHttpServer = runHttpServer(gScheme, gHost);
  // The server will be run on a random port if the port number wasn't given.
  gPort = gHttpServer.identity.primaryPort;
      gHttpServer.identity.setPrimary(gScheme, gHost, gPort);
    } catch (ex) {
      info("We can't launch our http server successfully.")
    }
  }
  is(gHttpServer.identity.has(gScheme, gHost, gPort), true, "make sure we have this domain listed");

  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.autoFill", true],
          // Turn off speculative connect to the search engine.
          // Turn off search suggestion so we won't speculative connect to the search engine.
          ["browser.search.suggest.enabled", false],
          ["browser.urlbar.speculativeConnect.enabled", true],
          // In mochitest this number is 0 by default but we have to turn it on.
@@ -50,7 +41,7 @@ add_task(async function setup() {
  is(PrivateBrowsingUtils.isWindowPrivate(gPrivateWin), true, "A private window created.");

  // Bug 764062 - we can't get port number from autocomplete result, so we have to mock
  // this function to add it manually.
  // this function and add it manually.
  let oldSpeculativeConnect = gURLBar.popup.maybeSetupSpeculativeConnect.bind(gURLBar.popup);
  let newSpeculativeConnect = (uriString) => {
    gIsSpeculativeConnected = true;
@@ -88,6 +79,7 @@ add_task(async function autofill_tests() {
     `Autofilled value is as expected for search '${test.search}'`);
  is(gIsSpeculativeConnected, true, "Speculative connection should be called");
  await promiseSpeculativeConnection(gHttpServer);
  is(gHttpServer.connectionNumber, 1, `${gHttpServer.connectionNumber} speculative connection has been setup.`);
});

add_task(async function privateContext_test() {
+3 −13
Original line number Diff line number Diff line
@@ -7,7 +7,6 @@
// This test ensures that we setup a speculative network connection to
// current search engine if the first result is 'searchengine'.

let {HttpServer} = Cu.import("resource://testing-common/httpd.js", {});
let gHttpServer = null;
let gScheme = "http";
let gHost = "localhost"; // 'localhost' by default.
@@ -15,21 +14,11 @@ let gPort = 20709; // the port number must be identical to what we said in searc
const TEST_ENGINE_BASENAME = "searchSuggestionEngine2.xml";

add_task(async function setup() {
  if (!gHttpServer) {
    gHttpServer = new HttpServer();
    try {
      gHttpServer.start(gPort);
      gPort = gHttpServer.identity.primaryPort;
      gHttpServer.identity.setPrimary(gScheme, gHost, gPort);
    } catch (ex) {
      info("We can't launch our http server successfully.")
    }
  }
  is(gHttpServer.identity.has(gScheme, gHost, gPort), true, "make sure we have this domain listed");
  gHttpServer = runHttpServer(gScheme, gHost, gPort);

  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.autoFill", true],
          // Turn off speculative connect to the search engine.
          // Make sure search suggestion for location bar is enabled
          ["browser.search.suggest.enabled", true],
          ["browser.urlbar.suggest.searches", true],
          ["browser.urlbar.speculativeConnect.enabled", true],
@@ -62,5 +51,6 @@ add_task(async function autofill_tests() {
  let style = controller.getStyleAt(0);
  is(style.includes("searchengine"), true, "The first result type is searchengine");
  await promiseSpeculativeConnection(gHttpServer);
  is(gHttpServer.connectionNumber, 1, `${gHttpServer.connectionNumber} speculative connection has been setup.`);
});
+68 −0
Original line number Diff line number Diff line
/* 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";

// This test ensures that we setup a speculative network connection to
// the site in mousedown event before the http request happens(in mouseup).

let gHttpServer = null;
let gScheme = "http";
let gHost = "localhost"; // 'localhost' by default.
let gPort = -1;
let gIsSpeculativeConnected = false;

add_task(async function setup() {
  gHttpServer = runHttpServer(gScheme, gHost, gPort);
  // The server will be run on a random port if the port number wasn't given.
  gPort = gHttpServer.identity.primaryPort;

  await PlacesTestUtils.addVisits([{
    uri: `${gScheme}://${gHost}:${gPort}`,
    title: "test visit for speculative connection",
    transition: Ci.nsINavHistoryService.TRANSITION_TYPED,
  }]);

  await SpecialPowers.pushPrefEnv({
    set: [["browser.urlbar.autoFill", true],
          // Turn off search suggestion so we won't speculative connect to the search engine.
          ["browser.search.suggest.enabled", false],
          ["browser.urlbar.speculativeConnect.enabled", true],
          // In mochitest this number is 0 by default but we have to turn it on.
          ["network.http.speculative-parallel-limit", 6],
          // The http server is using IPv4, so it's better to disable IPv6 to avoid weird
          // networking problem.
          ["network.dns.disableIPv6", true]],
  });

  registerCleanupFunction(async function() {
    await PlacesUtils.history.clear();
    gHttpServer.identity.remove(gScheme, gHost, gPort);
    gHttpServer.stop(() => {
      gHttpServer = null;
    });
  });
});

add_task(async function autofill_tests() {
  const test = {
    // To not trigger autofill, search keyword starts from the second character.
    search: gHost.substr(1, 4),
    completeValue: `${gScheme}://${gHost}:${gPort}/`
  };
  info(`Searching for '${test.search}'`);
  await promiseAutocompleteResultPopup(test.search, window, true);
  // Check if the first result is with type "searchengine"
  let controller = gURLBar.popup.input.controller;
  // The first item should be 'Search with ...' thus we wan the second.
  let value = controller.getFinalCompleteValueAt(1);
  info(`The value of the second item is ${value}`);
  is(value, test.completeValue, "The second item has the url we visited.");

  let listitem = gURLBar.popup.richlistbox.childNodes[1];
  EventUtils.synthesizeMouse(listitem, 10, 10, {type: "mousedown"}, window);
  is(gURLBar.popup.richlistbox.selectedIndex, 1, "The second item is selected");
  await promiseSpeculativeConnection(gHttpServer);
  is(gHttpServer.connectionNumber, 1, `${gHttpServer.connectionNumber} speculative connection has been setup.`);
});
+15 −2
Original line number Diff line number Diff line
@@ -8,6 +8,8 @@ XPCOMUtils.defineLazyModuleGetter(this, "PlacesTestUtils",
  "resource://testing-common/PlacesTestUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Preferences",
  "resource://gre/modules/Preferences.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "HttpServer",
  "resource://testing-common/httpd.js");

/**
 * Waits for the next top-level document load in the current browser.  The URI
@@ -129,6 +131,19 @@ function is_element_hidden(element, msg) {
  ok(is_hidden(element), msg || "Element should be hidden");
}

function runHttpServer(scheme, host, port = -1) {
  let httpserver = new HttpServer();
  try {
    httpserver.start(port);
    port = httpserver.identity.primaryPort;
    httpserver.identity.setPrimary(scheme, host, port);
  } catch (ex) {
    info("We can't launch our http server successfully.")
  }
  is(httpserver.identity.has(scheme, host, port), true, `${scheme}://${host}:${port} is listening.`);
  return httpserver;
}

function promisePopupEvent(popup, eventSuffix) {
  let endState = {shown: "open", hidden: "closed"}[eventSuffix];

@@ -255,8 +270,6 @@ function promisePageActionViewShown() {
function promiseSpeculativeConnection(httpserver) {
  return BrowserTestUtils.waitForCondition(() => {
    if (httpserver) {
      is(httpserver.connectionNumber, 1,
         `${httpserver.connectionNumber} speculative connection has been setup.`)
      return httpserver.connectionNumber == 1;
    }
    return false;
Loading