dom/bluetooth/tests/marionette/test_dom_BluetoothManager_enabled.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 sts=2 et filetype=javascript
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 MARIONETTE_TIMEOUT = 60000;
michael@0 8 MARIONETTE_HEAD_JS = 'head.js';
michael@0 9
michael@0 10 function waitEitherEnabledOrDisabled() {
michael@0 11 let deferred = Promise.defer();
michael@0 12
michael@0 13 function onEnabledDisabled(aEvent) {
michael@0 14 bluetoothManager.removeEventListener("adapteradded", onEnabledDisabled);
michael@0 15 bluetoothManager.removeEventListener("disabled", onEnabledDisabled);
michael@0 16
michael@0 17 ok(true, "Got event " + aEvent.type);
michael@0 18 deferred.resolve(aEvent.type === "adapteradded");
michael@0 19 }
michael@0 20
michael@0 21 // Listen 'adapteradded' rather than 'enabled' since the current API can't
michael@0 22 // disable BT before the BT adapter is initialized.
michael@0 23 // We should listen to 'enabled' when gecko can handle the case I mentioned
michael@0 24 // above, please refer to the follow-up bug 973482.
michael@0 25 bluetoothManager.addEventListener("adapteradded", onEnabledDisabled);
michael@0 26 bluetoothManager.addEventListener("disabled", onEnabledDisabled);
michael@0 27
michael@0 28 return deferred.promise;
michael@0 29 }
michael@0 30
michael@0 31 function test(aEnabled) {
michael@0 32 log("Testing 'bluetooth.enabled' => " + aEnabled);
michael@0 33
michael@0 34 let deferred = Promise.defer();
michael@0 35
michael@0 36 // Ensures that we can always receive that "enabled"/"disabled" event by
michael@0 37 // installing the event handler *before* we ever enable/disable Bluetooth. Or
michael@0 38 // we might just miss those events and get a timeout error.
michael@0 39 let promises = [];
michael@0 40 promises.push(waitEitherEnabledOrDisabled());
michael@0 41 promises.push(setBluetoothEnabled(aEnabled));
michael@0 42 Promise.all(promises)
michael@0 43 .then(function(aResults) {
michael@0 44 /* aResults is an array of two elements:
michael@0 45 * [ <result of waitEitherEnabledOrDisabled>,
michael@0 46 * <result of setBluetoothEnabled>]
michael@0 47 */
michael@0 48 log(" Examine results " + JSON.stringify(aResults));
michael@0 49
michael@0 50 is(bluetoothManager.enabled, aEnabled, "bluetoothManager.enabled");
michael@0 51 is(aResults[0], aEnabled, "'adapteradded' event received");
michael@0 52
michael@0 53 if (bluetoothManager.enabled === aEnabled && aResults[0] === aEnabled) {
michael@0 54 deferred.resolve();
michael@0 55 } else {
michael@0 56 deferred.reject();
michael@0 57 }
michael@0 58 });
michael@0 59
michael@0 60 return deferred.promise;
michael@0 61 }
michael@0 62
michael@0 63 startBluetoothTestBase(["settings-read", "settings-write"],
michael@0 64 function testCaseMain() {
michael@0 65 return getBluetoothEnabled()
michael@0 66 .then(function(aEnabled) {
michael@0 67 log("Original 'bluetooth.enabled' is " + aEnabled);
michael@0 68 // Set to !aEnabled and reset back to aEnabled.
michael@0 69 return test(!aEnabled).then(test.bind(null, aEnabled));
michael@0 70 });
michael@0 71 });

mercurial