michael@0: const StandardURL = Components.Constructor("@mozilla.org/network/standard-url;1", michael@0: "nsIStandardURL", michael@0: "init"); michael@0: const nsIStandardURL = Components.interfaces.nsIStandardURL; michael@0: michael@0: function symmetricEquality(expect, a, b) michael@0: { michael@0: /* Use if/else instead of |do_check_eq(expect, a.spec == b.spec)| so michael@0: that we get the specs output on the console if the check fails. michael@0: */ michael@0: if (expect) { michael@0: /* Check all the sub-pieces too, since that can help with michael@0: debugging cases when equals() returns something unexpected */ michael@0: /* We don't check port in the loop, because it can be defaulted in michael@0: some cases. */ michael@0: ["spec", "prePath", "scheme", "userPass", "username", "password", michael@0: "hostPort", "host", "path", "filePath", "param", "query", michael@0: "ref", "directory", "fileName", "fileBaseName", "fileExtension"] michael@0: .map(function(prop) { michael@0: dump("Testing '"+ prop + "'\n"); michael@0: do_check_eq(a[prop], b[prop]); michael@0: }); michael@0: } else { michael@0: do_check_neq(a.spec, b.spec); michael@0: } michael@0: do_check_eq(expect, a.equals(b)); michael@0: do_check_eq(expect, b.equals(a)); michael@0: } michael@0: michael@0: function stringToURL(str) { michael@0: return (new StandardURL(nsIStandardURL.URLTYPE_AUTHORITY, 80, michael@0: str, "UTF-8", null)) michael@0: .QueryInterface(Components.interfaces.nsIURL); michael@0: } michael@0: michael@0: function pairToURLs(pair) { michael@0: do_check_eq(pair.length, 2); michael@0: return pair.map(stringToURL); michael@0: } michael@0: michael@0: function test_setEmptyPath() michael@0: { michael@0: var pairs = michael@0: [ michael@0: ["http://example.com", "http://example.com/tests/dom/tests"], michael@0: ["http://example.com:80", "http://example.com/tests/dom/tests"], michael@0: ["http://example.com:80/", "http://example.com/tests/dom/test"], michael@0: ["http://example.com/", "http://example.com/tests/dom/tests"], michael@0: ["http://example.com/a", "http://example.com/tests/dom/tests"], michael@0: ["http://example.com:80/a", "http://example.com/tests/dom/tests"], michael@0: ].map(pairToURLs); michael@0: michael@0: for each (var [provided, target] in pairs) michael@0: { michael@0: symmetricEquality(false, target, provided); michael@0: michael@0: provided.path = ""; michael@0: target.path = ""; michael@0: michael@0: do_check_eq(provided.spec, target.spec); michael@0: symmetricEquality(true, target, provided); michael@0: } michael@0: } michael@0: michael@0: function test_setQuery() michael@0: { michael@0: var pairs = michael@0: [ michael@0: ["http://example.com", "http://example.com/?foo"], michael@0: ["http://example.com/bar", "http://example.com/bar?foo"], michael@0: ["http://example.com#bar", "http://example.com/?foo#bar"], michael@0: ["http://example.com/#bar", "http://example.com/?foo#bar"], michael@0: ["http://example.com/?longerthanfoo#bar", "http://example.com/?foo#bar"], michael@0: ["http://example.com/?longerthanfoo", "http://example.com/?foo"], michael@0: /* And one that's nonempty but shorter than "foo" */ michael@0: ["http://example.com/?f#bar", "http://example.com/?foo#bar"], michael@0: ["http://example.com/?f", "http://example.com/?foo"], michael@0: ].map(pairToURLs); michael@0: michael@0: for each (var [provided, target] in pairs) { michael@0: symmetricEquality(false, provided, target); michael@0: michael@0: provided.query = "foo"; michael@0: michael@0: do_check_eq(provided.spec, target.spec); michael@0: symmetricEquality(true, provided, target); michael@0: } michael@0: michael@0: [provided, target] = michael@0: ["http://example.com/#", "http://example.com/?foo#bar"].map(stringToURL); michael@0: symmetricEquality(false, provided, target); michael@0: provided.query = "foo"; michael@0: symmetricEquality(false, provided, target); michael@0: michael@0: var newProvided = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService) michael@0: .newURI("#bar", null, provided) michael@0: .QueryInterface(Components.interfaces.nsIURL); michael@0: michael@0: do_check_eq(newProvided.spec, target.spec); michael@0: symmetricEquality(true, newProvided, target); michael@0: michael@0: } michael@0: michael@0: function test_setRef() michael@0: { michael@0: var tests = michael@0: [ michael@0: ["http://example.com", "", "http://example.com/"], michael@0: ["http://example.com:80", "", "http://example.com:80/"], michael@0: ["http://example.com:80/", "", "http://example.com:80/"], michael@0: ["http://example.com/", "", "http://example.com/"], michael@0: ["http://example.com/a", "", "http://example.com/a"], michael@0: ["http://example.com:80/a", "", "http://example.com:80/a"], michael@0: michael@0: ["http://example.com", "x", "http://example.com/#x"], michael@0: ["http://example.com:80", "x", "http://example.com:80/#x"], michael@0: ["http://example.com:80/", "x", "http://example.com:80/#x"], michael@0: ["http://example.com/", "x", "http://example.com/#x"], michael@0: ["http://example.com/a", "x", "http://example.com/a#x"], michael@0: ["http://example.com:80/a", "x", "http://example.com:80/a#x"], michael@0: michael@0: ["http://example.com", "xx", "http://example.com/#xx"], michael@0: ["http://example.com:80", "xx", "http://example.com:80/#xx"], michael@0: ["http://example.com:80/", "xx", "http://example.com:80/#xx"], michael@0: ["http://example.com/", "xx", "http://example.com/#xx"], michael@0: ["http://example.com/a", "xx", "http://example.com/a#xx"], michael@0: ["http://example.com:80/a", "xx", "http://example.com:80/a#xx"], michael@0: michael@0: ["http://example.com", "xxxxxxxxxxxxxx", "http://example.com/#xxxxxxxxxxxxxx"], michael@0: ["http://example.com:80", "xxxxxxxxxxxxxx", "http://example.com:80/#xxxxxxxxxxxxxx"], michael@0: ["http://example.com:80/", "xxxxxxxxxxxxxx", "http://example.com:80/#xxxxxxxxxxxxxx"], michael@0: ["http://example.com/", "xxxxxxxxxxxxxx", "http://example.com/#xxxxxxxxxxxxxx"], michael@0: ["http://example.com/a", "xxxxxxxxxxxxxx", "http://example.com/a#xxxxxxxxxxxxxx"], michael@0: ["http://example.com:80/a", "xxxxxxxxxxxxxx", "http://example.com:80/a#xxxxxxxxxxxxxx"], michael@0: ]; michael@0: michael@0: for each (var [before, ref, result] in tests) michael@0: { michael@0: /* Test1: starting with empty ref */ michael@0: var a = stringToURL(before); michael@0: a.ref = ref; michael@0: var b = stringToURL(result); michael@0: michael@0: do_check_eq(a.spec, b.spec); michael@0: do_check_eq(ref, b.ref); michael@0: symmetricEquality(true, a, b); michael@0: michael@0: /* Test2: starting with non-empty */ michael@0: a.ref = "yyyy"; michael@0: var c = stringToURL(before); michael@0: c.ref = "yyyy"; michael@0: symmetricEquality(true, a, c); michael@0: michael@0: /* Test3: reset the ref */ michael@0: a.ref = ""; michael@0: symmetricEquality(true, a, stringToURL(before)); michael@0: michael@0: /* Test4: verify again after reset */ michael@0: a.ref = ref; michael@0: symmetricEquality(true, a, b); michael@0: } michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: test_setEmptyPath(); michael@0: test_setQuery(); michael@0: test_setRef(); michael@0: }