summaryrefslogtreecommitdiffstats
path: root/dom/presentation/tests/xpcshell/test_presentation_state_machine.js
blob: fcaf34da6b23ab1c64ca17e8e13ea84551b0a298 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */
/* jshint esnext:true, globalstrict:true, moz:true, undef:true, unused:true */
/* globals Components,Assert,run_next_test,add_test,do_execute_soon */

'use strict';

const { utils: Cu, results: Cr } = Components;

/* globals ControllerStateMachine */
Cu.import('resource://gre/modules/presentation/ControllerStateMachine.jsm');
/* globals ReceiverStateMachine */
Cu.import('resource://gre/modules/presentation/ReceiverStateMachine.jsm');
/* globals State */
Cu.import('resource://gre/modules/presentation/StateMachineHelper.jsm');

const testControllerId = 'test-controller-id';
const testPresentationId = 'test-presentation-id';
const testUrl = 'http://example.org';

let mockControllerChannel = {};
let mockReceiverChannel = {};

let controllerState = new ControllerStateMachine(mockControllerChannel, testControllerId);
let receiverState = new ReceiverStateMachine(mockReceiverChannel);

mockControllerChannel.sendCommand = function(command) {
  do_execute_soon(function() {
    receiverState.onCommand(command);
  });
};

mockReceiverChannel.sendCommand = function(command) {
  do_execute_soon(function() {
    controllerState.onCommand(command);
  });
};

function connect() {
  Assert.equal(controllerState.state, State.INIT, 'controller in init state');
  Assert.equal(receiverState.state, State.INIT, 'receiver in init state');
  // step 1: underlying connection is ready
  controllerState.onChannelReady();
  Assert.equal(controllerState.state, State.CONNECTING, 'controller in connecting state');
  receiverState.onChannelReady();
  Assert.equal(receiverState.state, State.CONNECTING, 'receiver in connecting state');

  // step 2: receiver reply to connect command
  mockReceiverChannel.notifyDeviceConnected = function(deviceId) {
    Assert.equal(deviceId, testControllerId, 'receiver connect to mock controller');
    Assert.equal(receiverState.state, State.CONNECTED, 'receiver in connected state');

    // step 3: controller receive connect-ack command
    mockControllerChannel.notifyDeviceConnected = function() {
      Assert.equal(controllerState.state, State.CONNECTED, 'controller in connected state');
      run_next_test();
    };
  };
}

function launch() {
  Assert.equal(controllerState.state, State.CONNECTED, 'controller in connected state');
  Assert.equal(receiverState.state, State.CONNECTED, 'receiver in connected state');

  controllerState.launch(testPresentationId, testUrl);
  mockReceiverChannel.notifyLaunch = function(presentationId, url) {
    Assert.equal(receiverState.state, State.CONNECTED, 'receiver in connected state');
    Assert.equal(presentationId, testPresentationId, 'expected presentationId received');
    Assert.equal(url, testUrl, 'expected url received');

    mockControllerChannel.notifyLaunch = function(presentationId) {
      Assert.equal(controllerState.state, State.CONNECTED, 'controller in connected state');
      Assert.equal(presentationId, testPresentationId, 'expected presentationId received from ack');

      run_next_test();
    };
  };
}

function terminateByController() {
  Assert.equal(controllerState.state, State.CONNECTED, 'controller in connected state');
  Assert.equal(receiverState.state, State.CONNECTED, 'receiver in connected state');

  controllerState.terminate(testPresentationId);
  mockReceiverChannel.notifyTerminate = function(presentationId) {
    Assert.equal(receiverState.state, State.CONNECTED, 'receiver in connected state');
    Assert.equal(presentationId, testPresentationId, 'expected presentationId received');

    mockControllerChannel.notifyTerminate = function(presentationId) {
      Assert.equal(controllerState.state, State.CONNECTED, 'controller in connected state');
      Assert.equal(presentationId, testPresentationId, 'expected presentationId received from ack');

      run_next_test();
    };

    receiverState.terminateAck(presentationId);
  };
}

function terminateByReceiver() {
  Assert.equal(controllerState.state, State.CONNECTED, 'controller in connected state');
  Assert.equal(receiverState.state, State.CONNECTED, 'receiver in connected state');

  receiverState.terminate(testPresentationId);
  mockControllerChannel.notifyTerminate = function(presentationId) {
    Assert.equal(controllerState.state, State.CONNECTED, 'controller in connected state');
    Assert.equal(presentationId, testPresentationId, 'expected presentationId received');

    mockReceiverChannel.notifyTerminate = function(presentationId) {
      Assert.equal(receiverState.state, State.CONNECTED, 'receiver in connected state');
      Assert.equal(presentationId, testPresentationId, 'expected presentationId received from ack');
      run_next_test();
    };

    controllerState.terminateAck(presentationId);
  };
}

function exchangeSDP() {
  Assert.equal(controllerState.state, State.CONNECTED, 'controller in connected state');
  Assert.equal(receiverState.state, State.CONNECTED, 'receiver in connected state');

  const testOffer = 'test-offer';
  const testAnswer = 'test-answer';
  const testIceCandidate = 'test-ice-candidate';
  controllerState.sendOffer(testOffer);
  mockReceiverChannel.notifyOffer = function(offer) {
    Assert.equal(offer, testOffer, 'expected offer received');

    receiverState.sendAnswer(testAnswer);
    mockControllerChannel.notifyAnswer = function(answer) {
      Assert.equal(answer, testAnswer, 'expected answer received');

      controllerState.updateIceCandidate(testIceCandidate);
      mockReceiverChannel.notifyIceCandidate = function(candidate) {
        Assert.equal(candidate, testIceCandidate, 'expected ice candidate received in receiver');

        receiverState.updateIceCandidate(testIceCandidate);
        mockControllerChannel.notifyIceCandidate = function(candidate) {
          Assert.equal(candidate, testIceCandidate, 'expected ice candidate received in controller');

          run_next_test();
        };
      };
    };
  };
}

function disconnect() {
  // step 1: controller send disconnect command
  controllerState.onChannelClosed(Cr.NS_OK, false);
  Assert.equal(controllerState.state, State.CLOSING, 'controller in closing state');

  mockReceiverChannel.notifyDisconnected = function(reason) {
    Assert.equal(reason, Cr.NS_OK, 'receive close reason');
    Assert.equal(receiverState.state, State.CLOSED, 'receiver in closed state');

    receiverState.onChannelClosed(Cr.NS_OK, true);
    Assert.equal(receiverState.state, State.CLOSED, 'receiver in closed state');

    mockControllerChannel.notifyDisconnected = function(reason) {
      Assert.equal(reason, Cr.NS_OK, 'receive close reason');
      Assert.equal(controllerState.state, State.CLOSED, 'controller in closed state');

      run_next_test();
    };
    controllerState.onChannelClosed(Cr.NS_OK, true);
  };
}

function receiverDisconnect() {
  // initial state: controller and receiver are connected
  controllerState.state = State.CONNECTED;
  receiverState.state = State.CONNECTED;

  // step 1: controller send disconnect command
  receiverState.onChannelClosed(Cr.NS_OK, false);
  Assert.equal(receiverState.state, State.CLOSING, 'receiver in closing state');

  mockControllerChannel.notifyDisconnected = function(reason) {
    Assert.equal(reason, Cr.NS_OK, 'receive close reason');
    Assert.equal(controllerState.state, State.CLOSED, 'controller in closed state');

    controllerState.onChannelClosed(Cr.NS_OK, true);
    Assert.equal(controllerState.state, State.CLOSED, 'controller in closed state');

    mockReceiverChannel.notifyDisconnected = function(reason) {
      Assert.equal(reason, Cr.NS_OK, 'receive close reason');
      Assert.equal(receiverState.state, State.CLOSED, 'receiver in closed state');

      run_next_test();
    };
    receiverState.onChannelClosed(Cr.NS_OK, true);
  };
}

function abnormalDisconnect() {
  // initial state: controller and receiver are connected
  controllerState.state = State.CONNECTED;
  receiverState.state = State.CONNECTED;

  const testErrorReason = Cr.NS_ERROR_FAILURE;
  // step 1: controller send disconnect command
  controllerState.onChannelClosed(testErrorReason, false);
  Assert.equal(controllerState.state, State.CLOSING, 'controller in closing state');

  mockReceiverChannel.notifyDisconnected = function(reason) {
    Assert.equal(reason, testErrorReason, 'receive abnormal close reason');
    Assert.equal(receiverState.state, State.CLOSED, 'receiver in closed state');

    receiverState.onChannelClosed(Cr.NS_OK, true);
    Assert.equal(receiverState.state, State.CLOSED, 'receiver in closed state');

    mockControllerChannel.notifyDisconnected = function(reason) {
      Assert.equal(reason, testErrorReason, 'receive abnormal close reason');
      Assert.equal(controllerState.state, State.CLOSED, 'controller in closed state');

      run_next_test();
    };
    controllerState.onChannelClosed(Cr.NS_OK, true);
  };
}

add_test(connect);
add_test(launch);
add_test(terminateByController);
add_test(terminateByReceiver);
add_test(exchangeSDP);
add_test(disconnect);
add_test(receiverDisconnect);
add_test(abnormalDisconnect);

function run_test() { // jshint ignore:line
  run_next_test();
}