Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(485)

Side by Side Diff: mojo/public/js/codec_unittests.js

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/js/codec.js ('k') | mojo/public/js/connection.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/js/codec",
8 "mojo/public/interfaces/bindings/tests/rect.mojom",
9 "mojo/public/interfaces/bindings/tests/sample_service.mojom",
10 "mojo/public/interfaces/bindings/tests/test_structs.mojom",
11 ], function(expect, codec, rect, sample, structs) {
12 testBar();
13 testFoo();
14 testNamedRegion();
15 testTypes();
16 testAlign();
17 testUtf8();
18 testTypedPointerValidation();
19 this.result = "PASS";
20
21 function testBar() {
22 var bar = new sample.Bar();
23 bar.alpha = 1;
24 bar.beta = 2;
25 bar.gamma = 3;
26 bar.type = 0x08070605;
27 bar.extraProperty = "banana";
28
29 var messageName = 42;
30 var payloadSize = sample.Bar.encodedSize;
31
32 var builder = new codec.MessageBuilder(messageName, payloadSize);
33 builder.encodeStruct(sample.Bar, bar);
34
35 var message = builder.finish();
36
37 var expectedMemory = new Uint8Array([
38 16, 0, 0, 0,
39 2, 0, 0, 0,
40 42, 0, 0, 0,
41 0, 0, 0, 0,
42
43 16, 0, 0, 0,
44 4, 0, 0, 0,
45
46 1, 2, 3, 0,
47 5, 6, 7, 8,
48 ]);
49
50 var actualMemory = new Uint8Array(message.buffer.arrayBuffer);
51 expect(actualMemory).toEqual(expectedMemory);
52
53 var reader = new codec.MessageReader(message);
54
55 expect(reader.payloadSize).toBe(payloadSize);
56 expect(reader.messageName).toBe(messageName);
57
58 var bar2 = reader.decodeStruct(sample.Bar);
59
60 expect(bar2.alpha).toBe(bar.alpha);
61 expect(bar2.beta).toBe(bar.beta);
62 expect(bar2.gamma).toBe(bar.gamma);
63 expect("extraProperty" in bar2).toBeFalsy();
64 }
65
66 function testFoo() {
67 var foo = new sample.Foo();
68 foo.x = 0x212B4D5;
69 foo.y = 0x16E93;
70 foo.a = 1;
71 foo.b = 0;
72 foo.c = 3; // This will get truncated to one bit.
73 foo.bar = new sample.Bar();
74 foo.bar.alpha = 91;
75 foo.bar.beta = 82;
76 foo.bar.gamma = 73;
77 foo.data = [
78 4, 5, 6, 7, 8,
79 ];
80 foo.extra_bars = [
81 new sample.Bar(), new sample.Bar(), new sample.Bar(),
82 ];
83 for (var i = 0; i < foo.extra_bars.length; ++i) {
84 foo.extra_bars[i].alpha = 1 * i;
85 foo.extra_bars[i].beta = 2 * i;
86 foo.extra_bars[i].gamma = 3 * i;
87 }
88 foo.name = "I am a banana";
89 // This is supposed to be a handle, but we fake it with an integer.
90 foo.source = 23423782;
91 foo.array_of_array_of_bools = [
92 [true], [false, true]
93 ];
94 foo.array_of_bools = [
95 true, false, true, false, true, false, true, true
96 ];
97
98
99 var messageName = 31;
100 var payloadSize = 304;
101
102 var builder = new codec.MessageBuilder(messageName, payloadSize);
103 builder.encodeStruct(sample.Foo, foo);
104
105 var message = builder.finish();
106
107 var expectedMemory = new Uint8Array([
108 /* 0: */ 16, 0, 0, 0, 2, 0, 0, 0,
109 /* 8: */ 31, 0, 0, 0, 0, 0, 0, 0,
110 /* 16: */ 96, 0, 0, 0, 15, 0, 0, 0,
111 /* 24: */ 0xD5, 0xB4, 0x12, 0x02, 0x93, 0x6E, 0x01, 0,
112 /* 32: */ 5, 0, 0, 0, 0, 0, 0, 0,
113 /* 40: */ 72, 0, 0, 0, 0, 0, 0, 0,
114 ]);
115 // TODO(abarth): Test more of the message's raw memory.
116 var actualMemory = new Uint8Array(message.buffer.arrayBuffer,
117 0, expectedMemory.length);
118 expect(actualMemory).toEqual(expectedMemory);
119
120 var expectedHandles = [
121 23423782,
122 ];
123
124 expect(message.handles).toEqual(expectedHandles);
125
126 var reader = new codec.MessageReader(message);
127
128 expect(reader.payloadSize).toBe(payloadSize);
129 expect(reader.messageName).toBe(messageName);
130
131 var foo2 = reader.decodeStruct(sample.Foo);
132
133 expect(foo2.x).toBe(foo.x);
134 expect(foo2.y).toBe(foo.y);
135
136 expect(foo2.a).toBe(foo.a & 1 ? true : false);
137 expect(foo2.b).toBe(foo.b & 1 ? true : false);
138 expect(foo2.c).toBe(foo.c & 1 ? true : false);
139
140 expect(foo2.bar).toEqual(foo.bar);
141 expect(foo2.data).toEqual(foo.data);
142
143 expect(foo2.extra_bars).toEqual(foo.extra_bars);
144 expect(foo2.name).toBe(foo.name);
145 expect(foo2.source).toEqual(foo.source);
146
147 expect(foo2.array_of_bools).toEqual(foo.array_of_bools);
148 }
149
150 function createRect(x, y, width, height) {
151 var r = new rect.Rect();
152 r.x = x;
153 r.y = y;
154 r.width = width;
155 r.height = height;
156 return r;
157 }
158
159 // Verify that the references to the imported Rect type in test_structs.mojom
160 // are generated correctly.
161 function testNamedRegion() {
162 var r = new structs.NamedRegion();
163 r.name = "rectangle";
164 r.rects = new Array(createRect(1, 2, 3, 4), createRect(10, 20, 30, 40));
165
166 var builder = new codec.MessageBuilder(1, structs.NamedRegion.encodedSize);
167 builder.encodeStruct(structs.NamedRegion, r);
168 var reader = new codec.MessageReader(builder.finish());
169 var result = reader.decodeStruct(structs.NamedRegion);
170
171 expect(result.name).toEqual("rectangle");
172 expect(result.rects[0]).toEqual(createRect(1, 2, 3, 4));
173 expect(result.rects[1]).toEqual(createRect(10, 20, 30, 40));
174 }
175
176 function testTypes() {
177 function encodeDecode(cls, input, expectedResult, encodedSize) {
178 var messageName = 42;
179 var payloadSize = encodedSize || cls.encodedSize;
180
181 var builder = new codec.MessageBuilder(messageName, payloadSize);
182 builder.encodeStruct(cls, input)
183 var message = builder.finish();
184
185 var reader = new codec.MessageReader(message);
186 expect(reader.payloadSize).toBe(payloadSize);
187 expect(reader.messageName).toBe(messageName);
188 var result = reader.decodeStruct(cls);
189 expect(result).toEqual(expectedResult);
190 }
191 encodeDecode(codec.String, "banana", "banana", 24);
192 encodeDecode(codec.NullableString, null, null, 8);
193 encodeDecode(codec.Int8, -1, -1);
194 encodeDecode(codec.Int8, 0xff, -1);
195 encodeDecode(codec.Int16, -1, -1);
196 encodeDecode(codec.Int16, 0xff, 0xff);
197 encodeDecode(codec.Int16, 0xffff, -1);
198 encodeDecode(codec.Int32, -1, -1);
199 encodeDecode(codec.Int32, 0xffff, 0xffff);
200 encodeDecode(codec.Int32, 0xffffffff, -1);
201 encodeDecode(codec.Float, 1.0, 1.0);
202 encodeDecode(codec.Double, 1.0, 1.0);
203 }
204
205 function testAlign() {
206 var aligned = [
207 0, // 0
208 8, // 1
209 8, // 2
210 8, // 3
211 8, // 4
212 8, // 5
213 8, // 6
214 8, // 7
215 8, // 8
216 16, // 9
217 16, // 10
218 16, // 11
219 16, // 12
220 16, // 13
221 16, // 14
222 16, // 15
223 16, // 16
224 24, // 17
225 24, // 18
226 24, // 19
227 24, // 20
228 ];
229 for (var i = 0; i < aligned.length; ++i)
230 expect(codec.align(i)).toBe(aligned[i]);
231 }
232
233 function testUtf8() {
234 var str = "B\u03ba\u1f79"; // some UCS-2 codepoints
235 var messageName = 42;
236 var payloadSize = 24;
237
238 var builder = new codec.MessageBuilder(messageName, payloadSize);
239 var encoder = builder.createEncoder(8);
240 encoder.encodeStringPointer(str);
241 var message = builder.finish();
242 var expectedMemory = new Uint8Array([
243 /* 0: */ 16, 0, 0, 0, 2, 0, 0, 0,
244 /* 8: */ 42, 0, 0, 0, 0, 0, 0, 0,
245 /* 16: */ 8, 0, 0, 0, 0, 0, 0, 0,
246 /* 24: */ 14, 0, 0, 0, 6, 0, 0, 0,
247 /* 32: */ 0x42, 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0, 0,
248 ]);
249 var actualMemory = new Uint8Array(message.buffer.arrayBuffer);
250 expect(actualMemory.length).toEqual(expectedMemory.length);
251 expect(actualMemory).toEqual(expectedMemory);
252
253 var reader = new codec.MessageReader(message);
254 expect(reader.payloadSize).toBe(payloadSize);
255 expect(reader.messageName).toBe(messageName);
256 var str2 = reader.decoder.decodeStringPointer();
257 expect(str2).toEqual(str);
258 }
259
260 function testTypedPointerValidation() {
261 var encoder = new codec.MessageBuilder(42, 24).createEncoder(8);
262 function DummyClass() {};
263 var testCases = [
264 // method, args, invalid examples, valid examples
265 [encoder.encodeArrayPointer, [DummyClass], [75],
266 [[], null, undefined, new Uint8Array([])]],
267 [encoder.encodeStringPointer, [], [75, new String("foo")],
268 ["", "bar", null, undefined]],
269 [encoder.encodeMapPointer, [DummyClass, DummyClass], [75],
270 [new Map(), null, undefined]],
271 ];
272
273 testCases.forEach(function(test) {
274 var method = test[0];
275 var baseArgs = test[1];
276 var invalidExamples = test[2];
277 var validExamples = test[3];
278
279 var encoder = new codec.MessageBuilder(42, 24).createEncoder(8);
280 invalidExamples.forEach(function(invalid) {
281 expect(function() {
282 method.apply(encoder, baseArgs.concat(invalid));
283 }).toThrow();
284 });
285
286 validExamples.forEach(function(valid) {
287 var encoder = new codec.MessageBuilder(42, 24).createEncoder(8);
288 method.apply(encoder, baseArgs.concat(valid));
289 });
290 });
291 }
292 });
OLDNEW
« no previous file with comments | « mojo/public/js/codec.js ('k') | mojo/public/js/connection.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698