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