2012-01-19 03:31:23 -08:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This file tests the validity of various triggers that add remove hosts from moz_hosts
|
|
|
|
*/
|
|
|
|
|
2012-02-17 05:24:58 -08:00
|
|
|
XPCOMUtils.defineLazyServiceGetter(this, "gHistory",
|
|
|
|
"@mozilla.org/browser/history;1",
|
|
|
|
"mozIAsyncHistory");
|
|
|
|
|
2012-01-19 03:31:23 -08:00
|
|
|
// add some visits and remove them, add a bookmark,
|
|
|
|
// change its uri, then remove it, and
|
|
|
|
// for each change check that moz_hosts has correctly been updated.
|
|
|
|
|
|
|
|
function isHostInMozPlaces(aURI)
|
|
|
|
{
|
|
|
|
let stmt = DBConn().createStatement(
|
|
|
|
"SELECT url "
|
|
|
|
+ "FROM moz_places "
|
|
|
|
+ "WHERE url = :host"
|
|
|
|
);
|
|
|
|
let result = false;
|
|
|
|
stmt.params.host = aURI.spec;
|
|
|
|
while(stmt.executeStep()) {
|
|
|
|
if (stmt.row.url == aURI.spec) {
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
stmt.finalize();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-01 05:38:39 -07:00
|
|
|
function isHostInMozHosts(aURI, aTyped, aPrefix)
|
2012-01-19 03:31:23 -08:00
|
|
|
{
|
|
|
|
let stmt = DBConn().createStatement(
|
2012-06-01 05:38:39 -07:00
|
|
|
"SELECT host, typed, prefix "
|
2012-01-19 03:31:23 -08:00
|
|
|
+ "FROM moz_hosts "
|
2012-04-06 16:17:51 -07:00
|
|
|
+ "WHERE host = fixup_url(:host) "
|
|
|
|
+ "AND frecency NOTNULL "
|
2012-01-19 03:31:23 -08:00
|
|
|
);
|
|
|
|
let result = false;
|
|
|
|
stmt.params.host = aURI.host;
|
2012-02-22 03:23:14 -08:00
|
|
|
if (stmt.executeStep()) {
|
2012-06-01 05:38:39 -07:00
|
|
|
result = aTyped == stmt.row.typed && aPrefix == stmt.row.prefix;
|
2012-01-19 03:31:23 -08:00
|
|
|
}
|
|
|
|
stmt.finalize();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
let urls = [{uri: NetUtil.newURI("http://visit1.mozilla.org"),
|
|
|
|
expected: "visit1.mozilla.org",
|
2012-06-01 05:38:39 -07:00
|
|
|
typed: 0,
|
|
|
|
prefix: null
|
2012-01-19 03:31:23 -08:00
|
|
|
},
|
|
|
|
{uri: NetUtil.newURI("http://visit2.mozilla.org"),
|
|
|
|
expected: "visit2.mozilla.org",
|
2012-06-01 05:38:39 -07:00
|
|
|
typed: 0,
|
|
|
|
prefix: null
|
2012-01-19 03:31:23 -08:00
|
|
|
},
|
|
|
|
{uri: NetUtil.newURI("http://www.foo.mozilla.org"),
|
|
|
|
expected: "foo.mozilla.org",
|
2012-06-01 05:38:39 -07:00
|
|
|
typed: 1,
|
|
|
|
prefix: "www."
|
2012-01-19 03:31:23 -08:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
let places = [];
|
|
|
|
urls.forEach(function(url) {
|
|
|
|
let place = {
|
|
|
|
uri: url.uri,
|
|
|
|
title: "test for " + url.url,
|
|
|
|
visits: [
|
2012-02-17 05:24:58 -08:00
|
|
|
new VisitInfo(url.typed ? TRANSITION_TYPED : undefined),
|
2012-01-19 03:31:23 -08:00
|
|
|
],
|
|
|
|
};
|
|
|
|
places.push(place);
|
|
|
|
});
|
|
|
|
|
|
|
|
gHistory.updatePlaces(places, {
|
|
|
|
handleResult: function () {
|
|
|
|
},
|
|
|
|
handleError: function () {
|
|
|
|
do_throw("gHistory.updatePlaces() failed");
|
|
|
|
},
|
|
|
|
handleCompletion: function () {
|
2012-06-01 05:38:39 -07:00
|
|
|
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));
|
2012-01-19 03:31:23 -08:00
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_remove_places()
|
|
|
|
{
|
|
|
|
for (let idx in urls) {
|
|
|
|
PlacesUtils.history.removePage(urls[idx].uri);
|
|
|
|
}
|
|
|
|
|
2012-11-11 05:01:49 -08:00
|
|
|
promiseClearHistory().then(function (){
|
2012-01-19 03:31:23 -08:00
|
|
|
for (let idx in urls) {
|
2012-06-01 05:38:39 -07:00
|
|
|
do_check_false(isHostInMozHosts(urls[idx].uri, urls[idx].typed, urls[idx].prefix));
|
2012-01-19 03:31:23 -08:00
|
|
|
}
|
|
|
|
run_next_test();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function test_bookmark_changes()
|
|
|
|
{
|
|
|
|
let testUri = NetUtil.newURI("http://test.mozilla.org");
|
|
|
|
|
|
|
|
let itemId = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
|
|
|
|
testUri,
|
|
|
|
PlacesUtils.bookmarks.DEFAULT_INDEX,
|
|
|
|
"bookmark title");
|
|
|
|
|
|
|
|
do_check_true(isHostInMozPlaces(testUri));
|
|
|
|
|
|
|
|
// Change the hostname
|
|
|
|
PlacesUtils.bookmarks.changeBookmarkURI(itemId, NetUtil.newURI(NEW_URL));
|
|
|
|
|
2012-11-11 05:01:49 -08:00
|
|
|
promiseClearHistory().then(function (){
|
2012-01-19 03:31:23 -08:00
|
|
|
let newUri = NetUtil.newURI(NEW_URL);
|
|
|
|
do_check_true(isHostInMozPlaces(newUri));
|
2012-06-01 05:38:39 -07:00
|
|
|
do_check_true(isHostInMozHosts(newUri, false, null));
|
|
|
|
do_check_false(isHostInMozHosts(NetUtil.newURI("http://test.mozilla.org"), false, null));
|
2012-01-19 03:31:23 -08:00
|
|
|
run_next_test();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2012-11-11 05:01:49 -08:00
|
|
|
promiseClearHistory().then(function (){
|
2012-06-01 05:38:39 -07:00
|
|
|
do_check_false(isHostInMozHosts(newUri, false, null));
|
2012-01-19 03:31:23 -08:00
|
|
|
run_next_test();
|
|
|
|
});
|
2012-02-17 05:24:58 -08:00
|
|
|
}
|
2012-01-19 03:31:23 -08:00
|
|
|
|
2012-02-17 05:24:58 -08:00
|
|
|
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)
|
|
|
|
]
|
|
|
|
}];
|
|
|
|
|
|
|
|
gHistory.updatePlaces(places, {
|
|
|
|
handleResult: function () {
|
|
|
|
},
|
|
|
|
handleError: function () {
|
|
|
|
do_throw("gHistory.updatePlaces() failed");
|
|
|
|
},
|
|
|
|
handleCompletion: function () {
|
2012-06-01 05:38:39 -07:00
|
|
|
do_check_true(isHostInMozHosts(TEST_URI, true, null));
|
2012-02-17 05:24:58 -08:00
|
|
|
run_next_test();
|
|
|
|
}
|
|
|
|
});
|
2012-01-19 03:31:23 -08:00
|
|
|
}
|
|
|
|
|
2012-02-22 03:23:14 -08:00
|
|
|
function test_moz_hosts_www_remove()
|
|
|
|
{
|
|
|
|
function test_removal(aURIToRemove, aURIToKeep, aCallback) {
|
|
|
|
let places = [{ uri: aURIToRemove
|
|
|
|
, title: "test for " + aURIToRemove.spec
|
|
|
|
, visits: [ new VisitInfo() ]
|
|
|
|
},
|
|
|
|
{ uri: aURIToKeep
|
|
|
|
, title: "test for " + aURIToKeep.spec
|
|
|
|
, visits: [ new VisitInfo() ]
|
|
|
|
}];
|
|
|
|
|
|
|
|
gHistory.updatePlaces(places, {
|
|
|
|
handleResult: function () {
|
|
|
|
},
|
|
|
|
handleError: function () {
|
|
|
|
do_throw("gHistory.updatePlaces() failed");
|
|
|
|
},
|
|
|
|
handleCompletion: function () {
|
|
|
|
PlacesUtils.history.removePage(aURIToRemove);
|
2012-06-01 05:38:39 -07:00
|
|
|
let prefix = /www/.test(aURIToKeep.spec) ? "www." : null;
|
|
|
|
do_check_true(isHostInMozHosts(aURIToKeep, false, prefix));
|
2012-11-11 05:01:49 -08:00
|
|
|
promiseClearHistory().then(aCallback);
|
2012-02-22 03:23:14 -08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2012-11-11 05:01:49 -08:00
|
|
|
promiseClearHistory().then(run_next_test);
|
2012-02-22 03:23:14 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-01-19 03:31:23 -08:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//// Test Runner
|
|
|
|
|
|
|
|
[
|
|
|
|
test_moz_hosts_update,
|
|
|
|
test_remove_places,
|
|
|
|
test_bookmark_changes,
|
|
|
|
test_bookmark_removal,
|
2012-02-17 05:24:58 -08:00
|
|
|
test_moz_hosts_typed_update,
|
2012-02-22 03:23:14 -08:00
|
|
|
test_moz_hosts_www_remove,
|
2012-01-19 03:31:23 -08:00
|
|
|
].forEach(add_test);
|
|
|
|
|
|
|
|
function run_test()
|
|
|
|
{
|
|
|
|
run_next_test();
|
|
|
|
}
|