uriloader/exthandler/tests/unit/head_handlerService.js

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 // Inspired by the Places infrastructure in head_bookmarks.js
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cr = Components.results;
michael@0 10 const Cu = Components.utils;
michael@0 11
michael@0 12 var HandlerServiceTest = {
michael@0 13 //**************************************************************************//
michael@0 14 // Convenience Getters
michael@0 15
michael@0 16 __dirSvc: null,
michael@0 17 get _dirSvc() {
michael@0 18 if (!this.__dirSvc)
michael@0 19 this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"].
michael@0 20 getService(Ci.nsIProperties).
michael@0 21 QueryInterface(Ci.nsIDirectoryService);
michael@0 22 return this.__dirSvc;
michael@0 23 },
michael@0 24
michael@0 25 __consoleSvc: null,
michael@0 26 get _consoleSvc() {
michael@0 27 if (!this.__consoleSvc)
michael@0 28 this.__consoleSvc = Cc["@mozilla.org/consoleservice;1"].
michael@0 29 getService(Ci.nsIConsoleService);
michael@0 30 return this.__consoleSvc;
michael@0 31 },
michael@0 32
michael@0 33
michael@0 34 //**************************************************************************//
michael@0 35 // nsISupports
michael@0 36
michael@0 37 interfaces: [Ci.nsIDirectoryServiceProvider, Ci.nsISupports],
michael@0 38
michael@0 39 QueryInterface: function HandlerServiceTest_QueryInterface(iid) {
michael@0 40 if (!this.interfaces.some( function(v) { return iid.equals(v) } ))
michael@0 41 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 42 return this;
michael@0 43 },
michael@0 44
michael@0 45
michael@0 46 //**************************************************************************//
michael@0 47 // Initialization & Destruction
michael@0 48
michael@0 49 init: function HandlerServiceTest_init() {
michael@0 50 // Register ourselves as a directory provider for the datasource file
michael@0 51 // if there isn't one registered already.
michael@0 52 try {
michael@0 53 this._dirSvc.get("UMimTyp", Ci.nsIFile);
michael@0 54 } catch (ex) {
michael@0 55 this._dirSvc.registerProvider(this);
michael@0 56 this._providerRegistered = true;
michael@0 57 }
michael@0 58
michael@0 59 // Delete the existing datasource file, if any, so we start from scratch.
michael@0 60 // We also do this after finishing the tests, so there shouldn't be an old
michael@0 61 // file lying around, but just in case we delete it here as well.
michael@0 62 this._deleteDatasourceFile();
michael@0 63
michael@0 64 // Turn on logging so we can troubleshoot problems with the tests.
michael@0 65 var prefBranch = Cc["@mozilla.org/preferences-service;1"].
michael@0 66 getService(Ci.nsIPrefBranch);
michael@0 67 prefBranch.setBoolPref("browser.contentHandling.log", true);
michael@0 68 },
michael@0 69
michael@0 70 destroy: function HandlerServiceTest_destroy() {
michael@0 71 // Delete the existing datasource file, if any, so we don't leave test files
michael@0 72 // lying around and we start from scratch the next time.
michael@0 73 this._deleteDatasourceFile();
michael@0 74 // Unregister the directory service provider
michael@0 75 if (this._providerRegistered)
michael@0 76 this._dirSvc.unregisterProvider(this);
michael@0 77 },
michael@0 78
michael@0 79
michael@0 80 //**************************************************************************//
michael@0 81 // nsIDirectoryServiceProvider
michael@0 82
michael@0 83 getFile: function HandlerServiceTest_getFile(property, persistent) {
michael@0 84 this.log("getFile: requesting " + property);
michael@0 85
michael@0 86 persistent.value = true;
michael@0 87
michael@0 88 if (property == "UMimTyp") {
michael@0 89 var datasourceFile = this._dirSvc.get("CurProcD", Ci.nsIFile);
michael@0 90 datasourceFile.append("mimeTypes.rdf");
michael@0 91 return datasourceFile;
michael@0 92 }
michael@0 93
michael@0 94 // This causes extraneous errors to show up in the log when the directory
michael@0 95 // service asks us first for CurProcD and MozBinD. I wish there was a way
michael@0 96 // to suppress those errors.
michael@0 97 this.log("the following NS_ERROR_FAILURE exception in " +
michael@0 98 "nsIDirectoryServiceProvider::getFile is expected, " +
michael@0 99 "as we don't provide the '" + property + "' file");
michael@0 100 throw Cr.NS_ERROR_FAILURE;
michael@0 101 },
michael@0 102
michael@0 103
michael@0 104 //**************************************************************************//
michael@0 105 // Utilities
michael@0 106
michael@0 107 /**
michael@0 108 * Delete the datasource file.
michael@0 109 */
michael@0 110 _deleteDatasourceFile: function HandlerServiceTest__deleteDatasourceFile() {
michael@0 111 var file = this._dirSvc.get("UMimTyp", Ci.nsIFile);
michael@0 112 if (file.exists())
michael@0 113 file.remove(false);
michael@0 114 },
michael@0 115
michael@0 116 /**
michael@0 117 * Get the contents of the datasource as a serialized string. Useful for
michael@0 118 * debugging problems with test failures, i.e.:
michael@0 119 *
michael@0 120 * HandlerServiceTest.log(HandlerServiceTest.getDatasourceContents());
michael@0 121 *
michael@0 122 * @returns {string} the serialized datasource
michael@0 123 */
michael@0 124 getDatasourceContents: function HandlerServiceTest_getDatasourceContents() {
michael@0 125 var rdf = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
michael@0 126
michael@0 127 var ioService = Cc["@mozilla.org/network/io-service;1"].
michael@0 128 getService(Ci.nsIIOService);
michael@0 129 var fileHandler = ioService.getProtocolHandler("file").
michael@0 130 QueryInterface(Ci.nsIFileProtocolHandler);
michael@0 131 var fileURL = fileHandler.getURLSpecFromFile(this.getDatasourceFile());
michael@0 132 var ds = rdf.GetDataSourceBlocking(fileURL);
michael@0 133
michael@0 134 var outputStream = {
michael@0 135 data: "",
michael@0 136 close: function() {},
michael@0 137 flush: function() {},
michael@0 138 write: function (buffer,count) {
michael@0 139 this.data += buffer;
michael@0 140 return count;
michael@0 141 },
michael@0 142 writeFrom: function (stream,count) {},
michael@0 143 isNonBlocking: false
michael@0 144 };
michael@0 145
michael@0 146 ds.QueryInterface(Components.interfaces.nsIRDFXMLSource);
michael@0 147 ds.Serialize(outputStream);
michael@0 148
michael@0 149 return outputStream.data;
michael@0 150 },
michael@0 151
michael@0 152 /**
michael@0 153 * Log a message to the console and the test log.
michael@0 154 */
michael@0 155 log: function HandlerServiceTest_log(message) {
michael@0 156 message = "*** HandlerServiceTest: " + message;
michael@0 157 this._consoleSvc.logStringMessage(message);
michael@0 158 print(message);
michael@0 159 }
michael@0 160
michael@0 161 };
michael@0 162
michael@0 163 HandlerServiceTest.init();

mercurial