michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim:set ts=2 sw=2 sts=2 et: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const NS_PLACES_INIT_COMPLETE_TOPIC = "places-init-complete"; michael@0: const NS_PLACES_DATABASE_LOCKED_TOPIC = "places-database-locked"; michael@0: michael@0: function run_test() { michael@0: do_test_pending(); michael@0: michael@0: // Create an observer for the Places notifications michael@0: var os = Cc["@mozilla.org/observer-service;1"]. michael@0: getService(Ci.nsIObserverService); michael@0: var observer = { michael@0: _lockedNotificationReceived: false, michael@0: observe: function thn_observe(aSubject, aTopic, aData) michael@0: { michael@0: switch (aTopic) { michael@0: case NS_PLACES_INIT_COMPLETE_TOPIC: michael@0: do_check_true(this._lockedNotificationReceived); michael@0: os.removeObserver(this, NS_PLACES_INIT_COMPLETE_TOPIC); michael@0: os.removeObserver(this, NS_PLACES_DATABASE_LOCKED_TOPIC); michael@0: do_test_finished(); michael@0: break; michael@0: case NS_PLACES_DATABASE_LOCKED_TOPIC: michael@0: if (this._lockedNotificationReceived) michael@0: do_throw("Locked notification should be observed only one time"); michael@0: this._lockedNotificationReceived = true; michael@0: break; michael@0: } michael@0: } michael@0: }; michael@0: os.addObserver(observer, NS_PLACES_INIT_COMPLETE_TOPIC, false); michael@0: os.addObserver(observer, NS_PLACES_DATABASE_LOCKED_TOPIC, false); michael@0: michael@0: // Create a dummy places.sqlite and open an unshared connection on it michael@0: var dirSvc = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties); michael@0: var db = dirSvc.get('ProfD', Ci.nsIFile); michael@0: db.append("places.sqlite"); michael@0: var storage = Cc["@mozilla.org/storage/service;1"]. michael@0: getService(Ci.mozIStorageService); michael@0: var dbConn = storage.openUnsharedDatabase(db); michael@0: do_check_true(db.exists()); michael@0: michael@0: // We need an exclusive lock on the db michael@0: dbConn.executeSimpleSQL("PRAGMA locking_mode = EXCLUSIVE"); michael@0: // Exclusive locking is lazy applied, we need to make a write to activate it michael@0: dbConn.executeSimpleSQL("PRAGMA USER_VERSION = 1"); michael@0: michael@0: // Try to create history service while the db is locked michael@0: try { michael@0: var hs1 = Cc["@mozilla.org/browser/nav-history-service;1"]. michael@0: getService(Ci.nsINavHistoryService); michael@0: do_throw("Creating an instance of history service on a locked db should throw"); michael@0: } catch (ex) {} michael@0: michael@0: // Close our connection and try to cleanup the file (could fail on Windows) michael@0: dbConn.close(); michael@0: if (db.exists()) { michael@0: try { michael@0: db.remove(false); michael@0: } catch(e) { dump("Unable to remove dummy places.sqlite"); } michael@0: } michael@0: michael@0: // Create history service correctly michael@0: try { michael@0: var hs2 = Cc["@mozilla.org/browser/nav-history-service;1"]. michael@0: getService(Ci.nsINavHistoryService); michael@0: } catch (ex) { michael@0: do_throw("Creating an instance of history service on a not locked db should not throw"); michael@0: } michael@0: }