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

Side by Side Diff: mojo/edk/js/tests/sample_service_tests.js

Issue 2645873003: Cleanup mojo-js tests. (Closed)
Patch Set: more deps fixes Created 3 years, 11 months 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
« no previous file with comments | « mojo/edk/js/tests/run_js_unittests.cc ('k') | mojo/public/js/BUILD.gn » ('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/interfaces/bindings/tests/sample_service.mojom",
8 "mojo/public/interfaces/bindings/tests/sample_import.mojom",
9 "mojo/public/interfaces/bindings/tests/sample_import2.mojom",
10 "mojo/public/js/bindings",
11 "mojo/public/js/core",
12 "mojo/public/js/threading",
13 ], function(expect, sample, imported, imported2, bindings, core, threading) {
14 testDefaultValues()
15 .then(testSampleService)
16 .then(function() {
17 this.result = "PASS";
18 threading.quit();
19 }.bind(this)).catch(function(e) {
20 this.result = "FAIL: " + (e.stack || e);
21 threading.quit();
22 }.bind(this));
23
24 // Checks that values are set to the defaults if we don't override them.
25 function testDefaultValues() {
26 var bar = new sample.Bar();
27 expect(bar.alpha).toBe(255);
28 expect(bar.type).toBe(sample.Bar.Type.VERTICAL);
29
30 var foo = new sample.Foo();
31 expect(foo.name).toBe("Fooby");
32 expect(foo.a).toBeTruthy();
33 expect(foo.data).toBeNull();
34
35 var defaults = new sample.DefaultsTest();
36 expect(defaults.a0).toBe(-12);
37 expect(defaults.a1).toBe(sample.kTwelve);
38 expect(defaults.a2).toBe(1234);
39 expect(defaults.a3).toBe(34567);
40 expect(defaults.a4).toBe(123456);
41 expect(defaults.a5).toBe(3456789012);
42 expect(defaults.a6).toBe(-111111111111);
43 // JS doesn't have a 64 bit integer type so this is just checking that the
44 // expected and actual values have the same closest double value.
45 expect(defaults.a7).toBe(9999999999999999999);
46 expect(defaults.a8).toBe(0x12345);
47 expect(defaults.a9).toBe(-0x12345);
48 expect(defaults.a10).toBe(1234);
49 expect(defaults.a11).toBe(true);
50 expect(defaults.a12).toBe(false);
51 expect(defaults.a13).toBe(123.25);
52 expect(defaults.a14).toBe(1234567890.123);
53 expect(defaults.a15).toBe(1E10);
54 expect(defaults.a16).toBe(-1.2E+20);
55 expect(defaults.a17).toBe(1.23E-20);
56 expect(defaults.a20).toBe(sample.Bar.Type.BOTH);
57 expect(defaults.a21).toBeNull();
58 expect(defaults.a22).toBeTruthy();
59 expect(defaults.a22.shape).toBe(imported.Shape.RECTANGLE);
60 expect(defaults.a22.color).toBe(imported2.Color.BLACK);
61 expect(defaults.a21).toBeNull();
62 expect(defaults.a23).toBe(0xFFFFFFFFFFFFFFFF);
63 expect(defaults.a24).toBe(0x123456789);
64 expect(defaults.a25).toBe(-0x123456789);
65
66 return Promise.resolve();
67 }
68
69 function testSampleService() {
70 function ServiceImpl() {
71 }
72
73 ServiceImpl.prototype.frobinate = function(foo, baz, port) {
74 checkFoo(foo);
75 expect(baz).toBe(sample.Service.BazOptions.EXTRA);
76 expect(port.ptr.isBound()).toBeTruthy();
77 return Promise.resolve({result: 1234});
78 };
79
80 var foo = makeFoo();
81 checkFoo(foo);
82
83 var service = new sample.ServicePtr();
84 var request = bindings.makeRequest(service);
85 var serviceBinding = new bindings.Binding(
86 sample.Service, new ServiceImpl(), request);
87
88 var port = new sample.PortPtr();
89 bindings.makeRequest(port);
90 var promise = service.frobinate(
91 foo, sample.Service.BazOptions.EXTRA, port)
92 .then(function(response) {
93 expect(response.result).toBe(1234);
94
95 return Promise.resolve();
96 });
97
98 return promise;
99 }
100
101 function makeFoo() {
102 var bar = new sample.Bar();
103 bar.alpha = 20;
104 bar.beta = 40;
105 bar.gamma = 60;
106 bar.type = sample.Bar.Type.VERTICAL;
107
108 var extra_bars = new Array(3);
109 for (var i = 0; i < extra_bars.length; ++i) {
110 var base = i * 100;
111 var type = i % 2 ?
112 sample.Bar.Type.VERTICAL : sample.Bar.Type.HORIZONTAL;
113 extra_bars[i] = new sample.Bar();
114 extra_bars[i].alpha = base;
115 extra_bars[i].beta = base + 20;
116 extra_bars[i].gamma = base + 40;
117 extra_bars[i].type = type;
118 }
119
120 var data = new Array(10);
121 for (var i = 0; i < data.length; ++i) {
122 data[i] = data.length - i;
123 }
124
125 var foo = new sample.Foo();
126 foo.name = "foopy";
127 foo.x = 1;
128 foo.y = 2;
129 foo.a = false;
130 foo.b = true;
131 foo.c = false;
132 foo.bar = bar;
133 foo.extra_bars = extra_bars;
134 foo.data = data;
135
136 // TODO(yzshen): currently setting it to null will cause connection error,
137 // even if the field is defined as nullable. crbug.com/575753
138 foo.source = core.createMessagePipe().handle0;
139
140 return foo;
141 }
142
143 // Checks that the given |Foo| is identical to the one made by |makeFoo()|.
144 function checkFoo(foo) {
145 expect(foo.name).toBe("foopy");
146 expect(foo.x).toBe(1);
147 expect(foo.y).toBe(2);
148 expect(foo.a).toBeFalsy();
149 expect(foo.b).toBeTruthy();
150 expect(foo.c).toBeFalsy();
151 expect(foo.bar.alpha).toBe(20);
152 expect(foo.bar.beta).toBe(40);
153 expect(foo.bar.gamma).toBe(60);
154 expect(foo.bar.type).toBe(sample.Bar.Type.VERTICAL);
155
156 expect(foo.extra_bars.length).toBe(3);
157 for (var i = 0; i < foo.extra_bars.length; ++i) {
158 var base = i * 100;
159 var type = i % 2 ?
160 sample.Bar.Type.VERTICAL : sample.Bar.Type.HORIZONTAL;
161 expect(foo.extra_bars[i].alpha).toBe(base);
162 expect(foo.extra_bars[i].beta).toBe(base + 20);
163 expect(foo.extra_bars[i].gamma).toBe(base + 40);
164 expect(foo.extra_bars[i].type).toBe(type);
165 }
166
167 expect(foo.data.length).toBe(10);
168 for (var i = 0; i < foo.data.length; ++i)
169 expect(foo.data[i]).toBe(foo.data.length - i);
170
171 expect(core.isHandle(foo.source)).toBeTruthy();
172 }
173 });
OLDNEW
« no previous file with comments | « mojo/edk/js/tests/run_js_unittests.cc ('k') | mojo/public/js/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698