Commit b20b7a12 authored by Mark Banner's avatar Mark Banner
Browse files

Bug 1834176 - Avoid using NetUtil as a test file in js/xpconnect/tests. r=arai

parent 2250cee7
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
/* global base */
var ns = {};
Services.scriptloader.loadSubScript("resource://gre/modules/NetUtil.jsm", ns);
var NetUtil = ns.NetUtil;
Services.scriptloader.loadSubScript(base + "file_expandosharing.jsm", ns);
var checkFromJSM = ns.checkFromJSM;
+12 −9
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
  <script type="application/javascript" src="outoflinexulscript.js"></script>
  <script type="application/javascript"><![CDATA[
const {NetUtil} = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
let base = /.*\//.exec(window.location.href)[0];
const {checkFromJSM} = ChromeUtils.import(base + "file_expandosharing.jsm");

function inlinefunction() {
         return 42;
@@ -27,21 +29,22 @@ is(src.charAt(src.length - 1), "}", "inline XUL source should end with '}'");
src = outoflinefunction.toSource();
isnot(src.indexOf("return 42"), -1, "out of line XUL script should have source")
is(src.charAt(src.length - 1), "}", "out of line XUL source should end with '}'");
src = NetUtil.asyncFetch.toSource();
isnot(src.indexOf("return"), -1, "JSM should have source");
src = checkFromJSM.toSource();
isnot(src.indexOf("catch"), -1, "JSM should have source");
var ns = {};
Services.scriptloader.loadSubScript("resource://gre/modules/NetUtil.jsm", ns);
src = ns.NetUtil.asyncFetch.toSource();
isnot(src.indexOf("return"), -1, "subscript should have source");
Services.scriptloader.loadSubScript(base + "file_expandosharing.jsm", ns);
src = ns.checkFromJSM.toSource();
isnot(src.indexOf("catch"), -1, "subscript should have source");

var base = /.*\//.exec(window.location.href)[0];
var reg = Cc["@mozilla.org/chrome/chrome-registry;1"].getService(Ci.nsIChromeRegistry);
var resolvedBase = reg.convertChromeURL(NetUtil.newURI(base)).spec;

ns = {};
ns = {
  base,
};
Services.scriptloader.loadSubScript(resolvedBase + "subscript.js", ns);
src = ns.NetUtil.asyncFetch.toSource();
isnot(src.indexOf("return"), -1, "subscript of a subscript should have source");
src = ns.checkFromJSM.toSource();
isnot(src.indexOf("catch"), -1, "subscript of a subscript should have source");

ns = {};
Services.scriptloader.loadSubScript(resolvedBase + "utf8_subscript.js", ns);
+10 −12
Original line number Diff line number Diff line
function run_test() {
  // Existing module.
  Assert.ok(Cu.isModuleLoaded("resource://gre/modules/NetUtil.jsm"),
  Assert.ok(!Cu.isModuleLoaded("resource://test/jsm_loaded-1.jsm"),
            "isModuleLoaded returned correct value for non-loaded module");
  ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
  Assert.ok(Cu.isModuleLoaded("resource://gre/modules/NetUtil.jsm"),
  ChromeUtils.import("resource://test/jsm_loaded-1.jsm");
  Assert.ok(Cu.isModuleLoaded("resource://test/jsm_loaded-1.jsm"),
            "isModuleLoaded returned true after loading that module");
  Cu.unload("resource://gre/modules/NetUtil.jsm");
  Assert.ok(!Cu.isModuleLoaded("resource://gre/modules/NetUtil.jsm"),
  Cu.unload("resource://test/jsm_loaded-1.jsm");
  Assert.ok(!Cu.isModuleLoaded("resource://test/jsm_loaded-1.jsm"),
            "isModuleLoaded returned false after unloading that module");

  // Non-existing module
  Assert.ok(!Cu.isModuleLoaded("resource://gre/modules/non-existing-module.jsm"),
            "isModuleLoaded returned correct value for non-loaded module");
  try {
    ChromeUtils.import("resource://gre/modules/non-existing-module.jsm");
    Assert.ok(false,
              "Should have thrown while trying to load a non existing file");
  } catch (ex) {}
  Assert.ok(!Cu.isModuleLoaded("resource://gre/modules/non-existing-module.jsm"),
            "isModuleLoaded returned correct value for non-loaded module");
  Assert.throws(
    () => ChromeUtils.import("resource://gre/modules/non-existing-module.jsm"),
    /NS_ERROR_FILE_NOT_FOUND/,
    "Should have thrown while trying to load a non existing file"
  );
}
+10 −10
Original line number Diff line number Diff line
@@ -75,39 +75,39 @@ add_task(function test_lazy_proxy() {
add_task(function test_module_version() {
  // Test that passing a string instead of an initialization function
  // makes this behave like a lazy module getter.
  const NET_UTIL_URI = "resource://gre/modules/NetUtil.jsm";
  const TEST_FILE_URI = "resource://test/TestFile.jsm";
  let underlyingObject;

  Cu.unload(NET_UTIL_URI);
  Cu.unload(TEST_FILE_URI);

  let lazyProxy = XPCOMUtils.defineLazyProxy(
    null,
    "NetUtil",
    NET_UTIL_URI,
    "TestFile",
    TEST_FILE_URI,
    null, /* no stubs */
    function untrapCallback(object) {
      underlyingObject = object;
    }
  );

  Assert.ok(!Cu.isModuleLoaded(NET_UTIL_URI), "The NetUtil module was not loaded by the lazy proxy definition");
  Assert.ok(!Cu.isModuleLoaded(TEST_FILE_URI), "The NetUtil module was not loaded by the lazy proxy definition");

  // Access the object, which will evaluate the proxy.
  lazyProxy.foo = "bar";

  // Module was loaded.
  Assert.ok(Cu.isModuleLoaded(NET_UTIL_URI), "The NetUtil module was loaded");
  Assert.ok(Cu.isModuleLoaded(TEST_FILE_URI), "The NetUtil module was loaded");

  let { NetUtil } = ChromeUtils.import(NET_UTIL_URI, {});
  let { TestFile } = ChromeUtils.import(TEST_FILE_URI, {});

  // Avoids a gigantic stringification in the logs.
  Assert.ok(NetUtil === underlyingObject, "The module loaded is the same as the one directly obtained by ChromeUtils.import");
  Assert.ok(TestFile === underlyingObject, "The module loaded is the same as the one directly obtained by ChromeUtils.import");

  // Proxy correctly passed the setter to the underlying object.
  Assert.equal(NetUtil.foo, "bar", "Proxy correctly passed the setter to the underlying object");
  Assert.equal(TestFile.foo, "bar", "Proxy correctly passed the setter to the underlying object");

  delete lazyProxy.foo;

  // Proxy correctly passed the delete operation to the underlying object.
  Assert.ok(!NetUtil.hasOwnProperty("foo"), "Proxy correctly passed the delete operation to the underlying object");
  Assert.ok(!TestFile.hasOwnProperty("foo"), "Proxy correctly passed the delete operation to the underlying object");
});
+8 −8
Original line number Diff line number Diff line
@@ -4,25 +4,25 @@

function run_test() {
  var scope1 = {};
  var exports1 = ChromeUtils.import("resource://gre/modules/NetUtil.jsm", scope1);
  var exports1 = ChromeUtils.import("resource://test/TestBlob.jsm", scope1);

  var scope2 = {};
  var exports2 = ChromeUtils.import("resource://gre/modules/NetUtil.jsm", scope2);
  var exports2 = ChromeUtils.import("resource://test/TestBlob.jsm", scope2);

  Assert.ok(exports1 === exports2);
  Assert.ok(scope1.NetUtil === scope2.NetUtil);
  Assert.ok(scope1.TestBlob === scope2.TestBlob);

  Cu.unload("resource://gre/modules/NetUtil.jsm");
  Cu.unload("resource://test/TestBlob.jsm");

  var scope3 = {};
  var exports3 = ChromeUtils.import("resource://gre/modules/NetUtil.jsm", scope3);
  var exports3 = ChromeUtils.import("resource://test/TestBlob.jsm", scope3);

  Assert.equal(false, exports1 === exports3);
  Assert.equal(false, scope1.NetUtil === scope3.NetUtil);
  Assert.equal(false, scope1.TestBlob === scope3.TestBlob);

  // When the jsm was unloaded, the value of all its global's properties were
  // set to undefined. While it must be safe (not crash) to call into the
  // module, we expect it to throw an error (e.g., when trying to use Ci).
  try { scope1.NetUtil.newURI("http://www.example.com"); } catch (e) {}
  try { scope3.NetUtil.newURI("http://www.example.com"); } catch (e) {}
  try { scope1.TestBlob.doTest(() => {}); } catch (e) {}
  try { scope3.TestBlob.doTest(() => {}); } catch (e) {}
}