uriloader/exthandler/tests/unit/head_handlerService.js

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

mercurial