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.
7 "mojo/apps/js/test/hexdump",
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 ], function(console, hexdump, expect, sample, imported, imported2) {
16 // Set this variable to true to print the binary message in hex.
17 var dumpMessageAsHex = false;
20 var bar = new sample.Bar();
24 bar.type = sample.Bar.Type.VERTICAL;
26 var extra_bars = new Array(3);
27 for (var i = 0; i < extra_bars.length; ++i) {
30 sample.Bar.Type.VERTICAL : sample.Bar.Type.HORIZONTAL;
31 extra_bars[i] = new sample.Bar();
32 extra_bars[i].alpha = base;
33 extra_bars[i].beta = base + 20;
34 extra_bars[i].gamma = base + 40;
35 extra_bars[i].type = type;
38 var data = new Array(10);
39 for (var i = 0; i < data.length; ++i) {
40 data[i] = data.length - i;
43 var source = 0xFFFF; // Invent a dummy handle.
45 var foo = new sample.Foo();
53 foo.extra_bars = extra_bars;
59 // Check that the given |Foo| is identical to the one made by |MakeFoo()|.
60 function checkFoo(foo) {
61 expect(foo.name).toBe("foopy");
62 expect(foo.x).toBe(1);
63 expect(foo.y).toBe(2);
64 expect(foo.a).toBeFalsy();
65 expect(foo.b).toBeTruthy();
66 expect(foo.c).toBeFalsy();
67 expect(foo.bar.alpha).toBe(20);
68 expect(foo.bar.beta).toBe(40);
69 expect(foo.bar.gamma).toBe(60);
70 expect(foo.bar.type).toBe(sample.Bar.Type.VERTICAL);
72 expect(foo.extra_bars.length).toBe(3);
73 for (var i = 0; i < foo.extra_bars.length; ++i) {
76 sample.Bar.Type.VERTICAL : sample.Bar.Type.HORIZONTAL;
77 expect(foo.extra_bars[i].alpha).toBe(base);
78 expect(foo.extra_bars[i].beta).toBe(base + 20);
79 expect(foo.extra_bars[i].gamma).toBe(base + 40);
80 expect(foo.extra_bars[i].type).toBe(type);
83 expect(foo.data.length).toBe(10);
84 for (var i = 0; i < foo.data.length; ++i)
85 expect(foo.data[i]).toBe(foo.data.length - i);
87 expect(foo.source).toBe(0xFFFF);
90 // Check that values are set to the defaults if we don't override them.
91 function checkDefaultValues() {
92 var bar = new sample.Bar();
93 expect(bar.alpha).toBe(255);
94 expect(bar.type).toBe(sample.Bar.Type.VERTICAL);
96 var foo = new sample.Foo();
97 expect(foo.name).toBe("Fooby");
98 expect(foo.a).toBeTruthy();
99 expect(foo.data).toBeNull();
101 var defaults = new sample.DefaultsTest();
102 expect(defaults.a0).toBe(-12);
103 expect(defaults.a1).toBe(sample.kTwelve);
104 expect(defaults.a2).toBe(1234);
105 expect(defaults.a3).toBe(34567);
106 expect(defaults.a4).toBe(123456);
107 expect(defaults.a5).toBe(3456789012);
108 expect(defaults.a6).toBe(-111111111111);
109 // JS doesn't have a 64 bit integer type so this is just checking that the
110 // expected and actual values have the same closest double value.
111 expect(defaults.a7).toBe(9999999999999999999);
112 expect(defaults.a8).toBe(0x12345);
113 expect(defaults.a9).toBe(-0x12345);
114 expect(defaults.a10).toBe(1234);
115 expect(defaults.a11).toBe(true);
116 expect(defaults.a12).toBe(false);
117 expect(defaults.a13).toBe(123.25);
118 expect(defaults.a14).toBe(1234567890.123);
119 expect(defaults.a15).toBe(1E10);
120 expect(defaults.a16).toBe(-1.2E+20);
121 expect(defaults.a17).toBe(1.23E-20);
122 expect(defaults.a20).toBe(sample.Bar.Type.BOTH);
123 expect(defaults.a21).toBeNull();
124 expect(defaults.a22).toBeTruthy();
125 expect(defaults.a22.shape).toBe(imported.Shape.RECTANGLE);
126 expect(defaults.a22.color).toBe(imported2.Color.BLACK);
127 expect(defaults.a21).toBeNull();
128 expect(defaults.a23).toBe(0xFFFFFFFFFFFFFFFF);
129 expect(defaults.a24).toBe(0x123456789);
130 expect(defaults.a25).toBe(-0x123456789);
133 function ServiceImpl() {
136 ServiceImpl.prototype = Object.create(sample.ServiceStub.prototype);
138 ServiceImpl.prototype.frobinate = function(foo, baz, port) {
140 expect(baz).toBe(sample.ServiceStub.BazOptions.EXTRA);
141 expect(port).toBe(10);
142 global.result = "PASS";
145 function SimpleMessageReceiver() {
148 SimpleMessageReceiver.prototype.accept = function(message) {
149 if (dumpMessageAsHex) {
150 var uint8Array = new Uint8Array(message.buffer.arrayBuffer);
151 console.log(hexdump.dumpArray(uint8Array));
153 // Imagine some IPC happened here.
154 var serviceImpl = new ServiceImpl();
155 serviceImpl.accept(message);
158 var receiver = new SimpleMessageReceiver();
159 var serviceProxy = new sample.ServiceProxy(receiver);
161 checkDefaultValues();
167 serviceProxy.frobinate(foo, sample.ServiceProxy.BazOptions.EXTRA, port);