services/sync/tests/unit/test_places_guid_downgrade.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_places_guid_downgrade.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,215 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://gre/modules/PlacesUtils.jsm");
     1.8 +Cu.import("resource://services-common/async.js");
     1.9 +Cu.import("resource://services-sync/util.js");
    1.10 +Cu.import("resource://services-sync/engines.js");
    1.11 +Cu.import("resource://services-sync/engines/history.js");
    1.12 +Cu.import("resource://services-sync/engines/bookmarks.js");
    1.13 +Cu.import("resource://services-sync/service.js");
    1.14 +
    1.15 +const kDBName = "places.sqlite";
    1.16 +const storageSvc = Cc["@mozilla.org/storage/service;1"]
    1.17 +                     .getService(Ci.mozIStorageService);
    1.18 +
    1.19 +const fxuri = Utils.makeURI("http://getfirefox.com/");
    1.20 +const tburi = Utils.makeURI("http://getthunderbird.com/");
    1.21 +
    1.22 +function setPlacesDatabase(aFileName) {
    1.23 +  removePlacesDatabase();
    1.24 +  _("Copying over places.sqlite.");
    1.25 +  let file = do_get_file(aFileName);
    1.26 +  file.copyTo(gSyncProfile, kDBName);
    1.27 +}
    1.28 +
    1.29 +function removePlacesDatabase() {
    1.30 +  _("Removing places.sqlite.");
    1.31 +  let file = gSyncProfile.clone();
    1.32 +  file.append(kDBName);
    1.33 +  try {
    1.34 +    file.remove(false);
    1.35 +  } catch (ex) {
    1.36 +    // Windows is awesome. NOT.
    1.37 +  }
    1.38 +}
    1.39 +
    1.40 +Svc.Obs.add("places-shutdown", function () {
    1.41 +  do_timeout(0, removePlacesDatabase);
    1.42 +});
    1.43 +
    1.44 +
    1.45 +// Verify initial database state. Function borrowed from places tests.
    1.46 +add_test(function test_initial_state() {
    1.47 +  _("Verify initial setup: v11 database is available");
    1.48 +
    1.49 +  // Mostly sanity checks our starting DB to make sure it's setup as we expect
    1.50 +  // it to be.
    1.51 +  let dbFile = gSyncProfile.clone();
    1.52 +  dbFile.append(kDBName);
    1.53 +  let db = storageSvc.openUnsharedDatabase(dbFile);
    1.54 +
    1.55 +  let stmt = db.createStatement("PRAGMA journal_mode");
    1.56 +  do_check_true(stmt.executeStep());
    1.57 +  // WAL journal mode should have been unset this database when it was migrated
    1.58 +  // down to v10.
    1.59 +  do_check_neq(stmt.getString(0).toLowerCase(), "wal");
    1.60 +  stmt.finalize();
    1.61 +
    1.62 +  do_check_true(db.indexExists("moz_bookmarks_guid_uniqueindex"));
    1.63 +  do_check_true(db.indexExists("moz_places_guid_uniqueindex"));
    1.64 +
    1.65 +  // There should be a non-zero amount of bookmarks without a guid.
    1.66 +  stmt = db.createStatement(
    1.67 +    "SELECT COUNT(1) "
    1.68 +  + "FROM moz_bookmarks "
    1.69 +  + "WHERE guid IS NULL "
    1.70 +  );
    1.71 +  do_check_true(stmt.executeStep());
    1.72 +  do_check_neq(stmt.getInt32(0), 0);
    1.73 +  stmt.finalize();
    1.74 +
    1.75 +  // There should be a non-zero amount of places without a guid.
    1.76 +  stmt = db.createStatement(
    1.77 +    "SELECT COUNT(1) "
    1.78 +  + "FROM moz_places "
    1.79 +  + "WHERE guid IS NULL "
    1.80 +  );
    1.81 +  do_check_true(stmt.executeStep());
    1.82 +  do_check_neq(stmt.getInt32(0), 0);
    1.83 +  stmt.finalize();
    1.84 +
    1.85 +  // Check our schema version to make sure it is actually at 10.
    1.86 +  do_check_eq(db.schemaVersion, 10);
    1.87 +
    1.88 +  db.close();
    1.89 +
    1.90 +  run_next_test();
    1.91 +});
    1.92 +
    1.93 +add_test(function test_history_guids() {
    1.94 +  let engine = new HistoryEngine(Service);
    1.95 +  let store = engine._store;
    1.96 +
    1.97 +  let places = [
    1.98 +    {
    1.99 +      uri: fxuri,
   1.100 +      title: "Get Firefox!",
   1.101 +      visits: [{
   1.102 +        visitDate: Date.now() * 1000,
   1.103 +        transitionType: Ci.nsINavHistoryService.TRANSITION_LINK
   1.104 +      }]
   1.105 +    },
   1.106 +    {
   1.107 +      uri: tburi,
   1.108 +      title: "Get Thunderbird!",
   1.109 +      visits: [{
   1.110 +        visitDate: Date.now() * 1000,
   1.111 +        transitionType: Ci.nsINavHistoryService.TRANSITION_LINK
   1.112 +      }]
   1.113 +    }
   1.114 +  ];
   1.115 +  PlacesUtils.asyncHistory.updatePlaces(places, {
   1.116 +    handleError: function handleError() {
   1.117 +      do_throw("Unexpected error in adding visit.");
   1.118 +    },
   1.119 +    handleResult: function handleResult() {},
   1.120 +    handleCompletion: onVisitAdded
   1.121 +  });
   1.122 +
   1.123 +  function onVisitAdded() {
   1.124 +    let fxguid = store.GUIDForUri(fxuri, true);
   1.125 +    let tbguid = store.GUIDForUri(tburi, true);
   1.126 +    dump("fxguid: " + fxguid + "\n");
   1.127 +    dump("tbguid: " + tbguid + "\n");
   1.128 +
   1.129 +    _("History: Verify GUIDs are added to the guid column.");
   1.130 +    let connection = PlacesUtils.history
   1.131 +                                .QueryInterface(Ci.nsPIPlacesDatabase)
   1.132 +                                .DBConnection;
   1.133 +    let stmt = connection.createAsyncStatement(
   1.134 +      "SELECT id FROM moz_places WHERE guid = :guid");
   1.135 +
   1.136 +    stmt.params.guid = fxguid;
   1.137 +    let result = Async.querySpinningly(stmt, ["id"]);
   1.138 +    do_check_eq(result.length, 1);
   1.139 +
   1.140 +    stmt.params.guid = tbguid;
   1.141 +    result = Async.querySpinningly(stmt, ["id"]);
   1.142 +    do_check_eq(result.length, 1);
   1.143 +    stmt.finalize();
   1.144 +
   1.145 +    _("History: Verify GUIDs weren't added to annotations.");
   1.146 +    stmt = connection.createAsyncStatement(
   1.147 +      "SELECT a.content AS guid FROM moz_annos a WHERE guid = :guid");
   1.148 +
   1.149 +    stmt.params.guid = fxguid;
   1.150 +    result = Async.querySpinningly(stmt, ["guid"]);
   1.151 +    do_check_eq(result.length, 0);
   1.152 +
   1.153 +    stmt.params.guid = tbguid;
   1.154 +    result = Async.querySpinningly(stmt, ["guid"]);
   1.155 +    do_check_eq(result.length, 0);
   1.156 +    stmt.finalize();
   1.157 +
   1.158 +    run_next_test();
   1.159 +  }
   1.160 +});
   1.161 +
   1.162 +add_test(function test_bookmark_guids() {
   1.163 +  let engine = new BookmarksEngine(Service);
   1.164 +  let store = engine._store;
   1.165 +
   1.166 +  let fxid = PlacesUtils.bookmarks.insertBookmark(
   1.167 +    PlacesUtils.bookmarks.toolbarFolder,
   1.168 +    fxuri,
   1.169 +    PlacesUtils.bookmarks.DEFAULT_INDEX,
   1.170 +    "Get Firefox!");
   1.171 +  let tbid = PlacesUtils.bookmarks.insertBookmark(
   1.172 +    PlacesUtils.bookmarks.toolbarFolder,
   1.173 +    tburi,
   1.174 +    PlacesUtils.bookmarks.DEFAULT_INDEX,
   1.175 +    "Get Thunderbird!");
   1.176 +
   1.177 +  let fxguid = store.GUIDForId(fxid);
   1.178 +  let tbguid = store.GUIDForId(tbid);
   1.179 +
   1.180 +  _("Bookmarks: Verify GUIDs are added to the guid column.");
   1.181 +  let connection = PlacesUtils.history
   1.182 +                              .QueryInterface(Ci.nsPIPlacesDatabase)
   1.183 +                              .DBConnection;
   1.184 +  let stmt = connection.createAsyncStatement(
   1.185 +    "SELECT id FROM moz_bookmarks WHERE guid = :guid");
   1.186 +
   1.187 +  stmt.params.guid = fxguid;
   1.188 +  let result = Async.querySpinningly(stmt, ["id"]);
   1.189 +  do_check_eq(result.length, 1);
   1.190 +  do_check_eq(result[0].id, fxid);
   1.191 +
   1.192 +  stmt.params.guid = tbguid;
   1.193 +  result = Async.querySpinningly(stmt, ["id"]);
   1.194 +  do_check_eq(result.length, 1);
   1.195 +  do_check_eq(result[0].id, tbid);
   1.196 +  stmt.finalize();
   1.197 +
   1.198 +  _("Bookmarks: Verify GUIDs weren't added to annotations.");
   1.199 +  stmt = connection.createAsyncStatement(
   1.200 +    "SELECT a.content AS guid FROM moz_items_annos a WHERE guid = :guid");
   1.201 +
   1.202 +  stmt.params.guid = fxguid;
   1.203 +  result = Async.querySpinningly(stmt, ["guid"]);
   1.204 +  do_check_eq(result.length, 0);
   1.205 +
   1.206 +  stmt.params.guid = tbguid;
   1.207 +  result = Async.querySpinningly(stmt, ["guid"]);
   1.208 +  do_check_eq(result.length, 0);
   1.209 +  stmt.finalize();
   1.210 +
   1.211 +  run_next_test();
   1.212 +});
   1.213 +
   1.214 +function run_test() {
   1.215 +  setPlacesDatabase("places_v10_from_v11.sqlite");
   1.216 +
   1.217 +  run_next_test();
   1.218 +}

mercurial