uriloader/exthandler/tests/unit/head_handlerService.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/uriloader/exthandler/tests/unit/head_handlerService.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,163 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// Inspired by the Places infrastructure in head_bookmarks.js
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cr = Components.results;
    1.13 +const Cu = Components.utils;
    1.14 +
    1.15 +var HandlerServiceTest = {
    1.16 +  //**************************************************************************//
    1.17 +  // Convenience Getters
    1.18 +
    1.19 +  __dirSvc: null,
    1.20 +  get _dirSvc() {
    1.21 +    if (!this.__dirSvc)
    1.22 +      this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"].
    1.23 +                      getService(Ci.nsIProperties).
    1.24 +                      QueryInterface(Ci.nsIDirectoryService);
    1.25 +    return this.__dirSvc;
    1.26 +  },
    1.27 +
    1.28 +  __consoleSvc: null,
    1.29 +  get _consoleSvc() {
    1.30 +    if (!this.__consoleSvc)
    1.31 +      this.__consoleSvc = Cc["@mozilla.org/consoleservice;1"].
    1.32 +                          getService(Ci.nsIConsoleService);
    1.33 +    return this.__consoleSvc;
    1.34 +  },
    1.35 +
    1.36 +
    1.37 +  //**************************************************************************//
    1.38 +  // nsISupports
    1.39 +  
    1.40 +  interfaces: [Ci.nsIDirectoryServiceProvider, Ci.nsISupports],
    1.41 +
    1.42 +  QueryInterface: function HandlerServiceTest_QueryInterface(iid) {
    1.43 +    if (!this.interfaces.some( function(v) { return iid.equals(v) } ))
    1.44 +      throw Cr.NS_ERROR_NO_INTERFACE;
    1.45 +    return this;
    1.46 +  },
    1.47 +
    1.48 +
    1.49 +  //**************************************************************************//
    1.50 +  // Initialization & Destruction
    1.51 +  
    1.52 +  init: function HandlerServiceTest_init() {
    1.53 +    // Register ourselves as a directory provider for the datasource file
    1.54 +    // if there isn't one registered already.
    1.55 +    try {
    1.56 +      this._dirSvc.get("UMimTyp", Ci.nsIFile);
    1.57 +    } catch (ex) {
    1.58 +      this._dirSvc.registerProvider(this);
    1.59 +      this._providerRegistered = true;
    1.60 +    }
    1.61 +
    1.62 +    // Delete the existing datasource file, if any, so we start from scratch.
    1.63 +    // We also do this after finishing the tests, so there shouldn't be an old
    1.64 +    // file lying around, but just in case we delete it here as well.
    1.65 +    this._deleteDatasourceFile();
    1.66 +
    1.67 +    // Turn on logging so we can troubleshoot problems with the tests.
    1.68 +    var prefBranch = Cc["@mozilla.org/preferences-service;1"].
    1.69 +                     getService(Ci.nsIPrefBranch);
    1.70 +    prefBranch.setBoolPref("browser.contentHandling.log", true);
    1.71 +  },
    1.72 +
    1.73 +  destroy: function HandlerServiceTest_destroy() {
    1.74 +    // Delete the existing datasource file, if any, so we don't leave test files
    1.75 +    // lying around and we start from scratch the next time.
    1.76 +    this._deleteDatasourceFile();
    1.77 +    // Unregister the directory service provider
    1.78 +    if (this._providerRegistered)
    1.79 +      this._dirSvc.unregisterProvider(this);
    1.80 +  },
    1.81 +
    1.82 +
    1.83 +  //**************************************************************************//
    1.84 +  // nsIDirectoryServiceProvider
    1.85 +
    1.86 +  getFile: function HandlerServiceTest_getFile(property, persistent) {
    1.87 +    this.log("getFile: requesting " + property);
    1.88 +
    1.89 +    persistent.value = true;
    1.90 +
    1.91 +    if (property == "UMimTyp") {
    1.92 +      var datasourceFile = this._dirSvc.get("CurProcD", Ci.nsIFile);
    1.93 +      datasourceFile.append("mimeTypes.rdf");
    1.94 +      return datasourceFile;
    1.95 +    }
    1.96 +
    1.97 +    // This causes extraneous errors to show up in the log when the directory
    1.98 +    // service asks us first for CurProcD and MozBinD.  I wish there was a way
    1.99 +    // to suppress those errors.
   1.100 +    this.log("the following NS_ERROR_FAILURE exception in " +
   1.101 +             "nsIDirectoryServiceProvider::getFile is expected, " +
   1.102 +             "as we don't provide the '" + property + "' file");
   1.103 +    throw Cr.NS_ERROR_FAILURE;
   1.104 +  },
   1.105 +
   1.106 +
   1.107 +  //**************************************************************************//
   1.108 +  // Utilities
   1.109 +
   1.110 +  /**
   1.111 +   * Delete the datasource file.
   1.112 +   */
   1.113 +  _deleteDatasourceFile: function HandlerServiceTest__deleteDatasourceFile() {
   1.114 +    var file = this._dirSvc.get("UMimTyp", Ci.nsIFile);
   1.115 +    if (file.exists())
   1.116 +      file.remove(false);
   1.117 +  },
   1.118 +
   1.119 +  /**
   1.120 +   * Get the contents of the datasource as a serialized string.  Useful for
   1.121 +   * debugging problems with test failures, i.e.:
   1.122 +   *
   1.123 +   * HandlerServiceTest.log(HandlerServiceTest.getDatasourceContents());
   1.124 +   *
   1.125 +   * @returns {string} the serialized datasource
   1.126 +   */
   1.127 +  getDatasourceContents: function HandlerServiceTest_getDatasourceContents() {
   1.128 +    var rdf = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
   1.129 +  
   1.130 +    var ioService = Cc["@mozilla.org/network/io-service;1"].
   1.131 +                    getService(Ci.nsIIOService);
   1.132 +    var fileHandler = ioService.getProtocolHandler("file").
   1.133 +                      QueryInterface(Ci.nsIFileProtocolHandler);
   1.134 +    var fileURL = fileHandler.getURLSpecFromFile(this.getDatasourceFile());
   1.135 +    var ds = rdf.GetDataSourceBlocking(fileURL);
   1.136 +  
   1.137 +    var outputStream = {
   1.138 +      data: "",
   1.139 +      close: function() {},
   1.140 +      flush: function() {},
   1.141 +      write: function (buffer,count) {
   1.142 +        this.data += buffer;
   1.143 +        return count;
   1.144 +      },
   1.145 +      writeFrom: function (stream,count) {},
   1.146 +      isNonBlocking: false
   1.147 +    };
   1.148 +  
   1.149 +    ds.QueryInterface(Components.interfaces.nsIRDFXMLSource);
   1.150 +    ds.Serialize(outputStream);
   1.151 +  
   1.152 +    return outputStream.data;
   1.153 +  },
   1.154 +
   1.155 +  /**
   1.156 +   * Log a message to the console and the test log.
   1.157 +   */
   1.158 +  log: function HandlerServiceTest_log(message) {
   1.159 +    message = "*** HandlerServiceTest: " + message;
   1.160 +    this._consoleSvc.logStringMessage(message);
   1.161 +    print(message);
   1.162 +  }
   1.163 +
   1.164 +};
   1.165 +
   1.166 +HandlerServiceTest.init();

mercurial