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 "mojom/sample_service",
10 "mojom/sample_import",
11 "mojom/sample_import2",
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.BarType.BAR_VERTICAL;
26 var extra_bars = new Array(3);
27 for (var i = 0; i < extra_bars.length; ++i) {
30 sample.BarType.BAR_VERTICAL : sample.BarType.BAR_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.BarType.BAR_VERTICAL);
72 expect(foo.extra_bars.length).toBe(3);
73 for (var i = 0; i < foo.extra_bars.length; ++i) {
76 sample.BarType.BAR_VERTICAL : sample.BarType.BAR_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);
95 var foo = new sample.Foo();
96 expect(foo.name).toBe("Fooby");
97 expect(foo.a).toBeTruthy();
99 expect(foo.data.length).toBe(3);
100 expect(foo.data[0]).toBe(1);
101 expect(foo.data[1]).toBe(2);
102 expect(foo.data[2]).toBe(3);
104 var inner = new sample.DefaultsTestInner();
105 expect(inner.names.length).toBe(1);
106 expect(inner.names[0]).toBe("Jim");
107 expect(inner.height).toBe(6*12);
109 var full = new sample.DefaultsTest();
110 expect(full.people.length).toBe(1);
111 expect(full.people[0].age).toBe(32);
112 expect(full.people[0].names.length).toBe(2);
113 expect(full.people[0].names[0]).toBe("Bob");
114 expect(full.people[0].names[1]).toBe("Bobby");
115 expect(full.people[0].height).toBe(6*12);
117 expect(full.point.x).toBe(7);
118 expect(full.point.y).toBe(15);
120 expect(full.shape_masks.length).toBe(1);
121 expect(full.shape_masks[0]).toBe(1 << imported.Shape.SHAPE_RECTANGLE);
123 expect(full.thing.shape).toBe(imported.Shape.SHAPE_CIRCLE);
124 expect(full.thing.color).toBe(imported2.Color.COLOR_BLACK);
127 function ServiceImpl() {
130 ServiceImpl.prototype = Object.create(sample.ServiceStub.prototype);
132 ServiceImpl.prototype.frobinate = function(foo, baz, port) {
134 expect(baz).toBe(sample.ServiceStub.BazOptions.BAZ_EXTRA);
135 expect(port).toBe(10);
136 global.result = "PASS";
139 function SimpleMessageReceiver() {
142 SimpleMessageReceiver.prototype.accept = function(message) {
143 if (dumpMessageAsHex)
144 console.log(hexdump.dumpArray(message.memory));
145 // Imagine some IPC happened here.
146 var serviceImpl = new ServiceImpl();
147 serviceImpl.accept(message);
150 var receiver = new SimpleMessageReceiver();
151 var serviceProxy = new sample.ServiceProxy(receiver);
153 checkDefaultValues();
159 serviceProxy.frobinate(foo, sample.ServiceProxy.BazOptions.BAZ_EXTRA, port);