summaryrefslogtreecommitdiffstats
path: root/mojo/apps/js/bindings/connector_unittests.js
blob: d50f740502e32657f3938e24f702a8a7549dcc01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Mock out the support module to avoid depending on the message loop.
define("mojo/bindings/js/support", function() {
  var waitingCallbacks = [];

  function WaitCookie(id) {
    this.id = id;
  }

  function asyncWait(handle, flags, callback) {
    var id = waitingCallbacks.length;
    waitingCallbacks.push(callback);
    return new WaitCookie(id);
  }

  function cancelWait(cookie) {
    waitingCallbacks[cookie.id] = null;
  }

  function numberOfWaitingCallbacks() {
    var count = 0;
    for (var i = 0; i < waitingCallbacks.length; ++i) {
      if (waitingCallbacks[i])
        ++count;
    }
    return count;
  }

  function pumpOnce(result) {
    var callbacks = waitingCallbacks;
    waitingCallbacks = [];
    for (var i = 0; i < callbacks.length; ++i)
      callbacks[i](result);
  }

  var exports = {};
  exports.asyncWait = asyncWait;
  exports.cancelWait = cancelWait;
  exports.numberOfWaitingCallbacks = numberOfWaitingCallbacks;
  exports.pumpOnce = pumpOnce;
  return exports;
});

define([
    "gin/test/expect",
    "mojo/bindings/js/support",
    "mojo/bindings/js/core",
    "mojo/public/bindings/js/connector",
    "mojo/public/bindings/tests/sample_service.mojom",
], function(expect, mockSupport, core, connector, sample) {

  var receivedFrobinate = false;
  var receivedDidFrobinate = false;

  // ServiceImpl --------------------------------------------------------------

  function ServiceImpl(peer) {
    this.peer = peer;
  }

  ServiceImpl.prototype = Object.create(sample.ServiceStub.prototype);

  ServiceImpl.prototype.frobinate = function(foo, baz, port) {
    receivedFrobinate = true;

    expect(foo.name).toBe("Example name");
    expect(baz).toBeTruthy();
    expect(core.close(port)).toBe(core.RESULT_OK);

    this.peer.didFrobinate(42);
  };

  // ServiceImpl --------------------------------------------------------------

  function ServiceClientImpl(peer) {
    this.peer = peer;
  }

  ServiceClientImpl.prototype =
      Object.create(sample.ServiceClientStub.prototype);

  ServiceClientImpl.prototype.didFrobinate = function(result) {
    receivedDidFrobinate = true;

    expect(result).toBe(42);
  };

  var pipe = core.createMessagePipe();
  var anotherPipe = core.createMessagePipe();
  var sourcePipe = core.createMessagePipe();

  var connection0 = new connector.Connection(
      pipe.handle0, ServiceImpl, sample.ServiceClientProxy);

  var connection1 = new connector.Connection(
      pipe.handle1, ServiceClientImpl, sample.ServiceProxy);

  var foo = new sample.Foo();
  foo.bar = new sample.Bar();
  foo.name = "Example name";
  foo.source = sourcePipe.handle0;
  connection1.remote.frobinate(foo, true, anotherPipe.handle0);

  mockSupport.pumpOnce(core.RESULT_OK);

  expect(receivedFrobinate).toBeTruthy();
  expect(receivedDidFrobinate).toBeTruthy();

  connection0.close();
  connection1.close();

  expect(mockSupport.numberOfWaitingCallbacks()).toBe(0);

  // sourcePipe.handle0 was closed automatically when sent over IPC.
  expect(core.close(sourcePipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT);
  // sourcePipe.handle1 hasn't been closed yet.
  expect(core.close(sourcePipe.handle1)).toBe(core.RESULT_OK);

  // anotherPipe.handle0 was closed automatically when sent over IPC.
  expect(core.close(anotherPipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT);
  // anotherPipe.handle1 hasn't been closed yet.
  expect(core.close(anotherPipe.handle1)).toBe(core.RESULT_OK);

  // The Connection object is responsible for closing these handles.
  expect(core.close(pipe.handle0)).toBe(core.RESULT_INVALID_ARGUMENT);
  expect(core.close(pipe.handle1)).toBe(core.RESULT_INVALID_ARGUMENT);

  this.result = "PASS";
});