toolkit/components/places/tests/unit/test_hosts_triggers.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:c09c47141df9
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
3
4 /**
5 * This file tests the validity of various triggers that add remove hosts from moz_hosts
6 */
7
8 XPCOMUtils.defineLazyServiceGetter(this, "gHistory",
9 "@mozilla.org/browser/history;1",
10 "mozIAsyncHistory");
11
12 // add some visits and remove them, add a bookmark,
13 // change its uri, then remove it, and
14 // for each change check that moz_hosts has correctly been updated.
15
16 function isHostInMozPlaces(aURI)
17 {
18 let stmt = DBConn().createStatement(
19 "SELECT url "
20 + "FROM moz_places "
21 + "WHERE url = :host"
22 );
23 let result = false;
24 stmt.params.host = aURI.spec;
25 while(stmt.executeStep()) {
26 if (stmt.row.url == aURI.spec) {
27 result = true;
28 break;
29 }
30 }
31 stmt.finalize();
32 return result;
33 }
34
35 function isHostInMozHosts(aURI, aTyped, aPrefix)
36 {
37 let stmt = DBConn().createStatement(
38 "SELECT host, typed, prefix "
39 + "FROM moz_hosts "
40 + "WHERE host = fixup_url(:host) "
41 + "AND frecency NOTNULL "
42 );
43 let result = false;
44 stmt.params.host = aURI.host;
45 if (stmt.executeStep()) {
46 result = aTyped == stmt.row.typed && aPrefix == stmt.row.prefix;
47 }
48 stmt.finalize();
49 return result;
50 }
51
52 let urls = [{uri: NetUtil.newURI("http://visit1.mozilla.org"),
53 expected: "visit1.mozilla.org",
54 typed: 0,
55 prefix: null
56 },
57 {uri: NetUtil.newURI("http://visit2.mozilla.org"),
58 expected: "visit2.mozilla.org",
59 typed: 0,
60 prefix: null
61 },
62 {uri: NetUtil.newURI("http://www.foo.mozilla.org"),
63 expected: "foo.mozilla.org",
64 typed: 1,
65 prefix: "www."
66 },
67 ];
68
69 const NEW_URL = "http://different.mozilla.org/";
70
71 add_task(function test_moz_hosts_update()
72 {
73 let places = [];
74 urls.forEach(function(url) {
75 let place = { uri: url.uri,
76 title: "test for " + url.url,
77 transition: url.typed ? TRANSITION_TYPED : undefined };
78 places.push(place);
79 });
80
81 yield promiseAddVisits(places);
82
83 do_check_true(isHostInMozHosts(urls[0].uri, urls[0].typed, urls[0].prefix));
84 do_check_true(isHostInMozHosts(urls[1].uri, urls[1].typed, urls[1].prefix));
85 do_check_true(isHostInMozHosts(urls[2].uri, urls[2].typed, urls[2].prefix));
86 });
87
88 add_task(function test_remove_places()
89 {
90 for (let idx in urls) {
91 PlacesUtils.history.removePage(urls[idx].uri);
92 }
93
94 yield promiseClearHistory();
95
96 for (let idx in urls) {
97 do_check_false(isHostInMozHosts(urls[idx].uri, urls[idx].typed, urls[idx].prefix));
98 }
99 });
100
101 add_task(function test_bookmark_changes()
102 {
103 let testUri = NetUtil.newURI("http://test.mozilla.org");
104
105 let itemId = PlacesUtils.bookmarks.insertBookmark(PlacesUtils.unfiledBookmarksFolderId,
106 testUri,
107 PlacesUtils.bookmarks.DEFAULT_INDEX,
108 "bookmark title");
109
110 do_check_true(isHostInMozPlaces(testUri));
111
112 // Change the hostname
113 PlacesUtils.bookmarks.changeBookmarkURI(itemId, NetUtil.newURI(NEW_URL));
114
115 yield promiseClearHistory();
116
117 let newUri = NetUtil.newURI(NEW_URL);
118 do_check_true(isHostInMozPlaces(newUri));
119 do_check_true(isHostInMozHosts(newUri, false, null));
120 do_check_false(isHostInMozHosts(NetUtil.newURI("http://test.mozilla.org"), false, null));
121 });
122
123 add_task(function test_bookmark_removal()
124 {
125 let itemId = PlacesUtils.bookmarks.getIdForItemAt(PlacesUtils.unfiledBookmarksFolderId,
126 PlacesUtils.bookmarks.DEFAULT_INDEX);
127 let newUri = NetUtil.newURI(NEW_URL);
128 PlacesUtils.bookmarks.removeItem(itemId);
129 yield promiseClearHistory();
130
131 do_check_false(isHostInMozHosts(newUri, false, null));
132 });
133
134 add_task(function test_moz_hosts_typed_update()
135 {
136 const TEST_URI = NetUtil.newURI("http://typed.mozilla.com");
137 let places = [{ uri: TEST_URI
138 , title: "test for " + TEST_URI.spec
139 },
140 { uri: TEST_URI
141 , title: "test for " + TEST_URI.spec
142 , transition: TRANSITION_TYPED
143 }];
144
145 yield promiseAddVisits(places);
146
147 do_check_true(isHostInMozHosts(TEST_URI, true, null));
148 yield promiseClearHistory();
149 });
150
151 add_task(function test_moz_hosts_www_remove()
152 {
153 function test_removal(aURIToRemove, aURIToKeep, aCallback) {
154 let places = [{ uri: aURIToRemove
155 , title: "test for " + aURIToRemove.spec
156 , transition: TRANSITION_TYPED
157 },
158 { uri: aURIToKeep
159 , title: "test for " + aURIToKeep.spec
160 , transition: TRANSITION_TYPED
161 }];
162
163 yield promiseAddVisits(places);
164 print("removing " + aURIToRemove.spec + " keeping " + aURIToKeep);
165 dump_table("moz_hosts");
166 dump_table("moz_places");
167 PlacesUtils.history.removePage(aURIToRemove);
168 let prefix = /www/.test(aURIToKeep.spec) ? "www." : null;
169 dump_table("moz_hosts");
170 dump_table("moz_places");
171 do_check_true(isHostInMozHosts(aURIToKeep, true, prefix));
172 }
173
174 const TEST_URI = NetUtil.newURI("http://rem.mozilla.com");
175 const TEST_WWW_URI = NetUtil.newURI("http://www.rem.mozilla.com");
176 yield test_removal(TEST_URI, TEST_WWW_URI);
177 yield test_removal(TEST_WWW_URI, TEST_URI);
178 yield promiseClearHistory();
179 });
180
181 add_task(function test_moz_hosts_ftp_matchall()
182 {
183 const TEST_URI_1 = NetUtil.newURI("ftp://www.mozilla.com/");
184 const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/");
185
186 yield promiseAddVisits([{ uri: TEST_URI_1, transition: TRANSITION_TYPED },
187 { uri: TEST_URI_2, transition: TRANSITION_TYPED }]);
188
189 do_check_true(isHostInMozHosts(TEST_URI_1, true, "ftp://"));
190 });
191
192 add_task(function test_moz_hosts_ftp_not_matchall()
193 {
194 const TEST_URI_1 = NetUtil.newURI("http://mozilla.com/");
195 const TEST_URI_2 = NetUtil.newURI("ftp://mozilla.com/");
196
197 yield promiseAddVisits([{ uri: TEST_URI_1, transition: TRANSITION_TYPED },
198 { uri: TEST_URI_2, transition: TRANSITION_TYPED }]);
199
200 do_check_true(isHostInMozHosts(TEST_URI_1, true, null));
201 });
202
203 add_task(function test_moz_hosts_update_2()
204 {
205 // Check that updating trigger takes into account prefixes for different
206 // rev_hosts.
207 const TEST_URI_1 = NetUtil.newURI("https://www.google.it/");
208 const TEST_URI_2 = NetUtil.newURI("https://google.it/");
209 let places = [{ uri: TEST_URI_1
210 , transition: TRANSITION_TYPED
211 },
212 { uri: TEST_URI_2
213 }];
214 yield promiseAddVisits(places);
215
216 do_check_true(isHostInMozHosts(TEST_URI_1, true, "https://www."));
217 });
218
219 function run_test()
220 {
221 run_next_test();
222 }

mercurial