michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 sts=2 et filetype=javascript michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: MARIONETTE_TIMEOUT = 60000; michael@0: MARIONETTE_HEAD_JS = 'head.js'; michael@0: michael@0: function waitEitherEnabledOrDisabled() { michael@0: let deferred = Promise.defer(); michael@0: michael@0: function onEnabledDisabled(aEvent) { michael@0: bluetoothManager.removeEventListener("adapteradded", onEnabledDisabled); michael@0: bluetoothManager.removeEventListener("disabled", onEnabledDisabled); michael@0: michael@0: ok(true, "Got event " + aEvent.type); michael@0: deferred.resolve(aEvent.type === "adapteradded"); michael@0: } michael@0: michael@0: // Listen 'adapteradded' rather than 'enabled' since the current API can't michael@0: // disable BT before the BT adapter is initialized. michael@0: // We should listen to 'enabled' when gecko can handle the case I mentioned michael@0: // above, please refer to the follow-up bug 973482. michael@0: bluetoothManager.addEventListener("adapteradded", onEnabledDisabled); michael@0: bluetoothManager.addEventListener("disabled", onEnabledDisabled); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function test(aEnabled) { michael@0: log("Testing 'bluetooth.enabled' => " + aEnabled); michael@0: michael@0: let deferred = Promise.defer(); michael@0: michael@0: // Ensures that we can always receive that "enabled"/"disabled" event by michael@0: // installing the event handler *before* we ever enable/disable Bluetooth. Or michael@0: // we might just miss those events and get a timeout error. michael@0: let promises = []; michael@0: promises.push(waitEitherEnabledOrDisabled()); michael@0: promises.push(setBluetoothEnabled(aEnabled)); michael@0: Promise.all(promises) michael@0: .then(function(aResults) { michael@0: /* aResults is an array of two elements: michael@0: * [ , michael@0: * ] michael@0: */ michael@0: log(" Examine results " + JSON.stringify(aResults)); michael@0: michael@0: is(bluetoothManager.enabled, aEnabled, "bluetoothManager.enabled"); michael@0: is(aResults[0], aEnabled, "'adapteradded' event received"); michael@0: michael@0: if (bluetoothManager.enabled === aEnabled && aResults[0] === aEnabled) { michael@0: deferred.resolve(); michael@0: } else { michael@0: deferred.reject(); michael@0: } michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: startBluetoothTestBase(["settings-read", "settings-write"], michael@0: function testCaseMain() { michael@0: return getBluetoothEnabled() michael@0: .then(function(aEnabled) { michael@0: log("Original 'bluetooth.enabled' is " + aEnabled); michael@0: // Set to !aEnabled and reset back to aEnabled. michael@0: return test(!aEnabled).then(test.bind(null, aEnabled)); michael@0: }); michael@0: });