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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/places/tests/unit/test_history_notifications.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,75 @@
     1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim:set ts=2 sw=2 sts=2 et: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +const NS_PLACES_INIT_COMPLETE_TOPIC = "places-init-complete";
    1.11 +const NS_PLACES_DATABASE_LOCKED_TOPIC = "places-database-locked";
    1.12 +
    1.13 +function run_test() {
    1.14 +  do_test_pending();
    1.15 +
    1.16 +  // Create an observer for the Places notifications
    1.17 +  var os = Cc["@mozilla.org/observer-service;1"].
    1.18 +         getService(Ci.nsIObserverService);
    1.19 +  var observer = {
    1.20 +    _lockedNotificationReceived: false,
    1.21 +    observe: function thn_observe(aSubject, aTopic, aData)
    1.22 +    {
    1.23 +      switch (aTopic) {
    1.24 +        case NS_PLACES_INIT_COMPLETE_TOPIC:
    1.25 +          do_check_true(this._lockedNotificationReceived);
    1.26 +          os.removeObserver(this, NS_PLACES_INIT_COMPLETE_TOPIC);
    1.27 +          os.removeObserver(this, NS_PLACES_DATABASE_LOCKED_TOPIC);
    1.28 +          do_test_finished();
    1.29 +          break;
    1.30 +        case NS_PLACES_DATABASE_LOCKED_TOPIC:
    1.31 +          if (this._lockedNotificationReceived)
    1.32 +            do_throw("Locked notification should be observed only one time");
    1.33 +          this._lockedNotificationReceived = true;
    1.34 +          break;
    1.35 +      }
    1.36 +    }
    1.37 +  };
    1.38 +  os.addObserver(observer, NS_PLACES_INIT_COMPLETE_TOPIC, false);
    1.39 +  os.addObserver(observer, NS_PLACES_DATABASE_LOCKED_TOPIC, false);
    1.40 +
    1.41 +  // Create a dummy places.sqlite and open an unshared connection on it
    1.42 +  var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
    1.43 +               getService(Ci.nsIProperties);
    1.44 +  var db = dirSvc.get('ProfD', Ci.nsIFile);
    1.45 +  db.append("places.sqlite");
    1.46 +  var storage = Cc["@mozilla.org/storage/service;1"].
    1.47 +                getService(Ci.mozIStorageService);
    1.48 +  var dbConn = storage.openUnsharedDatabase(db);
    1.49 +  do_check_true(db.exists());
    1.50 +
    1.51 +  // We need an exclusive lock on the db
    1.52 +  dbConn.executeSimpleSQL("PRAGMA locking_mode = EXCLUSIVE");
    1.53 +  // Exclusive locking is lazy applied, we need to make a write to activate it
    1.54 +  dbConn.executeSimpleSQL("PRAGMA USER_VERSION = 1");
    1.55 +
    1.56 +  // Try to create history service while the db is locked
    1.57 +  try {
    1.58 +    var hs1 = Cc["@mozilla.org/browser/nav-history-service;1"].
    1.59 +              getService(Ci.nsINavHistoryService);
    1.60 +    do_throw("Creating an instance of history service on a locked db should throw");
    1.61 +  } catch (ex) {}
    1.62 +
    1.63 +  // Close our connection and try to cleanup the file (could fail on Windows)
    1.64 +  dbConn.close();
    1.65 +  if (db.exists()) {
    1.66 +    try {
    1.67 +      db.remove(false);
    1.68 +    } catch(e) { dump("Unable to remove dummy places.sqlite"); }
    1.69 +  }
    1.70 +
    1.71 +  // Create history service correctly
    1.72 +  try {
    1.73 +    var hs2 = Cc["@mozilla.org/browser/nav-history-service;1"].
    1.74 +              getService(Ci.nsINavHistoryService);
    1.75 +  } catch (ex) {
    1.76 +    do_throw("Creating an instance of history service on a not locked db should not throw");
    1.77 +  }
    1.78 +}

mercurial