From 9e86197ead5f331da531a2bb1be7aa75044848b5 Mon Sep 17 00:00:00 2001 From: Marco Bonardo Date: Mon, 10 Jun 2013 23:59:15 +0200 Subject: [PATCH] Bug 769348 - Change selection algorithm for autofilled URLs prefixes. This new variant limits selection to typed pages and suggests only prefixes that match all of those addresses. r=paolo --- toolkit/components/places/Database.cpp | 40 ++-- toolkit/components/places/Database.h | 3 +- toolkit/components/places/History.cpp | 5 +- toolkit/components/places/nsPlacesTriggers.h | 45 +++-- .../places/tests/browser/browser_notfound.js | 10 +- .../components/places/tests/head_common.js | 2 +- .../places/tests/inline/test_trimming.js | 141 ++++++++++--- .../places/tests/unit/test_hosts_triggers.js | 190 +++++++++--------- 8 files changed, 274 insertions(+), 162 deletions(-) diff --git a/toolkit/components/places/Database.cpp b/toolkit/components/places/Database.cpp index 84d377703a87..3f4093e109bc 100644 --- a/toolkit/components/places/Database.cpp +++ b/toolkit/components/places/Database.cpp @@ -728,6 +728,13 @@ Database::InitSchema(bool* aDatabaseMigrated) // Firefox 22 uses schema version 22. + if (currentSchemaVersion < 23) { + rv = MigrateV23Up(); + NS_ENSURE_SUCCESS(rv, rv); + } + + // Firefox 24 uses schema version 23. + // Schema Upgrades must add migration code here. rv = UpdateBookmarkRootTitles(); @@ -1864,19 +1871,6 @@ Database::MigrateV21Up() NS_ENSURE_SUCCESS(rv, rv); } - // Update prefixes. - nsCOMPtr updatePrefixesStmt; - rv = mMainConn->CreateAsyncStatement(NS_LITERAL_CSTRING( - "UPDATE moz_hosts SET prefix = ( " - HOSTS_PREFIX_PRIORITY_FRAGMENT - ") " - ), getter_AddRefs(updatePrefixesStmt)); - NS_ENSURE_SUCCESS(rv, rv); - - nsCOMPtr ps; - rv = updatePrefixesStmt->ExecuteAsync(nullptr, getter_AddRefs(ps)); - NS_ENSURE_SUCCESS(rv, rv); - return NS_OK; } @@ -1895,6 +1889,26 @@ Database::MigrateV22Up() return NS_OK; } + +nsresult +Database::MigrateV23Up() +{ + MOZ_ASSERT(NS_IsMainThread()); + + // Recalculate hosts prefixes. + nsCOMPtr updatePrefixesStmt; + nsresult rv = mMainConn->CreateAsyncStatement(NS_LITERAL_CSTRING( + "UPDATE moz_hosts SET prefix = ( " HOSTS_PREFIX_PRIORITY_FRAGMENT ") " + ), getter_AddRefs(updatePrefixesStmt)); + NS_ENSURE_SUCCESS(rv, rv); + + nsCOMPtr ps; + rv = updatePrefixesStmt->ExecuteAsync(nullptr, getter_AddRefs(ps)); + NS_ENSURE_SUCCESS(rv, rv); + + return NS_OK; +} + void Database::Shutdown() { diff --git a/toolkit/components/places/Database.h b/toolkit/components/places/Database.h index 42ca243e07c5..c99ace46ffb7 100644 --- a/toolkit/components/places/Database.h +++ b/toolkit/components/places/Database.h @@ -15,7 +15,7 @@ // This is the schema version. Update it at any schema change and add a // corresponding migrateVxx method below. -#define DATABASE_SCHEMA_VERSION 22 +#define DATABASE_SCHEMA_VERSION 23 // Fired after Places inited. #define TOPIC_PLACES_INIT_COMPLETE "places-init-complete" @@ -273,6 +273,7 @@ protected: nsresult MigrateV20Up(); nsresult MigrateV21Up(); nsresult MigrateV22Up(); + nsresult MigrateV23Up(); nsresult UpdateBookmarkRootTitles(); nsresult CheckAndUpdateGUIDs(); diff --git a/toolkit/components/places/History.cpp b/toolkit/components/places/History.cpp index ba53fb58a1fc..4cadd3bf5b38 100644 --- a/toolkit/components/places/History.cpp +++ b/toolkit/components/places/History.cpp @@ -2217,7 +2217,10 @@ History::VisitURI(nsIURI* aURI, else if (aFlags & IHistory::REDIRECT_PERMANENT) { transitionType = nsINavHistoryService::TRANSITION_REDIRECT_PERMANENT; } - else if (recentFlags & nsNavHistory::RECENT_TYPED) { + else if ((recentFlags & nsNavHistory::RECENT_TYPED) && + !(aFlags & IHistory::UNRECOVERABLE_ERROR)) { + // Don't mark error pages as typed, even if they were actually typed by + // the user. This is useful to limit their score in autocomplete. transitionType = nsINavHistoryService::TRANSITION_TYPED; } else if (recentFlags & nsNavHistory::RECENT_BOOKMARKED) { diff --git a/toolkit/components/places/nsPlacesTriggers.h b/toolkit/components/places/nsPlacesTriggers.h index 3bcbd58316d0..d7190cca44c8 100644 --- a/toolkit/components/places/nsPlacesTriggers.h +++ b/toolkit/components/places/nsPlacesTriggers.h @@ -46,31 +46,44 @@ "END" \ ) +/** + * A predicate matching pages on rev_host, based on a given host value. + * 'host' may be either the moz_hosts.host column or an alias representing an + * equivalent value. + */ +#define HOST_TO_REVHOST_PREDICATE \ + "rev_host = get_unreversed_host(host || '.') || '.' " \ + "OR rev_host = get_unreversed_host(host || '.') || '.www.'" + /** * Select the best prefix for a host, based on existing pages registered for it. * Prefixes have a priority, from the top to the bottom, so that secure pages * have higher priority, and more generically "www." prefixed hosts come before * unprefixed ones. - * Each condition just checks if a page exists for a specific prefixed host, - * and if so returns the relative prefix. + * Given a host, examine associated pages and: + * - if all of the typed pages start with https://www. return https://www. + * - if all of the typed pages start with https:// return https:// + * - if all of the typed pages start with ftp: return ftp:// + * - if all of the typed pages start with www. return www. + * - otherwise don't use any prefix */ #define HOSTS_PREFIX_PRIORITY_FRAGMENT \ "SELECT CASE " \ - "WHEN EXISTS( " \ - "SELECT 1 FROM moz_places WHERE url BETWEEN 'https://www.' || host || '/' " \ - "AND 'https://www.' || host || '/' || X'FFFF' " \ + "WHEN 1 = ( " \ + "SELECT min(substr(url,1,12) = 'https://www.') FROM moz_places h " \ + "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ ") THEN 'https://www.' " \ - "WHEN EXISTS( " \ - "SELECT 1 FROM moz_places WHERE url BETWEEN 'https://' || host || '/' " \ - "AND 'https://' || host || '/' || X'FFFF' " \ + "WHEN 1 = ( " \ + "SELECT min(substr(url,1,8) = 'https://') FROM moz_places h " \ + "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ ") THEN 'https://' " \ - "WHEN EXISTS( " \ - "SELECT 1 FROM moz_places WHERE url BETWEEN 'ftp://' || host || '/' " \ - "AND 'ftp://' || host || '/' || X'FFFF' " \ + "WHEN 1 = ( " \ + "SELECT min(substr(url,1,4) = 'ftp:') FROM moz_places h " \ + "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ ") THEN 'ftp://' " \ - "WHEN EXISTS( " \ - "SELECT 1 FROM moz_places WHERE url BETWEEN 'http://www.' || host || '/' " \ - "AND 'http://www.' || host || '/' || X'FFFF' " \ + "WHEN 1 = ( " \ + "SELECT min(substr(url,1,11) = 'http://www.') FROM moz_places h " \ + "WHERE (" HOST_TO_REVHOST_PREDICATE ") AND +h.typed = 1 " \ ") THEN 'www.' " \ "END " @@ -109,9 +122,7 @@ "OR rev_host = get_unreversed_host(host || '.') || '.www.' " \ "); " \ "UPDATE moz_hosts " \ - "SET prefix = (" \ - HOSTS_PREFIX_PRIORITY_FRAGMENT \ - ") " \ + "SET prefix = (" HOSTS_PREFIX_PRIORITY_FRAGMENT ") " \ "WHERE host = fixup_url(get_unreversed_host(OLD.rev_host)); " \ "END" \ ) diff --git a/toolkit/components/places/tests/browser/browser_notfound.js b/toolkit/components/places/tests/browser/browser_notfound.js index 5c702791bfc6..e59c08e49f4d 100644 --- a/toolkit/components/places/tests/browser/browser_notfound.js +++ b/toolkit/components/places/tests/browser/browser_notfound.js @@ -9,7 +9,10 @@ function test() { registerCleanupFunction(function() { gBrowser.removeCurrentTab(); }); - gBrowser.selectedTab.linkedBrowser.loadURI("http://mochi.test:8888/notFoundPage.html"); + const TEST_URL = "http://mochi.test:8888/notFoundPage.html"; + // Used to verify errors are not marked as typed. + PlacesUtils.history.markPageAsTyped(NetUtil.newURI(TEST_URL)); + gBrowser.selectedTab.linkedBrowser.loadURI(TEST_URL); // Create and add history observer. let historyObserver = { @@ -21,7 +24,10 @@ function test() { is(aFrecency, 0, "Frecency should be 0"); fieldForUrl(aURI, "hidden", function (aHidden) { is(aHidden, 0, "Page should not be hidden"); - promiseClearHistory().then(finish); + fieldForUrl(aURI, "typed", function (aTyped) { + is(aTyped, 0, "page should not be marked as typed"); + promiseClearHistory().then(finish); + }); }); }); }, diff --git a/toolkit/components/places/tests/head_common.js b/toolkit/components/places/tests/head_common.js index a62ecce8ba46..8f1956b221d6 100644 --- a/toolkit/components/places/tests/head_common.js +++ b/toolkit/components/places/tests/head_common.js @@ -3,7 +3,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -const CURRENT_SCHEMA_VERSION = 22; +const CURRENT_SCHEMA_VERSION = 23; const NS_APP_USER_PROFILE_50_DIR = "ProfD"; const NS_APP_PROFILE_DIR_STARTUP = "ProfDS"; diff --git a/toolkit/components/places/tests/inline/test_trimming.js b/toolkit/components/places/tests/inline/test_trimming.js index 6b814199c62b..7c7919a04b8d 100644 --- a/toolkit/components/places/tests/inline/test_trimming.js +++ b/toolkit/components/places/tests/inline/test_trimming.js @@ -7,7 +7,8 @@ add_autocomplete_test([ "mo", { autoFilled: "mozilla.org/", completed: "https://www.mozilla.org/" }, function () { - addBookmark({ url: "https://www.mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("https://www.mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); @@ -16,7 +17,8 @@ add_autocomplete_test([ "mozilla.org/t", { autoFilled: "mozilla.org/test/", completed: "https://www.mozilla.org/test/" }, function () { - addBookmark({ url: "https://www.mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("https://www.mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); @@ -25,7 +27,8 @@ add_autocomplete_test([ "mo", { autoFilled: "mozilla.org/", completed: "https://mozilla.org/" }, function () { - addBookmark({ url: "https://mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("https://mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); @@ -34,7 +37,8 @@ add_autocomplete_test([ "mozilla.org/t", { autoFilled: "mozilla.org/test/", completed: "https://mozilla.org/test/" }, function () { - addBookmark({ url: "https://mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("https://mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); @@ -43,7 +47,8 @@ add_autocomplete_test([ "mo", { autoFilled: "mozilla.org/", completed: "www.mozilla.org/" }, function () { - addBookmark({ url: "http://www.mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("http://www.mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); @@ -52,7 +57,8 @@ add_autocomplete_test([ "mozilla.org/t", { autoFilled: "mozilla.org/test/", completed: "http://www.mozilla.org/test/" }, function () { - addBookmark({ url: "http://www.mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("http://www.mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); @@ -61,7 +67,8 @@ add_autocomplete_test([ "mo", { autoFilled: "mozilla.org/", completed: "ftp://mozilla.org/" }, function () { - addBookmark({ url: "ftp://mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("ftp://mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); @@ -70,53 +77,92 @@ add_autocomplete_test([ "mozilla.org/t", { autoFilled: "mozilla.org/test/", completed: "ftp://mozilla.org/test/" }, function () { - addBookmark({ url: "ftp://mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("ftp://mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); add_autocomplete_test([ "Ensuring correct priority 1", "mo", - { autoFilled: "mozilla.org/", completed: "https://www.mozilla.org/" }, + { autoFilled: "mozilla.org/", completed: "mozilla.org/" }, function () { - addBookmark({ url: "https://www.mozilla.org/test/" }); - addBookmark({ url: "https://mozilla.org/test/" }); - addBookmark({ url: "ftp://mozilla.org/test/" }); - addBookmark({ url: "http://www.mozilla.org/test/" }); - addBookmark({ url: "http://mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("https://www.mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("https://mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("ftp://mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("http://www.mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("http://mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); add_autocomplete_test([ "Ensuring correct priority 2", "mo", - { autoFilled: "mozilla.org/", completed: "https://mozilla.org/" }, + { autoFilled: "mozilla.org/", completed: "mozilla.org/" }, function () { - addBookmark({ url: "https://mozilla.org/test/" }); - addBookmark({ url: "ftp://mozilla.org/test/" }); - addBookmark({ url: "http://www.mozilla.org/test/" }); - addBookmark({ url: "http://mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("https://mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("ftp://mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("http://www.mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("http://mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); add_autocomplete_test([ "Ensuring correct priority 3", "mo", - { autoFilled: "mozilla.org/", completed: "ftp://mozilla.org/" }, + { autoFilled: "mozilla.org/", completed: "mozilla.org/" }, function () { - addBookmark({ url: "ftp://mozilla.org/test/" }); - addBookmark({ url: "http://www.mozilla.org/test/" }); - addBookmark({ url: "http://mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("ftp://mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("http://www.mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("http://mozilla.org/test/"), + transition: TRANSITION_TYPED }); }, ]); add_autocomplete_test([ "Ensuring correct priority 4", "mo", + { autoFilled: "mozilla.org/", completed: "mozilla.org/" }, + function () { + promiseAddVisits({ uri: NetUtil.newURI("http://www.mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("http://mozilla.org/test/"), + transition: TRANSITION_TYPED }); + }, +]); + +add_autocomplete_test([ + "Ensuring correct priority 5", + "mo", + { autoFilled: "mozilla.org/", completed: "ftp://mozilla.org/" }, + function () { + promiseAddVisits({ uri: NetUtil.newURI("ftp://mozilla.org/test/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("ftp://www.mozilla.org/test/"), + transition: TRANSITION_TYPED }); + }, +]); + +add_autocomplete_test([ + "Ensuring correct priority 6", + "mo", { autoFilled: "mozilla.org/", completed: "www.mozilla.org/" }, function () { - addBookmark({ url: "http://www.mozilla.org/test/" }); - addBookmark({ url: "http://mozilla.org/test/" }); + promiseAddVisits({ uri: NetUtil.newURI("http://www.mozilla.org/test1/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("http://www.mozilla.org/test2/"), + transition: TRANSITION_TYPED }); }, ]); @@ -127,9 +173,12 @@ add_autocomplete_test([ function () { // The .co should be preferred, but should not get the https from the .com. // The .co domain must be added later to activate the trigger bug. - addBookmark({ url: "https://mozilla.com/" }); - addBookmark({ url: "http://mozilla.co/" }); - addBookmark({ url: "http://mozilla.co/" }); + promiseAddVisits({ uri: NetUtil.newURI("https://mozilla.com/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("http://mozilla.co/"), + transition: TRANSITION_TYPED }); + promiseAddVisits({ uri: NetUtil.newURI("http://mozilla.co/"), + transition: TRANSITION_TYPED }); }, ]); @@ -162,3 +211,39 @@ add_autocomplete_test([ transition: TRANSITION_TYPED }); }, ]); + +add_autocomplete_test([ + "Untyped is not accounted for www", + "mo", + { autoFilled: "moz.org/", completed: "moz.org/" }, + function () { + promiseAddVisits({ uri: NetUtil.newURI("http://www.moz.org/test/") }); + }, +]); + +add_autocomplete_test([ + "Untyped is not accounted for ftp", + "mo", + { autoFilled: "moz.org/", completed: "moz.org/" }, + function () { + promiseAddVisits({ uri: NetUtil.newURI("ftp://moz.org/test/") }); + }, +]); + +add_autocomplete_test([ + "Untyped is not accounted for https", + "mo", + { autoFilled: "moz.org/", completed: "moz.org/" }, + function () { + promiseAddVisits({ uri: NetUtil.newURI("https://moz.org/test/") }); + }, +]); + +add_autocomplete_test([ + "Untyped is not accounted for https://www", + "mo", + { autoFilled: "moz.org/", completed: "moz.org/" }, + function () { + promiseAddVisits({ uri: NetUtil.newURI("https://www.moz.org/test/") }); + }, +]); diff --git a/toolkit/components/places/tests/unit/test_hosts_triggers.js b/toolkit/components/places/tests/unit/test_hosts_triggers.js index 73610d82efb3..d983a37c7f37 100644 --- a/toolkit/components/places/tests/unit/test_hosts_triggers.js +++ b/toolkit/components/places/tests/unit/test_hosts_triggers.js @@ -66,59 +66,39 @@ let urls = [{uri: NetUtil.newURI("http://visit1.mozilla.org"), }, ]; -function VisitInfo(aTransitionType, aVisitTime) -{ - this.transitionType = - aTransitionType === undefined ? TRANSITION_LINK : aTransitionType; - this.visitDate = aVisitTime || Date.now() * 1000; -} - const NEW_URL = "http://different.mozilla.org/"; -function test_moz_hosts_update() +add_task(function test_moz_hosts_update() { let places = []; urls.forEach(function(url) { - let place = { - uri: url.uri, + let place = { uri: url.uri, title: "test for " + url.url, - visits: [ - new VisitInfo(url.typed ? TRANSITION_TYPED : undefined), - ], - }; + transition: url.typed ? TRANSITION_TYPED : undefined }; places.push(place); }); - gHistory.updatePlaces(places, { - handleResult: function () { - }, - handleError: function () { - do_throw("gHistory.updatePlaces() failed"); - }, - handleCompletion: function () { - do_check_true(isHostInMozHosts(urls[0].uri, urls[0].typed, urls[0].prefix)); - do_check_true(isHostInMozHosts(urls[1].uri, urls[1].typed, urls[1].prefix)); - do_check_true(isHostInMozHosts(urls[2].uri, urls[2].typed, urls[2].prefix)); - run_next_test(); - } - }); -} + yield promiseAddVisits(places); -function test_remove_places() + do_check_true(isHostInMozHosts(urls[0].uri, urls[0].typed, urls[0].prefix)); + do_check_true(isHostInMozHosts(urls[1].uri, urls[1].typed, urls[1].prefix)); + do_check_true(isHostInMozHosts(urls[2].uri, urls[2].typed, urls[2].prefix)); +}); + +add_task(function test_remove_places() { for (let idx in urls) { PlacesUtils.history.removePage(urls[idx].uri); } - promiseClearHistory().then(function (){ - for (let idx in urls) { - do_check_false(isHostInMozHosts(urls[idx].uri, urls[idx].typed, urls[idx].prefix)); - } - run_next_test(); - }); -} + yield promiseClearHistory(); -function test_bookmark_changes() + for (let idx in urls) { + do_check_false(isHostInMozHosts(urls[idx].uri, urls[idx].typed, urls[idx].prefix)); + } +}); + +add_task(function test_bookmark_changes() { let testUri = NetUtil.newURI("http://test.mozilla.org"); @@ -132,97 +112,109 @@ function test_bookmark_changes() // Change the hostname PlacesUtils.bookmarks.changeBookmarkURI(itemId, NetUtil.newURI(NEW_URL)); - promiseClearHistory().then(function (){ - let newUri = NetUtil.newURI(NEW_URL); - do_check_true(isHostInMozPlaces(newUri)); - do_check_true(isHostInMozHosts(newUri, false, null)); - do_check_false(isHostInMozHosts(NetUtil.newURI("http://test.mozilla.org"), false, null)); - run_next_test(); - }); -} + yield promiseClearHistory(); -function test_bookmark_removal() + let newUri = NetUtil.newURI(NEW_URL); + do_check_true(isHostInMozPlaces(newUri)); + do_check_true(isHostInMozHosts(newUri, false, null)); + do_check_false(isHostInMozHosts(NetUtil.newURI("http://test.mozilla.org"), false, null)); +}); + +add_task(function test_bookmark_removal() { let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, PlacesUtils.bookmarks.DEFAULT_INDEX); let newUri = NetUtil.newURI(NEW_URL); PlacesUtils.bookmarks.removeItem(itemId); - promiseClearHistory().then(function (){ - do_check_false(isHostInMozHosts(newUri, false, null)); - run_next_test(); - }); -} + yield promiseClearHistory(); -function test_moz_hosts_typed_update() + do_check_false(isHostInMozHosts(newUri, false, null)); +}); + +add_task(function test_moz_hosts_typed_update() { const TEST_URI = NetUtil.newURI("http://typed.mozilla.com"); let places = [{ uri: TEST_URI , title: "test for " + TEST_URI.spec - , visits: [ new VisitInfo(TRANSITION_LINK) - , new VisitInfo(TRANSITION_TYPED) - ] + }, + { uri: TEST_URI + , title: "test for " + TEST_URI.spec + , transition: TRANSITION_TYPED }]; - gHistory.updatePlaces(places, { - handleResult: function () { - }, - handleError: function () { - do_throw("gHistory.updatePlaces() failed"); - }, - handleCompletion: function () { - do_check_true(isHostInMozHosts(TEST_URI, true, null)); - run_next_test(); - } - }); -} + yield promiseAddVisits(places); -function test_moz_hosts_www_remove() + do_check_true(isHostInMozHosts(TEST_URI, true, null)); + yield promiseClearHistory(); +}); + +add_task(function test_moz_hosts_www_remove() { function test_removal(aURIToRemove, aURIToKeep, aCallback) { let places = [{ uri: aURIToRemove , title: "test for " + aURIToRemove.spec - , visits: [ new VisitInfo() ] + , transition: TRANSITION_TYPED }, { uri: aURIToKeep , title: "test for " + aURIToKeep.spec - , visits: [ new VisitInfo() ] + , transition: TRANSITION_TYPED }]; - gHistory.updatePlaces(places, { - handleResult: function () { - }, - handleError: function () { - do_throw("gHistory.updatePlaces() failed"); - }, - handleCompletion: function () { - PlacesUtils.history.removePage(aURIToRemove); - let prefix = /www/.test(aURIToKeep.spec) ? "www." : null; - do_check_true(isHostInMozHosts(aURIToKeep, false, prefix)); - promiseClearHistory().then(aCallback); - } - }); + yield promiseAddVisits(places); + print("removing " + aURIToRemove.spec + " keeping " + aURIToKeep); + dump_table("moz_hosts"); + dump_table("moz_places"); + PlacesUtils.history.removePage(aURIToRemove); + let prefix = /www/.test(aURIToKeep.spec) ? "www." : null; + dump_table("moz_hosts"); + dump_table("moz_places"); + do_check_true(isHostInMozHosts(aURIToKeep, true, prefix)); } const TEST_URI = NetUtil.newURI("http://rem.mozilla.com"); const TEST_WWW_URI = NetUtil.newURI("http://www.rem.mozilla.com"); - test_removal(TEST_URI, TEST_WWW_URI, function() { - test_removal(TEST_WWW_URI, TEST_URI, function() { - promiseClearHistory().then(run_next_test); - }); - }); -} + yield test_removal(TEST_URI, TEST_WWW_URI); + yield test_removal(TEST_WWW_URI, TEST_URI); + yield promiseClearHistory(); +}); -//////////////////////////////////////////////////////////////////////////////// -//// Test Runner +add_task(function test_moz_hosts_ftp_matchall() +{ + const TEST_URI_1 = NetUtil.newURI("ftp://www.mozilla.com/"); + const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/"); -[ - test_moz_hosts_update, - test_remove_places, - test_bookmark_changes, - test_bookmark_removal, - test_moz_hosts_typed_update, - test_moz_hosts_www_remove, -].forEach(add_test); + yield promiseAddVisits([{ uri: TEST_URI_1, transition: TRANSITION_TYPED }, + { uri: TEST_URI_2, transition: TRANSITION_TYPED }]); + + do_check_true(isHostInMozHosts(TEST_URI_1, true, "ftp://")); +}); + +add_task(function test_moz_hosts_ftp_not_matchall() +{ + const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/"); + const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/"); + + yield promiseAddVisits([{ uri: TEST_URI_1, transition: TRANSITION_TYPED }, + { uri: TEST_URI_2, transition: TRANSITION_TYPED }]); + + do_check_true(isHostInMozHosts(TEST_URI_1, true, null)); +}); + +add_task(function test_moz_hosts_update_2() +{ + // Check that updating trigger takes into account prefixes for different + // rev_hosts. + const TEST_URI_1 = NetUtil.newURI("https://www.google.it/"); + const TEST_URI_2 = NetUtil.newURI("https://google.it/"); + let places = [{ uri: TEST_URI_1 + , transition: TRANSITION_TYPED + }, + { uri: TEST_URI_2 + }]; + yield promiseAddVisits(places); + + do_check_true(isHostInMozHosts(TEST_URI_1, true, "https://www.")); +}); function run_test() {