diff options
Diffstat (limited to 'toolkit/components/places/tests/unit/test_origins.js')
-rw-r--r-- | toolkit/components/places/tests/unit/test_origins.js | 79 |
1 files changed, 47 insertions, 32 deletions
diff --git a/toolkit/components/places/tests/unit/test_origins.js b/toolkit/components/places/tests/unit/test_origins.js index 67b6d59c7d..f74313a125 100644 --- a/toolkit/components/places/tests/unit/test_origins.js +++ b/toolkit/components/places/tests/unit/test_origins.js @@ -1005,6 +1005,33 @@ add_task(async function moreOriginFrecencyStats() { await cleanUp(); }); +add_task(async function test_cutoff() { + // Add first page with visit. + await PlacesTestUtils.addVisits([{ uri: "http://example.com/0" }]); + // Add a second page last visited before the cutoff, it should be ignored. + let visitDate = PlacesUtils.toPRTime( + new Date( + new Date().setDate( + -Services.prefs.getIntPref("places.frecency.originsCutOffDays", 90) + ) + ) + ); + await PlacesTestUtils.addVisits([{ uri: "http://example.com/1", visitDate }]); + // Add a third page with visit both before and after the cutoff, should count. + await PlacesTestUtils.addVisits([ + { uri: "http://example.com/2" }, + { uri: "http://example.com/2", visitDate }, + ]); + await checkDB([ + [ + "http://", + "example.com", + ["http://example.com/0", "http://example.com/2"], + ], + ]); + await cleanUp(); +}); + /** * Returns the expected frecency of the origin of the given URLs, i.e., the sum * of their frecencies. Each URL is expected to have the same origin. @@ -1017,12 +1044,15 @@ async function expectedOriginFrecency(urls) { let value = 0; for (let url of urls) { let v = Math.max( - await PlacesTestUtils.getDatabaseValue("moz_places", "frecency", { url }), + (await PlacesTestUtils.getDatabaseValue("moz_places", "frecency", { + url, + last_visit_date: [">", 0], + })) ?? 0, 0 ); value += v; } - return value; + return value || 1.0; } /** @@ -1064,49 +1094,34 @@ async function checkDB(expectedOrigins) { } Assert.deepEqual(actualOrigins, expected); if (checkFrecencies) { - await checkStats(expected.map(o => o[2]).filter(o => o > 0)); + info("Checking threshold"); + await PlacesTestUtils.dumpTable({ db, table: "moz_origins" }); + await checkThreshold(expected.map(o => o[2])); } } /** - * Asserts that the origin frecency stats are correct. + * Asserts that the origin frecency threshold is correct. * * @param expectedOriginFrecencies * An array of expected origin frecencies. */ -async function checkStats(expectedOriginFrecencies) { - let stats = await promiseStats(); - Assert.equal(stats.count, expectedOriginFrecencies.length); - Assert.equal( - stats.sum, - expectedOriginFrecencies.reduce((sum, f) => sum + f, 0) +async function checkThreshold(expectedOriginFrecencies) { + const DEFAULT_THRESHOLD = 2.0; + let threshold = await PlacesUtils.metadata.get( + "origin_frecency_threshold", + DEFAULT_THRESHOLD ); + Assert.equal( - stats.squares, - expectedOriginFrecencies.reduce((squares, f) => squares + f * f, 0) + threshold, + expectedOriginFrecencies.length + ? expectedOriginFrecencies.reduce((a, b) => a + b, 0) / + expectedOriginFrecencies.length + : DEFAULT_THRESHOLD ); } -/** - * Returns the origin frecency stats. - * - * @return An object: { count, sum, squares } - */ -async function promiseStats() { - let db = await PlacesUtils.promiseDBConnection(); - let rows = await db.execute(` - SELECT - IFNULL((SELECT value FROM moz_meta WHERE key = 'origin_frecency_count'), 0), - IFNULL((SELECT value FROM moz_meta WHERE key = 'origin_frecency_sum'), 0), - IFNULL((SELECT value FROM moz_meta WHERE key = 'origin_frecency_sum_of_squares'), 0) - `); - return { - count: rows[0].getResultByIndex(0), - sum: rows[0].getResultByIndex(1), - squares: rows[0].getResultByIndex(2), - }; -} - async function cleanUp() { await PlacesUtils.bookmarks.eraseEverything(); await PlacesUtils.history.clear(); |