OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 define([ | |
6 "gin/test/expect", | |
7 "mojo/public/interfaces/bindings/tests/rect.mojom", | |
8 "mojo/public/interfaces/bindings/tests/test_structs.mojom", | |
9 "mojo/public/js/bindings/codec", | |
10 "mojo/public/js/bindings/validator", | |
11 ], function(expect, | |
12 rect, | |
13 testStructs, | |
14 codec, | |
15 validator) { | |
16 | |
17 function testConstructors() { | |
18 var r = new rect.Rect(); | |
19 expect(r).toEqual(new rect.Rect({x:0, y:0, width:0, height:0})); | |
20 expect(r).toEqual(new rect.Rect({foo:100, bar:200})); | |
21 | |
22 r.x = 10; | |
23 r.y = 20; | |
24 r.width = 30; | |
25 r.height = 40; | |
26 var rp = new testStructs.RectPair({first: r, second: r}); | |
27 expect(rp.first).toEqual(r); | |
28 expect(rp.second).toEqual(r); | |
29 | |
30 expect(new testStructs.RectPair({second: r}).first).toBeNull(); | |
31 | |
32 var nr = new testStructs.NamedRegion(); | |
33 expect(nr.name).toBeNull(); | |
34 expect(nr.rects).toBeNull(); | |
35 expect(nr).toEqual(new testStructs.NamedRegion({})); | |
36 | |
37 nr.name = "foo"; | |
38 nr.rects = [r, r, r]; | |
39 expect(nr).toEqual(new testStructs.NamedRegion({ | |
40 name: "foo", | |
41 rects: [r, r, r], | |
42 })); | |
43 | |
44 var e = new testStructs.EmptyStruct(); | |
45 expect(e).toEqual(new testStructs.EmptyStruct({foo:123})); | |
46 } | |
47 | |
48 function testNoDefaultFieldValues() { | |
49 var s = new testStructs.NoDefaultFieldValues(); | |
50 expect(s.f0).toEqual(false); | |
51 | |
52 // f1 - f10, number type fields | |
53 for (var i = 1; i <= 10; i++) | |
54 expect(s["f" + i]).toEqual(0); | |
55 | |
56 // f11,12 strings, f13-22 handles, f23-f26 arrays, f27,28 structs | |
57 for (var i = 11; i <= 28; i++) | |
58 expect(s["f" + i]).toBeNull(); | |
59 } | |
60 | |
61 function testDefaultFieldValues() { | |
62 var s = new testStructs.DefaultFieldValues(); | |
63 expect(s.f0).toEqual(true); | |
64 | |
65 // f1 - f12, number type fields | |
66 for (var i = 1; i <= 12; i++) | |
67 expect(s["f" + i]).toEqual(100); | |
68 | |
69 // f13,14 "foo" | |
70 for (var i = 13; i <= 14; i++) | |
71 expect(s["f" + i]).toEqual("foo"); | |
72 | |
73 // f15,16 a default instance of Rect | |
74 var r = new rect.Rect(); | |
75 expect(s.f15).toEqual(r); | |
76 expect(s.f16).toEqual(r); | |
77 } | |
78 | |
79 function testScopedConstants() { | |
80 expect(testStructs.ScopedConstants.TEN).toEqual(10); | |
81 expect(testStructs.ScopedConstants.ALSO_TEN).toEqual(10); | |
82 expect(testStructs.ScopedConstants.TEN_TOO).toEqual(10); | |
83 | |
84 expect(testStructs.ScopedConstants.EType.E0).toEqual(0); | |
85 expect(testStructs.ScopedConstants.EType.E1).toEqual(1); | |
86 expect(testStructs.ScopedConstants.EType.E2).toEqual(10); | |
87 expect(testStructs.ScopedConstants.EType.E3).toEqual(10); | |
88 expect(testStructs.ScopedConstants.EType.E4).toEqual(11); | |
89 | |
90 var s = new testStructs.ScopedConstants(); | |
91 expect(s.f0).toEqual(0); | |
92 expect(s.f1).toEqual(1); | |
93 expect(s.f2).toEqual(10); | |
94 expect(s.f3).toEqual(10); | |
95 expect(s.f4).toEqual(11); | |
96 expect(s.f5).toEqual(10); | |
97 expect(s.f6).toEqual(10); | |
98 } | |
99 | |
100 function structEncodeDecode(struct) { | |
101 var structClass = struct.constructor; | |
102 var builder = new codec.MessageBuilder(1234, structClass.encodedSize); | |
103 builder.encodeStruct(structClass, struct); | |
104 var message = builder.finish(); | |
105 | |
106 var messageValidator = new validator.Validator(message); | |
107 var err = structClass.validate(messageValidator, codec.kMessageHeaderSize); | |
108 expect(err).toEqual(validator.validationError.NONE); | |
109 | |
110 var reader = new codec.MessageReader(message); | |
111 return reader.decodeStruct(structClass); | |
112 } | |
113 | |
114 function testMapKeyTypes() { | |
115 var mapFieldsStruct = new testStructs.MapKeyTypes({ | |
116 f0: new Map([[true, false], [false, true]]), // map<bool, bool> | |
117 f1: new Map([[0, 0], [1, 127], [-1, -128]]), // map<int8, int8> | |
118 f2: new Map([[0, 0], [1, 127], [2, 255]]), // map<uint8, uint8> | |
119 f3: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int16, int16> | |
120 f4: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint16, uint16> | |
121 f5: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int32, int32> | |
122 f6: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint32, uint32> | |
123 f7: new Map([[0, 0], [1, 32767], [2, -32768]]), // map<int64, int64> | |
124 f8: new Map([[0, 0], [1, 32768], [2, 0xFFFF]]), // map<uint64, uint64> | |
125 f9: new Map([[1000.5, -50000], [100.5, 5000]]), // map<float, float> | |
126 f10: new Map([[-100.5, -50000], [0, 50000000]]), // map<double, double> | |
127 f11: new Map([["one", "two"], ["free", "four"]]), // map<string, string> | |
128 }); | |
129 var decodedStruct = structEncodeDecode(mapFieldsStruct); | |
130 expect(decodedStruct.f0).toEqual(mapFieldsStruct.f0); | |
131 expect(decodedStruct.f1).toEqual(mapFieldsStruct.f1); | |
132 expect(decodedStruct.f2).toEqual(mapFieldsStruct.f2); | |
133 expect(decodedStruct.f3).toEqual(mapFieldsStruct.f3); | |
134 expect(decodedStruct.f4).toEqual(mapFieldsStruct.f4); | |
135 expect(decodedStruct.f5).toEqual(mapFieldsStruct.f5); | |
136 expect(decodedStruct.f6).toEqual(mapFieldsStruct.f6); | |
137 expect(decodedStruct.f7).toEqual(mapFieldsStruct.f7); | |
138 expect(decodedStruct.f8).toEqual(mapFieldsStruct.f8); | |
139 expect(decodedStruct.f9).toEqual(mapFieldsStruct.f9); | |
140 expect(decodedStruct.f10).toEqual(mapFieldsStruct.f10); | |
141 expect(decodedStruct.f11).toEqual(mapFieldsStruct.f11); | |
142 } | |
143 | |
144 function testMapValueTypes() { | |
145 var mapFieldsStruct = new testStructs.MapValueTypes({ | |
146 f0: new Map([["a", ["b", "c"]], ["d", ["e"]]]), // array<string>> | |
147 f1: new Map([["a", null], ["b", ["c", "d"]]]), // array<string>?> | |
148 f2: new Map([["a", [null]], ["b", [null, "d"]]]), // array<string?>> | |
149 f3: new Map([["a", ["1", "2"]], ["b", ["1", "2"]]]), // array<string,2>> | |
150 f4: new Map([["a", [["1"]]], ["b", [null]]]), // array<array<string, 1>?> | |
151 f5: new Map([["a", [["1", "2"]]]]), // array<array<string, 2>, 1>> | |
152 }); | |
153 var decodedStruct = structEncodeDecode(mapFieldsStruct); | |
154 expect(decodedStruct.f0).toEqual(mapFieldsStruct.f0); | |
155 expect(decodedStruct.f1).toEqual(mapFieldsStruct.f1); | |
156 expect(decodedStruct.f2).toEqual(mapFieldsStruct.f2); | |
157 expect(decodedStruct.f3).toEqual(mapFieldsStruct.f3); | |
158 expect(decodedStruct.f4).toEqual(mapFieldsStruct.f4); | |
159 expect(decodedStruct.f5).toEqual(mapFieldsStruct.f5); | |
160 } | |
161 | |
162 testConstructors(); | |
163 testNoDefaultFieldValues(); | |
164 testDefaultFieldValues(); | |
165 testScopedConstants(); | |
166 testMapKeyTypes(); | |
167 testMapValueTypes(); | |
168 this.result = "PASS"; | |
169 }); | |
OLD | NEW |