js/xpconnect/tests/unit/test_tearoffs.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:119a6fa3749f
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 const Cc = Components.classes;
6 const Ci = Components.interfaces;
7
8 function run_test() {
9
10 // Load the component manifest containing our test interface implementations.
11 Components.manager.autoRegister(do_get_file('../components/js/xpctest.manifest'));
12
13 // Shortcut the interfaces we're using.
14 var ifs = {
15 a: Ci['nsIXPCTestInterfaceA'],
16 b: Ci['nsIXPCTestInterfaceB'],
17 c: Ci['nsIXPCTestInterfaceC']
18 };
19
20 // Shortcut the class we're instantiating. This implements all three interfaces.
21 var cls = Cc["@mozilla.org/js/xpc/test/js/TestInterfaceAll;1"];
22
23 // Run through the logic a few times.
24 for (i = 0; i < 2; ++i)
25 play_with_tearoffs(ifs, cls);
26 }
27
28 function play_with_tearoffs(ifs, cls) {
29
30 // Allocate a bunch of objects, QI-ed to B.
31 var instances = [];
32 for (var i = 0; i < 300; ++i)
33 instances.push(cls.createInstance(ifs.b));
34
35 // Nothing to collect.
36 gc();
37
38 // QI them to A.
39 instances.forEach(function(v, i, a) { v.QueryInterface(ifs.a); });
40
41 // QI them to C.
42 instances.forEach(function(v, i, a) { v.QueryInterface(ifs.c); });
43
44 // Check
45 do_check_true('name' in instances[10], 'Have the prop from A/B');
46 do_check_true('someInteger' in instances[10], 'Have the prop from C');
47
48 // Grab tearoff reflections for a and b.
49 var aTearOffs = instances.map(function(v, i, a) { return v.nsIXPCTestInterfaceA; } );
50 var bTearOffs = instances.map(function(v, i, a) { return v.nsIXPCTestInterfaceB; } );
51
52 // Check
53 do_check_true('name' in aTearOffs[1], 'Have the prop from A');
54 do_check_true(!('someInteger' in aTearOffs[1]), 'Dont have the prop from C');
55
56 // Nothing to collect.
57 gc();
58
59 // Null out every even instance pointer.
60 for (var i = 0; i < instances.length; ++i)
61 if (i % 2 == 0)
62 instances[i] = null;
63
64 // Nothing to collect, since we still have the A and B tearoff reflections.
65 gc();
66
67 // Null out A tearoff reflections that are a multiple of 3.
68 for (var i = 0; i < aTearOffs.length; ++i)
69 if (i % 3 == 0)
70 aTearOffs[i] = null;
71
72 // Nothing to collect, since we still have the B tearoff reflections.
73 gc();
74
75 // Null out B tearoff reflections that are a multiple of 5.
76 for (var i = 0; i < bTearOffs.length; ++i)
77 if (i % 5 == 0)
78 bTearOffs[i] = null;
79
80 // This should collect every 30th object (indices that are multiples of 2, 3, and 5).
81 gc();
82
83 // Kill the b tearoffs entirely.
84 bTearOffs = 0;
85
86 // Collect more.
87 gc();
88
89 // Get C tearoffs.
90 var cTearOffs = instances.map(function(v, i, a) { return v ? v.nsIXPCTestInterfaceC : null; } );
91
92 // Check.
93 do_check_true(!('name' in cTearOffs[1]), 'Dont have the prop from A');
94 do_check_true('someInteger' in cTearOffs[1], 'have the prop from C');
95
96 // Null out the a tearoffs.
97 aTearOffs = null;
98
99 // Collect all even indices.
100 gc();
101
102 // Collect all indices.
103 instances = null;
104 gc();
105
106 // Give ourselves a pat on the back. :-)
107 do_check_true(true, "Got all the way through without crashing!");
108 }

mercurial