commit 04e69a8d3939ddd22d1858c1435551face5adab1
parent 18e78b9083d2caea7b47c53ff5e583c1e07b59be
Author: James Teow <jteow@mozilla.com>
Date: Tue, 11 Nov 2025 02:37:14 +0000
Bug 1986285 - Part 6: Reduce dependency on fixed frecency values in test - r=daisuke,home-newtab-reviewers,nbarrett
Since the value of frecency in the table is the days until the score for a website
becomes 1, the values are a moving target. Thus, we should look to do basic
comparisons in the test.
Additionally, the ordering of the reset was causing TV errors.
Differential Revision: https://phabricator.services.mozilla.com/D264740
Diffstat:
1 file changed, 125 insertions(+), 72 deletions(-)
diff --git a/browser/components/newtab/test/browser/browser_sponsored_annotation.js b/browser/components/newtab/test/browser/browser_sponsored_annotation.js
@@ -6,9 +6,9 @@
// Test whether a visit information is annotated correctly when clicking a tile.
if (AppConstants.platform === "macosx") {
- requestLongerTimeout(4);
+ requestLongerTimeout(5);
} else {
- requestLongerTimeout(2);
+ requestLongerTimeout(3);
}
ChromeUtils.defineESModuleGetters(this, {
@@ -30,17 +30,6 @@ const OPEN_TYPE = {
NEWWINDOW_BY_CONTEXTMENU_OF_TILE: 5,
};
-const FRECENCY = {
- TYPED: 2000,
- VISITED: 100,
- SPONSORED: -1,
- BOOKMARKED: 2075,
- MIDDLECLICK_TYPED: 100,
- MIDDLECLICK_BOOKMARKED: 175,
- NEWWINDOW_TYPED: 100,
- NEWWINDOW_BOOKMARKED: 175,
-};
-
const {
VISIT_SOURCE_ORGANIC,
VISIT_SOURCE_SPONSORED,
@@ -76,13 +65,6 @@ async function waitForVisitNotification(href) {
}
async function assertDatabase({ targetURL, expected }) {
- const frecency = await PlacesTestUtils.getDatabaseValue(
- "moz_places",
- "frecency",
- { url: targetURL }
- );
- Assert.equal(frecency, expected.frecency, "Frecency is correct");
-
const placesId = await PlacesTestUtils.getDatabaseValue("moz_places", "id", {
url: targetURL,
});
@@ -322,6 +304,19 @@ function unpin(link) {
NewTabUtils.pinnedLinks.unpin(link);
}
+async function appendPlaceData(arr, url, description) {
+ let frecency = await PlacesTestUtils.getDatabaseValue(
+ "moz_places",
+ "frecency",
+ { url }
+ );
+ arr.push({ frecency, url, description });
+}
+
+function sortFrecencyValues(frecencyValues) {
+ return [...frecencyValues].sort((a, b) => b.frecency - a.frecency);
+}
+
add_setup(async function () {
await clearHistoryAndBookmarks();
registerCleanupFunction(async () => {
@@ -356,7 +351,6 @@ add_task(async function basic() {
link: SPONSORED_LINK,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
protection: true,
},
},
@@ -366,7 +360,6 @@ add_task(async function basic() {
openType: OPEN_TYPE.NEWTAB_BY_CLICK,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
protection: true,
},
},
@@ -376,7 +369,6 @@ add_task(async function basic() {
openType: OPEN_TYPE.NEWTAB_BY_MIDDLECLICK,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
protection: true,
},
},
@@ -386,7 +378,6 @@ add_task(async function basic() {
openType: OPEN_TYPE.NEWTAB_BY_CONTEXTMENU,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
protection: true,
},
},
@@ -396,7 +387,6 @@ add_task(async function basic() {
openType: OPEN_TYPE.NEWWINDOW_BY_CONTEXTMENU,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
protection: true,
},
},
@@ -406,7 +396,6 @@ add_task(async function basic() {
openType: OPEN_TYPE.NEWWINDOW_BY_CONTEXTMENU_OF_TILE,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
protection: true,
},
},
@@ -416,7 +405,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_BOOKMARKED,
- frecency: FRECENCY.BOOKMARKED,
protection: false,
},
},
@@ -427,7 +415,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_BOOKMARKED,
- frecency: FRECENCY.BOOKMARKED,
protection: false,
},
},
@@ -438,7 +425,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_BOOKMARKED,
- frecency: FRECENCY.MIDDLECLICK_BOOKMARKED,
protection: false,
},
},
@@ -449,7 +435,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_BOOKMARKED,
- frecency: FRECENCY.MIDDLECLICK_BOOKMARKED,
protection: false,
},
},
@@ -460,7 +445,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_BOOKMARKED,
- frecency: FRECENCY.NEWWINDOW_BOOKMARKED,
protection: false,
},
},
@@ -471,7 +455,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_BOOKMARKED,
- frecency: FRECENCY.BOOKMARKED,
protection: false,
},
},
@@ -481,7 +464,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.BOOKMARKED,
protection: true,
},
},
@@ -493,7 +475,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.BOOKMARKED,
protection: true,
},
},
@@ -504,7 +485,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.MIDDLECLICK_BOOKMARKED,
protection: true,
},
},
@@ -515,7 +495,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.MIDDLECLICK_BOOKMARKED,
protection: true,
},
},
@@ -527,7 +506,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.NEWWINDOW_BOOKMARKED,
protection: true,
},
},
@@ -539,7 +517,6 @@ add_task(async function basic() {
bookmarks: BOOKMARKS,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.BOOKMARKED,
protection: true,
},
},
@@ -548,7 +525,6 @@ add_task(async function basic() {
link: NORMAL_LINK,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.TYPED,
protection: false,
},
},
@@ -558,7 +534,6 @@ add_task(async function basic() {
openType: OPEN_TYPE.NEWTAB_BY_CLICK,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.TYPED,
protection: false,
},
},
@@ -568,7 +543,6 @@ add_task(async function basic() {
openType: OPEN_TYPE.NEWTAB_BY_MIDDLECLICK,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.MIDDLECLICK_TYPED,
protection: false,
},
},
@@ -578,7 +552,6 @@ add_task(async function basic() {
openType: OPEN_TYPE.NEWTAB_BY_CONTEXTMENU,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.MIDDLECLICK_TYPED,
protection: false,
},
},
@@ -588,7 +561,6 @@ add_task(async function basic() {
openType: OPEN_TYPE.NEWWINDOW_BY_CONTEXTMENU,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.NEWWINDOW_TYPED,
protection: false,
},
},
@@ -598,7 +570,6 @@ add_task(async function basic() {
openType: OPEN_TYPE.NEWWINDOW_BY_CONTEXTMENU_OF_TILE,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.TYPED,
protection: false,
},
},
@@ -622,9 +593,8 @@ add_task(async function basic() {
expected,
});
- await clearHistoryAndBookmarks();
-
unpin(link);
+ await clearHistoryAndBookmarks();
});
}
});
@@ -652,7 +622,6 @@ add_task(async function redirection() {
openType: OPEN_TYPE.NEWTAB_BY_CLICK,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
protection: true,
},
@@ -663,9 +632,9 @@ add_task(async function redirection() {
targetURL: link.url,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
},
});
+
await clearHistoryAndBookmarks();
// Test with same tab.
@@ -676,7 +645,6 @@ add_task(async function redirection() {
openType: OPEN_TYPE.NEWTAB_BY_CLICK,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
protection: true,
},
@@ -687,11 +655,10 @@ add_task(async function redirection() {
targetURL: link.url,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
},
});
- await clearHistoryAndBookmarks();
unpin(link);
+ await clearHistoryAndBookmarks();
});
});
@@ -723,7 +690,6 @@ add_task(async function inherit() {
linkURL: link.url,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
protection: true,
},
});
@@ -735,7 +701,6 @@ add_task(async function inherit() {
openType: OPEN_TYPE.NEWWINDOW_BY_CONTEXTMENU,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
protection: true,
},
@@ -751,7 +716,6 @@ add_task(async function inherit() {
openType: OPEN_TYPE.NEWTAB_BY_CLICK,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
protection: true,
},
@@ -767,7 +731,6 @@ add_task(async function inherit() {
openType: OPEN_TYPE.NEWTAB_BY_MIDDLECLICK,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
protection: true,
},
@@ -780,7 +743,6 @@ add_task(async function inherit() {
linkURL: secondURL,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
protection: true,
},
@@ -793,7 +755,6 @@ add_task(async function inherit() {
openType: OPEN_TYPE.NEWWINDOW_BY_CONTEXTMENU,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
protection: true,
},
@@ -809,7 +770,6 @@ add_task(async function inherit() {
openType: OPEN_TYPE.NEWTAB_BY_CONTEXTMENU,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
protection: true,
},
@@ -825,7 +785,6 @@ add_task(async function inherit() {
openType: OPEN_TYPE.NEWTAB_BY_MIDDLECLICK,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
protection: true,
},
@@ -838,7 +797,6 @@ add_task(async function inherit() {
linkURL: thirdURL,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
protection: true,
},
@@ -850,7 +808,6 @@ add_task(async function inherit() {
linkURL: outsideURL,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.VISITED,
protection: true,
},
});
@@ -859,23 +816,24 @@ add_task(async function inherit() {
const onLoad = BrowserTestUtils.browserLoaded(
gBrowser.selectedBrowser,
false,
- host
+ "https://www.example.com/"
);
+
await UrlbarTestUtils.promiseAutocompleteResultPopup({
window,
value: host,
waitForFocus: SimpleTest.waitForFocus,
});
- let promiseVisited = waitForVisitNotification(host);
+
+ let promiseVisited = waitForVisitNotification("https://www.example.com/");
EventUtils.synthesizeKey("KEY_Enter");
await onLoad;
await promiseVisited;
await assertDatabase({
- targetURL: host,
+ targetURL: "https://www.example.com/",
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
triggerURL: link.url,
},
});
@@ -910,7 +868,6 @@ add_task(async function timeout() {
linkURL: link.url,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
protection: true,
},
});
@@ -931,7 +888,6 @@ add_task(async function timeout() {
openType: OPEN_TYPE.NEWWINDOW_BY_CONTEXTMENU,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.VISITED,
protection: true,
},
});
@@ -946,7 +902,6 @@ add_task(async function timeout() {
openType: OPEN_TYPE.NEWTAB_BY_CLICK,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.VISITED,
protection: true,
},
});
@@ -961,7 +916,6 @@ add_task(async function timeout() {
openType: OPEN_TYPE.NEWTAB_BY_MIDDLECLICK,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.VISITED,
protection: true,
},
});
@@ -973,13 +927,13 @@ add_task(async function timeout() {
linkURL: secondURL,
expected: {
source: VISIT_SOURCE_ORGANIC,
- frecency: FRECENCY.VISITED,
protection: true,
},
});
unpin(link);
await clearHistoryAndBookmarks();
+ await SpecialPowers.popPrefEnv();
});
});
@@ -1020,7 +974,6 @@ add_task(async function fixup() {
targetURL: destinationURL,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
},
});
@@ -1063,7 +1016,6 @@ add_task(async function noTriggeringURL() {
targetURL,
expected: {
source: VISIT_SOURCE_SPONSORED,
- frecency: FRECENCY.SPONSORED,
},
});
@@ -1078,3 +1030,104 @@ add_task(async function noTriggeringURL() {
await clearHistoryAndBookmarks();
});
});
+
+add_task(async function basic_frecency_check() {
+ const SPONSORED_LINK = {
+ label: "test_label",
+ url: "https://example.com/",
+ sponsored_position: 1,
+ sponsored_tile_id: 12345,
+ sponsored_impression_url: "https://impression.example.com/",
+ sponsored_click_url: "https://click.example.com/",
+ };
+
+ const NORMAL_LINK = {
+ label: "test_label",
+ url: "https://example.com/",
+ };
+
+ const BOOKMARKS = [
+ {
+ parentGuid: PlacesUtils.bookmarks.toolbarGuid,
+ url: Services.io.newURI("https://example.com/"),
+ title: "test bookmark",
+ },
+ ];
+
+ // This is a basic, slim sanity check the frecency ordering is expected.
+ // Ties are broken by their order.
+ const testData = [
+ // Typed visits and bookmarked visits.
+ {
+ description: "Bookmarked result",
+ link: NORMAL_LINK,
+ bookmarks: BOOKMARKS,
+ expected: {
+ source: VISIT_SOURCE_BOOKMARKED,
+ protection: false,
+ },
+ },
+ {
+ description: "Organic tile",
+ link: NORMAL_LINK,
+ expected: {
+ source: VISIT_SOURCE_ORGANIC,
+ protection: false,
+ },
+ },
+ // Sponsored.
+ {
+ description: "Sponsored and bookmarked result",
+ link: SPONSORED_LINK,
+ bookmarks: BOOKMARKS,
+ expected: {
+ source: VISIT_SOURCE_SPONSORED,
+ protection: true,
+ },
+ },
+ {
+ description: "Sponsored tile",
+ link: SPONSORED_LINK,
+ expected: {
+ source: VISIT_SOURCE_SPONSORED,
+ protection: true,
+ },
+ },
+ ];
+
+ let frecencyResults = [];
+
+ for (const { description, link, openType, bookmarks, expected } of testData) {
+ info(description);
+
+ await BrowserTestUtils.withNewTab("about:home", async () => {
+ // Setup test tile.
+ await pin(link);
+
+ for (const bookmark of bookmarks || []) {
+ await PlacesUtils.bookmarks.insert(bookmark);
+ }
+
+ await openAndTest({
+ linkSelector: ".top-site-button",
+ linkURL: link.url,
+ openType,
+ expected,
+ });
+
+ await appendPlaceData(frecencyResults, link.url, description);
+
+ unpin(link);
+ await clearHistoryAndBookmarks();
+ });
+ }
+
+ let sortedByFrecency = sortFrecencyValues(frecencyResults);
+ for (let i = 0; i < testData.length; i++) {
+ Assert.equal(
+ sortedByFrecency[i].description,
+ frecencyResults[i].description,
+ `Expected "${frecencyResults[i].description}" to be at position ${i} when sorted by frecency`
+ );
+ }
+});