2 <script src=
"../../resources/js-test.js"></script>
4 description('IDL dictionary unittest');
6 var testObject1
= { foo
: 'x' };
7 var testObject2
= { bar
: 'y' };
9 if (window
.internals
&& internals
.dictionaryTest
) {
10 var dictionaryTest
= window
.internals
.dictionaryTest();
12 debug('Test for setting an empty dictionary');
13 dictionaryTest
.set({});
14 dict
= dictionaryTest
.get();
15 shouldBeUndefined('dict.longMember');
16 shouldBe('dict.longMemberWithDefault', '42');
17 shouldBeUndefined('dict.longOrNullMember');
18 shouldBeNull('dict.longOrNullMemberWithDefault');
19 shouldBeUndefined('dict.booleanMember');
20 shouldBeUndefined('dict.doubleMember');
21 shouldBeUndefined('dict.unrestrictedDoubleMember');
22 shouldBeUndefined('dict.stringMember');
23 shouldBeEqualToString('dict.stringMemberWithDefault', 'defaultStringValue');
24 shouldBeUndefined('dict.byteStringMember');
25 shouldBeUndefined('dict.usvStringMember');
26 shouldBeUndefined('dict.stringSequenceMember');
27 shouldBe('dict.stringSequenceMemberWithDefault', '[]');
28 shouldBeUndefined('dict.stringSequenceOrNullMember');
29 shouldBeUndefined('dict.elementMember');
30 shouldBeUndefined('dict.elementOrNullMember');
31 shouldBeUndefined('dict.enumMember');
32 shouldBeUndefined('dict.enumArrayMember');
33 shouldBeEqualToString('dict.enumMemberWithDefault', 'foo');
34 shouldBeUndefined('dict.enumOrNullMember');
35 shouldBeUndefined('dict.objectMember');
36 shouldBeNull('dict.objectOrNullMemberWithDefault');
37 shouldBeUndefined('dict.doubleOrStringMember');
38 shouldBeUndefined('dict.doubleOrStringSequenceMember');
39 shouldBeNull('dict.eventTargetOrNullMember');
42 debug('Test for setting undefined');
43 dictionaryTest
.set(undefined);
44 dict
= dictionaryTest
.get();
45 shouldBeUndefined('dict.longMember');
46 shouldBe('dict.longMemberWithDefault', '42');
47 shouldBeUndefined('dict.longOrNullMember');
48 shouldBeNull('dict.longOrNullMemberWithDefault');
49 shouldBeUndefined('dict.booleanMember');
50 shouldBeUndefined('dict.doubleMember');
51 shouldBeUndefined('dict.unrestrictedDoubleMember');
52 shouldBeUndefined('dict.stringMember');
53 shouldBeUndefined('dict.byteStringMember');
54 shouldBeUndefined('dict.usvStringMember');
55 shouldBeEqualToString('dict.stringMemberWithDefault', 'defaultStringValue');
56 shouldBeUndefined('dict.stringSequenceMember');
57 shouldBe('dict.stringSequenceMemberWithDefault', '[]');
58 shouldBeUndefined('dict.stringSequenceOrNullMember');
59 shouldBeUndefined('dict.elementMember');
60 shouldBeUndefined('dict.elementOrNullMember');
61 shouldBeUndefined('dict.objectMember');
62 shouldBeNull('dict.objectOrNullMemberWithDefault');
63 shouldBeNull('dict.eventTargetOrNullMember');
66 var element1
= document
.createElement('div');
68 debug('Test for setting valid values');
71 longMemberWithDefault
: 2,
73 longOrNullMemberWithDefault
: 4,
74 stringMember
: 'modifiedString1',
75 stringMemberWithDefault
: 'modifiedString2',
76 byteStringMember
: '\x00\x01\xFE\xFF',
77 usvStringMember
: '!@#123ABCabc\xA0\uD800\uDC00',
80 unrestrictedDoubleMember
: NaN
,
81 stringSequenceMember
: ['foo', 'bar', 'baz'],
82 stringSequenceMemberWithDefault
: ['foo', 'bar', 'baz'],
83 stringSequenceOrNullMember
: [],
84 elementMember
: element1
,
85 elementOrNullMember
: null,
87 enumMemberWithDefault
: 'bar',
88 enumOrNullMember
: 'baz',
89 enumArrayMember
: ['foo', 'bar', 'baz'],
90 objectMember
: testObject1
,
91 objectOrNullMemberWithDefault
: testObject2
,
92 doubleOrStringMember
: 3.14,
93 doubleOrStringSequenceMember
: [3.14, 'Hello'],
94 eventTargetOrNullMember
: element1
96 dict
= dictionaryTest
.get();
97 shouldBe('dict.longMember', '1');
98 shouldBe('dict.longMemberWithDefault', '2');
99 shouldBe('dict.longOrNullMember', '3');
100 shouldBe('dict.longOrNullMemberWithDefault', '4');
101 shouldBeEqualToString('dict.stringMember', 'modifiedString1');
102 shouldBeEqualToString('dict.stringMemberWithDefault', 'modifiedString2');
103 shouldBeEqualToString('dict.byteStringMember', '\x00\x01\xFE\xFF');
104 shouldBeEqualToString('dict.usvStringMember', '!@#123ABCabc\xA0\uD800\uDC00');
105 shouldBeTrue('dict.booleanMember');
106 shouldBe('dict.doubleMember', '3.14');
107 shouldBe('dict.unrestrictedDoubleMember', 'NaN');
108 shouldBe('dict.stringSequenceMember', '["foo", "bar", "baz"]');
109 shouldBe('dict.stringSequenceMemberWithDefault', '["foo", "bar", "baz"]');
110 shouldBe('dict.stringSequenceOrNullMember', '[]');
111 shouldBe('dict.elementMember', 'element1');
112 // This will be undefined as the spec says
113 shouldBeUndefined('dict.elementOrNullMember');
114 shouldBeEqualToString('dict.enumMember', 'foo');
115 shouldBeEqualToString('dict.enumMemberWithDefault', 'bar');
116 shouldBeEqualToString('dict.enumOrNullMember', 'baz');
117 shouldBe('dict.enumArrayMember', '["foo", "bar", "baz"]');
118 shouldBe('dict.objectMember', 'testObject1');
119 shouldBe('dict.objectOrNullMemberWithDefault', 'testObject2');
120 shouldBe('dict.doubleOrStringMember', '3.14');
121 shouldBe('dict.doubleOrStringSequenceMember', '[3.14, "Hello"]');
122 shouldBe('dict.eventTargetOrNullMember', 'element1');
125 debug('Additional test for union type members');
127 doubleOrStringMember
: "foo",
129 dict
= dictionaryTest
.get();
130 shouldBeEqualToString('dict.doubleOrStringMember', 'foo');
132 doubleOrStringMember
: {},
134 dict
= dictionaryTest
.get();
135 shouldBeEqualToString('dict.doubleOrStringMember', '[object Object]');
137 doubleOrStringMember
: [],
139 dict
= dictionaryTest
.get();
140 shouldBeEqualToString('dict.doubleOrStringMember', '');
142 doubleOrStringMember
: null,
144 dict
= dictionaryTest
.get();
145 shouldBeEqualToString('dict.doubleOrStringMember', 'null');
147 doubleOrStringMember
: undefined,
149 dict
= dictionaryTest
.get();
150 shouldBeUndefined('dict.doubleOrStringMember');
154 debug('Test for explicit undefined or null, and missing members');
156 longMember
: undefined,
157 longMemberWithDefault
: null,
158 longOrNullMember
: undefined,
159 longOrNullMemberWithDefault
: null,
161 dict
= dictionaryTest
.get();
162 shouldBeUndefined('dict.longMember');
163 // ToNumber(null) results in 0.
164 shouldBe('dict.longMemberWithDefault', '0');
165 // Passing undefined shouldn't invoke any conversion.
166 shouldBeUndefined('dict.longOrNullMember');
167 // Nullable and its default value is null
168 shouldBeNull('dict.longOrNullMemberWithDefault');
171 debug('Test for setting invalid member');
172 dictionaryTest
.set({invalidMember
: 'shouldNotBeSet'});
173 dict
= dictionaryTest
.get();
174 shouldBeUndefined('dict.invalidMember');
177 debug('Test for setting invalid double value');
178 shouldThrow("dictionaryTest.set({doubleMember: NaN})");
179 shouldThrow("dictionaryTest.set({doubleMember: Infinity})");
180 shouldThrow("dictionaryTest.set({doubleMember: -Infinity})");
181 shouldThrow("dictionaryTest.set({doubleMember: 'invalid'})");
184 debug('Test for setting invalid ByteString value');
185 shouldThrow("dictionaryTest.set({byteStringMember: '\u0100'})");
188 debug('Test for setting invalid USVString value');
189 dictionaryTest
.set({usvStringMember
: '_\uDC00_\uD800_\uDC00\uD800_'});
190 dict
= dictionaryTest
.get();
191 shouldBeEqualToString('dict.usvStringMember', '_\uFFFD_\uFFFD_\uFFFD\uFFFD_');
193 debug('Test for setting invalid enum value');
194 shouldThrow("dictionaryTest.set({enumMember: 'invalid'})");
195 shouldThrow("dictionaryTest.set({enumArrayMember: ['foo', 'invalid', 'baz']})");
198 debug('Test for setting invalid object value');
199 shouldThrow("dictionaryTest.set({objectMember: 42})");
200 shouldThrow("dictionaryTest.set({objectMember: 'invalid'})");
203 debug('Test for setting invalid Element value');
204 shouldThrow("dictionaryTest.set({elementMember: 42})");
205 shouldThrow("dictionaryTest.set({elementMember: testObject1})");
206 shouldThrow("dictionaryTest.set({elementMember: []})");
207 shouldThrow("dictionaryTest.set({elementMember: document})");
210 debug('Test for passing invalid dictionary values');
211 shouldThrow("dictionaryTest.set(42)");
212 shouldThrow("dictionaryTest.set('string')");
215 debug('Test for [Clamp] and [EnforceRange] member');
217 longMember
: 2147483648,
218 longMemberWithClamp
: 2147483648
220 dict
= dictionaryTest
.get();
221 shouldBe('dict.longMember', '-2147483648');
222 shouldBe('dict.longMemberWithClamp', '2147483647');
223 shouldThrow('dictionaryTest.set({ longMemberWithEnforceRange: 2147483648 })');
226 debug('Test for [ImplementedAs] and [DeprecateAs] member');
228 deprecateLongMember
: 42
230 dict
= dictionaryTest
.get();
231 shouldBe('dict.longMember', '42');
234 debug('Test for passing EventTarget');
236 eventTargetOrNullMember
: window
238 dict
= dictionaryTest
.get();
239 shouldBe('dict.eventTargetOrNullMember', 'window');
241 eventTargetOrNullMember
: null
243 dict
= dictionaryTest
.get();
244 shouldBeNull('dict.eventTargetOrNullMember');
246 eventTargetOrNullMember
: undefined
248 dict
= dictionaryTest
.get();
249 shouldBeNull('dict.eventTargetOrNullMember');
250 shouldThrow("dictionaryTest.set({eventTargetOrNullMember: 'invalid'})");
251 shouldThrow("dictionaryTest.set({eventTargetOrNullMember: 42})");
252 shouldThrow("dictionaryTest.set({eventTargetOrNullMember: []})");
253 shouldThrow("dictionaryTest.set({eventTargetOrNullMember: {}})");
256 debug('Test for passing Dictionary (not IDL dictionary)');
258 dictionaryMember
: {'foo': 'x', 'bar': 'y'}
260 properties
= dictionaryTest
.getDictionaryMemberProperties();
261 shouldBeEqualToString('properties.foo', 'x');
262 shouldBeEqualToString('properties.bar', 'y');
263 shouldBeUndefined('properties.baz');
264 dictionaryTest
.set({dictionaryMember
: undefined});
265 properties
= dictionaryTest
.getDictionaryMemberProperties();
266 shouldBe('properties', '{}');
267 shouldThrow("dictionaryTest.set({dictionaryMember: 42})");
268 shouldThrow("dictionaryTest.set({dictionaryMember: 'foo'})");
271 debug('Test for derived dictionary');
272 dictionaryTest
.setDerived({ requiredBooleanMember
: true });
273 derived
= dictionaryTest
.getDerived();
274 shouldBeUndefined('derived.longMember');
275 shouldBe('derived.longMemberWithDefault', '42');
276 shouldBeUndefined('derived.longOrNullMember');
277 shouldBeNull('derived.longOrNullMemberWithDefault');
278 shouldBeUndefined('derived.booleanMember');
279 shouldBeUndefined('derived.doubleMember');
280 shouldBeUndefined('derived.stringMember');
281 shouldBeEqualToString('derived.stringMemberWithDefault', 'defaultStringValue');
282 shouldBeUndefined('derived.stringSequenceMember');
283 shouldBe('derived.stringSequenceMemberWithDefault', '[]');
284 shouldBeUndefined('derived.stringSequenceOrNullMember');
285 shouldBeUndefined('derived.elementMember');
286 shouldBeUndefined('derived.elementOrNullMember');
287 shouldBeUndefined('derived.enumMember');
288 shouldBeEqualToString('derived.enumMemberWithDefault', 'foo');
289 shouldBeUndefined('derived.enumOrNullMember');
290 shouldBeUndefined('derived.objectMember');
291 shouldBeNull('derived.objectOrNullMemberWithDefault');
292 shouldBeUndefined('derived.derivedStringMember');
293 shouldBeEqualToString('derived.derivedStringMemberWithDefault', 'derivedDefaultStringValue');
294 shouldBeTrue('derived.requiredBooleanMember');
297 dictionaryTest
.setDerived({
299 stringMemberWithDefault
: 'modifiedString',
300 derivedStringMember
: 'modifiedString2',
301 derivedStringMemberWithDefault
: 'modifiedString3',
302 invalidMember
: 'shouldNotBeSet',
303 requiredBooleanMember
: false,
305 derived
= dictionaryTest
.getDerived();
306 shouldBe('derived.longMember', '1');
307 shouldBeEqualToString('derived.stringMemberWithDefault', 'modifiedString');
308 shouldBeEqualToString('derived.derivedStringMember', 'modifiedString2');
309 shouldBeEqualToString('derived.derivedStringMemberWithDefault', 'modifiedString3');
310 shouldBeUndefined('derived.invalidMember');
311 shouldBeFalse('derived.requiredBooleanMember');
314 debug('Test for passing invalid values as derived dictionary');
315 shouldThrow("dictionaryTest.setDerived({objectMember: 42, requiredBooleanMember: false })");
316 shouldThrow("dictionaryTest.setDerived({})"); // Missing required member.