michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: /** michael@0: * This file tests the validity of various triggers that add remove hosts from moz_hosts michael@0: */ michael@0: michael@0: XPCOMUtils.defineLazyServiceGetter(this, "gHistory", michael@0: "@mozilla.org/browser/history;1", michael@0: "mozIAsyncHistory"); michael@0: michael@0: // add some visits and remove them, add a bookmark, michael@0: // change its uri, then remove it, and michael@0: // for each change check that moz_hosts has correctly been updated. michael@0: michael@0: function isHostInMozPlaces(aURI) michael@0: { michael@0: let stmt = DBConn().createStatement( michael@0: "SELECT url " michael@0: + "FROM moz_places " michael@0: + "WHERE url = :host" michael@0: ); michael@0: let result = false; michael@0: stmt.params.host = aURI.spec; michael@0: while(stmt.executeStep()) { michael@0: if (stmt.row.url == aURI.spec) { michael@0: result = true; michael@0: break; michael@0: } michael@0: } michael@0: stmt.finalize(); michael@0: return result; michael@0: } michael@0: michael@0: function isHostInMozHosts(aURI, aTyped, aPrefix) michael@0: { michael@0: let stmt = DBConn().createStatement( michael@0: "SELECT host, typed, prefix " michael@0: + "FROM moz_hosts " michael@0: + "WHERE host = fixup_url(:host) " michael@0: + "AND frecency NOTNULL " michael@0: ); michael@0: let result = false; michael@0: stmt.params.host = aURI.host; michael@0: if (stmt.executeStep()) { michael@0: result = aTyped == stmt.row.typed && aPrefix == stmt.row.prefix; michael@0: } michael@0: stmt.finalize(); michael@0: return result; michael@0: } michael@0: michael@0: let urls = [{uri: NetUtil.newURI("http://visit1.mozilla.org"), michael@0: expected: "visit1.mozilla.org", michael@0: typed: 0, michael@0: prefix: null michael@0: }, michael@0: {uri: NetUtil.newURI("http://visit2.mozilla.org"), michael@0: expected: "visit2.mozilla.org", michael@0: typed: 0, michael@0: prefix: null michael@0: }, michael@0: {uri: NetUtil.newURI("http://www.foo.mozilla.org"), michael@0: expected: "foo.mozilla.org", michael@0: typed: 1, michael@0: prefix: "www." michael@0: }, michael@0: ]; michael@0: michael@0: const NEW_URL = "http://different.mozilla.org/"; michael@0: michael@0: add_task(function test_moz_hosts_update() michael@0: { michael@0: let places = []; michael@0: urls.forEach(function(url) { michael@0: let place = { uri: url.uri, michael@0: title: "test for " + url.url, michael@0: transition: url.typed ? TRANSITION_TYPED : undefined }; michael@0: places.push(place); michael@0: }); michael@0: michael@0: yield promiseAddVisits(places); michael@0: michael@0: do_check_true(isHostInMozHosts(urls[0].uri, urls[0].typed, urls[0].prefix)); michael@0: do_check_true(isHostInMozHosts(urls[1].uri, urls[1].typed, urls[1].prefix)); michael@0: do_check_true(isHostInMozHosts(urls[2].uri, urls[2].typed, urls[2].prefix)); michael@0: }); michael@0: michael@0: add_task(function test_remove_places() michael@0: { michael@0: for (let idx in urls) { michael@0: PlacesUtils.history.removePage(urls[idx].uri); michael@0: } michael@0: michael@0: yield promiseClearHistory(); michael@0: michael@0: for (let idx in urls) { michael@0: do_check_false(isHostInMozHosts(urls[idx].uri, urls[idx].typed, urls[idx].prefix)); michael@0: } michael@0: }); michael@0: michael@0: add_task(function test_bookmark_changes() michael@0: { michael@0: let testUri = NetUtil.newURI("http://test.mozilla.org"); michael@0: michael@0: let itemId = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, michael@0: testUri, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX, michael@0: "bookmark title"); michael@0: michael@0: do_check_true(isHostInMozPlaces(testUri)); michael@0: michael@0: // Change the hostname michael@0: PlacesUtils.bookmarks.changeBookmarkURI(itemId, NetUtil.newURI(NEW_URL)); michael@0: michael@0: yield promiseClearHistory(); michael@0: michael@0: let newUri = NetUtil.newURI(NEW_URL); michael@0: do_check_true(isHostInMozPlaces(newUri)); michael@0: do_check_true(isHostInMozHosts(newUri, false, null)); michael@0: do_check_false(isHostInMozHosts(NetUtil.newURI("http://test.mozilla.org"), false, null)); michael@0: }); michael@0: michael@0: add_task(function test_bookmark_removal() michael@0: { michael@0: let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, michael@0: PlacesUtils.bookmarks.DEFAULT_INDEX); michael@0: let newUri = NetUtil.newURI(NEW_URL); michael@0: PlacesUtils.bookmarks.removeItem(itemId); michael@0: yield promiseClearHistory(); michael@0: michael@0: do_check_false(isHostInMozHosts(newUri, false, null)); michael@0: }); michael@0: michael@0: add_task(function test_moz_hosts_typed_update() michael@0: { michael@0: const TEST_URI = NetUtil.newURI("http://typed.mozilla.com"); michael@0: let places = [{ uri: TEST_URI michael@0: , title: "test for " + TEST_URI.spec michael@0: }, michael@0: { uri: TEST_URI michael@0: , title: "test for " + TEST_URI.spec michael@0: , transition: TRANSITION_TYPED michael@0: }]; michael@0: michael@0: yield promiseAddVisits(places); michael@0: michael@0: do_check_true(isHostInMozHosts(TEST_URI, true, null)); michael@0: yield promiseClearHistory(); michael@0: }); michael@0: michael@0: add_task(function test_moz_hosts_www_remove() michael@0: { michael@0: function test_removal(aURIToRemove, aURIToKeep, aCallback) { michael@0: let places = [{ uri: aURIToRemove michael@0: , title: "test for " + aURIToRemove.spec michael@0: , transition: TRANSITION_TYPED michael@0: }, michael@0: { uri: aURIToKeep michael@0: , title: "test for " + aURIToKeep.spec michael@0: , transition: TRANSITION_TYPED michael@0: }]; michael@0: michael@0: yield promiseAddVisits(places); michael@0: print("removing " + aURIToRemove.spec + " keeping " + aURIToKeep); michael@0: dump_table("moz_hosts"); michael@0: dump_table("moz_places"); michael@0: PlacesUtils.history.removePage(aURIToRemove); michael@0: let prefix = /www/.test(aURIToKeep.spec) ? "www." : null; michael@0: dump_table("moz_hosts"); michael@0: dump_table("moz_places"); michael@0: do_check_true(isHostInMozHosts(aURIToKeep, true, prefix)); michael@0: } michael@0: michael@0: const TEST_URI = NetUtil.newURI("http://rem.mozilla.com"); michael@0: const TEST_WWW_URI = NetUtil.newURI("http://www.rem.mozilla.com"); michael@0: yield test_removal(TEST_URI, TEST_WWW_URI); michael@0: yield test_removal(TEST_WWW_URI, TEST_URI); michael@0: yield promiseClearHistory(); michael@0: }); michael@0: michael@0: add_task(function test_moz_hosts_ftp_matchall() michael@0: { michael@0: const TEST_URI_1 = NetUtil.newURI("ftp://www.mozilla.com/"); michael@0: const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/"); michael@0: michael@0: yield promiseAddVisits([{ uri: TEST_URI_1, transition: TRANSITION_TYPED }, michael@0: { uri: TEST_URI_2, transition: TRANSITION_TYPED }]); michael@0: michael@0: do_check_true(isHostInMozHosts(TEST_URI_1, true, "ftp://")); michael@0: }); michael@0: michael@0: add_task(function test_moz_hosts_ftp_not_matchall() michael@0: { michael@0: const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/"); michael@0: const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/"); michael@0: michael@0: yield promiseAddVisits([{ uri: TEST_URI_1, transition: TRANSITION_TYPED }, michael@0: { uri: TEST_URI_2, transition: TRANSITION_TYPED }]); michael@0: michael@0: do_check_true(isHostInMozHosts(TEST_URI_1, true, null)); michael@0: }); michael@0: michael@0: add_task(function test_moz_hosts_update_2() michael@0: { michael@0: // Check that updating trigger takes into account prefixes for different michael@0: // rev_hosts. michael@0: const TEST_URI_1 = NetUtil.newURI("https://www.google.it/"); michael@0: const TEST_URI_2 = NetUtil.newURI("https://google.it/"); michael@0: let places = [{ uri: TEST_URI_1 michael@0: , transition: TRANSITION_TYPED michael@0: }, michael@0: { uri: TEST_URI_2 michael@0: }]; michael@0: yield promiseAddVisits(places); michael@0: michael@0: do_check_true(isHostInMozHosts(TEST_URI_1, true, "https://www.")); michael@0: }); michael@0: michael@0: function run_test() michael@0: { michael@0: run_next_test(); michael@0: }