Skip to content
This repository was archived by the owner on Aug 4, 2022. It is now read-only.

Commit b57f7aa

Browse files
author
Tim Taubert
committed
Bug 867550 - Make browser_aboutHome.js not fail if it starts before but runs past midnight; r=mak
1 parent 22ae6c4 commit b57f7aa

File tree

1 file changed

+69
-50
lines changed

1 file changed

+69
-50
lines changed

browser/base/content/test/browser_aboutHome.js

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -89,27 +89,8 @@ let gTests = [
8989
},
9090

9191
{
92-
desc: "Check that performing a search fires a search event.",
93-
setup: function () { },
94-
run: function () {
95-
let deferred = Promise.defer();
96-
let doc = gBrowser.contentDocument;
97-
98-
doc.addEventListener("AboutHomeSearchEvent", function onSearch(e) {
99-
is(e.detail, doc.documentElement.getAttribute("searchEngineName"), "Detail is search engine name");
100-
101-
gBrowser.stop();
102-
deferred.resolve();
103-
}, true, true);
104-
105-
doc.getElementById("searchText").value = "it works";
106-
doc.getElementById("searchSubmit").click();
107-
return deferred.promise;
108-
}
109-
},
110-
111-
{
112-
desc: "Check that performing a search records to Firefox Health Report.",
92+
desc: "Check that performing a search fires a search event and records to " +
93+
"Firefox Health Report.",
11394
setup: function () { },
11495
run: function () {
11596
try {
@@ -120,46 +101,33 @@ let gTests = [
120101
return Promise.resolve();
121102
}
122103

104+
let numSearchesBefore = 0;
123105
let deferred = Promise.defer();
124106
let doc = gBrowser.contentDocument;
125107

126108
// We rely on the listener in browser.js being installed and fired before
127109
// this one. If this ever changes, we should add an executeSoon() or similar.
128110
doc.addEventListener("AboutHomeSearchEvent", function onSearch(e) {
129-
executeSoon(gBrowser.stop.bind(gBrowser));
130-
let reporter = Components.classes["@mozilla.org/datareporting/service;1"]
131-
.getService()
132-
.wrappedJSObject
133-
.healthReporter;
134-
ok(reporter, "Health Reporter instance available.");
135-
136-
reporter.onInit().then(function onInit() {
137-
let provider = reporter.getProvider("org.mozilla.searches");
138-
ok(provider, "Searches provider is available.");
139-
140-
let engineName = doc.documentElement.getAttribute("searchEngineName");
141-
let id = Services.search.getEngineByName(engineName).identifier;
142-
143-
let m = provider.getMeasurement("counts", 2);
144-
m.getValues().then(function onValues(data) {
145-
let now = new Date();
146-
ok(data.days.hasDay(now), "Have data for today.");
147-
148-
let day = data.days.getDay(now);
149-
let field = id + ".abouthome";
150-
ok(day.has(field), "Have data for about home on this engine.");
151-
152-
// Note the search from the previous test.
153-
is(day.get(field), 2, "Have searches recorded.");
111+
let engineName = doc.documentElement.getAttribute("searchEngineName");
112+
is(e.detail, engineName, "Detail is search engine name");
154113

155-
deferred.resolve();
156-
});
114+
gBrowser.stop();
157115

116+
getNumberOfSearches().then(num => {
117+
is(num, numSearchesBefore + 1, "One more search recorded.");
118+
deferred.resolve();
158119
});
159120
}, true, true);
160121

161-
doc.getElementById("searchText").value = "a search";
162-
doc.getElementById("searchSubmit").click();
122+
// Get the current number of recorded searches.
123+
getNumberOfSearches().then(num => {
124+
numSearchesBefore = num;
125+
126+
info("Perform a search.");
127+
doc.getElementById("searchText").value = "a search";
128+
doc.getElementById("searchSubmit").click();
129+
});
130+
163131
return deferred.promise;
164132
}
165133
},
@@ -422,3 +390,54 @@ function promiseBrowserAttributes(aTab)
422390

423391
return deferred.promise;
424392
}
393+
394+
/**
395+
* Retrieves the number of about:home searches recorded for the current day.
396+
*
397+
* @return {Promise} Returns a promise resolving to the number of searches.
398+
*/
399+
function getNumberOfSearches() {
400+
let reporter = Components.classes["@mozilla.org/datareporting/service;1"]
401+
.getService()
402+
.wrappedJSObject
403+
.healthReporter;
404+
ok(reporter, "Health Reporter instance available.");
405+
406+
return reporter.onInit().then(function onInit() {
407+
let provider = reporter.getProvider("org.mozilla.searches");
408+
ok(provider, "Searches provider is available.");
409+
410+
let m = provider.getMeasurement("counts", 2);
411+
return m.getValues().then(data => {
412+
let now = new Date();
413+
let yday = new Date(now);
414+
yday.setDate(yday.getDate() - 1);
415+
416+
// Add the number of searches recorded yesterday to the number of searches
417+
// recorded today. This makes the test not fail intermittently when it is
418+
// run at midnight and we accidentally compare the number of searches from
419+
// different days. Tests are always run with an empty profile so there
420+
// are no searches from yesterday, normally. Should the test happen to run
421+
// past midnight we make sure to count them in as well.
422+
return getNumberOfSearchesByDate(data, now) +
423+
getNumberOfSearchesByDate(data, yday);
424+
});
425+
});
426+
}
427+
428+
function getNumberOfSearchesByDate(aData, aDate) {
429+
if (aData.days.hasDay(aDate)) {
430+
let doc = gBrowser.contentDocument;
431+
let engineName = doc.documentElement.getAttribute("searchEngineName");
432+
let id = Services.search.getEngineByName(engineName).identifier;
433+
434+
let day = aData.days.getDay(aDate);
435+
let field = id + ".abouthome";
436+
437+
if (day.has(field)) {
438+
return day.get(field) || 0;
439+
}
440+
}
441+
442+
return 0; // No records found.
443+
}

0 commit comments

Comments
 (0)