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: // Inspired by the Places infrastructure in head_bookmarks.js michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: const Cu = Components.utils; michael@0: michael@0: var HandlerServiceTest = { michael@0: //**************************************************************************// michael@0: // Convenience Getters michael@0: michael@0: __dirSvc: null, michael@0: get _dirSvc() { michael@0: if (!this.__dirSvc) michael@0: this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties). michael@0: QueryInterface(Ci.nsIDirectoryService); michael@0: return this.__dirSvc; michael@0: }, michael@0: michael@0: __consoleSvc: null, michael@0: get _consoleSvc() { michael@0: if (!this.__consoleSvc) michael@0: this.__consoleSvc = Cc["@mozilla.org/consoleservice;1"]. michael@0: getService(Ci.nsIConsoleService); michael@0: return this.__consoleSvc; michael@0: }, michael@0: michael@0: michael@0: //**************************************************************************// michael@0: // nsISupports michael@0: michael@0: interfaces: [Ci.nsIDirectoryServiceProvider, Ci.nsISupports], michael@0: michael@0: QueryInterface: function HandlerServiceTest_QueryInterface(iid) { michael@0: if (!this.interfaces.some( function(v) { return iid.equals(v) } )) michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: return this; michael@0: }, michael@0: michael@0: michael@0: //**************************************************************************// michael@0: // Initialization & Destruction michael@0: michael@0: init: function HandlerServiceTest_init() { michael@0: // Register ourselves as a directory provider for the datasource file michael@0: // if there isn't one registered already. michael@0: try { michael@0: this._dirSvc.get("UMimTyp", Ci.nsIFile); michael@0: } catch (ex) { michael@0: this._dirSvc.registerProvider(this); michael@0: this._providerRegistered = true; michael@0: } michael@0: michael@0: // Delete the existing datasource file, if any, so we start from scratch. michael@0: // We also do this after finishing the tests, so there shouldn't be an old michael@0: // file lying around, but just in case we delete it here as well. michael@0: this._deleteDatasourceFile(); michael@0: michael@0: // Turn on logging so we can troubleshoot problems with the tests. michael@0: var prefBranch = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: prefBranch.setBoolPref("browser.contentHandling.log", true); michael@0: }, michael@0: michael@0: destroy: function HandlerServiceTest_destroy() { michael@0: // Delete the existing datasource file, if any, so we don't leave test files michael@0: // lying around and we start from scratch the next time. michael@0: this._deleteDatasourceFile(); michael@0: // Unregister the directory service provider michael@0: if (this._providerRegistered) michael@0: this._dirSvc.unregisterProvider(this); michael@0: }, michael@0: michael@0: michael@0: //**************************************************************************// michael@0: // nsIDirectoryServiceProvider michael@0: michael@0: getFile: function HandlerServiceTest_getFile(property, persistent) { michael@0: this.log("getFile: requesting " + property); michael@0: michael@0: persistent.value = true; michael@0: michael@0: if (property == "UMimTyp") { michael@0: var datasourceFile = this._dirSvc.get("CurProcD", Ci.nsIFile); michael@0: datasourceFile.append("mimeTypes.rdf"); michael@0: return datasourceFile; michael@0: } michael@0: michael@0: // This causes extraneous errors to show up in the log when the directory michael@0: // service asks us first for CurProcD and MozBinD. I wish there was a way michael@0: // to suppress those errors. michael@0: this.log("the following NS_ERROR_FAILURE exception in " + michael@0: "nsIDirectoryServiceProvider::getFile is expected, " + michael@0: "as we don't provide the '" + property + "' file"); michael@0: throw Cr.NS_ERROR_FAILURE; michael@0: }, michael@0: michael@0: michael@0: //**************************************************************************// michael@0: // Utilities michael@0: michael@0: /** michael@0: * Delete the datasource file. michael@0: */ michael@0: _deleteDatasourceFile: function HandlerServiceTest__deleteDatasourceFile() { michael@0: var file = this._dirSvc.get("UMimTyp", Ci.nsIFile); michael@0: if (file.exists()) michael@0: file.remove(false); michael@0: }, michael@0: michael@0: /** michael@0: * Get the contents of the datasource as a serialized string. Useful for michael@0: * debugging problems with test failures, i.e.: michael@0: * michael@0: * HandlerServiceTest.log(HandlerServiceTest.getDatasourceContents()); michael@0: * michael@0: * @returns {string} the serialized datasource michael@0: */ michael@0: getDatasourceContents: function HandlerServiceTest_getDatasourceContents() { michael@0: var rdf = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService); michael@0: michael@0: var ioService = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: var fileHandler = ioService.getProtocolHandler("file"). michael@0: QueryInterface(Ci.nsIFileProtocolHandler); michael@0: var fileURL = fileHandler.getURLSpecFromFile(this.getDatasourceFile()); michael@0: var ds = rdf.GetDataSourceBlocking(fileURL); michael@0: michael@0: var outputStream = { michael@0: data: "", michael@0: close: function() {}, michael@0: flush: function() {}, michael@0: write: function (buffer,count) { michael@0: this.data += buffer; michael@0: return count; michael@0: }, michael@0: writeFrom: function (stream,count) {}, michael@0: isNonBlocking: false michael@0: }; michael@0: michael@0: ds.QueryInterface(Components.interfaces.nsIRDFXMLSource); michael@0: ds.Serialize(outputStream); michael@0: michael@0: return outputStream.data; michael@0: }, michael@0: michael@0: /** michael@0: * Log a message to the console and the test log. michael@0: */ michael@0: log: function HandlerServiceTest_log(message) { michael@0: message = "*** HandlerServiceTest: " + message; michael@0: this._consoleSvc.logStringMessage(message); michael@0: print(message); michael@0: } michael@0: michael@0: }; michael@0: michael@0: HandlerServiceTest.init();