Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test gets skipped with pattern: os == 'android'
- Manifest: browser/components/urlbar/tests/unit/xpcshell.toml
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
"use strict";
const HEURISTIC_FALLBACK_PROVIDERNAME = "HeuristicFallback";
const origin = "example.com";
async function cleanup() {
let suggestPrefs = ["history", "bookmark", "openpage"];
for (let type of suggestPrefs) {
Services.prefs.clearUserPref("browser.urlbar.suggest." + type);
}
await cleanupPlaces();
}
testEngine_setup();
registerCleanupFunction(async () => {
Services.prefs.clearUserPref("browser.urlbar.suggest.quickactions");
});
Services.prefs.setBoolPref("browser.urlbar.suggest.quickactions", false);
// should be treated as if it didn't have the trailing slash.
add_task(async function trailingSlash() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext(`${origin}/`, { isPrivate: false });
await check_results({
context,
autofilled: `${origin}/`,
matches: [
makeVisitResult(context, {
heuristic: true,
}),
],
});
await cleanup();
});
// should be treated as if it didn't have the trailing slash.
add_task(async function trailingSlashWWW() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext(`${origin}/`, { isPrivate: false });
await check_results({
context,
autofilled: "example.com/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
],
});
await cleanup();
});
add_task(async function port() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext("ex", { isPrivate: false });
await check_results({
context,
autofilled: "example.com:8888/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
],
});
await cleanup();
});
// be completed.
add_task(async function portPartial() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext(`${origin}:8`, { isPrivate: false });
await check_results({
context,
autofilled: "example.com:8888/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
],
});
await cleanup();
});
// should be preserved in the autofilled value.
add_task(async function preserveCase() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext("EXaM", { isPrivate: false });
await check_results({
context,
autofilled: "EXaMple.com/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
],
});
await cleanup();
});
// and the case of the search string should be preserved in the autofilled
// value.
add_task(async function preserveCasePort() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext("EXaM", { isPrivate: false });
await check_results({
context,
autofilled: "EXaMple.com:8888/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
],
});
await cleanup();
});
add_task(async function portNoMatch1() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext(`${origin}:89`, { isPrivate: false });
await check_results({
context,
matches: [
makeVisitResult(context, {
source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
fallbackTitle: `${origin}:89/`,
iconUri: "",
heuristic: true,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
}),
],
});
await cleanup();
});
add_task(async function portNoMatch2() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext(`${origin}:9`, { isPrivate: false });
await check_results({
context,
matches: [
makeVisitResult(context, {
source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
fallbackTitle: `${origin}:9/`,
iconUri: "",
heuristic: true,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
}),
],
});
await cleanup();
});
add_task(async function trailingSlash_2() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext("example/", { isPrivate: false });
await check_results({
context,
matches: [
makeVisitResult(context, {
source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
fallbackTitle: "example/",
heuristic: true,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
}),
],
});
await cleanup();
});
// multi.dotted.domain, search up to dot.
add_task(async function multidotted() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext("www.example.co.", { isPrivate: false });
await check_results({
context,
autofilled: "www.example.co.jp:8888/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
],
});
await cleanup();
});
add_task(async function test_ip() {
// IP addresses have complicated rules around whether they show
// HeuristicFallback's backup search result. Flip this pref to disable that
// backup search and simplify ths subtest.
Services.prefs.setBoolPref("keyword.enabled", false);
for (let str of [
"192.168.1.1/",
"255.255.255.255:8080/",
"[2001:db8::1428:57ab]/",
"[::c0a8:5909]/",
"[::1]/",
]) {
info("testing " + str);
await PlacesTestUtils.addVisits("http://" + str);
for (let i = 1; i < str.length; ++i) {
let context = createContext(str.substring(0, i), { isPrivate: false });
await check_results({
context,
autofilled: str,
completed: "http://" + str,
matches: [
makeVisitResult(context, {
uri: "http://" + str,
heuristic: true,
}),
],
});
}
await cleanup();
}
Services.prefs.clearUserPref("keyword.enabled");
});
// host starting with large number.
add_task(async function large_number_host() {
await PlacesTestUtils.addVisits([
{
},
]);
let context = createContext("1234", { isPrivate: false });
await check_results({
context,
autofilled: "12345example.it:8888/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
],
});
await cleanup();
});
// When determining which origins should be autofilled, all the origins sharing
// a host should be added together to get their combined frecency -- i.e.,
// prefixes should be collapsed. And then from that list, the origin with the
// highest frecency should be chosen.
add_task(async function groupByHost() {
// Add some visits to the same host, example.com. Add one http and two https
// so that https has a higher frecency and is therefore the origin that should
// be autofilled. Also add another origin that has a higher frecency than
// both so that alone, neither http nor https would be autofilled, but added
// together they should be.
await PlacesTestUtils.addVisits([
]);
let httpFrec = await PlacesTestUtils.getDatabaseValue(
"moz_places",
"frecency",
);
let httpsFrec = await PlacesTestUtils.getDatabaseValue(
"moz_places",
"frecency",
);
let otherFrec = await PlacesTestUtils.getDatabaseValue(
"moz_places",
"frecency",
);
Assert.less(httpFrec, httpsFrec, "Sanity check");
Assert.less(httpsFrec, otherFrec, "Sanity check");
// Make sure the frecencies of the three origins are as expected in relation
// to the threshold.
let threshold = await getOriginAutofillThreshold();
Assert.less(httpFrec, threshold, "http origin should be < threshold");
Assert.less(httpsFrec, threshold, "https origin should be < threshold");
Assert.ok(threshold <= otherFrec, "Other origin should cross threshold");
Assert.ok(
threshold <= httpFrec + httpsFrec,
"http and https origin added together should cross threshold"
);
// The https origin should be autofilled.
let context = createContext("ex", { isPrivate: false });
await check_results({
context,
autofilled: "example.com/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
],
});
await cleanup();
});
// This is the same as the previous (groupByHost), but it changes the standard
// deviation multiplier by setting the corresponding pref. This makes sure that
// the pref is respected.
add_task(async function groupByHostNonDefaultStddevMultiplier() {
let stddevMultiplier = 1.5;
Services.prefs.setCharPref(
"browser.urlbar.autoFill.stddevMultiplier",
Number(stddevMultiplier).toFixed(1)
);
await PlacesTestUtils.addVisits([
]);
let httpFrec = await PlacesTestUtils.getDatabaseValue(
"moz_places",
"frecency",
{
}
);
let httpsFrec = await PlacesTestUtils.getDatabaseValue(
"moz_places",
"frecency",
{
}
);
let otherFrec = await PlacesTestUtils.getDatabaseValue(
"moz_places",
"frecency",
{
}
);
Assert.less(httpFrec, httpsFrec, "Sanity check");
Assert.less(httpsFrec, otherFrec, "Sanity check");
// Make sure the frecencies of the three origins are as expected in relation
// to the threshold.
let threshold = await getOriginAutofillThreshold();
Assert.less(httpFrec, threshold, "http origin should be < threshold");
Assert.less(httpsFrec, threshold, "https origin should be < threshold");
Assert.ok(threshold <= otherFrec, "Other origin should cross threshold");
Assert.ok(
threshold <= httpFrec + httpsFrec,
"http and https origin added together should cross threshold"
);
// The https origin should be autofilled.
let context = createContext("ex", { isPrivate: false });
await check_results({
context,
autofilled: "example.com/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
],
});
Services.prefs.clearUserPref("browser.urlbar.autoFill.stddevMultiplier");
await cleanup();
});
// This is similar to suggestHistoryFalse_bookmark_0 in test_autofill_tasks.js,
// but it adds unbookmarked visits for multiple URLs with the same origin.
add_task(async function suggestHistoryFalse_bookmark_multiple() {
// Force only bookmarked pages to be suggested and therefore only bookmarked
// pages to be completed.
Services.prefs.setBoolPref("browser.urlbar.suggest.history", false);
let search = "ex";
let bookmarkedURL = baseURL + "bookmarked";
// Add visits for three different URLs all sharing the same origin, and then
// bookmark the second one. After that, the origin should be autofilled. The
// reason for adding unbookmarked visits before and after adding the
// bookmarked visit is to make sure our aggregate SQL query for determining
// whether an origin is bookmarked is correct.
await PlacesTestUtils.addVisits([
{
uri: baseURL + "other1",
},
]);
let context = createContext(search, { isPrivate: false });
await check_results({
context,
matches: [
makeSearchResult(context, {
engineName: SUGGESTIONS_ENGINE_NAME,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
heuristic: true,
}),
],
});
await PlacesTestUtils.addVisits([
{
uri: bookmarkedURL,
},
]);
context = createContext(search, { isPrivate: false });
await check_results({
context,
matches: [
makeSearchResult(context, {
engineName: SUGGESTIONS_ENGINE_NAME,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
heuristic: true,
}),
],
});
await PlacesTestUtils.addVisits([
{
uri: baseURL + "other2",
},
]);
context = createContext(search, { isPrivate: false });
await check_results({
context,
matches: [
makeSearchResult(context, {
engineName: SUGGESTIONS_ENGINE_NAME,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
heuristic: true,
}),
],
});
// Now bookmark the second URL. It should be suggested and completed.
await PlacesTestUtils.addBookmarkWithDetails({
uri: bookmarkedURL,
});
context = createContext(search, { isPrivate: false });
await check_results({
context,
autofilled: "example.com/",
completed: baseURL,
matches: [
makeVisitResult(context, {
uri: baseURL,
fallbackTitle: UrlbarTestUtils.trimURL(baseURL),
heuristic: true,
}),
makeBookmarkResult(context, {
uri: bookmarkedURL,
title: "A bookmark",
}),
],
});
await cleanup();
});
// This is similar to suggestHistoryFalse_bookmark_prefix_0 in
// autofill_test_autofill_originsAndQueries.js, but it adds unbookmarked visits
// for multiple URLs with the same origin.
add_task(async function suggestHistoryFalse_bookmark_prefix_multiple() {
// Force only bookmarked pages to be suggested and therefore only bookmarked
// pages to be completed.
Services.prefs.setBoolPref("browser.urlbar.suggest.history", false);
let bookmarkedURL = baseURL + "bookmarked";
// Add visits for three different URLs all sharing the same origin, and then
// bookmark the second one. After that, the origin should be autofilled. The
// reason for adding unbookmarked visits before and after adding the
// bookmarked visit is to make sure our aggregate SQL query for determining
// whether an origin is bookmarked is correct.
await PlacesTestUtils.addVisits([
{
uri: baseURL + "other1",
},
]);
let context = createContext(search, { isPrivate: false });
await check_results({
context,
matches: [
makeVisitResult(context, {
source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
uri: `${search}/`,
fallbackTitle: `${search}/`,
iconUri: "",
heuristic: true,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
}),
],
});
await PlacesTestUtils.addVisits([
{
uri: bookmarkedURL,
},
]);
context = createContext(search, { isPrivate: false });
await check_results({
context,
matches: [
makeVisitResult(context, {
source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
uri: `${search}/`,
fallbackTitle: `${search}/`,
iconUri: "",
heuristic: true,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
}),
],
});
await PlacesTestUtils.addVisits([
{
uri: baseURL + "other2",
},
]);
context = createContext(search, { isPrivate: false });
await check_results({
context,
matches: [
makeVisitResult(context, {
source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
uri: `${search}/`,
fallbackTitle: `${search}/`,
iconUri: "",
heuristic: true,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
}),
],
});
// Now bookmark the second URL. It should be suggested and completed.
await PlacesTestUtils.addBookmarkWithDetails({
uri: bookmarkedURL,
});
context = createContext(search, { isPrivate: false });
await check_results({
context,
completed: baseURL,
matches: [
makeVisitResult(context, {
uri: baseURL,
fallbackTitle: UrlbarTestUtils.trimURL(baseURL),
heuristic: true,
}),
makeBookmarkResult(context, {
uri: bookmarkedURL,
title: "A bookmark",
}),
],
});
await cleanup();
});
// When the autofilled URL is `example.com/`, a visit for `example.com/?` should
// not be included in the results since it dupes the autofill result.
add_task(async function searchParams() {
await PlacesTestUtils.addVisits([
]);
// First, do a search with autofill disabled to make sure the visits were
// properly added. `example.com/?foo` has the highest frecency because it was
// added last; `example.com/?` has the next highest. `example.com/` dupes
// `example.com/?`, so it should not appear.
UrlbarPrefs.set("autoFill", false);
let context = createContext("ex", { isPrivate: false });
await check_results({
context,
matches: [
makeSearchResult(context, {
engineName: SUGGESTIONS_ENGINE_NAME,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
heuristic: true,
}),
makeVisitResult(context, {
}),
makeVisitResult(context, {
}),
],
});
// Now do a search with autofill enabled. This time `example.com/` will be
// autofilled, and since `example.com/?` dupes it, `example.com/?` should not
// appear.
UrlbarPrefs.clear("autoFill");
context = createContext("ex", { isPrivate: false });
await check_results({
context,
autofilled: "example.com/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
makeVisitResult(context, {
}),
],
});
await cleanup();
});
// When the autofilled URL is `example.com/`, a visit for `example.com/?` should
// not be included in the results since it dupes the autofill result. (Same as
// the previous task but with https URLs instead of http. There shouldn't be any
// substantive difference.)
add_task(async function searchParams_https() {
await PlacesTestUtils.addVisits([
]);
// First, do a search with autofill disabled to make sure the visits were
// properly added. `example.com/?foo` has the highest frecency because it was
// added last; `example.com/?` has the next highest. `example.com/` dupes
// `example.com/?`, so it should not appear.
UrlbarPrefs.set("autoFill", false);
let context = createContext("ex", { isPrivate: false });
await check_results({
context,
matches: [
makeSearchResult(context, {
engineName: SUGGESTIONS_ENGINE_NAME,
providerName: HEURISTIC_FALLBACK_PROVIDERNAME,
heuristic: true,
}),
makeVisitResult(context, {
}),
makeVisitResult(context, {
}),
],
});
// Now do a search with autofill enabled. This time `example.com/` will be
// autofilled, and since `example.com/?` dupes it, `example.com/?` should not
// appear.
UrlbarPrefs.clear("autoFill");
context = createContext("ex", { isPrivate: false });
await check_results({
context,
autofilled: "example.com/",
matches: [
makeVisitResult(context, {
heuristic: true,
}),
makeVisitResult(context, {
}),
],
});
await cleanup();
});
// Checks an origin that looks like a prefix: a scheme with no dots + a port.
add_task(async function originLooksLikePrefix() {
let hostAndPort = "localhost:8888";
await PlacesTestUtils.addVisits([{ uri: address }]);
// addTestSuggestionsEngine adds a search engine
// with localhost as a server, so we have to disable the
// TTS result or else it will show up as a second result
// when searching l to localhost
UrlbarPrefs.set("suggest.engines", false);
for (let search of ["lo", "localhost", "localhost:", "localhost:8888"]) {
let context = createContext(search, { isPrivate: false });
await check_results({
context,
autofilled: hostAndPort + "/",
completed: address,
matches: [
makeVisitResult(context, {
uri: address,
heuristic: true,
}),
],
});
}
await cleanup();
});
// Checks an origin whose prefix is "about:".
add_task(async function about() {
const testData = [
{
uri: "about:config",
input: "conf",
results: [
context =>
makeSearchResult(context, {
engineName: "Suggestions",
heuristic: true,
}),
context =>
makeBookmarkResult(context, {
uri: "about:config",
title: "A bookmark",
}),
],
},
{
uri: "about:blank",
input: "about:blan",
results: [
context =>
makeVisitResult(context, {
uri: "about:blan",
fallbackTitle: "about:blan",
source: UrlbarUtils.RESULT_SOURCE.OTHER_LOCAL,
heuristic: true,
}),
context =>
makeBookmarkResult(context, {
uri: "about:blank",
title: "A bookmark",
}),
],
},
];
for (const { uri, input, results } of testData) {
await PlacesTestUtils.addBookmarkWithDetails({ uri });
const context = createContext(input, { isPrivate: false });
await check_results({
context,
matches: results.map(f => f(context)),
});
await cleanup();
}
});
// Checks an origin whose prefix is "place:".
add_task(async function place() {
const testData = [
{
uri: "place:transition=7&sort=4",
input: "tran",
},
{
uri: "place:transition=7&sort=4",
input: "place:tran",
},
];
for (const { uri, input } of testData) {
await PlacesTestUtils.addBookmarkWithDetails({ uri });
const context = createContext(input, { isPrivate: false });
await check_results({
context,
matches: [
makeSearchResult(context, {
engineName: "Suggestions",
heuristic: true,
}),
],
});
await cleanup();
}
});
add_task(async function nullTitle() {
await doTitleTest({
visits: [
{
// Set title of visits data to an empty string causes
// the title to be null in the database.
title: "",
frecency: 100,
},
{
title: "high frecency",
frecency: 50,
},
{
title: "low frecency",
frecency: 1,
},
],
input: "example.com",
expected: {
autofilled: "example.com/",
matches: context => [
makeVisitResult(context, {
title: "high frecency",
heuristic: true,
}),
makeVisitResult(context, {
title: "high frecency",
}),
],
},
});
});
add_task(async function domainTitle() {
await doTitleTest({
visits: [
{
title: "example.com",
frecency: 100,
},
{
title: "",
frecency: 50,
},
{
title: "lowest frecency but has title",
frecency: 1,
},
],
input: "example.com",
expected: {
autofilled: "example.com/",
matches: context => [
makeVisitResult(context, {
title: "lowest frecency but has title",
heuristic: true,
}),
makeVisitResult(context, {
title: "www.example.com",
}),
],
},
});
});
add_task(async function exactMatchedTitle() {
await doTitleTest({
visits: [
{
title: "exact match",
frecency: 50,
},
{
title: "high frecency uri",
frecency: 100,
},
],
expected: {
matches: context => [
makeVisitResult(context, {
title: "exact match",
heuristic: true,
}),
makeVisitResult(context, {
title: "high frecency uri",
}),
],
},
});
});
async function doTitleTest({ visits, input, expected }) {
await PlacesTestUtils.addVisits(visits);
for (const { uri, frecency } of visits) {
// Prepare data.
await PlacesUtils.withConnectionWrapper("test::doTitleTest", async db => {
await db.execute(
`UPDATE moz_places SET frecency = :frecency, recalc_frecency=0 WHERE url = :url`,
{
frecency,
url: uri,
}
);
await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
});
}
const context = createContext(input, { isPrivate: false });
await check_results({
context,
autofilled: expected.autofilled,
completed: expected.completed,
matches: expected.matches(context),
});
await cleanup();
}
/* Tests sorting order when only unvisited bookmarks are available (e.g. in
permanent private browsing mode), then the only information we have is the
number of bookmarks per origin, and we're going to use that. */
add_task(async function just_multiple_unvisited_bookmarks() {
// These are sorted to avoid confusion with natural sorting, so the one with
// the highest score is added in the middle.
let urls = [
{
count: 1,
},
{
count: 2,
},
{
url: filledUrl,
count: 2,
},
{
count: 3,
},
];
await PlacesUtils.history.clear();
for (let { url, count } of urls) {
while (count--) {
await PlacesTestUtils.addBookmarkWithDetails({
uri: url,
});
}
}
await PlacesFrecencyRecalculator.recalculateAnyOutdatedFrecencies();
let context = createContext("tld", { isPrivate: false });
await check_results({
context,
autofilled: "tld2.com/",
completed: filledUrl,
matches: [
makeVisitResult(context, {
uri: filledUrl,
title: "A bookmark",
heuristic: true,
}),
makeBookmarkResult(context, {
title: "A bookmark",
}),
makeBookmarkResult(context, {
title: "A bookmark",
}),
makeBookmarkResult(context, {
title: "A bookmark",
}),
],
});
await cleanup();
});