|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Cu.import("resource://gre/modules/PlacesUtils.jsm"); |
|
5 Cu.import("resource://services-common/async.js"); |
|
6 Cu.import("resource://services-sync/util.js"); |
|
7 Cu.import("resource://services-sync/engines.js"); |
|
8 Cu.import("resource://services-sync/engines/history.js"); |
|
9 Cu.import("resource://services-sync/engines/bookmarks.js"); |
|
10 Cu.import("resource://services-sync/service.js"); |
|
11 |
|
12 const kDBName = "places.sqlite"; |
|
13 const storageSvc = Cc["@mozilla.org/storage/service;1"] |
|
14 .getService(Ci.mozIStorageService); |
|
15 |
|
16 const fxuri = Utils.makeURI("http://getfirefox.com/"); |
|
17 const tburi = Utils.makeURI("http://getthunderbird.com/"); |
|
18 |
|
19 function setPlacesDatabase(aFileName) { |
|
20 removePlacesDatabase(); |
|
21 _("Copying over places.sqlite."); |
|
22 let file = do_get_file(aFileName); |
|
23 file.copyTo(gSyncProfile, kDBName); |
|
24 } |
|
25 |
|
26 function removePlacesDatabase() { |
|
27 _("Removing places.sqlite."); |
|
28 let file = gSyncProfile.clone(); |
|
29 file.append(kDBName); |
|
30 try { |
|
31 file.remove(false); |
|
32 } catch (ex) { |
|
33 // Windows is awesome. NOT. |
|
34 } |
|
35 } |
|
36 |
|
37 Svc.Obs.add("places-shutdown", function () { |
|
38 do_timeout(0, removePlacesDatabase); |
|
39 }); |
|
40 |
|
41 |
|
42 // Verify initial database state. Function borrowed from places tests. |
|
43 add_test(function test_initial_state() { |
|
44 _("Verify initial setup: v11 database is available"); |
|
45 |
|
46 // Mostly sanity checks our starting DB to make sure it's setup as we expect |
|
47 // it to be. |
|
48 let dbFile = gSyncProfile.clone(); |
|
49 dbFile.append(kDBName); |
|
50 let db = storageSvc.openUnsharedDatabase(dbFile); |
|
51 |
|
52 let stmt = db.createStatement("PRAGMA journal_mode"); |
|
53 do_check_true(stmt.executeStep()); |
|
54 // WAL journal mode should have been unset this database when it was migrated |
|
55 // down to v10. |
|
56 do_check_neq(stmt.getString(0).toLowerCase(), "wal"); |
|
57 stmt.finalize(); |
|
58 |
|
59 do_check_true(db.indexExists("moz_bookmarks_guid_uniqueindex")); |
|
60 do_check_true(db.indexExists("moz_places_guid_uniqueindex")); |
|
61 |
|
62 // There should be a non-zero amount of bookmarks without a guid. |
|
63 stmt = db.createStatement( |
|
64 "SELECT COUNT(1) " |
|
65 + "FROM moz_bookmarks " |
|
66 + "WHERE guid IS NULL " |
|
67 ); |
|
68 do_check_true(stmt.executeStep()); |
|
69 do_check_neq(stmt.getInt32(0), 0); |
|
70 stmt.finalize(); |
|
71 |
|
72 // There should be a non-zero amount of places without a guid. |
|
73 stmt = db.createStatement( |
|
74 "SELECT COUNT(1) " |
|
75 + "FROM moz_places " |
|
76 + "WHERE guid IS NULL " |
|
77 ); |
|
78 do_check_true(stmt.executeStep()); |
|
79 do_check_neq(stmt.getInt32(0), 0); |
|
80 stmt.finalize(); |
|
81 |
|
82 // Check our schema version to make sure it is actually at 10. |
|
83 do_check_eq(db.schemaVersion, 10); |
|
84 |
|
85 db.close(); |
|
86 |
|
87 run_next_test(); |
|
88 }); |
|
89 |
|
90 add_test(function test_history_guids() { |
|
91 let engine = new HistoryEngine(Service); |
|
92 let store = engine._store; |
|
93 |
|
94 let places = [ |
|
95 { |
|
96 uri: fxuri, |
|
97 title: "Get Firefox!", |
|
98 visits: [{ |
|
99 visitDate: Date.now() * 1000, |
|
100 transitionType: Ci.nsINavHistoryService.TRANSITION_LINK |
|
101 }] |
|
102 }, |
|
103 { |
|
104 uri: tburi, |
|
105 title: "Get Thunderbird!", |
|
106 visits: [{ |
|
107 visitDate: Date.now() * 1000, |
|
108 transitionType: Ci.nsINavHistoryService.TRANSITION_LINK |
|
109 }] |
|
110 } |
|
111 ]; |
|
112 PlacesUtils.asyncHistory.updatePlaces(places, { |
|
113 handleError: function handleError() { |
|
114 do_throw("Unexpected error in adding visit."); |
|
115 }, |
|
116 handleResult: function handleResult() {}, |
|
117 handleCompletion: onVisitAdded |
|
118 }); |
|
119 |
|
120 function onVisitAdded() { |
|
121 let fxguid = store.GUIDForUri(fxuri, true); |
|
122 let tbguid = store.GUIDForUri(tburi, true); |
|
123 dump("fxguid: " + fxguid + "\n"); |
|
124 dump("tbguid: " + tbguid + "\n"); |
|
125 |
|
126 _("History: Verify GUIDs are added to the guid column."); |
|
127 let connection = PlacesUtils.history |
|
128 .QueryInterface(Ci.nsPIPlacesDatabase) |
|
129 .DBConnection; |
|
130 let stmt = connection.createAsyncStatement( |
|
131 "SELECT id FROM moz_places WHERE guid = :guid"); |
|
132 |
|
133 stmt.params.guid = fxguid; |
|
134 let result = Async.querySpinningly(stmt, ["id"]); |
|
135 do_check_eq(result.length, 1); |
|
136 |
|
137 stmt.params.guid = tbguid; |
|
138 result = Async.querySpinningly(stmt, ["id"]); |
|
139 do_check_eq(result.length, 1); |
|
140 stmt.finalize(); |
|
141 |
|
142 _("History: Verify GUIDs weren't added to annotations."); |
|
143 stmt = connection.createAsyncStatement( |
|
144 "SELECT a.content AS guid FROM moz_annos a WHERE guid = :guid"); |
|
145 |
|
146 stmt.params.guid = fxguid; |
|
147 result = Async.querySpinningly(stmt, ["guid"]); |
|
148 do_check_eq(result.length, 0); |
|
149 |
|
150 stmt.params.guid = tbguid; |
|
151 result = Async.querySpinningly(stmt, ["guid"]); |
|
152 do_check_eq(result.length, 0); |
|
153 stmt.finalize(); |
|
154 |
|
155 run_next_test(); |
|
156 } |
|
157 }); |
|
158 |
|
159 add_test(function test_bookmark_guids() { |
|
160 let engine = new BookmarksEngine(Service); |
|
161 let store = engine._store; |
|
162 |
|
163 let fxid = PlacesUtils.bookmarks.insertBookmark( |
|
164 PlacesUtils.bookmarks.toolbarFolder, |
|
165 fxuri, |
|
166 PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
167 "Get Firefox!"); |
|
168 let tbid = PlacesUtils.bookmarks.insertBookmark( |
|
169 PlacesUtils.bookmarks.toolbarFolder, |
|
170 tburi, |
|
171 PlacesUtils.bookmarks.DEFAULT_INDEX, |
|
172 "Get Thunderbird!"); |
|
173 |
|
174 let fxguid = store.GUIDForId(fxid); |
|
175 let tbguid = store.GUIDForId(tbid); |
|
176 |
|
177 _("Bookmarks: Verify GUIDs are added to the guid column."); |
|
178 let connection = PlacesUtils.history |
|
179 .QueryInterface(Ci.nsPIPlacesDatabase) |
|
180 .DBConnection; |
|
181 let stmt = connection.createAsyncStatement( |
|
182 "SELECT id FROM moz_bookmarks WHERE guid = :guid"); |
|
183 |
|
184 stmt.params.guid = fxguid; |
|
185 let result = Async.querySpinningly(stmt, ["id"]); |
|
186 do_check_eq(result.length, 1); |
|
187 do_check_eq(result[0].id, fxid); |
|
188 |
|
189 stmt.params.guid = tbguid; |
|
190 result = Async.querySpinningly(stmt, ["id"]); |
|
191 do_check_eq(result.length, 1); |
|
192 do_check_eq(result[0].id, tbid); |
|
193 stmt.finalize(); |
|
194 |
|
195 _("Bookmarks: Verify GUIDs weren't added to annotations."); |
|
196 stmt = connection.createAsyncStatement( |
|
197 "SELECT a.content AS guid FROM moz_items_annos a WHERE guid = :guid"); |
|
198 |
|
199 stmt.params.guid = fxguid; |
|
200 result = Async.querySpinningly(stmt, ["guid"]); |
|
201 do_check_eq(result.length, 0); |
|
202 |
|
203 stmt.params.guid = tbguid; |
|
204 result = Async.querySpinningly(stmt, ["guid"]); |
|
205 do_check_eq(result.length, 0); |
|
206 stmt.finalize(); |
|
207 |
|
208 run_next_test(); |
|
209 }); |
|
210 |
|
211 function run_test() { |
|
212 setPlacesDatabase("places_v10_from_v11.sqlite"); |
|
213 |
|
214 run_next_test(); |
|
215 } |