|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
6 |
|
7 const Ci = Components.interfaces; |
|
8 |
|
9 function do_check_true(cond, text) { |
|
10 // we don't have the test harness' utilities in this scope, so we need this |
|
11 // little helper. In the failure case, the exception is propagated to the |
|
12 // caller in the main run_test() function, and the test fails. |
|
13 if (!cond) |
|
14 throw "Failed check: " + text; |
|
15 } |
|
16 |
|
17 function FileComponent() { |
|
18 this.wrappedJSObject = this; |
|
19 } |
|
20 FileComponent.prototype = |
|
21 { |
|
22 doTest: function() { |
|
23 // throw if anything goes wrong |
|
24 |
|
25 // find the current directory path |
|
26 var file = Components.classes["@mozilla.org/file/directory_service;1"] |
|
27 .getService(Ci.nsIProperties) |
|
28 .get("CurWorkD", Ci.nsIFile); |
|
29 file.append("xpcshell.ini"); |
|
30 |
|
31 // should be able to construct a file |
|
32 var f1 = File(file.path); |
|
33 // with either constructor syntax |
|
34 var f2 = new File(file.path); |
|
35 // and with nsIFiles |
|
36 var f3 = File(file); |
|
37 var f4 = new File(file); |
|
38 |
|
39 // do some tests |
|
40 do_check_true(f1 instanceof Ci.nsIDOMFile, "Should be a DOM File"); |
|
41 do_check_true(f2 instanceof Ci.nsIDOMFile, "Should be a DOM File"); |
|
42 do_check_true(f3 instanceof Ci.nsIDOMFile, "Should be a DOM File"); |
|
43 do_check_true(f4 instanceof Ci.nsIDOMFile, "Should be a DOM File"); |
|
44 |
|
45 do_check_true(f1.name == "xpcshell.ini", "Should be the right file"); |
|
46 do_check_true(f2.name == "xpcshell.ini", "Should be the right file"); |
|
47 do_check_true(f3.name == "xpcshell.ini", "Should be the right file"); |
|
48 do_check_true(f4.name == "xpcshell.ini", "Should be the right file"); |
|
49 |
|
50 do_check_true(f1.type == "", "Should be the right type"); |
|
51 do_check_true(f2.type == "", "Should be the right type"); |
|
52 do_check_true(f3.type == "", "Should be the right type"); |
|
53 do_check_true(f4.type == "", "Should be the right type"); |
|
54 |
|
55 var threw = false; |
|
56 try { |
|
57 // Needs a ctor argument |
|
58 var f7 = File(); |
|
59 } catch (e) { |
|
60 threw = true; |
|
61 } |
|
62 do_check_true(threw, "No ctor arguments should throw"); |
|
63 |
|
64 var threw = false; |
|
65 try { |
|
66 // Needs a valid ctor argument |
|
67 var f7 = File(Date(132131532)); |
|
68 } catch (e) { |
|
69 threw = true; |
|
70 } |
|
71 do_check_true(threw, "Passing a random object should fail"); |
|
72 |
|
73 var threw = false |
|
74 try { |
|
75 // Directories fail |
|
76 var dir = Components.classes["@mozilla.org/file/directory_service;1"] |
|
77 .getService(Ci.nsIProperties) |
|
78 .get("CurWorkD", Ci.nsIFile); |
|
79 var f7 = File(dir) |
|
80 } catch (e) { |
|
81 threw = true; |
|
82 } |
|
83 do_check_true(threw, "Can't create a File object for a directory"); |
|
84 |
|
85 return true; |
|
86 }, |
|
87 |
|
88 // nsIClassInfo + information for XPCOM registration code in XPCOMUtils.jsm |
|
89 classDescription: "File in components scope code", |
|
90 classID: Components.ID("{da332370-91d4-464f-a730-018e14769cab}"), |
|
91 contractID: "@mozilla.org/tests/component-file;1", |
|
92 |
|
93 // nsIClassInfo |
|
94 implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT, |
|
95 flags: 0, |
|
96 |
|
97 getInterfaces: function getInterfaces(aCount) { |
|
98 var interfaces = [Components.interfaces.nsIClassInfo]; |
|
99 aCount.value = interfaces.length; |
|
100 return interfaces; |
|
101 }, |
|
102 |
|
103 getHelperForLanguage: function getHelperForLanguage(aLanguage) { |
|
104 return null; |
|
105 }, |
|
106 |
|
107 // nsISupports |
|
108 QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIClassInfo]) |
|
109 }; |
|
110 |
|
111 var gComponentsArray = [FileComponent]; |
|
112 this.NSGetFactory = XPCOMUtils.generateNSGetFactory(gComponentsArray); |