michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: michael@0: const Ci = Components.interfaces; michael@0: michael@0: function do_check_true(cond, text) { michael@0: // we don't have the test harness' utilities in this scope, so we need this michael@0: // little helper. In the failure case, the exception is propagated to the michael@0: // caller in the main run_test() function, and the test fails. michael@0: if (!cond) michael@0: throw "Failed check: " + text; michael@0: } michael@0: michael@0: function FileComponent() { michael@0: this.wrappedJSObject = this; michael@0: } michael@0: FileComponent.prototype = michael@0: { michael@0: doTest: function() { michael@0: // throw if anything goes wrong michael@0: michael@0: // find the current directory path michael@0: var file = Components.classes["@mozilla.org/file/directory_service;1"] michael@0: .getService(Ci.nsIProperties) michael@0: .get("CurWorkD", Ci.nsIFile); michael@0: file.append("xpcshell.ini"); michael@0: michael@0: // should be able to construct a file michael@0: var f1 = File(file.path); michael@0: // with either constructor syntax michael@0: var f2 = new File(file.path); michael@0: // and with nsIFiles michael@0: var f3 = File(file); michael@0: var f4 = new File(file); michael@0: michael@0: // do some tests michael@0: do_check_true(f1 instanceof Ci.nsIDOMFile, "Should be a DOM File"); michael@0: do_check_true(f2 instanceof Ci.nsIDOMFile, "Should be a DOM File"); michael@0: do_check_true(f3 instanceof Ci.nsIDOMFile, "Should be a DOM File"); michael@0: do_check_true(f4 instanceof Ci.nsIDOMFile, "Should be a DOM File"); michael@0: michael@0: do_check_true(f1.name == "xpcshell.ini", "Should be the right file"); michael@0: do_check_true(f2.name == "xpcshell.ini", "Should be the right file"); michael@0: do_check_true(f3.name == "xpcshell.ini", "Should be the right file"); michael@0: do_check_true(f4.name == "xpcshell.ini", "Should be the right file"); michael@0: michael@0: do_check_true(f1.type == "", "Should be the right type"); michael@0: do_check_true(f2.type == "", "Should be the right type"); michael@0: do_check_true(f3.type == "", "Should be the right type"); michael@0: do_check_true(f4.type == "", "Should be the right type"); michael@0: michael@0: var threw = false; michael@0: try { michael@0: // Needs a ctor argument michael@0: var f7 = File(); michael@0: } catch (e) { michael@0: threw = true; michael@0: } michael@0: do_check_true(threw, "No ctor arguments should throw"); michael@0: michael@0: var threw = false; michael@0: try { michael@0: // Needs a valid ctor argument michael@0: var f7 = File(Date(132131532)); michael@0: } catch (e) { michael@0: threw = true; michael@0: } michael@0: do_check_true(threw, "Passing a random object should fail"); michael@0: michael@0: var threw = false michael@0: try { michael@0: // Directories fail michael@0: var dir = Components.classes["@mozilla.org/file/directory_service;1"] michael@0: .getService(Ci.nsIProperties) michael@0: .get("CurWorkD", Ci.nsIFile); michael@0: var f7 = File(dir) michael@0: } catch (e) { michael@0: threw = true; michael@0: } michael@0: do_check_true(threw, "Can't create a File object for a directory"); michael@0: michael@0: return true; michael@0: }, michael@0: michael@0: // nsIClassInfo + information for XPCOM registration code in XPCOMUtils.jsm michael@0: classDescription: "File in components scope code", michael@0: classID: Components.ID("{da332370-91d4-464f-a730-018e14769cab}"), michael@0: contractID: "@mozilla.org/tests/component-file;1", michael@0: michael@0: // nsIClassInfo michael@0: implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT, michael@0: flags: 0, michael@0: michael@0: getInterfaces: function getInterfaces(aCount) { michael@0: var interfaces = [Components.interfaces.nsIClassInfo]; michael@0: aCount.value = interfaces.length; michael@0: return interfaces; michael@0: }, michael@0: michael@0: getHelperForLanguage: function getHelperForLanguage(aLanguage) { michael@0: return null; michael@0: }, michael@0: michael@0: // nsISupports michael@0: QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIClassInfo]) michael@0: }; michael@0: michael@0: var gComponentsArray = [FileComponent]; michael@0: this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray);