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