OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/bindings/js/codec", | |
8 "mojo/public/bindings/tests/sample_service.mojom", | |
9 ], function(expect, codec, sample) { | |
10 testBar(); | |
11 testFoo(); | |
12 testAlign(); | |
13 this.result = "PASS"; | |
14 | |
15 function testBar() { | |
16 var bar = new sample.Bar(); | |
17 bar.alpha = 1; | |
18 bar.beta = 2; | |
19 bar.gamma = 3; | |
20 bar.type = 0x08070605; | |
21 bar.extraProperty = "banana"; | |
22 | |
23 var messageName = 42; | |
24 var payloadSize = sample.Bar.encodedSize; | |
25 | |
26 var builder = new codec.MessageBuilder(messageName, payloadSize); | |
27 builder.encodeStruct(sample.Bar, bar); | |
28 | |
29 var message = builder.finish(); | |
30 | |
31 var expectedMemory = new Uint8Array([ | |
32 24, 0, 0, 0, | |
33 42, 0, 0, 0, | |
34 | |
35 16, 0, 0, 0, | |
36 4, 0, 0, 0, | |
37 | |
38 1, 2, 3, 0, | |
39 5, 6, 7, 8, | |
40 ]); | |
41 | |
42 expect(message.memory).toEqual(expectedMemory); | |
43 | |
44 var reader = new codec.MessageReader(message); | |
45 | |
46 expect(reader.payloadSize).toBe(payloadSize); | |
47 expect(reader.messageName).toBe(messageName); | |
48 | |
49 var bar2 = reader.decodeStruct(sample.Bar); | |
50 | |
51 expect(bar2.alpha).toBe(bar.alpha); | |
52 expect(bar2.beta).toBe(bar.beta); | |
53 expect(bar2.gamma).toBe(bar.gamma); | |
54 expect("extraProperty" in bar2).toBeFalsy(); | |
55 } | |
56 | |
57 function testFoo() { | |
58 var foo = new sample.Foo(); | |
59 foo.x = 0x212B4D5; | |
60 foo.y = 0x16E93; | |
61 foo.a = 1; | |
62 foo.b = 0; | |
63 foo.c = 3; // This will get truncated to one bit. | |
64 foo.bar = new sample.Bar(); | |
65 foo.bar.alpha = 91; | |
66 foo.bar.beta = 82; | |
67 foo.bar.gamma = 73; | |
68 foo.data = [ | |
69 4, 5, 6, 7, 8, | |
70 ]; | |
71 foo.extra_bars = [ | |
72 new sample.Bar(), new sample.Bar(), new sample.Bar(), | |
73 ]; | |
74 for (var i = 0; i < foo.extra_bars.length; ++i) { | |
75 foo.extra_bars[i].alpha = 1 * i; | |
76 foo.extra_bars[i].beta = 2 * i; | |
77 foo.extra_bars[i].gamma = 3 * i; | |
78 } | |
79 foo.name = "I am a banana"; | |
80 // This is supposed to be a handle, but we fake it with an integer. | |
81 foo.source = 23423782; | |
82 | |
83 var messageName = 31; | |
84 var payloadSize = 224; | |
85 | |
86 var builder = new codec.MessageBuilder(messageName, payloadSize); | |
87 builder.encodeStruct(sample.Foo, foo); | |
88 | |
89 var message = builder.finish(); | |
90 | |
91 var expectedMemory = new Uint8Array([ | |
92 /* 0: */ 232, 0, 0, 0, 31, 0, 0, 0, | |
93 /* 8: */ 72, 0, 0, 0, 12, 0, 0, 0, | |
94 /* 16: */ 0xD5, 0xB4, 0x12, 0x02, 0x93, 0x6E, 0x01, 0, | |
95 /* 24: */ 5, 0, 0, 0, 0, 0, 0, 0, | |
96 /* 32: */ 48, 0, 0, 0, 0, 0, 0, 0, | |
97 ]); | |
98 // TODO(abarth): Test more of the message's raw memory. | |
99 var actualMemory = new Uint8Array(message.memory.buffer, | |
100 0, expectedMemory.length); | |
101 expect(actualMemory).toEqual(expectedMemory); | |
102 | |
103 var expectedHandles = [ | |
104 23423782, | |
105 ]; | |
106 | |
107 expect(message.handles).toEqual(expectedHandles); | |
108 | |
109 var reader = new codec.MessageReader(message); | |
110 | |
111 expect(reader.payloadSize).toBe(payloadSize); | |
112 expect(reader.messageName).toBe(messageName); | |
113 | |
114 var foo2 = reader.decodeStruct(sample.Foo); | |
115 | |
116 expect(foo2.x).toBe(foo.x); | |
117 expect(foo2.y).toBe(foo.y); | |
118 | |
119 expect(foo2.a).toBe(foo.a & 1 ? true : false); | |
120 expect(foo2.b).toBe(foo.b & 1 ? true : false); | |
121 expect(foo2.c).toBe(foo.c & 1 ? true : false); | |
122 | |
123 expect(foo2.bar).toEqual(foo.bar); | |
124 expect(foo2.data).toEqual(foo.data); | |
125 | |
126 expect(foo2.extra_bars).toEqual(foo.extra_bars); | |
127 expect(foo2.name).toBe(foo.name); | |
128 expect(foo2.source).toEqual(foo.source); | |
129 } | |
130 | |
131 function testAlign() { | |
132 var aligned = [ | |
133 0, // 0 | |
134 8, // 1 | |
135 8, // 2 | |
136 8, // 3 | |
137 8, // 4 | |
138 8, // 5 | |
139 8, // 6 | |
140 8, // 7 | |
141 8, // 8 | |
142 16, // 9 | |
143 16, // 10 | |
144 16, // 11 | |
145 16, // 12 | |
146 16, // 13 | |
147 16, // 14 | |
148 16, // 15 | |
149 16, // 16 | |
150 24, // 17 | |
151 24, // 18 | |
152 24, // 19 | |
153 24, // 20 | |
154 ]; | |
155 for (var i = 0; i < aligned.length; ++i) | |
156 expect(codec.align(i)).toBe(aligned[i]); | |
157 } | |
158 }); | |
OLD | NEW |