michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: MARIONETTE_TIMEOUT = 60000; michael@0: MARIONETTE_HEAD_JS = 'head.js'; michael@0: michael@0: let outNumber = "5555551111"; michael@0: let inNumber = "5555552222"; michael@0: let outgoingCall; michael@0: let incomingCall; michael@0: let gotOriginalConnected = false; michael@0: let gotHeld = false; michael@0: let gotConnected = false; michael@0: michael@0: function dial() { michael@0: log("Make an outgoing call."); michael@0: telephony.dial(outNumber).then(call => { michael@0: outgoingCall = call; michael@0: ok(outgoingCall); michael@0: is(outgoingCall.number, outNumber); michael@0: is(outgoingCall.state, "dialing"); michael@0: michael@0: outgoingCall.onalerting = function onalerting(event) { michael@0: log("Received 'alerting' call event."); michael@0: michael@0: is(outgoingCall, event.call); michael@0: is(outgoingCall.state, "alerting"); michael@0: is(outgoingCall, telephony.active); michael@0: is(telephony.calls.length, 1); michael@0: is(telephony.calls[0], outgoingCall); michael@0: michael@0: emulator.run("gsm list", function(result) { michael@0: log("Call list is now: " + result); michael@0: is(result[0], "outbound to " + outNumber + " : ringing"); michael@0: is(result[1], "OK"); michael@0: answer(); michael@0: }); michael@0: }; michael@0: }); michael@0: } michael@0: michael@0: function answer() { michael@0: log("Answering the outgoing call."); michael@0: michael@0: // We get no "connecting" event when the remote party answers the call. michael@0: outgoingCall.onconnected = function onconnectedOut(event) { michael@0: log("Received 'connected' call event for the original outgoing call."); michael@0: michael@0: is(outgoingCall, event.call); michael@0: is(outgoingCall.state, "connected"); michael@0: is(outgoingCall, telephony.active); michael@0: michael@0: emulator.run("gsm list", function(result) { michael@0: log("Call list is now: " + result); michael@0: is(result[0], "outbound to " + outNumber + " : active"); michael@0: is(result[1], "OK"); michael@0: michael@0: if(!gotOriginalConnected){ michael@0: gotOriginalConnected = true; michael@0: holdCall(); michael@0: } else { michael@0: // Received connected event for original call multiple times (fail) michael@0: ok(false, michael@0: "Received 'connected' event for original call multiple times."); michael@0: } michael@0: }); michael@0: }; michael@0: emulator.run("gsm accept " + outNumber); michael@0: } michael@0: michael@0: function holdCall() { michael@0: log("Putting the original (outgoing) call on hold."); michael@0: michael@0: let gotHolding = false; michael@0: outgoingCall.onholding = function onholding(event) { michael@0: log("Received 'holding' call event"); michael@0: is(outgoingCall, event.call); michael@0: is(outgoingCall.state, "holding"); michael@0: gotHolding = true; michael@0: }; michael@0: michael@0: outgoingCall.onheld = function onheld(event) { michael@0: log("Received 'held' call event"); michael@0: is(outgoingCall, event.call); michael@0: is(outgoingCall.state, "held"); michael@0: ok(gotHolding); michael@0: michael@0: is(telephony.active, null); michael@0: is(telephony.calls.length, 1); michael@0: is(telephony.calls[0], outgoingCall); michael@0: michael@0: emulator.run("gsm list", function(result) { michael@0: log("Call list is now: " + result); michael@0: is(result[0], "outbound to " + outNumber + " : held"); michael@0: is(result[1], "OK"); michael@0: simulateIncoming(); michael@0: }); michael@0: }; michael@0: outgoingCall.hold(); michael@0: } michael@0: michael@0: // With one call on hold, simulate an incoming call michael@0: function simulateIncoming() { michael@0: log("Simulating an incoming call (with one call already held)."); michael@0: michael@0: telephony.onincoming = function onincoming(event) { michael@0: log("Received 'incoming' call event."); michael@0: incomingCall = event.call; michael@0: ok(incomingCall); michael@0: is(incomingCall.number, inNumber); michael@0: is(incomingCall.state, "incoming"); michael@0: michael@0: // Should be two calls now michael@0: is(telephony.calls.length, 2); michael@0: is(telephony.calls[0], outgoingCall); michael@0: is(telephony.calls[1], incomingCall); michael@0: michael@0: emulator.run("gsm list", function(result) { michael@0: log("Call list is now: " + result); michael@0: is(result[0], "outbound to " + outNumber + " : held"); michael@0: is(result[1], "inbound from " + inNumber + " : incoming"); michael@0: is(result[2], "OK"); michael@0: answerIncoming(); michael@0: }); michael@0: }; michael@0: emulator.run("gsm call " + inNumber); michael@0: } michael@0: michael@0: // Answer incoming call; original outgoing call should be held michael@0: function answerIncoming() { michael@0: log("Answering the incoming call."); michael@0: michael@0: let gotConnecting = false; michael@0: incomingCall.onconnecting = function onconnectingIn(event) { michael@0: log("Received 'connecting' call event for incoming/2nd call."); michael@0: is(incomingCall, event.call); michael@0: is(incomingCall.state, "connecting"); michael@0: gotConnecting = true; michael@0: }; michael@0: michael@0: incomingCall.onconnected = function onconnectedIn(event) { michael@0: log("Received 'connected' call event for incoming/2nd call."); michael@0: is(incomingCall, event.call); michael@0: ok(gotConnecting); michael@0: michael@0: is(incomingCall, telephony.active); michael@0: michael@0: // Original outbound call now held, incoming call active michael@0: is(outgoingCall.state, "held"); michael@0: is(incomingCall.state, "connected"); michael@0: michael@0: emulator.run("gsm list", function(result) { michael@0: log("Call list is now: " + result); michael@0: is(result[0], "outbound to " + outNumber + " : held"); michael@0: is(result[1], "inbound from " + inNumber + " : active"); michael@0: is(result[2], "OK"); michael@0: swapCalls(); michael@0: }); michael@0: }; michael@0: incomingCall.answer(); michael@0: } michael@0: michael@0: // Now swap the held and active calls michael@0: function swapCalls() { michael@0: log("Swapping the held and active calls."); michael@0: michael@0: // Swap calls by resuming the held (outgoing) call. Will force active michael@0: // (incoming) call to hold. michael@0: michael@0: incomingCall.onheld = function onheldIn(event) { michael@0: log("Received 'held' call event for incoming call."); michael@0: gotHeld = true; michael@0: michael@0: if (gotHeld && gotConnected) { verifySwap(); } michael@0: }; michael@0: michael@0: let gotResuming = false; michael@0: outgoingCall.onresuming = function onresuming(event) { michael@0: log("Received 'resuming' call event for outbound call."); michael@0: is(outgoingCall, event.call); michael@0: is(outgoingCall.state, "resuming"); michael@0: gotResuming = true; michael@0: }; michael@0: michael@0: outgoingCall.onconnected = function onconnected(event) { michael@0: log("Received 'connected' call event for outbound call."); michael@0: is(outgoingCall, event.call); michael@0: ok(gotResuming); michael@0: michael@0: is(outgoingCall, telephony.active); michael@0: is(telephony.calls.length, 2); michael@0: is(telephony.calls[0], outgoingCall); michael@0: is(telephony.calls[1], incomingCall); michael@0: gotConnected = true; michael@0: michael@0: if (gotHeld && gotConnected) { verifySwap(); } michael@0: }; michael@0: outgoingCall.resume(); michael@0: } michael@0: michael@0: function verifySwap() { michael@0: // Call status reflects swap michael@0: is(outgoingCall.state, "connected"); michael@0: is(incomingCall.state, "held"); michael@0: michael@0: emulator.run("gsm list", function(result) { michael@0: log("Call list is now: " + result); michael@0: is(result[0], "outbound to " + outNumber + " : active"); michael@0: is(result[1], "inbound from " + inNumber + " : held"); michael@0: michael@0: // Begin hang-up michael@0: hangUpOutgoing(); michael@0: }); michael@0: } michael@0: michael@0: // Hang-up original outgoing (now active) call michael@0: function hangUpOutgoing() { michael@0: log("Hanging up the original outgoing (now active) call."); michael@0: michael@0: let gotDisconnecting = false; michael@0: outgoingCall.ondisconnecting = function ondisconnectingOut(event) { michael@0: log("Received 'disconnecting' call event for original outgoing call."); michael@0: is(outgoingCall, event.call); michael@0: is(outgoingCall.state, "disconnecting"); michael@0: gotDisconnecting = true; michael@0: }; michael@0: michael@0: outgoingCall.ondisconnected = function ondisconnectedOut(event) { michael@0: log("Received 'disconnected' call event for original outgoing call."); michael@0: is(outgoingCall, event.call); michael@0: is(outgoingCall.state, "disconnected"); michael@0: ok(gotDisconnecting); michael@0: michael@0: // Back to one call now michael@0: is(telephony.calls.length, 1); michael@0: is(incomingCall.state, "held"); michael@0: michael@0: emulator.run("gsm list", function(result) { michael@0: log("Call list is now: " + result); michael@0: is(result[0], "inbound from " + inNumber + " : held"); michael@0: is(result[1], "OK"); michael@0: hangUpIncoming(); michael@0: }); michael@0: }; michael@0: outgoingCall.hangUp(); michael@0: } michael@0: michael@0: // Hang-up remaining (now held) call michael@0: function hangUpIncoming() { michael@0: log("Hanging up the remaining (now held) call."); michael@0: michael@0: let gotDisconnecting = false; michael@0: incomingCall.ondisconnecting = function ondisconnectingIn(event) { michael@0: log("Received 'disconnecting' call event for remaining (held) call."); michael@0: is(incomingCall, event.call); michael@0: is(incomingCall.state, "disconnecting"); michael@0: gotDisconnecting = true; michael@0: }; michael@0: michael@0: incomingCall.ondisconnected = function ondisconnectedIn(event) { michael@0: log("Received 'disconnected' call event for remaining (incoming) call."); michael@0: is(incomingCall, event.call); michael@0: is(incomingCall.state, "disconnected"); michael@0: ok(gotDisconnecting); michael@0: michael@0: // Zero calls left michael@0: is(telephony.active, null); michael@0: is(telephony.calls.length, 0); michael@0: michael@0: emulator.run("gsm list", function(result) { michael@0: log("Call list is now: " + result); michael@0: is(result[0], "OK"); michael@0: cleanUp(); michael@0: }); michael@0: }; michael@0: incomingCall.hangUp(); michael@0: } michael@0: michael@0: function cleanUp() { michael@0: telephony.onincoming = null; michael@0: finish(); michael@0: } michael@0: michael@0: startTest(function() { michael@0: dial(); michael@0: });