1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/unit/test_hosts_triggers.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,222 @@ 1.4 +/* Any copyright is dedicated to the Public Domain. 1.5 + http://creativecommons.org/publicdomain/zero/1.0/ */ 1.6 + 1.7 +/** 1.8 + * This file tests the validity of various triggers that add remove hosts from moz_hosts 1.9 + */ 1.10 + 1.11 +XPCOMUtils.defineLazyServiceGetter(this, "gHistory", 1.12 + "@mozilla.org/browser/history;1", 1.13 + "mozIAsyncHistory"); 1.14 + 1.15 +// add some visits and remove them, add a bookmark, 1.16 +// change its uri, then remove it, and 1.17 +// for each change check that moz_hosts has correctly been updated. 1.18 + 1.19 +function isHostInMozPlaces(aURI) 1.20 +{ 1.21 + let stmt = DBConn().createStatement( 1.22 + "SELECT url " 1.23 + + "FROM moz_places " 1.24 + + "WHERE url = :host" 1.25 + ); 1.26 + let result = false; 1.27 + stmt.params.host = aURI.spec; 1.28 + while(stmt.executeStep()) { 1.29 + if (stmt.row.url == aURI.spec) { 1.30 + result = true; 1.31 + break; 1.32 + } 1.33 + } 1.34 + stmt.finalize(); 1.35 + return result; 1.36 +} 1.37 + 1.38 +function isHostInMozHosts(aURI, aTyped, aPrefix) 1.39 +{ 1.40 + let stmt = DBConn().createStatement( 1.41 + "SELECT host, typed, prefix " 1.42 + + "FROM moz_hosts " 1.43 + + "WHERE host = fixup_url(:host) " 1.44 + + "AND frecency NOTNULL " 1.45 + ); 1.46 + let result = false; 1.47 + stmt.params.host = aURI.host; 1.48 + if (stmt.executeStep()) { 1.49 + result = aTyped == stmt.row.typed && aPrefix == stmt.row.prefix; 1.50 + } 1.51 + stmt.finalize(); 1.52 + return result; 1.53 +} 1.54 + 1.55 +let urls = [{uri: NetUtil.newURI("http://visit1.mozilla.org"), 1.56 + expected: "visit1.mozilla.org", 1.57 + typed: 0, 1.58 + prefix: null 1.59 + }, 1.60 + {uri: NetUtil.newURI("http://visit2.mozilla.org"), 1.61 + expected: "visit2.mozilla.org", 1.62 + typed: 0, 1.63 + prefix: null 1.64 + }, 1.65 + {uri: NetUtil.newURI("http://www.foo.mozilla.org"), 1.66 + expected: "foo.mozilla.org", 1.67 + typed: 1, 1.68 + prefix: "www." 1.69 + }, 1.70 + ]; 1.71 + 1.72 +const NEW_URL = "http://different.mozilla.org/"; 1.73 + 1.74 +add_task(function test_moz_hosts_update() 1.75 +{ 1.76 + let places = []; 1.77 + urls.forEach(function(url) { 1.78 + let place = { uri: url.uri, 1.79 + title: "test for " + url.url, 1.80 + transition: url.typed ? TRANSITION_TYPED : undefined }; 1.81 + places.push(place); 1.82 + }); 1.83 + 1.84 + yield promiseAddVisits(places); 1.85 + 1.86 + do_check_true(isHostInMozHosts(urls[0].uri, urls[0].typed, urls[0].prefix)); 1.87 + do_check_true(isHostInMozHosts(urls[1].uri, urls[1].typed, urls[1].prefix)); 1.88 + do_check_true(isHostInMozHosts(urls[2].uri, urls[2].typed, urls[2].prefix)); 1.89 +}); 1.90 + 1.91 +add_task(function test_remove_places() 1.92 +{ 1.93 + for (let idx in urls) { 1.94 + PlacesUtils.history.removePage(urls[idx].uri); 1.95 + } 1.96 + 1.97 + yield promiseClearHistory(); 1.98 + 1.99 + for (let idx in urls) { 1.100 + do_check_false(isHostInMozHosts(urls[idx].uri, urls[idx].typed, urls[idx].prefix)); 1.101 + } 1.102 +}); 1.103 + 1.104 +add_task(function test_bookmark_changes() 1.105 +{ 1.106 + let testUri = NetUtil.newURI("http://test.mozilla.org"); 1.107 + 1.108 + let itemId = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId, 1.109 + testUri, 1.110 + PlacesUtils.bookmarks.DEFAULT_INDEX, 1.111 + "bookmark title"); 1.112 + 1.113 + do_check_true(isHostInMozPlaces(testUri)); 1.114 + 1.115 + // Change the hostname 1.116 + PlacesUtils.bookmarks.changeBookmarkURI(itemId, NetUtil.newURI(NEW_URL)); 1.117 + 1.118 + yield promiseClearHistory(); 1.119 + 1.120 + let newUri = NetUtil.newURI(NEW_URL); 1.121 + do_check_true(isHostInMozPlaces(newUri)); 1.122 + do_check_true(isHostInMozHosts(newUri, false, null)); 1.123 + do_check_false(isHostInMozHosts(NetUtil.newURI("http://test.mozilla.org"), false, null)); 1.124 +}); 1.125 + 1.126 +add_task(function test_bookmark_removal() 1.127 +{ 1.128 + let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId, 1.129 + PlacesUtils.bookmarks.DEFAULT_INDEX); 1.130 + let newUri = NetUtil.newURI(NEW_URL); 1.131 + PlacesUtils.bookmarks.removeItem(itemId); 1.132 + yield promiseClearHistory(); 1.133 + 1.134 + do_check_false(isHostInMozHosts(newUri, false, null)); 1.135 +}); 1.136 + 1.137 +add_task(function test_moz_hosts_typed_update() 1.138 +{ 1.139 + const TEST_URI = NetUtil.newURI("http://typed.mozilla.com"); 1.140 + let places = [{ uri: TEST_URI 1.141 + , title: "test for " + TEST_URI.spec 1.142 + }, 1.143 + { uri: TEST_URI 1.144 + , title: "test for " + TEST_URI.spec 1.145 + , transition: TRANSITION_TYPED 1.146 + }]; 1.147 + 1.148 + yield promiseAddVisits(places); 1.149 + 1.150 + do_check_true(isHostInMozHosts(TEST_URI, true, null)); 1.151 + yield promiseClearHistory(); 1.152 +}); 1.153 + 1.154 +add_task(function test_moz_hosts_www_remove() 1.155 +{ 1.156 + function test_removal(aURIToRemove, aURIToKeep, aCallback) { 1.157 + let places = [{ uri: aURIToRemove 1.158 + , title: "test for " + aURIToRemove.spec 1.159 + , transition: TRANSITION_TYPED 1.160 + }, 1.161 + { uri: aURIToKeep 1.162 + , title: "test for " + aURIToKeep.spec 1.163 + , transition: TRANSITION_TYPED 1.164 + }]; 1.165 + 1.166 + yield promiseAddVisits(places); 1.167 + print("removing " + aURIToRemove.spec + " keeping " + aURIToKeep); 1.168 + dump_table("moz_hosts"); 1.169 + dump_table("moz_places"); 1.170 + PlacesUtils.history.removePage(aURIToRemove); 1.171 + let prefix = /www/.test(aURIToKeep.spec) ? "www." : null; 1.172 + dump_table("moz_hosts"); 1.173 + dump_table("moz_places"); 1.174 + do_check_true(isHostInMozHosts(aURIToKeep, true, prefix)); 1.175 + } 1.176 + 1.177 + const TEST_URI = NetUtil.newURI("http://rem.mozilla.com"); 1.178 + const TEST_WWW_URI = NetUtil.newURI("http://www.rem.mozilla.com"); 1.179 + yield test_removal(TEST_URI, TEST_WWW_URI); 1.180 + yield test_removal(TEST_WWW_URI, TEST_URI); 1.181 + yield promiseClearHistory(); 1.182 +}); 1.183 + 1.184 +add_task(function test_moz_hosts_ftp_matchall() 1.185 +{ 1.186 + const TEST_URI_1 = NetUtil.newURI("ftp://www.mozilla.com/"); 1.187 + const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/"); 1.188 + 1.189 + yield promiseAddVisits([{ uri: TEST_URI_1, transition: TRANSITION_TYPED }, 1.190 + { uri: TEST_URI_2, transition: TRANSITION_TYPED }]); 1.191 + 1.192 + do_check_true(isHostInMozHosts(TEST_URI_1, true, "ftp://")); 1.193 +}); 1.194 + 1.195 +add_task(function test_moz_hosts_ftp_not_matchall() 1.196 +{ 1.197 + const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/"); 1.198 + const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/"); 1.199 + 1.200 + yield promiseAddVisits([{ uri: TEST_URI_1, transition: TRANSITION_TYPED }, 1.201 + { uri: TEST_URI_2, transition: TRANSITION_TYPED }]); 1.202 + 1.203 + do_check_true(isHostInMozHosts(TEST_URI_1, true, null)); 1.204 +}); 1.205 + 1.206 +add_task(function test_moz_hosts_update_2() 1.207 +{ 1.208 + // Check that updating trigger takes into account prefixes for different 1.209 + // rev_hosts. 1.210 + const TEST_URI_1 = NetUtil.newURI("https://www.google.it/"); 1.211 + const TEST_URI_2 = NetUtil.newURI("https://google.it/"); 1.212 + let places = [{ uri: TEST_URI_1 1.213 + , transition: TRANSITION_TYPED 1.214 + }, 1.215 + { uri: TEST_URI_2 1.216 + }]; 1.217 + yield promiseAddVisits(places); 1.218 + 1.219 + do_check_true(isHostInMozHosts(TEST_URI_1, true, "https://www.")); 1.220 +}); 1.221 + 1.222 +function run_test() 1.223 +{ 1.224 + run_next_test(); 1.225 +}