summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/chrome/test_wrappers-2.xul
blob: abab47f802b00e4bab49d6f80750f5903408289d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=500931
-->
<window title="Mozilla Bug 500931"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=403005"
     target="_blank">Mozilla Bug 403005</a>
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=409298"
     target="_blank">Mozilla Bug 409298</a>
  </body>

  <!-- test code goes here -->
  <script type="application/javascript"><![CDATA[

    /** Test for Bug 403005 and 409298 **/

  SimpleTest.waitForExplicitFinish();

  function go()
  {
    ok(window instanceof Object, "window is instanceof Object");

    let wrap = $('ifr').contentWindow.wrappedJSObject;
    ok(!('obj' in XPCNativeWrapper(wrap)), "XPCNativeWrapper constructor works");
    let iwin = $('ifr').contentWindow;
    is(iwin.document.ELEMENT_NODE, 1, 'constants work through XrayWrapper');
    is(iwin.document.nodeName, "#document", 'attributes work through XrayWrappe

    location.foopy = 3;

    var xow_answer = [];
    for (let i in location)
        xow_answer.push(i);

    var xpcnw_answer = [];
    var xpcnw = new XPCNativeWrapper(location);
    xpcnw.barpy = 4;
    for (let i in xpcnw)
        xpcnw_answer.push(i);

    var expected = [
        "hash",
        "host",
        "hostname",
        "href",
        "pathname",
        "port",
        "protocol",
        "search",
        "reload",
        "replace",
        "assign",
        "foopy"
    ];

    var xpcnw_expected = expected.slice(0, expected.length - 1);
    xpcnw_expected.push("barpy");

    is(xow_answer.sort().toString(),
       expected.sort().toString(),
       'enumeration over XOWs walks the prototype chain');

    is(xpcnw_answer.sort().toString(),
       xpcnw_expected.sort().toString(),
       'enumeration over XPCNWs walks the prototype chain');

    var for_each_expected = [];
    for each (let i in new XPCNativeWrapper(location))
        for_each_expected.push(typeof i);

    var for_each_answer = [];
    for each (let i in Iterator(new XPCNativeWrapper(location)))
        for_each_answer.push(typeof i);

    is(for_each_answer.sort().toString(),
       for_each_expected.sort().toString(),
       'wrapper iterators are properly iterators');

    let sjow_answer = [];
    let (obj = { a: 3, next:1 }) {
        for (let i in wrap.to_iterate)
            sjow_answer.push(i);
        is(sjow_answer.sort().toString(), ['a', 'next'].sort().toString(),
           'enumeration over SJOWs walks the prototype chain');
    }

    sjow_answer = [];
    for (let i in wrap.location)
        sjow_answer.push(i);

    is(sjow_answer.sort().toString(),
       expected.sort().toString(),
       'enumeration over SJOWs walks the prototype chain and works over XOWs');

    for (let i in wrap.enumerate) {
        is(typeof i, "string", "enumeration on wrappers only returns strings");
    }

    isnot(wrap, new XPCNativeWrapper(iwin),
         'SJOWs equality hook returns false correctly against XPCNW');

    is(typeof(wrap.func), 'function',
       'SJOWs look like functions when they wrap functions');
    is(typeof(wrap.o), 'object',
       'SJOWs look like objects when they wrap objects');
    ok(wrap.o === wrap.o,
       "Identity holds when accessing the same object twice");

    var origProto = iwin.__proto__;
    try {
        iwin.__proto__ = iwin;
        ok(false, 'cyclic proto value allowed');
        iwin.__proto__ = origProto;
    } catch (e) {
        is(e.toString(),
           'TypeError: cyclic __proto__ value',
           'throw the right exception for a cyclic proto');
        is(iwin.__proto__, origProto, 'reset __proto__ after a cyclic proto');
    }

    try {
        is('PASS', iwin.eval("'PASS'"), 'iwin.eval throws an exception');
    } catch (e) {
        ok(false, 'iwin.eval does not throw an exception');
    }

    try {
        new XPCNativeWrapper("");
        ok(false, "How did we construct a wrapper around a primitive?");
    } catch (e) {
        ok(true, "Unable to new XPCNativeWrapper(primitive)");
    }

    try {
        is(XPCNativeWrapper(""), "", "XPCNativeWrapper as a function allows primitives");
    } catch (e) {
        ok(false, "Unable to wrap a primitive, even without 'new'");
    }

    // Some tests for SJOWs too.
    isnot(wrap.document.body != document.body, "body should not be the same");

    XPCNativeWrapper.prototype = {};
    let (w = new XPCNativeWrapper(iwin)) {
        ok(iwin.location, "able to set XPCNativeWrapper.prototype and do stuff with it");
    }

    is(new XPCNativeWrapper(iwin, Window).closed, false,
       "able to wrap a window in a window XPCNativeWrapper");
    try {
        new XPCNativeWrapper(document, Window);
        todo(false, "Able to wrap a document in a Window XPCNativeWrapper?")
    } catch (e) {
        ok(/ILLEGAL_VALUE/(e), "not able to wrap a document in a Window XPCNativeWrapper");
    }

    let (w = new XPCNativeWrapper($('ifr').contentWindow)) {
        w.foopybar = 5;
        ok(!("foopybar" in iwin), "XPCNativeWrappers allow expandos through");
        is(w.foopybar, 5, "can set expandos on XPCNativeWrappers, though");

        ok(delete w.foopybar, "deleting properties returns true correctly");
        ok(!("foopybar" in w), "Can delete properties from XPCNativeWrappers");

        is(w.window, iwin, "w.window exists and is the window");
        ok(delete w.iwin, "can delete builtin properties");
        is(w.window, iwin, "w.window is automatically recreated");

        iwin.foopy = 5;
        ok(delete w.foopy, "delete returns true");
        is(iwin.foopy, 5, "delete doesn't delete underlying properties");
        ok(delete iwin.foopy, "can delete window.foopy");
        ok(!("foopy" in iwin), "foopy is no longer in window");
    }

    try {
      is((function(x) { return x+1; }).apply(this, wrap.a), 2,
         "able to call apply with an XPCSafeJSObjectWrapped array");
    } catch (e) {
        ok(false,
           "Unable to call apply() with a XPCSafeJSObjectWrapped array");
    }

    try {
        iwin.__proto__ = null;
        is(iwin.__proto__, null,
           "allowed to update iwin.__proto__ to null");
    } catch (e) {
        ok(false, "some crazy exception was thrown");
    }

    try {
        new XPCSafeJSObjectWrapped(<x />).foo;
        ok(false, "Allowed to wrap E4X in SJOWs?");
    } catch (e) {
    }

    SimpleTest.finish();
  }
  ]]></script>
  <iframe type="content"
          src="http://mochi.test:8888/tests/js/xpconnect/tests/mochitest/file_wrappers-2.html"
          onload="go()"
          id="ifr">
  </iframe>
</window>