Bug 1480888 - Implement blocking system for special search shortcut top sites r=k88hudson
authorUrsula Sarracini <usarracini@mozilla.com>
Fri, 10 Aug 2018 19:54:24 +0000
changeset 486154 e623335f541313cd59ced846c6cecaeaca7f2c85
parent 486153 68dbca6faa6e5d21684d73048cf7beb579acb7b5
child 486155 1a93db801952311861e6f4817bff9113fe60f531
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersk88hudson
bugs1480888
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1480888 - Implement blocking system for special search shortcut top sites r=k88hudson Differential Revision: https://phabricator.services.mozilla.com/D3035
toolkit/components/extensions/test/xpcshell/test_ext_topSites.js
toolkit/components/thumbnails/test/browser_thumbnails_bg_topsites.js
toolkit/modules/NewTabUtils.jsm
toolkit/modules/tests/xpcshell/head.js
toolkit/modules/tests/xpcshell/test_NewTabUtils.js
toolkit/modules/tests/xpcshell/xpcshell.ini
--- a/toolkit/components/extensions/test/xpcshell/test_ext_topSites.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_topSites.js
@@ -1,14 +1,17 @@
 "use strict";
 
 ChromeUtils.import("resource://gre/modules/PlacesUtils.jsm");
 ChromeUtils.import("resource://gre/modules/NewTabUtils.jsm");
 ChromeUtils.import("resource://testing-common/PlacesTestUtils.jsm");
 
+// Disable top site search shortcuts for this test
+Services.prefs.setBoolPref("browser.newtabpage.activity-stream.improvesearch.topSiteSearchShortcuts", false);
+
 // A small 1x1 test png
 const IMAGE_1x1 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg==";
 
 add_task(async function test_topSites() {
   let visits = [];
   const numVisits = 15; // To make sure we get frecency.
   let visitDate = new Date(1999, 9, 9, 9, 9).getTime();
 
--- a/toolkit/components/thumbnails/test/browser_thumbnails_bg_topsites.js
+++ b/toolkit/components/thumbnails/test/browser_thumbnails_bg_topsites.js
@@ -1,16 +1,19 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
 const image1x1 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg==";
 const image96x96 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAAAJCAYAAADNYymqAAAAKklEQVR42u3RMQEAAAjDMFCO9CGDg1RC00lN6awGAACADQAACAAAAXjXApPGFm+IdJG9AAAAAElFTkSuQmCC";
 const baseURL = "http://mozilla${i}.com/";
 
 function* runTests() {
+  yield SpecialPowers.pushPrefEnv({
+    set: [["browser.newtabpage.activity-stream.improvesearch.topSiteSearchShortcuts", false]]
+  });
   // Add 3 top sites - 2 visits each so it can pass frecency threshold of the top sites query
   for (let i = 1; i <= 3; i++) {
     yield PlacesTestUtils.addVisits(baseURL.replace("${i}", i));
     yield PlacesTestUtils.addVisits(baseURL.replace("${i}", i));
   }
 
   // Add favicon data for 2 of the top sites
   let faviconData = new Map();
--- a/toolkit/modules/NewTabUtils.jsm
+++ b/toolkit/modules/NewTabUtils.jsm
@@ -5,16 +5,28 @@
 "use strict";
 
 var EXPORTED_SYMBOLS = ["NewTabUtils"];
 
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.importGlobalProperties(["btoa", "URL"]);
 
+// Android tests don't import these properly, so guard against that
+let shortURL = {};
+let searchShortcuts = {};
+let didSuccessfulImport = false;
+try {
+  ChromeUtils.import("resource://activity-stream/lib/ShortURL.jsm", shortURL);
+  ChromeUtils.import("resource://activity-stream/lib/SearchShortcuts.jsm", searchShortcuts);
+  didSuccessfulImport = true;
+} catch (e) {
+  // The test failed to import these files
+}
+
 ChromeUtils.defineModuleGetter(this, "PlacesUtils",
   "resource://gre/modules/PlacesUtils.jsm");
 
 ChromeUtils.defineModuleGetter(this, "PageThumbs",
   "resource://gre/modules/PageThumbs.jsm");
 
 ChromeUtils.defineModuleGetter(this, "BinarySearch",
   "resource://gre/modules/BinarySearch.jsm");
@@ -1165,16 +1177,26 @@ var ActivityStreamProvider = {
         if (isOtherBetter(link, other)) {
           link = other;
         }
         combiner(link, other);
       }
       map.set(host, link);
     }
 
+    // Convert all links that are supposed to be a seach shortcut to its canonical URL
+    if (didSuccessfulImport && Services.prefs.getBoolPref(`browser.newtabpage.activity-stream.${searchShortcuts.SEARCH_SHORTCUTS_EXPERIMENT}`)) {
+      links.forEach(link => {
+        let searchProvider = searchShortcuts.getSearchProvider(shortURL.shortURL(link));
+        if (searchProvider) {
+          link.url = searchProvider.url;
+        }
+      });
+    }
+
     // Remove any blocked links.
     if (!options.ignoreBlocked) {
       links = links.filter(link => !BlockedLinks.isBlocked(link));
     }
 
     if (options.onePerDomain) {
       // De-dup the links.
       const exactHosts = new Map();
--- a/toolkit/modules/tests/xpcshell/head.js
+++ b/toolkit/modules/tests/xpcshell/head.js
@@ -5,22 +5,24 @@ Cu.importGlobalProperties(["btoa"]);
 
 ChromeUtils.defineModuleGetter(this, "PlacesTestUtils",
   "resource://testing-common/PlacesTestUtils.jsm");
 
 ChromeUtils.defineModuleGetter(this, "PlacesUtils",
   "resource://gre/modules/PlacesUtils.jsm");
 
 const PREF_NEWTAB_ENHANCED = "browser.newtabpage.enhanced";
+const SEARCH_SHORTCUTS_EXPERIMENT_PREF = "browser.newtabpage.activity-stream.improvesearch.topSiteSearchShortcuts";
 
 // use time at the start of the tests, chnaging it inside timeDaysAgo()
 // may cause tiny time differences, which break expected sql ordering
 const TIME_NOW = (new Date()).getTime();
 
 Services.prefs.setBoolPref(PREF_NEWTAB_ENHANCED, true);
+Services.prefs.setBoolPref(SEARCH_SHORTCUTS_EXPERIMENT_PREF, false);
 
 do_get_profile();
 
 // utility function to compute past timestamp in microseconds
 function timeDaysAgo(numDays) {
   return (TIME_NOW - (numDays * 24 * 60 * 60 * 1000)) * 1000;
 }
 
--- a/toolkit/modules/tests/xpcshell/test_NewTabUtils.js
+++ b/toolkit/modules/tests/xpcshell/test_NewTabUtils.js
@@ -674,16 +674,32 @@ add_task(async function getTopFrecentSit
   links = await provider.getTopSites();
   Assert.equal(links.length, 0, "adding a single visit doesn't exceed default threshold");
 
   links = await provider.getTopSites({topsiteFrecency: 100});
   Assert.equal(links.length, 1, "adding a visit yields a link");
   Assert.equal(links[0].url, testURI, "added visit corresponds to added url");
 });
 
+add_task(async function getTopFrecentSites_improveSearch() {
+  await setUpActivityStreamTest();
+  const SEARCH_SHORTCUTS_EXPERIMENT_PREF = "browser.newtabpage.activity-stream.improvesearch.topSiteSearchShortcuts";
+  Services.prefs.setBoolPref(SEARCH_SHORTCUTS_EXPERIMENT_PREF, true);
+
+  let testURI = "https://www.amazon.com?search=tv";
+  await PlacesTestUtils.addVisits(testURI);
+
+  let provider = NewTabUtils.activityStreamLinks;
+  let links = await provider.getTopSites({topsiteFrecency: 100});
+  Assert.equal(links.length, 1, "sanity check that we got the link from top sites");
+  Assert.equal(links[0].url, "https://amazon.com", "the amazon site was converted to generic search shortcut site");
+
+  Services.prefs.setBoolPref(SEARCH_SHORTCUTS_EXPERIMENT_PREF, false);
+});
+
 add_task(async function getTopFrecentSites_no_dedup() {
   await setUpActivityStreamTest();
 
   let provider = NewTabUtils.activityStreamLinks;
   let links = await provider.getTopSites({topsiteFrecency: 100});
   Assert.equal(links.length, 0, "empty history yields empty links");
 
   // Add a visits in reverse order they will be returned in when not deduped.
--- a/toolkit/modules/tests/xpcshell/xpcshell.ini
+++ b/toolkit/modules/tests/xpcshell/xpcshell.ini
@@ -1,10 +1,11 @@
 [DEFAULT]
 head = head.js
+firefox-appdir = browser
 support-files =
   propertyLists/bug710259_propertyListBinary.plist
   propertyLists/bug710259_propertyListXML.plist
   chromeappsstore.sqlite
   corrupt.sqlite
   zips/zen.zip
 
 [test_BinarySearch.js]