Bug 1361855 - Refactor browser/modules/test/browser/head.js:getHistogram() and getKeyedHistogram(); r?Gijs
MozReview-Commit-ID: 3GyNfmXm5g6
--- a/browser/modules/test/browser/browser_UsageTelemetry_content.js
+++ b/browser/modules/test/browser/browser_UsageTelemetry_content.js
@@ -40,17 +40,17 @@ add_task(async function setup() {
Services.telemetry.setEventRecordingEnabled("navigation", false);
});
});
add_task(async function test_context_menu() {
// Let's reset the Telemetry data.
Services.telemetry.clearScalars();
Services.telemetry.clearEvents();
- let search_hist = getSearchCountsHistogram();
+ let search_hist = getAndClearKeyedHistogram("SEARCH_COUNTS");
// Open a new tab with a page containing some text.
let tab =
await BrowserTestUtils.openNewForegroundTab(gBrowser, "data:text/plain;charset=utf8,test%20search");
info("Select all the text in the page.");
await ContentTask.spawn(tab.linkedBrowser, "", async function() {
return new Promise(resolve => {
@@ -88,17 +88,17 @@ add_task(async function test_context_men
await BrowserTestUtils.removeTab(gBrowser.selectedTab);
await BrowserTestUtils.removeTab(tab);
});
add_task(async function test_about_newtab() {
// Let's reset the counts.
Services.telemetry.clearScalars();
Services.telemetry.clearEvents();
- let search_hist = getSearchCountsHistogram();
+ let search_hist = getAndClearKeyedHistogram("SEARCH_COUNTS");
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:newtab", false);
await ContentTask.spawn(tab.linkedBrowser, null, async function() {
await ContentTaskUtils.waitForCondition(() => !content.document.hidden);
});
info("Trigger a simple serch, just text + enter.");
let p = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
--- a/browser/modules/test/browser/browser_UsageTelemetry_content_aboutHome.js
+++ b/browser/modules/test/browser/browser_UsageTelemetry_content_aboutHome.js
@@ -42,17 +42,17 @@ add_task(async function setup() {
Services.telemetry.setEventRecordingEnabled("navigation", false);
});
});
add_task(async function test_abouthome_simpleQuery() {
// Let's reset the counts.
Services.telemetry.clearScalars();
Services.telemetry.clearEvents();
- let search_hist = getSearchCountsHistogram();
+ let search_hist = getAndClearKeyedHistogram("SEARCH_COUNTS");
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);
info("Setup waiting for AboutHomeLoadSnippetsCompleted.");
let promiseAboutHomeLoaded = new Promise(resolve => {
tab.linkedBrowser.addEventListener("AboutHomeLoadSnippetsCompleted", function loadListener(event) {
tab.linkedBrowser.removeEventListener("AboutHomeLoadSnippetsCompleted", loadListener, true);
resolve();
--- a/browser/modules/test/browser/browser_UsageTelemetry_searchbar.js
+++ b/browser/modules/test/browser/browser_UsageTelemetry_searchbar.js
@@ -101,19 +101,18 @@ add_task(async function setup() {
Services.telemetry.setEventRecordingEnabled("navigation", false);
});
});
add_task(async function test_plainQuery() {
// Let's reset the counts.
Services.telemetry.clearScalars();
Services.telemetry.clearEvents();
- let resultMethodHist = Services.telemetry.getHistogramById("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
- resultMethodHist.clear();
- let search_hist = getSearchCountsHistogram();
+ let resultMethodHist = getAndClearHistogram("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
+ let search_hist = getAndClearKeyedHistogram("SEARCH_COUNTS");
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank");
info("Simulate entering a simple search.");
let p = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
await searchInSearchbar("simple query");
EventUtils.sendKey("return");
await p;
@@ -142,19 +141,18 @@ add_task(async function test_plainQuery(
});
// Performs a search using the first result, a one-off button, and the Return
// (Enter) key.
add_task(async function test_oneOff_enter() {
// Let's reset the counts.
Services.telemetry.clearScalars();
Services.telemetry.clearEvents();
- let resultMethodHist = Services.telemetry.getHistogramById("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
- resultMethodHist.clear();
- let search_hist = getSearchCountsHistogram();
+ let resultMethodHist = getAndClearHistogram("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
+ let search_hist = getAndClearKeyedHistogram("SEARCH_COUNTS");
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank");
info("Perform a one-off search using the first engine.");
let p = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
await searchInSearchbar("query");
info("Pressing Alt+Down to highlight the first one off engine.");
@@ -186,18 +184,17 @@ add_task(async function test_oneOff_ente
});
// Performs a search using the second result, a one-off button, and the Return
// (Enter) key. This only tests the FX_SEARCHBAR_SELECTED_RESULT_METHOD
// histogram since test_oneOff_enter covers everything else.
add_task(async function test_oneOff_enterSelection() {
// Let's reset the counts.
Services.telemetry.clearScalars();
- let resultMethodHist = Services.telemetry.getHistogramById("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
- resultMethodHist.clear();
+ let resultMethodHist = getAndClearHistogram("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
// Create an engine to generate search suggestions and add it as default
// for this test.
const url = getRootDirectory(gTestPath) + "usageTelemetrySearchSuggestions.xml";
let suggestionEngine = await new Promise((resolve, reject) => {
Services.search.addEngine(url, null, "", false, {
onSuccess(engine) { resolve(engine) },
onError() { reject() }
@@ -230,18 +227,17 @@ add_task(async function test_oneOff_ente
});
// Performs a search using a click on a one-off button. This only tests the
// FX_SEARCHBAR_SELECTED_RESULT_METHOD histogram since test_oneOff_enter covers
// everything else.
add_task(async function test_oneOff_click() {
// Let's reset the counts.
Services.telemetry.clearScalars();
- let resultMethodHist = Services.telemetry.getHistogramById("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
- resultMethodHist.clear();
+ let resultMethodHist = getAndClearHistogram("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank");
info("Type a query.");
let p = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
await searchInSearchbar("query");
info("Click the first one-off button.");
BrowserSearch.searchBar.textbox.popup.oneOffButtons.getSelectableButtons(false)[0].click();
@@ -255,19 +251,18 @@ add_task(async function test_oneOff_clic
await BrowserTestUtils.removeTab(tab);
});
// Clicks the first suggestion offered by the test search engine.
add_task(async function test_suggestion_click() {
// Let's reset the counts.
Services.telemetry.clearScalars();
Services.telemetry.clearEvents();
- let resultMethodHist = Services.telemetry.getHistogramById("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
- resultMethodHist.clear();
- let search_hist = getSearchCountsHistogram();
+ let resultMethodHist = getAndClearHistogram("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
+ let search_hist = getAndClearKeyedHistogram("SEARCH_COUNTS");
// Create an engine to generate search suggestions and add it as default
// for this test.
const url = getRootDirectory(gTestPath) + "usageTelemetrySearchSuggestions.xml";
let suggestionEngine = await new Promise((resolve, reject) => {
Services.search.addEngine(url, null, "", false, {
onSuccess(engine) { resolve(engine) },
onError() { reject() }
@@ -314,18 +309,17 @@ add_task(async function test_suggestion_
// Selects and presses the Return (Enter) key on the first suggestion offered by
// the test search engine. This only tests the
// FX_SEARCHBAR_SELECTED_RESULT_METHOD histogram since test_suggestion_click
// covers everything else.
add_task(async function test_suggestion_enterSelection() {
// Let's reset the counts.
Services.telemetry.clearScalars();
- let resultMethodHist = Services.telemetry.getHistogramById("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
- resultMethodHist.clear();
+ let resultMethodHist = getAndClearHistogram("FX_SEARCHBAR_SELECTED_RESULT_METHOD");
// Create an engine to generate search suggestions and add it as default
// for this test.
const url = getRootDirectory(gTestPath) + "usageTelemetrySearchSuggestions.xml";
let suggestionEngine = await new Promise((resolve, reject) => {
Services.search.addEngine(url, null, "", false, {
onSuccess(engine) { resolve(engine) },
onError() { reject() }
--- a/browser/modules/test/browser/browser_UsageTelemetry_urlbar.js
+++ b/browser/modules/test/browser/browser_UsageTelemetry_urlbar.js
@@ -103,26 +103,22 @@ add_task(async function setup() {
Services.telemetry.setEventRecordingEnabled("navigation", false);
});
});
add_task(async function test_simpleQuery() {
// Let's reset the counts.
Services.telemetry.clearScalars();
Services.telemetry.clearEvents();
- let resultIndexHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_INDEX");
- let resultTypeHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_TYPE");
- let resultMethodHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_METHOD");
- let resultIndexByTypeHist = Services.telemetry.getKeyedHistogramById("FX_URLBAR_SELECTED_RESULT_INDEX_BY_TYPE");
- resultIndexByTypeHist.clear();
- resultIndexHist.clear();
- resultTypeHist.clear();
- resultMethodHist.clear();
- let search_hist = getSearchCountsHistogram();
+ let resultIndexHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_INDEX");
+ let resultTypeHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_TYPE");
+ let resultIndexByTypeHist = getAndClearKeyedHistogram("FX_URLBAR_SELECTED_RESULT_INDEX_BY_TYPE");
+ let resultMethodHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_METHOD");
+ let search_hist = getAndClearKeyedHistogram("SEARCH_COUNTS");
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank");
info("Simulate entering a simple search.");
let p = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
await searchInAwesomebar("simple query");
EventUtils.sendKey("return");
await p;
@@ -162,26 +158,22 @@ add_task(async function test_simpleQuery
await BrowserTestUtils.removeTab(tab);
});
add_task(async function test_searchAlias() {
// Let's reset the counts.
Services.telemetry.clearScalars();
Services.telemetry.clearEvents();
- let resultIndexHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_INDEX");
- let resultTypeHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_TYPE");
- let resultIndexByTypeHist = Services.telemetry.getKeyedHistogramById("FX_URLBAR_SELECTED_RESULT_INDEX_BY_TYPE");
- let resultMethodHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_METHOD");
- resultIndexByTypeHist.clear();
- resultIndexHist.clear();
- resultTypeHist.clear();
- resultMethodHist.clear();
- let search_hist = getSearchCountsHistogram();
+ let resultIndexHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_INDEX");
+ let resultTypeHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_TYPE");
+ let resultIndexByTypeHist = getAndClearKeyedHistogram("FX_URLBAR_SELECTED_RESULT_INDEX_BY_TYPE");
+ let resultMethodHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_METHOD");
+ let search_hist = getAndClearKeyedHistogram("SEARCH_COUNTS");
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank");
info("Search using a search alias.");
let p = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
await searchInAwesomebar("mozalias query");
EventUtils.sendKey("return");
await p;
@@ -223,26 +215,22 @@ add_task(async function test_searchAlias
});
// Performs a search using the first result, a one-off button, and the Return
// (Enter) key.
add_task(async function test_oneOff_enter() {
// Let's reset the counts.
Services.telemetry.clearScalars();
Services.telemetry.clearEvents();
- let resultIndexHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_INDEX");
- let resultTypeHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_TYPE");
- let resultIndexByTypeHist = Services.telemetry.getKeyedHistogramById("FX_URLBAR_SELECTED_RESULT_INDEX_BY_TYPE");
- let resultMethodHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_METHOD");
- resultIndexByTypeHist.clear();
- resultIndexHist.clear();
- resultTypeHist.clear();
- resultMethodHist.clear();
- let search_hist = getSearchCountsHistogram();
+ let resultIndexHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_INDEX");
+ let resultTypeHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_TYPE");
+ let resultIndexByTypeHist = getAndClearKeyedHistogram("FX_URLBAR_SELECTED_RESULT_INDEX_BY_TYPE");
+ let resultMethodHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_METHOD");
+ let search_hist = getAndClearKeyedHistogram("SEARCH_COUNTS");
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank");
info("Perform a one-off search using the first engine.");
let p = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
await searchInAwesomebar("query");
info("Pressing Alt+Down to take us to the first one-off engine.");
@@ -288,18 +276,17 @@ add_task(async function test_oneOff_ente
// Performs a search using the second result, a one-off button, and the Return
// (Enter) key. This only tests the FX_URLBAR_SELECTED_RESULT_METHOD histogram
// since test_oneOff_enter covers everything else.
add_task(async function test_oneOff_enterSelection() {
// Let's reset the counts.
Services.telemetry.clearScalars();
- let resultMethodHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_METHOD");
- resultMethodHist.clear();
+ let resultMethodHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_METHOD");
// Create an engine to generate search suggestions and add it as default
// for this test.
const url = getRootDirectory(gTestPath) + "usageTelemetrySearchSuggestions.xml";
let suggestionEngine = await new Promise((resolve, reject) => {
Services.search.addEngine(url, null, "", false, {
onSuccess(engine) { resolve(engine) },
onError() { reject() }
@@ -333,18 +320,17 @@ add_task(async function test_oneOff_ente
// Performs a search using a click on a one-off button. This only tests the
// FX_URLBAR_SELECTED_RESULT_METHOD histogram since test_oneOff_enter covers
// everything else.
add_task(async function test_oneOff_click() {
// Let's reset the counts.
Services.telemetry.clearScalars();
- let resultMethodHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_METHOD");
- resultMethodHist.clear();
+ let resultMethodHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_METHOD");
let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "about:blank");
info("Type a query.");
let p = BrowserTestUtils.browserLoaded(tab.linkedBrowser);
await searchInAwesomebar("query");
info("Click the first one-off button.");
gURLBar.popup.oneOffSearchButtons.getSelectableButtons(false)[0].click();
@@ -358,26 +344,22 @@ add_task(async function test_oneOff_clic
await BrowserTestUtils.removeTab(tab);
});
// Clicks the first suggestion offered by the test search engine.
add_task(async function test_suggestion_click() {
// Let's reset the counts.
Services.telemetry.clearScalars();
Services.telemetry.clearEvents();
- let resultIndexHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_INDEX");
- let resultTypeHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_TYPE");
- let resultIndexByTypeHist = Services.telemetry.getKeyedHistogramById("FX_URLBAR_SELECTED_RESULT_INDEX_BY_TYPE");
- let resultMethodHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_METHOD");
- resultIndexByTypeHist.clear();
- resultIndexHist.clear();
- resultTypeHist.clear();
- resultMethodHist.clear();
- let search_hist = getSearchCountsHistogram();
+ let resultIndexHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_INDEX");
+ let resultTypeHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_TYPE");
+ let resultIndexByTypeHist = getAndClearKeyedHistogram("FX_URLBAR_SELECTED_RESULT_INDEX_BY_TYPE");
+ let resultMethodHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_METHOD");
+ let search_hist = getAndClearKeyedHistogram("SEARCH_COUNTS");
// Create an engine to generate search suggestions and add it as default
// for this test.
const url = getRootDirectory(gTestPath) + "usageTelemetrySearchSuggestions.xml";
let suggestionEngine = await new Promise((resolve, reject) => {
Services.search.addEngine(url, null, "", false, {
onSuccess(engine) { resolve(engine) },
onError() { reject() }
@@ -437,18 +419,17 @@ add_task(async function test_suggestion_
// Selects and presses the Return (Enter) key on the first suggestion offered by
// the test search engine. This only tests the FX_URLBAR_SELECTED_RESULT_METHOD
// histogram since test_suggestion_click covers everything else.
add_task(async function test_suggestion_enterSelection() {
// Let's reset the counts.
Services.telemetry.clearScalars();
- let resultMethodHist = Services.telemetry.getHistogramById("FX_URLBAR_SELECTED_RESULT_METHOD");
- resultMethodHist.clear();
+ let resultMethodHist = getAndClearHistogram("FX_URLBAR_SELECTED_RESULT_METHOD");
// Create an engine to generate search suggestions and add it as default
// for this test.
const url = getRootDirectory(gTestPath) + "usageTelemetrySearchSuggestions.xml";
let suggestionEngine = await new Promise((resolve, reject) => {
Services.search.addEngine(url, null, "", false, {
onSuccess(engine) { resolve(engine) },
onError() { reject() }
--- a/browser/modules/test/browser/head.js
+++ b/browser/modules/test/browser/head.js
@@ -71,25 +71,41 @@ let typeInSearchField = async function(b
await ContentTask.spawn(browser, [fieldName, text], async function([contentFieldName, contentText]) {
// Put the focus on the search box.
let searchInput = content.document.getElementById(contentFieldName);
searchInput.focus();
searchInput.value = contentText;
});
};
+
/**
- * Clear and get the SEARCH_COUNTS histogram.
+ * Clear and get the named histogram
+ * @param {String} name
+ * The name of the histogram
*/
-function getSearchCountsHistogram() {
- let search_hist = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS");
- search_hist.clear();
- return search_hist;
+function getAndClearHistogram(name) {
+ let histogram = Services.telemetry.getHistogramById(name);
+ histogram.clear();
+ return histogram;
}
+
+/**
+ * Clear and get the named keyed histogram
+ * @param {String} name
+ * The name of the keyed histogram
+ */
+function getAndClearKeyedHistogram(name) {
+ let histogram = Services.telemetry.getKeyedHistogramById(name);
+ histogram.clear();
+ return histogram;
+}
+
+
/**
* Check that the keyed histogram contains the right value.
*/
function checkKeyedHistogram(h, key, expectedValue) {
const snapshot = h.snapshot();
Assert.ok(key in snapshot, `The histogram must contain ${key}.`);
Assert.equal(snapshot[key].sum, expectedValue, `The key ${key} must contain ${expectedValue}.`);
}