gecko-dev/dom/presentation/tests/xpcshell/test_presentation_state_machine.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

230 lines
9.1 KiB
JavaScript

/* -*- 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";
/* globals ControllerStateMachine */
const {ControllerStateMachine} = ChromeUtils.import("resource://gre/modules/presentation/ControllerStateMachine.jsm");
/* globals ReceiverStateMachine */
const {ReceiverStateMachine} = ChromeUtils.import("resource://gre/modules/presentation/ReceiverStateMachine.jsm");
/* globals State */
const {State} = ChromeUtils.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) {
executeSoon(function() {
receiverState.onCommand(command);
});
};
mockReceiverChannel.sendCommand = function(command) {
executeSoon(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(presId) {
Assert.equal(controllerState.state, State.CONNECTED, "controller in connected state");
Assert.equal(presId, 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(presId) {
Assert.equal(controllerState.state, State.CONNECTED, "controller in connected state");
Assert.equal(presId, 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(presId) {
Assert.equal(receiverState.state, State.CONNECTED, "receiver in connected state");
Assert.equal(presId, 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(controllerCandidate) {
Assert.equal(controllerCandidate, 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(disconnectReason) {
Assert.equal(disconnectReason, 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(disconnectReason) {
Assert.equal(disconnectReason, 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(disconnectReason) {
Assert.equal(disconnectReason, 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);