Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / mojo / apps / js / bindings / sample_service_unittests.js
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/apps/js/test/hexdump",
8     "gin/test/expect",
9     "mojom/sample_service",
10     "mojom/sample_import",
11     "mojom/sample_import2",
12   ], function(console, hexdump, expect, sample, imported, imported2) {
13
14   var global = this;
15
16   // Set this variable to true to print the binary message in hex.
17   var dumpMessageAsHex = false;
18
19   function makeFoo() {
20     var bar = new sample.Bar();
21     bar.alpha = 20;
22     bar.beta = 40;
23     bar.gamma = 60;
24     bar.type = sample.BarType.BAR_VERTICAL;
25
26     var extra_bars = new Array(3);
27     for (var i = 0; i < extra_bars.length; ++i) {
28       var base = i * 100;
29       var type = i % 2 ?
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;
36     }
37
38     var data = new Array(10);
39     for (var i = 0; i < data.length; ++i) {
40       data[i] = data.length - i;
41     }
42
43     var source = 0xFFFF;  // Invent a dummy handle.
44
45     var foo = new sample.Foo();
46     foo.name = "foopy";
47     foo.x = 1;
48     foo.y = 2;
49     foo.a = false;
50     foo.b = true;
51     foo.c = false;
52     foo.bar = bar;
53     foo.extra_bars = extra_bars;
54     foo.data = data;
55     foo.source = source;
56     return foo;
57   }
58
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);
71
72     expect(foo.extra_bars.length).toBe(3);
73     for (var i = 0; i < foo.extra_bars.length; ++i) {
74       var base = i * 100;
75       var type = i % 2 ?
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);
81     }
82
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);
86
87     expect(foo.source).toBe(0xFFFF);
88   }
89
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
95     var foo = new sample.Foo();
96     expect(foo.name).toBe("Fooby");
97     expect(foo.a).toBeTruthy();
98
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);
103
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);
108
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);
116
117     expect(full.point.x).toBe(7);
118     expect(full.point.y).toBe(15);
119
120     expect(full.shape_masks.length).toBe(1);
121     expect(full.shape_masks[0]).toBe(1 << imported.Shape.SHAPE_RECTANGLE);
122
123     expect(full.thing.shape).toBe(imported.Shape.SHAPE_CIRCLE);
124     expect(full.thing.color).toBe(imported2.Color.COLOR_BLACK);
125   }
126
127   function ServiceImpl() {
128   }
129
130   ServiceImpl.prototype = Object.create(sample.ServiceStub.prototype);
131
132   ServiceImpl.prototype.frobinate = function(foo, baz, port) {
133     checkFoo(foo);
134     expect(baz).toBe(sample.ServiceStub.BazOptions.BAZ_EXTRA);
135     expect(port).toBe(10);
136     global.result = "PASS";
137   };
138
139   function SimpleMessageReceiver() {
140   }
141
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);
148   };
149
150   var receiver = new SimpleMessageReceiver();
151   var serviceProxy = new sample.ServiceProxy(receiver);
152
153   checkDefaultValues();
154
155   var foo = makeFoo();
156   checkFoo(foo);
157
158   var port = 10;
159   serviceProxy.frobinate(foo, sample.ServiceProxy.BazOptions.BAZ_EXTRA, port);
160 });