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 "console", | |
7 "mojo/edk/js/test/hexdump", | |
8 "gin/test/expect", | |
9 "mojo/public/interfaces/bindings/tests/sample_service.mojom", | |
10 "mojo/public/interfaces/bindings/tests/sample_import.mojom", | |
11 "mojo/public/interfaces/bindings/tests/sample_import2.mojom", | |
12 "mojo/public/js/core", | |
13 ], function(console, hexdump, expect, sample, imported, imported2, core) { | |
14 | |
15 var global = this; | |
16 | |
17 // Set this variable to true to print the binary message in hex. | |
18 var dumpMessageAsHex = false; | |
19 | |
20 function makeFoo() { | |
21 var bar = new sample.Bar(); | |
22 bar.alpha = 20; | |
23 bar.beta = 40; | |
24 bar.gamma = 60; | |
25 bar.type = sample.Bar.Type.VERTICAL; | |
26 | |
27 var extra_bars = new Array(3); | |
28 for (var i = 0; i < extra_bars.length; ++i) { | |
29 var base = i * 100; | |
30 var type = i % 2 ? | |
31 sample.Bar.Type.VERTICAL : sample.Bar.Type.HORIZONTAL; | |
32 extra_bars[i] = new sample.Bar(); | |
33 extra_bars[i].alpha = base; | |
34 extra_bars[i].beta = base + 20; | |
35 extra_bars[i].gamma = base + 40; | |
36 extra_bars[i].type = type; | |
37 } | |
38 | |
39 var data = new Array(10); | |
40 for (var i = 0; i < data.length; ++i) { | |
41 data[i] = data.length - i; | |
42 } | |
43 | |
44 var source = 0xFFFF; // Invent a dummy handle. | |
45 | |
46 var foo = new sample.Foo(); | |
47 foo.name = "foopy"; | |
48 foo.x = 1; | |
49 foo.y = 2; | |
50 foo.a = false; | |
51 foo.b = true; | |
52 foo.c = false; | |
53 foo.bar = bar; | |
54 foo.extra_bars = extra_bars; | |
55 foo.data = data; | |
56 foo.source = source; | |
57 return foo; | |
58 } | |
59 | |
60 // Check that the given |Foo| is identical to the one made by |MakeFoo()|. | |
61 function checkFoo(foo) { | |
62 expect(foo.name).toBe("foopy"); | |
63 expect(foo.x).toBe(1); | |
64 expect(foo.y).toBe(2); | |
65 expect(foo.a).toBeFalsy(); | |
66 expect(foo.b).toBeTruthy(); | |
67 expect(foo.c).toBeFalsy(); | |
68 expect(foo.bar.alpha).toBe(20); | |
69 expect(foo.bar.beta).toBe(40); | |
70 expect(foo.bar.gamma).toBe(60); | |
71 expect(foo.bar.type).toBe(sample.Bar.Type.VERTICAL); | |
72 | |
73 expect(foo.extra_bars.length).toBe(3); | |
74 for (var i = 0; i < foo.extra_bars.length; ++i) { | |
75 var base = i * 100; | |
76 var type = i % 2 ? | |
77 sample.Bar.Type.VERTICAL : sample.Bar.Type.HORIZONTAL; | |
78 expect(foo.extra_bars[i].alpha).toBe(base); | |
79 expect(foo.extra_bars[i].beta).toBe(base + 20); | |
80 expect(foo.extra_bars[i].gamma).toBe(base + 40); | |
81 expect(foo.extra_bars[i].type).toBe(type); | |
82 } | |
83 | |
84 expect(foo.data.length).toBe(10); | |
85 for (var i = 0; i < foo.data.length; ++i) | |
86 expect(foo.data[i]).toBe(foo.data.length - i); | |
87 | |
88 expect(foo.source).toBe(0xFFFF); | |
89 } | |
90 | |
91 // Check that values are set to the defaults if we don't override them. | |
92 function checkDefaultValues() { | |
93 var bar = new sample.Bar(); | |
94 expect(bar.alpha).toBe(255); | |
95 expect(bar.type).toBe(sample.Bar.Type.VERTICAL); | |
96 | |
97 var foo = new sample.Foo(); | |
98 expect(foo.name).toBe("Fooby"); | |
99 expect(foo.a).toBeTruthy(); | |
100 expect(foo.data).toBeNull(); | |
101 | |
102 var defaults = new sample.DefaultsTest(); | |
103 expect(defaults.a0).toBe(-12); | |
104 expect(defaults.a1).toBe(sample.kTwelve); | |
105 expect(defaults.a2).toBe(1234); | |
106 expect(defaults.a3).toBe(34567); | |
107 expect(defaults.a4).toBe(123456); | |
108 expect(defaults.a5).toBe(3456789012); | |
109 expect(defaults.a6).toBe(-111111111111); | |
110 // JS doesn't have a 64 bit integer type so this is just checking that the | |
111 // expected and actual values have the same closest double value. | |
112 expect(defaults.a7).toBe(9999999999999999999); | |
113 expect(defaults.a8).toBe(0x12345); | |
114 expect(defaults.a9).toBe(-0x12345); | |
115 expect(defaults.a10).toBe(1234); | |
116 expect(defaults.a11).toBe(true); | |
117 expect(defaults.a12).toBe(false); | |
118 expect(defaults.a13).toBe(123.25); | |
119 expect(defaults.a14).toBe(1234567890.123); | |
120 expect(defaults.a15).toBe(1E10); | |
121 expect(defaults.a16).toBe(-1.2E+20); | |
122 expect(defaults.a17).toBe(1.23E-20); | |
123 expect(defaults.a20).toBe(sample.Bar.Type.BOTH); | |
124 expect(defaults.a21).toBeNull(); | |
125 expect(defaults.a22).toBeTruthy(); | |
126 expect(defaults.a22.shape).toBe(imported.Shape.RECTANGLE); | |
127 expect(defaults.a22.color).toBe(imported2.Color.BLACK); | |
128 expect(defaults.a21).toBeNull(); | |
129 expect(defaults.a23).toBe(0xFFFFFFFFFFFFFFFF); | |
130 expect(defaults.a24).toBe(0x123456789); | |
131 expect(defaults.a25).toBe(-0x123456789); | |
132 } | |
133 | |
134 function ServiceImpl() { | |
135 } | |
136 | |
137 ServiceImpl.prototype = Object.create(sample.Service.stubClass.prototype); | |
138 | |
139 ServiceImpl.prototype.frobinate = function(foo, baz, port) { | |
140 checkFoo(foo); | |
141 expect(baz).toBe(sample.Service.BazOptions.EXTRA); | |
142 expect(core.isHandle(port)).toBeTruthy(); | |
143 global.result = "PASS"; | |
144 }; | |
145 | |
146 function SimpleMessageReceiver() { | |
147 } | |
148 | |
149 SimpleMessageReceiver.prototype.accept = function(message) { | |
150 if (dumpMessageAsHex) { | |
151 var uint8Array = new Uint8Array(message.buffer.arrayBuffer); | |
152 console.log(hexdump.dumpArray(uint8Array)); | |
153 } | |
154 // Imagine some IPC happened here. | |
155 var serviceImpl = new ServiceImpl(); | |
156 serviceImpl.accept(message); | |
157 }; | |
158 | |
159 var serviceProxy = new sample.Service.proxyClass; | |
160 serviceProxy.receiver_ = new SimpleMessageReceiver; | |
161 | |
162 checkDefaultValues(); | |
163 | |
164 var foo = makeFoo(); | |
165 checkFoo(foo); | |
166 | |
167 var pipe = core.createMessagePipe(); | |
168 serviceProxy.frobinate(foo, sample.Service.BazOptions.EXTRA, pipe.handle0); | |
169 expect(core.close(pipe.handle0)).toBe(core.RESULT_OK); | |
170 expect(core.close(pipe.handle1)).toBe(core.RESULT_OK); | |
171 }); | |
OLD | NEW |