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

Side by Side Diff: mojo/public/bindings/sample/sample_test.js

Issue 68323004: Port Mojo's sample_service.cc to JavaScript (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 1 month 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 | Annotate | Revision Log
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 "gtest",
7 // TODO(abarth): We shouldn't need to depend on codec, but there seems to
8 // be a bug in the module loading system whereby this test doesn't run if
9 // we don't import codec here.
10 "mojo/public/bindings/js/codec",
11 "mojo/public/bindings/sample/mojom/sample_service"
12 ], function(gtest, codec, sample) {
abarth-chromium 2013/11/18 23:30:44 This code is a direct transliteration of sample_te
13
14 var global = this;
15
16 function makeFoo() {
17 var bar = new sample.Bar();
18 bar.alpha = 20;
19 bar.beta = 40;
20 bar.gamma = 60;
21
22 var extra_bars = new Array(3);
23 for (var i = 0; i < extra_bars.length; ++i) {
24 var base = i * 100;
25 extra_bars[i] = new sample.Bar();
26 extra_bars[i].alpha = base;
27 extra_bars[i].beta = base + 20;
28 extra_bars[i].gamma = base + 40;
29 }
30
31 var data = new Array(10);
32 for (var i = 0; i < data.length; ++i) {
33 data[i] = data.length - i;
34 }
35
36 var files = new Array(4);
37 for (var i = 0; i < files.length; ++i) {
38 files[i] = 0xFFFF - i;
39 }
40
41 var foo = new sample.Foo();
42 foo.name = "foopy";
43 foo.x = 1;
44 foo.y = 2;
45 foo.a = false;
46 foo.b = true;
47 foo.c = false;
48 foo.bar = bar;
49 foo.extra_bars = extra_bars;
50 foo.data = data;
51 foo.files = files;
52 return foo;
53 }
54
55 // Check that the given |Foo| is identical to the one made by |MakeFoo()|.
56 function checkFoo(foo) {
57 gtest.expectEqual(foo.name, "foopy", "foo.name is " + foo.name);
58 gtest.expectEqual(foo.x, 1, "foo.x is " + foo.x);
59 gtest.expectEqual(foo.y, 2, "foo.y is " + foo.y);
60 gtest.expectFalse(foo.a, "foo.a is " + foo.a);
61 gtest.expectTrue(foo.b, "foo.b is " + foo.b);
62 gtest.expectFalse(foo.c, "foo.c is " + foo.c);
63 gtest.expectEqual(foo.bar.alpha, 20, "foo.bar.alpha is " + foo.bar.alpha);
64 gtest.expectEqual(foo.bar.beta, 40, "foo.bar.beta is " + foo.bar.beta);
65 gtest.expectEqual(foo.bar.gamma, 60, "foo.bar.gamma is " + foo.bar.gamma);
66
67 gtest.expectEqual(foo.extra_bars.length, 3,
68 "foo.extra_bars.length is " + foo.extra_bars.length);
69 for (var i = 0; i < foo.extra_bars.length; ++i) {
70 var base = i * 100;
71 gtest.expectEqual(foo.extra_bars[i].alpha, base,
72 "foo.extra_bars[" + i + "].alpha is " + foo.extra_bars[i].alpha);
73 gtest.expectEqual(foo.extra_bars[i].beta, base + 20,
74 "foo.extra_bars[" + i + "].beta is " + foo.extra_bars[i].beta);
75 gtest.expectEqual(foo.extra_bars[i].gamma, base + 40,
76 "foo.extra_bars[" + i + "].gamma is " + foo.extra_bars[i].gamma);
77 }
78
79 gtest.expectEqual(foo.data.length, 10,
80 "foo.data.length is " + foo.data.length);
81 for (var i = 0; i < foo.data.length; ++i) {
82 gtest.expectEqual(foo.data[i], foo.data.length - i,
83 "foo.data[" + i + "] is " + foo.data[i]);
84 }
85
86 gtest.expectEqual(foo.files.length, 4,
87 "foo.files.length " + foo.files.length);
88 for (var i = 0; i < foo.files.length; ++i) {
89 gtest.expectEqual(foo.files[i], 0xFFFF - i,
90 "foo.files[" + i + "] is " + foo.files[i]);
91 }
92 }
93
94 function ServiceImpl() {
95 }
96
97 ServiceImpl.prototype = Object.create(sample.ServiceStub.prototype);
98
99 ServiceImpl.prototype.frobinate = function(foo, baz, port) {
100 checkFoo(foo);
101 gtest.expectTrue(baz, "baz is " + baz);
102 gtest.expectEqual(port, 10, "port is " + port);
103 global.result = "PASS";
104 };
105
106 function SimpleMessageReceiver() {
107 }
108
109 SimpleMessageReceiver.prototype.accept = function(message) {
110 // Imagine some IPC happened here.
111 var serviceImpl = new ServiceImpl();
112 serviceImpl.accept(message);
113 };
114
115 var receiver = new SimpleMessageReceiver();
116 var serviceProxy = new sample.ServiceProxy(receiver);
117
118 var foo = makeFoo();
119 checkFoo(foo);
120
121 var port = 10;
122 serviceProxy.frobinate(foo, true, port);
123 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698