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

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

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

Powered by Google App Engine
This is Rietveld 408576698