Bug 532143 - NetUtil.newURI should take a string or nsIFile

You can now automagically get an nsIURI providing a string or an nsIFile.
r=bz
sr=vlad
This commit is contained in:
Shawn Wilsher 2009-12-15 23:33:03 -08:00
Родитель e9b69b691f
Коммит c1f9653a4e
2 изменённых файлов: 46 добавлений и 11 удалений

Просмотреть файл

@ -172,29 +172,36 @@ const NetUtil = {
}, },
/** /**
* Constructs a new URI for the given spec, character set, and base URI. * Constructs a new URI for the given spec, character set, and base URI, or
* an nsIFile.
* *
* @param aSpec * @param aTarget
* The spec for the desired URI. * The string spec for the desired URI or an nsIFile.
* @param aOriginCharset [optional] * @param aOriginCharset [optional]
* The character set for the URI. * The character set for the URI. Only used if aTarget is not an
* nsIFile.
* @param aBaseURI [optional] * @param aBaseURI [optional]
* The base URI for the spec. * The base URI for the spec. Only used if aTarget is not an
* nsIFile.
* *
* @return an nsIURI object. * @return an nsIURI object.
*/ */
newURI: function NetUtil_newURI(aSpec, aOriginCharset, aBaseURI) newURI: function NetUtil_newURI(aTarget, aOriginCharset, aBaseURI)
{ {
if (!aSpec) { if (!aTarget) {
let exception = new Components.Exception( let exception = new Components.Exception(
"Must have a non-null spec", "Must have a non-null string spec or nsIFile object",
Cr.NS_ERROR_INVALID_ARG, Cr.NS_ERROR_INVALID_ARG,
Components.stack.caller Components.stack.caller
); );
throw exception; throw exception;
} }
return this.ioService.newURI(aSpec, aOriginCharset, aBaseURI); if (aTarget instanceof Ci.nsIFile) {
return this.ioService.newFileURI(aTarget);
}
return this.ioService.newURI(aTarget, aOriginCharset, aBaseURI);
}, },
/** /**

Просмотреть файл

@ -163,7 +163,8 @@ function test_newURI()
{ {
let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
// Check that we get the same URI back from the IO service and the utility method. // Check that we get the same URI back from the IO service and the utility
// method.
const TEST_URI = "http://mozilla.org"; const TEST_URI = "http://mozilla.org";
let iosURI = ios.newURI(TEST_URI, null, null); let iosURI = ios.newURI(TEST_URI, null, null);
let NetUtilURI = NetUtil.newURI(TEST_URI); let NetUtilURI = NetUtil.newURI(TEST_URI);
@ -172,6 +173,25 @@ function test_newURI()
run_next_test(); run_next_test();
} }
function test_newURI_takes_nsIFile()
{
let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
// Create a test file that we can pass into NetUtil.newURI
let file = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties).
get("TmpD", Ci.nsIFile);
file.append("NetUtil-test-file.tmp");
// Check that we get the same URI back from the IO service and the utility
// method.
let iosURI = ios.newFileURI(file);
let NetUtilURI = NetUtil.newURI(file);
do_check_true(iosURI.equals(NetUtilURI));
run_next_test();
}
function test_ioService() function test_ioService()
{ {
do_check_true(NetUtil.ioService instanceof Ci.nsIIOService); do_check_true(NetUtil.ioService instanceof Ci.nsIIOService);
@ -274,6 +294,7 @@ let tests = [
test_async_write_file_nsISafeOutputStream, test_async_write_file_nsISafeOutputStream,
test_newURI_no_spec_throws, test_newURI_no_spec_throws,
test_newURI, test_newURI,
test_newURI_takes_nsIFile,
test_ioService, test_ioService,
test_asyncFetch_no_channel, test_asyncFetch_no_channel,
test_asyncFetch_no_callback, test_asyncFetch_no_callback,
@ -287,7 +308,14 @@ function run_next_test()
if (index < tests.length) { if (index < tests.length) {
do_test_pending(); do_test_pending();
print("Running the next test: " + tests[index].name); print("Running the next test: " + tests[index].name);
tests[index++]();
// Asynchronous test exceptions do not kill the test...
try {
tests[index++]();
}
catch (e) {
do_throw(e);
}
} }
do_test_finished(); do_test_finished();