Bug 1043180 - Write a marionette test for Bluetooth pairing based on Bluetooth API v2. r=btian

This commit is contained in:
Jamin Liu 2014-08-14 11:34:22 +08:00
parent a722811aa2
commit 22c8f01856
3 changed files with 329 additions and 1 deletions

View File

@ -581,7 +581,7 @@ function waitForDevicesFound(aDiscoveryHandle, aExpectedNumberOfDevices) {
let deferred = Promise.defer();
ok(aDiscoveryHandle instanceof BluetoothDiscoveryHandle,
"discoveryHandle should be a BluetoothDiscoveryHandle");
"aDiscoveryHandle should be a BluetoothDiscoveryHandle");
let devicesArray = [];
aDiscoveryHandle.ondevicefound = function onDeviceFound(aEvent) {
@ -598,6 +598,192 @@ function waitForDevicesFound(aDiscoveryHandle, aExpectedNumberOfDevices) {
return deferred.promise;
}
/**
* Wait for 'devicefound' events of specified devices.
*
* Resolve if every specified device has been found. Never reject.
*
* Fulfill params: an array which contains the BluetoothDeviceEvents that we
* expected to receive from the BluetoothDiscoveryHandle.
*
* @param aDiscoveryHandle
* A BluetoothDiscoveryHandle which is used to notify application of
* discovered remote bluetooth devices.
* @param aRemoteAddresses
* An array which contains addresses of remote devices.
*
* @return A deferred promise.
*/
function waitForSpecifiedDevicesFound(aDiscoveryHandle, aRemoteAddresses) {
let deferred = Promise.defer();
ok(aDiscoveryHandle instanceof BluetoothDiscoveryHandle,
"aDiscoveryHandle should be a BluetoothDiscoveryHandle");
let devicesArray = [];
aDiscoveryHandle.ondevicefound = function onDeviceFound(aEvent) {
ok(aEvent instanceof BluetoothDeviceEvent,
"aEvent should be a BluetoothDeviceEvent");
if (aRemoteAddresses.indexOf(aEvent.device.address) != -1) {
devicesArray.push(aEvent);
}
if (devicesArray.length == aRemoteAddresses.length) {
aDiscoveryHandle.ondevicefound = null;
ok(true, "BluetoothAdapter has found all remote devices.");
deferred.resolve(devicesArray);
}
};
return deferred.promise;
}
/**
* Verify the correctness of 'devicepaired' or 'deviceunpaired' events.
*
* Use BluetoothAdapter.getPairedDevices() to get current device array.
* Resolve if the device of from 'devicepaired' event exists in device array or
* the device of address from 'deviceunpaired' event has been removed from
* device array.
* Otherwise, reject the promise.
*
* Fulfill params: the device event from 'devicepaired' or 'deviceunpaired'.
*
* @param aAdapter
* The BluetoothAdapter you want to use.
* @param aDeviceEvent
* The device event from "devicepaired" or "deviceunpaired".
*
* @return A deferred promise.
*/
function verifyDevicePairedUnpairedEvent(aAdapter, aDeviceEvent) {
let deferred = Promise.defer();
let devices = aAdapter.getPairedDevices();
let isPromisePending = true;
if (aDeviceEvent.device) {
log(" - Verify 'devicepaired' event");
for (let i in devices) {
if (devices[i].address == aDeviceEvent.device.address) {
deferred.resolve(aDeviceEvent);
isPromisePending = false;
}
}
if (isPromisePending) {
deferred.reject(aDeviceEvent);
isPromisePending = false;
}
} else if (aDeviceEvent.address) {
log(" - Verify 'deviceunpaired' event");
for (let i in devices) {
if (devices[i].address == aDeviceEvent.address) {
deferred.reject(aDeviceEvent);
isPromisePending = false;
}
}
if (isPromisePending) {
deferred.resolve(aDeviceEvent);
isPromisePending = false;
}
} else {
log(" - Exception occurs. Unexpected aDeviceEvent properties.");
deferred.reject(aDeviceEvent);
isPromisePending = false;
}
return deferred.promise;
}
/**
* Add event handlers for pairing request listener.
*
* @param aAdapter
* The BluetoothAdapter you want to use.
* @param aSpecifiedBdAddress (optional)
* Bluetooth address of the specified remote device which adapter wants
* to pair with. If aSpecifiedBdAddress is an empty string, 'null' or
* 'undefined', this function accepts every pairing request.
*/
function addEventHandlerForPairingRequest(aAdapter, aSpecifiedBdAddress) {
log(" - Add event handlers for pairing requests.");
aAdapter.pairingReqs.ondisplaypasskeyreq = function onDisplayPasskeyReq(evt) {
let passkey = evt.handle.passkey; // passkey to display
ok(typeof passkey === 'string', "type checking for passkey.");
log(" - Received 'ondisplaypasskeyreq' event with passkey: " + passkey);
let device = evt.device;
if (!aSpecifiedBdAddress || device.address == aSpecifiedBdAddress) {
cleanupPairingListener(aAdapter.pairingReqs);
}
};
aAdapter.pairingReqs.onenterpincodereq = function onEnterPinCodeReq(evt) {
log(" - Received 'onenterpincodereq' event.");
let device = evt.device;
if (!aSpecifiedBdAddress || device.address == aSpecifiedBdAddress) {
// TODO: Allow user to enter pincode.
let UserEnteredPinCode = "0000";
let pinCode = UserEnteredPinCode;
evt.handle.setPinCode(pinCode).then(
function onResolve() {
log(" - 'setPinCode' resolve.");
cleanupPairingListener(aAdapter.pairingReqs);
},
function onReject() {
log(" - 'setPinCode' reject.");
cleanupPairingListener(aAdapter.pairingReqs);
});
}
};
aAdapter.pairingReqs.onpairingconfirmationreq
= function onPairingConfirmationReq(evt) {
let passkey = evt.handle.passkey; // passkey for user to confirm
ok(typeof passkey === 'string', "type checking for passkey.");
log(" - Received 'onpairingconfirmationreq' event with passkey: " + passkey);
let device = evt.device;
if (!aSpecifiedBdAddress || device.address == aSpecifiedBdAddress) {
let confirm = true;
evt.handle.setPairingConfirmation(confirm).then(
function onResolve() {
log(" - 'setPairingConfirmation' resolve.");
cleanupPairingListener(aAdapter.pairingReqs);
},
function onReject() {
log(" - 'setPairingConfirmation' reject.");
cleanupPairingListener(aAdapter.pairingReqs);
});
}
};
aAdapter.pairingReqs.onpairingconsentreq = function onPairingConsentReq(evt) {
log(" - Received 'onpairingconsentreq' event.");
let device = evt.device;
if (!aSpecifiedBdAddress || device.address == aSpecifiedBdAddress) {
cleanupPairingListener(aAdapter.pairingReqs);
}
};
}
/**
* Clean up event handlers for pairing listener.
*
* @param aPairingReqs
* A BluetoothPairingListener with event handlers.
*/
function cleanupPairingListener(aPairingReqs) {
aPairingReqs.ondisplaypasskeyreq = null;
aPairingReqs.onenterpasskeyreq = null;
aPairingReqs.onpairingconfirmationreq = null;
aPairingReqs.onpairingconsentreq = null;
}
/**
* Compare two uuid arrays to see if them are the same.
*

View File

@ -8,3 +8,4 @@ qemu = false
[test_dom_BluetoothAdapter_setters_API2.js]
[test_dom_BluetoothAdapter_discovery_API2.js]
[test_dom_BluetoothDevice_API2.js]
[test_dom_BluetoothAdapter_pair_API2.js]

View File

@ -0,0 +1,141 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
///////////////////////////////////////////////////////////////////////////////
// Test Purpose:
// To verify entire paring process of BluetoothAdapter, except for
// in-line pairing.
// Testers have to put a discoverable remote device near the testing device
// and click the 'confirm' button on remote device when it receives a pairing
// request from testing device.
// To pass this test, Bluetooth address of the remote device should equal to
// ADDRESS_OF_TARGETED_REMOTE_DEVICE.
//
// Test Procedure:
// [0] Set Bluetooth permission and enable default adapter.
// [1] Unpair device if it's already paired.
// [2] Start discovery.
// [3] Wait for the specified 'devicefound' event.
// [4] Type checking for BluetoothDeviceEvent and BluetoothDevice.
// [5] Pair and wait for confirmation.
// [6] Get paired devices and verify 'devicepaired' event.
// [7] Pair again.
// [8] Unpair.
// [9] Get paired devices and verify 'deviceunpaired' event.
// [10] Unpair again.
// [11] Stop discovery.
//
// Test Coverage:
// - BluetoothAdapter.pair()
// - BluetoothAdapter.unpair()
// - BluetoothAdapter.getPairedDevices()
// - BluetoothAdapter.ondevicepaired()
// - BluetoothAdapter.ondeviceunpaired()
// - BluetoothAdapter.pairingReqs
//
// - BluetoothPairingListener.ondisplaypassykeyreq()
// - BluetoothPairingListener.onenterpincodereq()
// - BluetoothPairingListener.onpairingconfirmationreq()
// - BluetoothPairingListener.onpairingconsentreq()
//
// - BluetoothPairingEvent.device
// - BluetoothPairingEvent.handle
//
// - BluetoothPairingHandle.setPinCode()
// - BluetoothPairingHandle.setPairingConfirmation()
//
///////////////////////////////////////////////////////////////////////////////
MARIONETTE_TIMEOUT = 60000;
MARIONETTE_HEAD_JS = 'head.js';
const ADDRESS_OF_TARGETED_REMOTE_DEVICE = "0c:bd:51:20:a1:e0";
startBluetoothTest(true, function testCaseMain(aAdapter) {
log("Checking adapter attributes ...");
is(aAdapter.state, "enabled", "adapter.state");
isnot(aAdapter.address, "", "adapter.address");
// Since adapter has just been re-enabled, these properties should be 'false'.
is(aAdapter.discovering, false, "adapter.discovering");
is(aAdapter.discoverable, false, "adapter.discoverable");
log("adapter.address: " + aAdapter.address);
log("adapter.name: " + aAdapter.name);
return Promise.resolve()
.then(function() {
log("[1] Unpair device if it's already paired ... ");
let devices = aAdapter.getPairedDevices();
for (let i in devices) {
if (devices[i].address == ADDRESS_OF_TARGETED_REMOTE_DEVICE) {
log(" - The device has already been paired. Unpair it ...");
return aAdapter.unpair(devices[i].address);
}
}
log(" - The device hasn't been paired. Skip to Step [2] ...");
return;
})
.then(function() {
log("[2] Start discovery ... ");
return aAdapter.startDiscovery();
})
.then(function(discoveryHandle) {
log("[3] Wait for the specified 'devicefound' event ... ");
return waitForSpecifiedDevicesFound(discoveryHandle, [ADDRESS_OF_TARGETED_REMOTE_DEVICE]);
})
.then(function(deviceEvents) {
log("[4] Type checking for BluetoothDeviceEvent and BluetoothDevice ... ");
let device = deviceEvents[0].device;
ok(deviceEvents[0] instanceof BluetoothDeviceEvent, "device should be a BluetoothDeviceEvent");
ok(device instanceof BluetoothDevice, "device should be a BluetoothDevice");
log(" - BluetoothDevice.address: " + device.address);
log(" - BluetoothDevice.name: " + device.name);
log(" - BluetoothDevice.cod: " + device.cod);
log(" - BluetoothDevice.paired: " + device.paired);
log(" - BluetoothDevice.uuids: " + device.uuids);
return device;
})
.then(function(device) {
log("[5] Pair and wait for confirmation ... ");
addEventHandlerForPairingRequest(aAdapter, device.address);
let promises = [];
promises.push(waitForAdapterEvent(aAdapter, "devicepaired"));
promises.push(aAdapter.pair(device.address));
return Promise.all(promises);
})
.then(function(results) {
log("[6] Get paired devices and verify 'devicepaired' event ... ");
return verifyDevicePairedUnpairedEvent(aAdapter, results[0]);
})
.then(function(deviceEvent) {
log("[7] Pair again... ");
return aAdapter.pair(deviceEvent.device.address).then(deviceEvent.device.address);
})
.then(function(deviceAddress) {
log("[8] Unpair ... ");
let promises = [];
promises.push(waitForAdapterEvent(aAdapter, "deviceunpaired"));
promises.push(aAdapter.unpair(deviceAddress));
return Promise.all(promises);
})
.then(function(results) {
log("[9] Get paired devices and verify 'deviceunpaired' event ... ");
return verifyDevicePairedUnpairedEvent(aAdapter, results[0])
})
.then(function(deviceEvent) {
log("[10] Unpair again... ");
return aAdapter.unpair(deviceEvent.address);
})
.then(function() {
log("[11] Stop discovery ... ");
return aAdapter.stopDiscovery();
});
});