Bug 1505411 - Add a basic test to check telemetry results for visits to SERP urls. r=adw
authorMark Banner <standard8@mozilla.com>
Wed, 05 Dec 2018 20:31:24 +0000
changeset 508748 78fddbcccf74adb9c820f4c8792bfcfa4d14618f
parent 508747 a10d682e28b207f41e02f5e59aaf67640458da4b
child 508749 5b5a072c0628fb13fa11c1bfc1c69491c34186f7
push id1905
push userffxbld-merge
push dateMon, 21 Jan 2019 12:33:13 +0000
treeherdermozilla-release@c2fca1944d8c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1505411
milestone65.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 1505411 - Add a basic test to check telemetry results for visits to SERP urls. r=adw Also simplify regexps as we don't no longer need to return match details. Differential Revision: https://phabricator.services.mozilla.com/D13239
browser/components/search/SearchTelemetry.jsm
browser/components/search/test/browser/browser.ini
browser/components/search/test/browser/browser_searchTelemetry.js
browser/components/search/test/browser/searchTelemetry.html
--- a/browser/components/search/SearchTelemetry.jsm
+++ b/browser/components/search/SearchTelemetry.jsm
@@ -12,41 +12,41 @@ XPCOMUtils.defineLazyModuleGetters(this,
   Services: "resource://gre/modules/Services.jsm",
 });
 
 const SEARCH_COUNTS_HISTOGRAM_KEY = "SEARCH_COUNTS";
 
 // Used to identify various parameters (query, code, etc.) in search URLS.
 const SEARCH_PROVIDER_INFO = {
   "google": {
-    "regexp": /^https:\/\/www\.(google)\.(?:.+)\/search/,
+    "regexp": /^https:\/\/www\.google\.(?:.+)\/search/,
     "queryParam": "q",
     "codeParam": "client",
     "codePrefixes": ["firefox"],
     "followonParams": ["oq", "ved", "ei"],
   },
   "duckduckgo": {
-    "regexp": /^https:\/\/(duckduckgo)\.com\//,
+    "regexp": /^https:\/\/duckduckgo\.com\//,
     "queryParam": "q",
     "codeParam": "t",
     "codePrefixes": ["ff"],
   },
   "yahoo": {
-    "regexp": /^https:\/\/(?:.*)search\.(yahoo)\.com\/search/,
+    "regexp": /^https:\/\/(?:.*)search\.yahoo\.com\/search/,
     "queryParam": "p",
   },
   "baidu": {
-    "regexp": /^https:\/\/www\.(baidu)\.com\/(?:s|baidu)/,
+    "regexp": /^https:\/\/www\.baidu\.com\/(?:s|baidu)/,
     "queryParam": "wd",
     "codeParam": "tn",
     "codePrefixes": ["monline_dg"],
     "followonParams": ["oq"],
   },
   "bing": {
-    "regexp": /^https:\/\/www\.(bing)\.com\/search/,
+    "regexp": /^https:\/\/www\.bing\.com\/search/,
     "queryParam": "q",
     "codeParam": "pc",
     "codePrefixes": ["MOZ", "MZ"],
   },
 };
 
 const BROWSER_SEARCH_PREF = "browser.search.";
 
--- a/browser/components/search/test/browser/browser.ini
+++ b/browser/components/search/test/browser/browser.ini
@@ -41,10 +41,13 @@ skip-if = os == "mac" #1421238
 [browser_aboutSearchReset.js]
 disabled = bug 1488946 - Telemetry probe needs extension
 [browser_searchbar_openpopup.js]
 skip-if = os == "linux" # Linux has different focus behaviours.
 [browser_searchbar_keyboard_navigation.js]
 [browser_searchbar_smallpanel_keyboard_navigation.js]
 [browser_searchEngine_behaviors.js]
 skip-if = artifact # bug 1315953
+[browser_searchTelemetry.js]
+support-files =
+  searchTelemetry.html
 [browser_webapi.js]
 [browser_tooManyEnginesOffered.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/search/test/browser/browser_searchTelemetry.js
@@ -0,0 +1,89 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+/*
+ * Main tests for SearchTelemetry - general engine visiting and link clicking.
+ */
+
+"use strict";
+
+const {SearchTelemetry} = ChromeUtils.import("resource:///modules/SearchTelemetry.jsm", null);
+
+const TEST_PROVIDER_INFO = {
+  "example": {
+    "regexp": /^http:\/\/mochi.test:.+\/browser\/browser\/components\/search\/test\/browser\/searchTelemetry.html/,
+    "queryParam": "s",
+    "codeParam": "abc",
+    "codePrefixes": ["ff"],
+    "followonParams": ["a"],
+    "extraAdServersRegexp": /^https:\/\/example\.com/,
+    "adPrefixes": ["ad", "ad2"],
+  },
+};
+
+const MAIN_TEST_PAGE =
+  "http://mochi.test:8888/browser/browser/components/search/test/browser/searchTelemetry.html";
+const TEST_PROVIDER_SERP_URL =
+  MAIN_TEST_PAGE + "?s=test&abc=ff";
+const TEST_PROVIDER_SERP_FOLLOWON_URL =
+  MAIN_TEST_PAGE + "?s=test&abc=ff&a=foo";
+
+const searchCounts = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS");
+
+async function assertTelemetry(expectedHistograms) {
+  let histSnapshot;
+
+  await TestUtils.waitForCondition(() => {
+    histSnapshot = searchCounts.snapshot();
+    return Object.getOwnPropertyNames(histSnapshot).length ==
+      Object.getOwnPropertyNames(expectedHistograms).length;
+  });
+
+  Assert.equal(Object.getOwnPropertyNames(histSnapshot).length,
+    Object.getOwnPropertyNames(expectedHistograms).length,
+    "Should only have one key");
+
+  for (let [key, value] of Object.entries(expectedHistograms)) {
+    Assert.ok(key in histSnapshot,
+      `Histogram should have the expected key: ${key}`);
+    Assert.equal(histSnapshot[key].sum, value,
+      `Should have counted the correct number of visits for ${key}`);
+  }
+}
+
+add_task(async function setup() {
+  SearchTelemetry.overrideSearchTelemetryForTests(TEST_PROVIDER_INFO);
+  // Enable local telemetry recording for the duration of the tests.
+  let oldCanRecord = Services.telemetry.canRecordExtended;
+  Services.telemetry.canRecordExtended = true;
+  Services.prefs.setBoolPref("browser.search.log", true);
+
+  registerCleanupFunction(async () => {
+    SearchTelemetry.overrideSearchTelemetryForTests();
+    Services.telemetry.canRecordExtended = oldCanRecord;
+    Services.telemetry.clearScalars();
+  });
+});
+
+add_task(async function test_simple_search_page_visit() {
+  searchCounts.clear();
+
+  await BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: TEST_PROVIDER_SERP_URL,
+  }, async () => {
+    await assertTelemetry({"example.in-content:sap:ff": 1});
+  });
+});
+
+add_task(async function test_follow_on_visit() {
+  await BrowserTestUtils.withNewTab({
+    gBrowser,
+    url: TEST_PROVIDER_SERP_FOLLOWON_URL,
+  }, async () => {
+    await assertTelemetry({
+      "example.in-content:sap:ff": 1,
+      "example.in-content:sap-follow-on:ff": 1,
+    });
+  });
+});
new file mode 100644
--- /dev/null
+++ b/browser/components/search/test/browser/searchTelemetry.html
@@ -0,0 +1,6 @@
+<!DOCTYPE html>
+<html>
+<head>
+</head>
+<body></body>
+</html>