Bug 969109: fix racing condition in test_dom_BluetoothManager_enabled.js. r=echou, f=jaliu

This commit is contained in:
Vicamo Yang 2014-02-10 12:03:43 +08:00
parent aa743aff2e
commit fdbcbdbad9
2 changed files with 33 additions and 31 deletions

View File

@ -183,8 +183,10 @@ function waitForManagerEvent(aEventName) {
* @return A deferred promise.
*/
function setBluetoothEnabledAndWait(aEnabled) {
return setBluetoothEnabled(aEnabled)
.then(waitForManagerEvent.bind(null, aEnabled ? "enabled" : "disabled"));
return Promise.all([
setBluetoothEnabled(aEnabled),
waitForManagerEvent(aEnabled ? "enabled" : "disabled"),
]);
}
/* Get default adapter.
@ -265,8 +267,10 @@ function startBluetoothTest(aReenable, aTestCaseMain) {
.then(function() {
if (needEnable) {
log(" Enable 'bluetooth.enabled' ...");
return setBluetoothEnabledAndWait(true)
.then(waitForManagerEvent.bind(null, "adapteradded"));
return Promise.all([
setBluetoothEnabledAndWait(true),
waitForManagerEvent("adapteradded"),
]);
}
})
.then(getDefaultAdapter)

View File

@ -7,14 +7,21 @@
MARIONETTE_TIMEOUT = 60000;
MARIONETTE_HEAD_JS = 'head.js';
let enabledEventReceived;
function onEnabled() {
enabledEventReceived = true;
}
function waitEitherEnabledOrDisabled() {
let deferred = Promise.defer();
let disabledEventReceived;
function onDisabled() {
disabledEventReceived = true;
function onEnabledDisabled(aEvent) {
bluetoothManager.removeEventListener("enabled", onEnabledDisabled);
bluetoothManager.removeEventListener("disabled", onEnabledDisabled);
ok(true, "Got event " + aEvent.type);
deferred.resolve(aEvent.type === "enabled");
}
bluetoothManager.addEventListener("enabled", onEnabledDisabled);
bluetoothManager.addEventListener("disabled", onEnabledDisabled);
return deferred.promise;
}
function test(aEnabled) {
@ -22,43 +29,34 @@ function test(aEnabled) {
let deferred = Promise.defer();
enabledEventReceived = false;
disabledEventReceived = false;
Promise.all([setBluetoothEnabled(aEnabled),
waitEitherEnabledOrDisabled()])
.then(function(aResults) {
/* aResults is an array of two elements:
* [ <result of setBluetoothEnabled>,
* <result of waitEitherEnabledOrDisabled> ]
*/
log(" Examine results " + JSON.stringify(aResults));
setBluetoothEnabled(aEnabled).then(function() {
log(" Settings set. Waiting 3 seconds and examine results.");
window.setTimeout(function() {
is(bluetoothManager.enabled, aEnabled, "bluetoothManager.enabled");
is(enabledEventReceived, aEnabled, "enabledEventReceived");
is(disabledEventReceived, !aEnabled, "disabledEventReceived");
is(aResults[1], aEnabled, "'enabled' event received");
if (bluetoothManager.enabled === aEnabled &&
enabledEventReceived === aEnabled &&
disabledEventReceived === !aEnabled) {
if (bluetoothManager.enabled === aEnabled && aResults[1] === aEnabled) {
deferred.resolve();
} else {
deferred.reject();
}
}, 3000);
});
});
return deferred.promise;
}
startBluetoothTestBase(["settings-read", "settings-write"],
function testCaseMain() {
bluetoothManager.addEventListener("enabled", onEnabled);
bluetoothManager.addEventListener("disabled", onDisabled);
return getBluetoothEnabled()
.then(function(aEnabled) {
log("Original 'bluetooth.enabled' is " + aEnabled);
// Set to !aEnabled and reset back to aEnabled.
return test(!aEnabled).then(test.bind(null, aEnabled));
})
.then(function() {
bluetoothManager.removeEventListener("enabled", onEnabled);
bluetoothManager.removeEventListener("disabled", onDisabled);
});
});