3 // Tests that JS iterators are automatically wrapped into
4 // equivalent nsISimpleEnumerator objects.
6 const Variant = Components.Constructor("@mozilla.org/variant;1",
9 const SupportsInterfacePointer = Components.Constructor(
10 "@mozilla.org/supports-interface-pointer;1", "nsISupportsInterfacePointer");
12 function wrapEnumerator1(iter) {
13 var ip = SupportsInterfacePointer();
15 return ip.data.QueryInterface(Ci.nsISimpleEnumerator);
18 function wrapEnumerator2(iter) {
19 var ip = SupportsInterfacePointer();
21 QueryInterface: ChromeUtils.generateQI(["nsIFilePicker"]),
26 return ip.data.QueryInterface(Ci.nsIFilePicker).files;
30 function enumToArray(iter) {
32 while (iter.hasMoreElements()) {
33 result.push(iter.getNext().QueryInterface(Ci.nsIVariant));
38 add_task(async function test_wrapped_js_enumerator() {
39 let array = [1, 2, 3, 4];
41 for (let wrapEnumerator of [wrapEnumerator1, wrapEnumerator2]) {
42 // Test a plain JS iterator. This should automatically be wrapped into
43 // an equivalent nsISimpleEnumerator.
45 let iter = wrapEnumerator(array.values());
46 let result = enumToArray(iter);
48 deepEqual(result, array, "Got correct result");
51 // Test an object with a QueryInterface method, which implements
52 // nsISimpleEnumerator. This should be wrapped and used directly.
55 QueryInterface: ChromeUtils.generateQI(["nsISimpleEnumerator"]),
58 return this._idx < array.length;
61 return Variant(array[this._idx++]);
65 let iter = wrapEnumerator(obj);
66 let result = enumToArray(iter);
68 deepEqual(result, array, "Got correct result");