Bug 1424879 - Enable mozilla/use-services for xpcom/. r?froydnj draft
authorMark Banner <standard8@mozilla.com>
Mon, 11 Dec 2017 20:19:38 +0000
changeset 710878 f5439d54bbca981aded9052a045dcd846c4aabb9
parent 710877 efef2822662d9753dfc3827c0af5fb8c31aede08
child 743675 0dbda199396d4afd7cd0de8fdf4fa08d172071dc
push id92925
push userbmo:standard8@mozilla.com
push dateTue, 12 Dec 2017 15:33:39 +0000
reviewersfroydnj
bugs1424879
milestone59.0a1
Bug 1424879 - Enable mozilla/use-services for xpcom/. r?froydnj MozReview-Commit-ID: FnQDHG3w6RQ
.eslintrc.js
xpcom/tests/unit/test_bug121341.js
xpcom/tests/unit/test_bug332389.js
xpcom/tests/unit/test_bug364285-1.js
xpcom/tests/unit/test_bug374754.js
xpcom/tests/unit/test_bug656331.js
xpcom/tests/unit/test_file_createUnique.js
xpcom/tests/unit/test_file_equality.js
xpcom/tests/unit/test_file_renameTo.js
xpcom/tests/unit/test_hidden_files.js
xpcom/tests/unit/test_home.js
xpcom/tests/unit/test_iniProcessor.js
xpcom/tests/unit/test_versioncomparator.js
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -36,15 +36,14 @@ module.exports = {
       "devtools/**",
       "dom/indexedDB/**",
       "dom/media/**",
       "extensions/pref/**",
       "mobile/android/**",
       "security/**",
       "testing/**",
       "tools/profiler/**",
-      "xpcom/**"
     ],
     "rules": {
       "mozilla/use-services": "off",
     }
   }]
 };
--- a/xpcom/tests/unit/test_bug121341.js
+++ b/xpcom/tests/unit/test_bug121341.js
@@ -1,17 +1,15 @@
 Cu.import("resource://gre/modules/NetUtil.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
 
 function run_test() {
-  var ios = Components.classes["@mozilla.org/network/io-service;1"].
-            getService(Components.interfaces.nsIIOService);
-
   var dataFile = do_get_file("data/bug121341.properties");
   var channel = NetUtil.newChannel({
-    uri: ios.newFileURI(dataFile, null, null),
+    uri: Services.io.newFileURI(dataFile, null, null),
     loadUsingSystemPrincipal: true
   });
   var inp = channel.open2();
 
   var properties = Components.classes["@mozilla.org/persistent-properties;1"].
                    createInstance(Components.interfaces.nsIPersistentProperties);
   properties.load(inp);
 
@@ -48,17 +46,17 @@ function run_test() {
   do_check_eq(value, "c\uCDEFd");
 
   value = properties.getStringProperty("11");
   do_check_eq(value, "\uABCD");
 
   dataFile = do_get_file("data/bug121341-2.properties");
 
   var channel2 = NetUtil.newChannel({
-    uri: ios.newFileURI(dataFile, null, null),
+    uri: Services.io.newFileURI(dataFile, null, null),
     loadUsingSystemPrincipal: true
   });
   inp = channel2.open2();
 
   var properties2 = Components.classes["@mozilla.org/persistent-properties;1"].
                     createInstance(Components.interfaces.nsIPersistentProperties);
   try {
     properties2.load(inp);
--- a/xpcom/tests/unit/test_bug332389.js
+++ b/xpcom/tests/unit/test_bug332389.js
@@ -1,12 +1,12 @@
+Cu.import("resource://gre/modules/Services.jsm");
+
 function run_test() {
-  var f =
-      Cc["@mozilla.org/file/directory_service;1"].
-      getService(Ci.nsIProperties).get("CurProcD", Ci.nsIFile);
+  var f = Services.dirsvc.get("CurProcD", Ci.nsIFile);
 
   var terminated = false;
   for (var i = 0; i < 100; i++) {
     if (f == null) {
       terminated = true;
       break;
     }
     f = f.parent;
--- a/xpcom/tests/unit/test_bug364285-1.js
+++ b/xpcom/tests/unit/test_bug364285-1.js
@@ -1,22 +1,22 @@
+Cu.import("resource://gre/modules/Services.jsm");
+
 var nameArray = [
  "ascii",                                           // ASCII
  "fran\u00E7ais",                                   // Latin-1
  "\u0420\u0443\u0441\u0441\u043A\u0438\u0439",      // Cyrillic
  "\u65E5\u672C\u8A9E",                              // Japanese
  "\u4E2D\u6587",                                    // Chinese
  "\uD55C\uAD6D\uC5B4",                              // Korean
  "\uD801\uDC0F\uD801\uDC2D\uD801\uDC3B\uD801\uDC2B" // Deseret
 ];
 
 function getTempDir() {
-    var dirService = Cc["@mozilla.org/file/directory_service;1"]
-        .getService(Ci.nsIProperties);
-    return dirService.get("TmpD", Ci.nsIFile);
+    return Services.dirsvc.get("TmpD", Ci.nsIFile);
 }
 
 function create_file(fileName) {
     var outFile = getTempDir();
     outFile.append(fileName);
     outFile.createUnique(outFile.NORMAL_FILE_TYPE, 0o600);
 
     var stream = Cc["@mozilla.org/network/file-output-stream;1"]
--- a/xpcom/tests/unit/test_bug374754.js
+++ b/xpcom/tests/unit/test_bug374754.js
@@ -1,8 +1,10 @@
+Cu.import("resource://gre/modules/Services.jsm");
+
 var addedTopic = "xpcom-category-entry-added";
 var removedTopic = "xpcom-category-entry-removed";
 var testCategory = "bug-test-category";
 var testEntry = "@mozilla.org/bug-test-entry;1";
 
 var testValue = "check validity";
 var result = "";
 var expected = "add remove add remove ";
@@ -15,19 +17,18 @@ var observer = {
 
     throw Components.results.NS_ERROR_NO_INTERFACE;
   },
 
   observe(subject, topic, data) {
     if (topic == "timer-callback") {
       do_check_eq(result, expected);
 
-      var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
-      observerService.removeObserver(this, addedTopic);
-      observerService.removeObserver(this, removedTopic);
+      Services.obs.removeObserver(this, addedTopic);
+      Services.obs.removeObserver(this, removedTopic);
 
       do_test_finished();
 
       timer = null;
     }
 
     if (subject.QueryInterface(Ci.nsISupportsCString).data != testEntry || data != testCategory)
       return;
@@ -37,19 +38,18 @@ var observer = {
     else if (topic == removedTopic)
       result += "remove ";
   }
 };
 
 function run_test() {
   do_test_pending();
 
-  var observerService = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
-  observerService.addObserver(observer, addedTopic);
-  observerService.addObserver(observer, removedTopic);
+  Services.obs.addObserver(observer, addedTopic);
+  Services.obs.addObserver(observer, removedTopic);
 
   var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
   categoryManager.addCategoryEntry(testCategory, testEntry, testValue, false, true);
   categoryManager.addCategoryEntry(testCategory, testEntry, testValue, false, true);
   categoryManager.deleteCategoryEntry(testCategory, testEntry, false);
 
   timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
   timer.init(observer, 0, timer.TYPE_ONE_SHOT);
--- a/xpcom/tests/unit/test_bug656331.js
+++ b/xpcom/tests/unit/test_bug656331.js
@@ -1,9 +1,10 @@
-Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/XPCOMUtils.jsm");
+Cu.import("resource://gre/modules/Services.jsm");
 
 /* global registerAppManifest */
 
 function info(s) {
   dump("TEST-INFO | test_bug656331.js | " + s + "\n");
 }
 
 var gMessageExpected = /Native module.*has version 3.*expected/;
@@ -14,25 +15,22 @@ const kConsoleListener = {
 
   observe: function listener_observe(message) {
     if (gMessageExpected.test(message.message))
       gFound = true;
   }
 };
 
 function run_test() {
-  let cs = Components.classes["@mozilla.org/consoleservice;1"].
-    getService(Ci.nsIConsoleService);
-  cs.registerListener(kConsoleListener);
+  Services.console.registerListener(kConsoleListener);
 
   let manifest = do_get_file("components/bug656331.manifest");
   registerAppManifest(manifest);
 
   do_check_false("{f18fb09b-28b4-4435-bc5b-8027f18df743}" in Components.classesByID);
 
   do_test_pending();
-  Components.classes["@mozilla.org/thread-manager;1"].
-    getService(Ci.nsIThreadManager).dispatchToMainThread(function() {
-      cs.unregisterListener(kConsoleListener);
-      do_check_true(gFound);
-      do_test_finished();
-    });
+  Services.tm.dispatchToMainThread(function() {
+    Services.console.unregisterListener(kConsoleListener);
+    do_check_true(gFound);
+    do_test_finished();
+  });
 }
--- a/xpcom/tests/unit/test_file_createUnique.js
+++ b/xpcom/tests/unit/test_file_createUnique.js
@@ -1,20 +1,21 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+Cu.import("resource://gre/modules/Services.jsm");
+
 function run_test() {
   // Generate a leaf name that is 255 characters long.
   var longLeafName = new Array(256).join("T");
 
   // Generate the path for a file located in a directory with a long name.
-  var tempFile = Cc["@mozilla.org/file/directory_service;1"].
-                 getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
+  var tempFile = Services.dirsvc.get("TmpD", Ci.nsIFile);
   tempFile.append(longLeafName);
   tempFile.append("test.txt");
 
   try {
     tempFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0o600);
     do_throw("Creating an item in a folder with a very long name should throw");
   } catch (e) {
     if (!(e instanceof Ci.nsIException &&
--- a/xpcom/tests/unit/test_file_equality.js
+++ b/xpcom/tests/unit/test_file_equality.js
@@ -1,24 +1,25 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
 /* vim:set ts=2 sw=2 sts=2 et: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 var LocalFile = CC("@mozilla.org/file/local;1", "nsIFile", "initWithPath");
 
+Cu.import("resource://gre/modules/Services.jsm");
+
 function run_test() {
   test_normalized_vs_non_normalized();
 }
 
 function test_normalized_vs_non_normalized() {
   // get a directory that exists on all platforms
-  var dirProvider = Components.classes["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
-  var tmp1 = dirProvider.get("TmpD", Ci.nsIFile);
+  var tmp1 = Services.dirsvc.get("TmpD", Ci.nsIFile);
   var exists = tmp1.exists();
   do_check_true(exists);
   if (!exists)
     return;
 
   // the test logic below assumes we're starting with a normalized path, but the
   // default location on macos is a symbolic link, so resolve it before starting
   tmp1.normalize();
--- a/xpcom/tests/unit/test_file_renameTo.js
+++ b/xpcom/tests/unit/test_file_renameTo.js
@@ -1,18 +1,18 @@
 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+Cu.import("resource://gre/modules/Services.jsm");
+
 function run_test() {
   // Create the base directory.
-  let base = Cc["@mozilla.org/file/directory_service;1"]
-             .getService(Ci.nsIProperties)
-             .get("TmpD", Ci.nsIFile);
+  let base = Services.dirsvc.get("TmpD", Ci.nsIFile);
   base.append("renameTesting");
   if (base.exists()) {
     base.remove(true);
   }
   base.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0777", 8));
 
   // Create a sub directory under the base.
   let subdir = base.clone();
--- a/xpcom/tests/unit/test_hidden_files.js
+++ b/xpcom/tests/unit/test_hidden_files.js
@@ -1,16 +1,17 @@
 const NS_OS_TEMP_DIR = "TmpD";
 
 const CWD = do_get_cwd();
 
+Cu.import("resource://gre/modules/Services.jsm");
+
 var hiddenUnixFile;
 function createUNIXHiddenFile() {
-  var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
-  var tmpDir = dirSvc.get(NS_OS_TEMP_DIR, Ci.nsIFile);
+  var tmpDir = Services.dirsvc.get(NS_OS_TEMP_DIR, Ci.nsIFile);
   hiddenUnixFile = tmpDir.clone();
   hiddenUnixFile.append(".foo");
   // we don't care if this already exists because we don't care
   // about the file's contents (just the name)
   if (!hiddenUnixFile.exists())
     hiddenUnixFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
   return hiddenUnixFile.exists();
 }
--- a/xpcom/tests/unit/test_home.js
+++ b/xpcom/tests/unit/test_home.js
@@ -1,21 +1,22 @@
+Cu.import("resource://gre/modules/Services.jsm");
+
 const CWD = do_get_cwd();
 function checkOS(os) {
   const nsILocalFile_ = "nsILocalFile" + os;
   return nsILocalFile_ in Components.interfaces &&
          CWD instanceof Components.interfaces[nsILocalFile_];
 }
 
 const isWin = checkOS("Win");
 
 function run_test() {
   var envVar = isWin ? "USERPROFILE" : "HOME";
 
-  var dirSvc = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
-  var homeDir = dirSvc.get("Home", Ci.nsIFile);
+  var homeDir = Services.dirsvc.get("Home", Ci.nsIFile);
 
   var env = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment);
   var expected = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
   expected.initWithPath(env.get(envVar));
 
   do_check_eq(homeDir.path, expected.path);
 }
--- a/xpcom/tests/unit/test_iniProcessor.js
+++ b/xpcom/tests/unit/test_iniProcessor.js
@@ -120,16 +120,18 @@ var testdata = [
                      reference: testdata[12].reference } );
     testdata.push( { filename: "data/iniparser14-utf8BOM.ini",
                      reference: testdata[13].reference } );
     testdata.push( { filename: "data/iniparser15-utf8BOM.ini",
                      reference: testdata[14].reference } );
     testdata.push( { filename: "data/iniparser16-utf8BOM.ini",
                      reference: testdata[15].reference } );
 
+    // Intentional test for appInfo that can't be preloaded.
+    // eslint-disable-next-line mozilla/use-services
     let os = Cc["@mozilla.org/xre/app-info;1"]
              .getService(Ci.nsIXULRuntime).OS;
     if ("WINNT" === os) {
         testdata.push( { filename: "data/iniparser01-utf16leBOM.ini",
                          reference: testdata[0].reference } );
         testdata.push( { filename: "data/iniparser02-utf16leBOM.ini",
                          reference: testdata[1].reference } );
         testdata.push( { filename: "data/iniparser03-utf16leBOM.ini",
--- a/xpcom/tests/unit/test_versioncomparator.js
+++ b/xpcom/tests/unit/test_versioncomparator.js
@@ -1,8 +1,10 @@
+Cu.import("resource://gre/modules/Services.jsm");
+
 // Versions to test listed in ascending order, none can be equal
 var comparisons = [
   "0.9",
   "0.9.1",
   "1.0pre1",
   "1.0pre2",
   "1.0",
   "1.1pre",
@@ -24,33 +26,30 @@ var comparisons = [
 // Every version in this list means the same version number
 var equality = [
   "1.1pre",
   "1.1pre0",
   "1.0+"
 ];
 
 function run_test() {
-  var vc = Components.classes["@mozilla.org/xpcom/version-comparator;1"]
-                     .getService(Components.interfaces.nsIVersionComparator);
-
   for (var i = 0; i < comparisons.length; i++) {
     for (var j = 0; j < comparisons.length; j++) {
-      var result = vc.compare(comparisons[i], comparisons[j]);
+      var result = Services.vc.compare(comparisons[i], comparisons[j]);
       if (i == j) {
         if (result != 0)
           do_throw(comparisons[i] + " should be the same as itself");
       } else if (i < j) {
         if (!(result < 0))
           do_throw(comparisons[i] + " should be less than " + comparisons[j]);
       } else if (!(result > 0)) {
         do_throw(comparisons[i] + " should be greater than " + comparisons[j]);
       }
     }
   }
 
   for (i = 0; i < equality.length; i++) {
     for (j = 0; j < equality.length; j++) {
-      if (vc.compare(equality[i], equality[j]) != 0)
+      if (Services.vc.compare(equality[i], equality[j]) != 0)
         do_throw(equality[i] + " should equal " + equality[j]);
     }
   }
 }