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