Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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 | function run_test() { |
michael@0 | 6 | //**************************************************************************// |
michael@0 | 7 | // Constants |
michael@0 | 8 | |
michael@0 | 9 | const handlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"]. |
michael@0 | 10 | getService(Ci.nsIHandlerService); |
michael@0 | 11 | |
michael@0 | 12 | const mimeSvc = Cc["@mozilla.org/mime;1"]. |
michael@0 | 13 | getService(Ci.nsIMIMEService); |
michael@0 | 14 | |
michael@0 | 15 | const protoSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"]. |
michael@0 | 16 | getService(Ci.nsIExternalProtocolService); |
michael@0 | 17 | |
michael@0 | 18 | const prefSvc = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 19 | getService(Ci.nsIPrefService); |
michael@0 | 20 | |
michael@0 | 21 | const ioService = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 22 | getService(Ci.nsIIOService); |
michael@0 | 23 | |
michael@0 | 24 | const env = Cc["@mozilla.org/process/environment;1"]. |
michael@0 | 25 | getService(Components.interfaces.nsIEnvironment); |
michael@0 | 26 | |
michael@0 | 27 | const rootPrefBranch = prefSvc.getBranch(""); |
michael@0 | 28 | |
michael@0 | 29 | let noMailto = false; |
michael@0 | 30 | let isWindows = ("@mozilla.org/windows-registry-key;1" in Components.classes); |
michael@0 | 31 | if (isWindows) { |
michael@0 | 32 | // Check mailto handler from registry. |
michael@0 | 33 | // If registry entry is nothing, no mailto handler |
michael@0 | 34 | let regSvc = Cc["@mozilla.org/windows-registry-key;1"]. |
michael@0 | 35 | createInstance(Ci.nsIWindowsRegKey); |
michael@0 | 36 | try { |
michael@0 | 37 | regSvc.open(regSvc.ROOT_KEY_CLASSES_ROOT, |
michael@0 | 38 | "mailto", |
michael@0 | 39 | regSvc.ACCESS_READ); |
michael@0 | 40 | noMailto = false; |
michael@0 | 41 | } catch (ex) { |
michael@0 | 42 | noMailto = true; |
michael@0 | 43 | } |
michael@0 | 44 | regSvc.close(); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | //**************************************************************************// |
michael@0 | 48 | // Sample Data |
michael@0 | 49 | |
michael@0 | 50 | // It doesn't matter whether or not this nsIFile is actually executable, |
michael@0 | 51 | // only that it has a path and exists. Since we don't know any executable |
michael@0 | 52 | // that exists on all platforms (except possibly the application being |
michael@0 | 53 | // tested, but there doesn't seem to be a way to get a reference to that |
michael@0 | 54 | // from the directory service), we use the temporary directory itself. |
michael@0 | 55 | var executable = HandlerServiceTest._dirSvc.get("TmpD", Ci.nsIFile); |
michael@0 | 56 | // XXX We could, of course, create an actual executable in the directory: |
michael@0 | 57 | //executable.append("localhandler"); |
michael@0 | 58 | //if (!executable.exists()) |
michael@0 | 59 | // executable.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0755); |
michael@0 | 60 | |
michael@0 | 61 | var localHandler = { |
michael@0 | 62 | name: "Local Handler", |
michael@0 | 63 | executable: executable, |
michael@0 | 64 | interfaces: [Ci.nsIHandlerApp, Ci.nsILocalHandlerApp, Ci.nsISupports], |
michael@0 | 65 | QueryInterface: function(iid) { |
michael@0 | 66 | if (!this.interfaces.some( function(v) { return iid.equals(v) } )) |
michael@0 | 67 | throw Cr.NS_ERROR_NO_INTERFACE; |
michael@0 | 68 | return this; |
michael@0 | 69 | } |
michael@0 | 70 | }; |
michael@0 | 71 | |
michael@0 | 72 | var webHandler = Cc["@mozilla.org/uriloader/web-handler-app;1"]. |
michael@0 | 73 | createInstance(Ci.nsIWebHandlerApp); |
michael@0 | 74 | webHandler.name = "Web Handler"; |
michael@0 | 75 | webHandler.uriTemplate = "http://www.example.com/?%s"; |
michael@0 | 76 | |
michael@0 | 77 | // FIXME: these tests create and manipulate enough variables that it would |
michael@0 | 78 | // make sense to move each test into its own scope so we don't run the risk |
michael@0 | 79 | // of one test stomping on another's data. |
michael@0 | 80 | |
michael@0 | 81 | |
michael@0 | 82 | //**************************************************************************// |
michael@0 | 83 | // Test Default Properties |
michael@0 | 84 | |
michael@0 | 85 | // Get a handler info for a MIME type that neither the application nor |
michael@0 | 86 | // the OS knows about and make sure its properties are set to the proper |
michael@0 | 87 | // default values. |
michael@0 | 88 | |
michael@0 | 89 | var handlerInfo = mimeSvc.getFromTypeAndExtension("nonexistent/type", null); |
michael@0 | 90 | |
michael@0 | 91 | // Make sure it's also an nsIHandlerInfo. |
michael@0 | 92 | do_check_true(handlerInfo instanceof Ci.nsIHandlerInfo); |
michael@0 | 93 | |
michael@0 | 94 | do_check_eq(handlerInfo.type, "nonexistent/type"); |
michael@0 | 95 | |
michael@0 | 96 | // Deprecated property, but we should still make sure it's set correctly. |
michael@0 | 97 | do_check_eq(handlerInfo.MIMEType, "nonexistent/type"); |
michael@0 | 98 | |
michael@0 | 99 | // These properties are the ones the handler service knows how to store. |
michael@0 | 100 | do_check_eq(handlerInfo.preferredAction, Ci.nsIHandlerInfo.saveToDisk); |
michael@0 | 101 | do_check_eq(handlerInfo.preferredApplicationHandler, null); |
michael@0 | 102 | do_check_eq(handlerInfo.possibleApplicationHandlers.length, 0); |
michael@0 | 103 | do_check_true(handlerInfo.alwaysAskBeforeHandling); |
michael@0 | 104 | |
michael@0 | 105 | // These properties are initialized to default values by the service, |
michael@0 | 106 | // so we might as well make sure they're initialized to the right defaults. |
michael@0 | 107 | do_check_eq(handlerInfo.description, ""); |
michael@0 | 108 | do_check_eq(handlerInfo.hasDefaultHandler, false); |
michael@0 | 109 | do_check_eq(handlerInfo.defaultDescription, ""); |
michael@0 | 110 | |
michael@0 | 111 | // test some default protocol info properties |
michael@0 | 112 | var haveDefaultHandlersVersion = false; |
michael@0 | 113 | try { |
michael@0 | 114 | // If we have a defaultHandlersVersion pref, then assume that we're in the |
michael@0 | 115 | // firefox tree and that we'll also have default handlers. |
michael@0 | 116 | // Bug 395131 has been filed to make this test work more generically |
michael@0 | 117 | // by providing our own prefs for this test rather than this icky |
michael@0 | 118 | // special casing. |
michael@0 | 119 | rootPrefBranch.getCharPref("gecko.handlerService.defaultHandlersVersion"); |
michael@0 | 120 | haveDefaultHandlersVersion = true; |
michael@0 | 121 | } catch (ex) {} |
michael@0 | 122 | |
michael@0 | 123 | const kExternalWarningDefault = |
michael@0 | 124 | "network.protocol-handler.warn-external-default"; |
michael@0 | 125 | prefSvc.setBoolPref(kExternalWarningDefault, true); |
michael@0 | 126 | |
michael@0 | 127 | // XXX add more thorough protocol info property checking |
michael@0 | 128 | |
michael@0 | 129 | // no OS default handler exists |
michael@0 | 130 | var protoInfo = protoSvc.getProtocolHandlerInfo("x-moz-rheet"); |
michael@0 | 131 | do_check_eq(protoInfo.preferredAction, protoInfo.alwaysAsk); |
michael@0 | 132 | do_check_true(protoInfo.alwaysAskBeforeHandling); |
michael@0 | 133 | |
michael@0 | 134 | // OS default exists, injected default does not exist, |
michael@0 | 135 | // explicit warning pref: false |
michael@0 | 136 | const kExternalWarningPrefPrefix = "network.protocol-handler.warn-external."; |
michael@0 | 137 | prefSvc.setBoolPref(kExternalWarningPrefPrefix + "http", false); |
michael@0 | 138 | protoInfo = protoSvc.getProtocolHandlerInfo("http"); |
michael@0 | 139 | do_check_eq(0, protoInfo.possibleApplicationHandlers.length); |
michael@0 | 140 | do_check_false(protoInfo.alwaysAskBeforeHandling); |
michael@0 | 141 | |
michael@0 | 142 | // OS default exists, injected default does not exist, |
michael@0 | 143 | // explicit warning pref: true |
michael@0 | 144 | prefSvc.setBoolPref(kExternalWarningPrefPrefix + "http", true); |
michael@0 | 145 | protoInfo = protoSvc.getProtocolHandlerInfo("http"); |
michael@0 | 146 | // OS handler isn't included in possibleApplicationHandlers, so length is 0 |
michael@0 | 147 | // Once they become instances of nsILocalHandlerApp, this number will need |
michael@0 | 148 | // to change. |
michael@0 | 149 | do_check_eq(0, protoInfo.possibleApplicationHandlers.length); |
michael@0 | 150 | do_check_true(protoInfo.alwaysAskBeforeHandling); |
michael@0 | 151 | |
michael@0 | 152 | // OS default exists, injected default exists, explicit warning pref: false |
michael@0 | 153 | prefSvc.setBoolPref(kExternalWarningPrefPrefix + "mailto", false); |
michael@0 | 154 | protoInfo = protoSvc.getProtocolHandlerInfo("mailto"); |
michael@0 | 155 | if (haveDefaultHandlersVersion) |
michael@0 | 156 | do_check_eq(2, protoInfo.possibleApplicationHandlers.length); |
michael@0 | 157 | else |
michael@0 | 158 | do_check_eq(0, protoInfo.possibleApplicationHandlers.length); |
michael@0 | 159 | |
michael@0 | 160 | // Win7+ might not have a default mailto: handler |
michael@0 | 161 | if (noMailto) |
michael@0 | 162 | do_check_true(protoInfo.alwaysAskBeforeHandling); |
michael@0 | 163 | else |
michael@0 | 164 | do_check_false(protoInfo.alwaysAskBeforeHandling); |
michael@0 | 165 | |
michael@0 | 166 | // OS default exists, injected default exists, explicit warning pref: true |
michael@0 | 167 | prefSvc.setBoolPref(kExternalWarningPrefPrefix + "mailto", true); |
michael@0 | 168 | protoInfo = protoSvc.getProtocolHandlerInfo("mailto"); |
michael@0 | 169 | if (haveDefaultHandlersVersion) { |
michael@0 | 170 | do_check_eq(2, protoInfo.possibleApplicationHandlers.length); |
michael@0 | 171 | // Win7+ might not have a default mailto: handler, but on other platforms |
michael@0 | 172 | // alwaysAskBeforeHandling is expected to be false here, because although |
michael@0 | 173 | // the pref is true, the value in RDF is false. The injected mailto handler |
michael@0 | 174 | // carried over the default pref value, and so when we set the pref above |
michael@0 | 175 | // to true it's ignored. |
michael@0 | 176 | if (noMailto) |
michael@0 | 177 | do_check_true(protoInfo.alwaysAskBeforeHandling); |
michael@0 | 178 | else |
michael@0 | 179 | do_check_false(protoInfo.alwaysAskBeforeHandling); |
michael@0 | 180 | |
michael@0 | 181 | } else { |
michael@0 | 182 | do_check_eq(0, protoInfo.possibleApplicationHandlers.length); |
michael@0 | 183 | do_check_true(protoInfo.alwaysAskBeforeHandling); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | if (haveDefaultHandlersVersion) { |
michael@0 | 187 | // Now set the value stored in RDF to true, and the pref to false, to make |
michael@0 | 188 | // sure we still get the right value. (Basically, same thing as above but |
michael@0 | 189 | // with the values reversed.) |
michael@0 | 190 | prefSvc.setBoolPref(kExternalWarningPrefPrefix + "mailto", false); |
michael@0 | 191 | protoInfo.alwaysAskBeforeHandling = true; |
michael@0 | 192 | handlerSvc.store(protoInfo); |
michael@0 | 193 | protoInfo = protoSvc.getProtocolHandlerInfo("mailto"); |
michael@0 | 194 | do_check_eq(2, protoInfo.possibleApplicationHandlers.length); |
michael@0 | 195 | do_check_true(protoInfo.alwaysAskBeforeHandling); |
michael@0 | 196 | } |
michael@0 | 197 | |
michael@0 | 198 | |
michael@0 | 199 | //**************************************************************************// |
michael@0 | 200 | // Test Round-Trip Data Integrity |
michael@0 | 201 | |
michael@0 | 202 | // Test round-trip data integrity by setting the properties of the handler |
michael@0 | 203 | // info object to different values, telling the handler service to store the |
michael@0 | 204 | // object, and then retrieving a new info object for the same type and making |
michael@0 | 205 | // sure its properties are identical. |
michael@0 | 206 | |
michael@0 | 207 | handlerInfo.preferredAction = Ci.nsIHandlerInfo.useHelperApp; |
michael@0 | 208 | handlerInfo.preferredApplicationHandler = localHandler; |
michael@0 | 209 | handlerInfo.alwaysAskBeforeHandling = false; |
michael@0 | 210 | |
michael@0 | 211 | handlerSvc.store(handlerInfo); |
michael@0 | 212 | |
michael@0 | 213 | handlerInfo = mimeSvc.getFromTypeAndExtension("nonexistent/type", null); |
michael@0 | 214 | |
michael@0 | 215 | do_check_eq(handlerInfo.preferredAction, Ci.nsIHandlerInfo.useHelperApp); |
michael@0 | 216 | |
michael@0 | 217 | do_check_neq(handlerInfo.preferredApplicationHandler, null); |
michael@0 | 218 | var preferredHandler = handlerInfo.preferredApplicationHandler; |
michael@0 | 219 | do_check_eq(typeof preferredHandler, "object"); |
michael@0 | 220 | do_check_eq(preferredHandler.name, "Local Handler"); |
michael@0 | 221 | do_check_true(preferredHandler instanceof Ci.nsILocalHandlerApp); |
michael@0 | 222 | preferredHandler.QueryInterface(Ci.nsILocalHandlerApp); |
michael@0 | 223 | do_check_eq(preferredHandler.executable.path, localHandler.executable.path); |
michael@0 | 224 | |
michael@0 | 225 | do_check_false(handlerInfo.alwaysAskBeforeHandling); |
michael@0 | 226 | |
michael@0 | 227 | // Make sure the handler service's enumerate method lists all known handlers. |
michael@0 | 228 | var handlerInfo2 = mimeSvc.getFromTypeAndExtension("nonexistent/type2", null); |
michael@0 | 229 | handlerSvc.store(handlerInfo2); |
michael@0 | 230 | var handlerTypes = ["nonexistent/type", "nonexistent/type2"]; |
michael@0 | 231 | if (haveDefaultHandlersVersion) { |
michael@0 | 232 | handlerTypes.push("webcal"); |
michael@0 | 233 | handlerTypes.push("mailto"); |
michael@0 | 234 | handlerTypes.push("irc"); |
michael@0 | 235 | handlerTypes.push("ircs"); |
michael@0 | 236 | } |
michael@0 | 237 | var handlers = handlerSvc.enumerate(); |
michael@0 | 238 | while (handlers.hasMoreElements()) { |
michael@0 | 239 | var handler = handlers.getNext().QueryInterface(Ci.nsIHandlerInfo); |
michael@0 | 240 | do_check_neq(handlerTypes.indexOf(handler.type), -1); |
michael@0 | 241 | handlerTypes.splice(handlerTypes.indexOf(handler.type), 1); |
michael@0 | 242 | } |
michael@0 | 243 | do_check_eq(handlerTypes.length, 0); |
michael@0 | 244 | |
michael@0 | 245 | // Make sure the handler service's remove method removes a handler record. |
michael@0 | 246 | handlerSvc.remove(handlerInfo2); |
michael@0 | 247 | handlers = handlerSvc.enumerate(); |
michael@0 | 248 | while (handlers.hasMoreElements()) |
michael@0 | 249 | do_check_neq(handlers.getNext().QueryInterface(Ci.nsIHandlerInfo).type, |
michael@0 | 250 | handlerInfo2.type); |
michael@0 | 251 | |
michael@0 | 252 | // Make sure we can store and retrieve a handler info object with no preferred |
michael@0 | 253 | // handler. |
michael@0 | 254 | var noPreferredHandlerInfo = |
michael@0 | 255 | mimeSvc.getFromTypeAndExtension("nonexistent/no-preferred-handler", null); |
michael@0 | 256 | handlerSvc.store(noPreferredHandlerInfo); |
michael@0 | 257 | noPreferredHandlerInfo = |
michael@0 | 258 | mimeSvc.getFromTypeAndExtension("nonexistent/no-preferred-handler", null); |
michael@0 | 259 | do_check_eq(noPreferredHandlerInfo.preferredApplicationHandler, null); |
michael@0 | 260 | |
michael@0 | 261 | // Make sure that the handler service removes an existing handler record |
michael@0 | 262 | // if we store a handler info object with no preferred handler. |
michael@0 | 263 | var removePreferredHandlerInfo = |
michael@0 | 264 | mimeSvc.getFromTypeAndExtension("nonexistent/rem-preferred-handler", null); |
michael@0 | 265 | removePreferredHandlerInfo.preferredApplicationHandler = localHandler; |
michael@0 | 266 | handlerSvc.store(removePreferredHandlerInfo); |
michael@0 | 267 | removePreferredHandlerInfo = |
michael@0 | 268 | mimeSvc.getFromTypeAndExtension("nonexistent/rem-preferred-handler", null); |
michael@0 | 269 | removePreferredHandlerInfo.preferredApplicationHandler = null; |
michael@0 | 270 | handlerSvc.store(removePreferredHandlerInfo); |
michael@0 | 271 | removePreferredHandlerInfo = |
michael@0 | 272 | mimeSvc.getFromTypeAndExtension("nonexistent/rem-preferred-handler", null); |
michael@0 | 273 | do_check_eq(removePreferredHandlerInfo.preferredApplicationHandler, null); |
michael@0 | 274 | |
michael@0 | 275 | // Make sure we can store and retrieve a handler info object with possible |
michael@0 | 276 | // handlers. We test both adding and removing handlers. |
michael@0 | 277 | |
michael@0 | 278 | // Get a handler info and make sure it has no possible handlers. |
michael@0 | 279 | var possibleHandlersInfo = |
michael@0 | 280 | mimeSvc.getFromTypeAndExtension("nonexistent/possible-handlers", null); |
michael@0 | 281 | do_check_eq(possibleHandlersInfo.possibleApplicationHandlers.length, 0); |
michael@0 | 282 | |
michael@0 | 283 | // Store and re-retrieve the handler and make sure it still has no possible |
michael@0 | 284 | // handlers. |
michael@0 | 285 | handlerSvc.store(possibleHandlersInfo); |
michael@0 | 286 | possibleHandlersInfo = |
michael@0 | 287 | mimeSvc.getFromTypeAndExtension("nonexistent/possible-handlers", null); |
michael@0 | 288 | do_check_eq(possibleHandlersInfo.possibleApplicationHandlers.length, 0); |
michael@0 | 289 | |
michael@0 | 290 | // Add two handlers, store the object, re-retrieve it, and make sure it has |
michael@0 | 291 | // two handlers. |
michael@0 | 292 | possibleHandlersInfo.possibleApplicationHandlers.appendElement(localHandler, |
michael@0 | 293 | false); |
michael@0 | 294 | possibleHandlersInfo.possibleApplicationHandlers.appendElement(webHandler, |
michael@0 | 295 | false); |
michael@0 | 296 | handlerSvc.store(possibleHandlersInfo); |
michael@0 | 297 | possibleHandlersInfo = |
michael@0 | 298 | mimeSvc.getFromTypeAndExtension("nonexistent/possible-handlers", null); |
michael@0 | 299 | do_check_eq(possibleHandlersInfo.possibleApplicationHandlers.length, 2); |
michael@0 | 300 | |
michael@0 | 301 | // Figure out which is the local and which is the web handler and the index |
michael@0 | 302 | // in the array of the local handler, which is the one we're going to remove |
michael@0 | 303 | // to test removal of a handler. |
michael@0 | 304 | var handler1 = possibleHandlersInfo.possibleApplicationHandlers. |
michael@0 | 305 | queryElementAt(0, Ci.nsIHandlerApp); |
michael@0 | 306 | var handler2 = possibleHandlersInfo.possibleApplicationHandlers. |
michael@0 | 307 | queryElementAt(1, Ci.nsIHandlerApp); |
michael@0 | 308 | var localPossibleHandler, webPossibleHandler, localIndex; |
michael@0 | 309 | if (handler1 instanceof Ci.nsILocalHandlerApp) |
michael@0 | 310 | [localPossibleHandler, webPossibleHandler, localIndex] = [handler1, |
michael@0 | 311 | handler2, |
michael@0 | 312 | 0]; |
michael@0 | 313 | else |
michael@0 | 314 | [localPossibleHandler, webPossibleHandler, localIndex] = [handler2, |
michael@0 | 315 | handler1, |
michael@0 | 316 | 1]; |
michael@0 | 317 | localPossibleHandler.QueryInterface(Ci.nsILocalHandlerApp); |
michael@0 | 318 | webPossibleHandler.QueryInterface(Ci.nsIWebHandlerApp); |
michael@0 | 319 | |
michael@0 | 320 | // Make sure the two handlers are the ones we stored. |
michael@0 | 321 | do_check_eq(localPossibleHandler.name, localHandler.name); |
michael@0 | 322 | do_check_true(localPossibleHandler.equals(localHandler)); |
michael@0 | 323 | do_check_eq(webPossibleHandler.name, webHandler.name); |
michael@0 | 324 | do_check_true(webPossibleHandler.equals(webHandler)); |
michael@0 | 325 | |
michael@0 | 326 | // Remove a handler, store the object, re-retrieve it, and make sure |
michael@0 | 327 | // it only has one handler. |
michael@0 | 328 | possibleHandlersInfo.possibleApplicationHandlers.removeElementAt(localIndex); |
michael@0 | 329 | handlerSvc.store(possibleHandlersInfo); |
michael@0 | 330 | possibleHandlersInfo = |
michael@0 | 331 | mimeSvc.getFromTypeAndExtension("nonexistent/possible-handlers", null); |
michael@0 | 332 | do_check_eq(possibleHandlersInfo.possibleApplicationHandlers.length, 1); |
michael@0 | 333 | |
michael@0 | 334 | // Make sure the handler is the one we didn't remove. |
michael@0 | 335 | webPossibleHandler = possibleHandlersInfo.possibleApplicationHandlers. |
michael@0 | 336 | queryElementAt(0, Ci.nsIWebHandlerApp); |
michael@0 | 337 | do_check_eq(webPossibleHandler.name, webHandler.name); |
michael@0 | 338 | do_check_true(webPossibleHandler.equals(webHandler)); |
michael@0 | 339 | |
michael@0 | 340 | ////////////////////////////////////////////////////// |
michael@0 | 341 | // handler info command line parameters and equality |
michael@0 | 342 | var localApp = Cc["@mozilla.org/uriloader/local-handler-app;1"]. |
michael@0 | 343 | createInstance(Ci.nsILocalHandlerApp); |
michael@0 | 344 | var handlerApp = localApp.QueryInterface(Ci.nsIHandlerApp); |
michael@0 | 345 | |
michael@0 | 346 | do_check_true(handlerApp.equals(localApp)); |
michael@0 | 347 | |
michael@0 | 348 | localApp.executable = executable; |
michael@0 | 349 | |
michael@0 | 350 | do_check_eq(0, localApp.parameterCount); |
michael@0 | 351 | localApp.appendParameter("-test1"); |
michael@0 | 352 | do_check_eq(1, localApp.parameterCount); |
michael@0 | 353 | localApp.appendParameter("-test2"); |
michael@0 | 354 | do_check_eq(2, localApp.parameterCount); |
michael@0 | 355 | do_check_true(localApp.parameterExists("-test1")); |
michael@0 | 356 | do_check_true(localApp.parameterExists("-test2")); |
michael@0 | 357 | do_check_false(localApp.parameterExists("-false")); |
michael@0 | 358 | localApp.clearParameters(); |
michael@0 | 359 | do_check_eq(0, localApp.parameterCount); |
michael@0 | 360 | |
michael@0 | 361 | var localApp2 = Cc["@mozilla.org/uriloader/local-handler-app;1"]. |
michael@0 | 362 | createInstance(Ci.nsILocalHandlerApp); |
michael@0 | 363 | |
michael@0 | 364 | localApp2.executable = executable; |
michael@0 | 365 | |
michael@0 | 366 | localApp.clearParameters(); |
michael@0 | 367 | do_check_true(localApp.equals(localApp2)); |
michael@0 | 368 | |
michael@0 | 369 | // equal: |
michael@0 | 370 | // cut -d 1 -f 2 |
michael@0 | 371 | // cut -d 1 -f 2 |
michael@0 | 372 | |
michael@0 | 373 | localApp.appendParameter("-test1"); |
michael@0 | 374 | localApp.appendParameter("-test2"); |
michael@0 | 375 | localApp.appendParameter("-test3"); |
michael@0 | 376 | localApp2.appendParameter("-test1"); |
michael@0 | 377 | localApp2.appendParameter("-test2"); |
michael@0 | 378 | localApp2.appendParameter("-test3"); |
michael@0 | 379 | do_check_true(localApp.equals(localApp2)); |
michael@0 | 380 | |
michael@0 | 381 | // not equal: |
michael@0 | 382 | // cut -d 1 -f 2 |
michael@0 | 383 | // cut -f 1 -d 2 |
michael@0 | 384 | |
michael@0 | 385 | localApp.clearParameters(); |
michael@0 | 386 | localApp2.clearParameters(); |
michael@0 | 387 | |
michael@0 | 388 | localApp.appendParameter("-test1"); |
michael@0 | 389 | localApp.appendParameter("-test2"); |
michael@0 | 390 | localApp.appendParameter("-test3"); |
michael@0 | 391 | localApp2.appendParameter("-test2"); |
michael@0 | 392 | localApp2.appendParameter("-test1"); |
michael@0 | 393 | localApp2.appendParameter("-test3"); |
michael@0 | 394 | do_check_false(localApp2.equals(localApp)); |
michael@0 | 395 | |
michael@0 | 396 | var str; |
michael@0 | 397 | str = localApp.getParameter(0) |
michael@0 | 398 | do_check_eq(str, "-test1"); |
michael@0 | 399 | str = localApp.getParameter(1) |
michael@0 | 400 | do_check_eq(str, "-test2"); |
michael@0 | 401 | str = localApp.getParameter(2) |
michael@0 | 402 | do_check_eq(str, "-test3"); |
michael@0 | 403 | |
michael@0 | 404 | // FIXME: test round trip integrity for a protocol. |
michael@0 | 405 | // FIXME: test round trip integrity for a handler info with a web handler. |
michael@0 | 406 | |
michael@0 | 407 | //**************************************************************************// |
michael@0 | 408 | // getTypeFromExtension tests |
michael@0 | 409 | |
michael@0 | 410 | // test nonexistent extension |
michael@0 | 411 | var lolType = handlerSvc.getTypeFromExtension("lolcat"); |
michael@0 | 412 | do_check_eq(lolType, ""); |
michael@0 | 413 | |
michael@0 | 414 | |
michael@0 | 415 | // add a handler for the extension |
michael@0 | 416 | var lolHandler = mimeSvc.getFromTypeAndExtension("application/lolcat", null); |
michael@0 | 417 | |
michael@0 | 418 | do_check_false(lolHandler.extensionExists("lolcat")); |
michael@0 | 419 | lolHandler.preferredAction = Ci.nsIHandlerInfo.useHelperApp; |
michael@0 | 420 | lolHandler.preferredApplicationHandler = localHandler; |
michael@0 | 421 | lolHandler.alwaysAskBeforeHandling = false; |
michael@0 | 422 | |
michael@0 | 423 | // store the handler |
michael@0 | 424 | do_check_false(handlerSvc.exists(lolHandler)); |
michael@0 | 425 | handlerSvc.store(lolHandler); |
michael@0 | 426 | do_check_true(handlerSvc.exists(lolHandler)); |
michael@0 | 427 | |
michael@0 | 428 | // Get a file:// string pointing to mimeTypes.rdf |
michael@0 | 429 | var rdfFile = HandlerServiceTest._dirSvc.get("UMimTyp", Ci.nsIFile); |
michael@0 | 430 | var fileHandler = ioService.getProtocolHandler("file").QueryInterface(Ci.nsIFileProtocolHandler); |
michael@0 | 431 | var rdfFileURI = fileHandler.getURLSpecFromFile(rdfFile); |
michael@0 | 432 | |
michael@0 | 433 | // Assign a file extenstion to the handler. handlerSvc.store() doesn't |
michael@0 | 434 | // actually store any file extensions added with setFileExtensions(), you |
michael@0 | 435 | // have to wade into RDF muck to do so. |
michael@0 | 436 | |
michael@0 | 437 | // Based on toolkit/mozapps/downloads/content/helperApps.js :: addExtension() |
michael@0 | 438 | var gRDF = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService); |
michael@0 | 439 | var mimeSource = gRDF.GetUnicodeResource("urn:mimetype:application/lolcat"); |
michael@0 | 440 | var valueProperty = gRDF.GetUnicodeResource("http://home.netscape.com/NC-rdf#fileExtensions"); |
michael@0 | 441 | var mimeLiteral = gRDF.GetLiteral("lolcat"); |
michael@0 | 442 | |
michael@0 | 443 | var DS = gRDF.GetDataSourceBlocking(rdfFileURI); |
michael@0 | 444 | DS.Assert(mimeSource, valueProperty, mimeLiteral, true); |
michael@0 | 445 | |
michael@0 | 446 | |
michael@0 | 447 | // test now-existent extension |
michael@0 | 448 | lolType = handlerSvc.getTypeFromExtension("lolcat"); |
michael@0 | 449 | do_check_eq(lolType, "application/lolcat"); |
michael@0 | 450 | |
michael@0 | 451 | if (env.get("PERSONAL_MAILCAP")) { |
michael@0 | 452 | handlerInfo = mimeSvc.getFromTypeAndExtension("text/plain", null); |
michael@0 | 453 | do_check_eq(handlerInfo.preferredAction, Ci.nsIHandlerInfo.useSystemDefault); |
michael@0 | 454 | do_check_eq(handlerInfo.defaultDescription, "sed"); |
michael@0 | 455 | } |
michael@0 | 456 | } |