Bug 1035652 - Write a marionette test for verifying the discovery process based on bluetooth API v2. r=btian
authorJamin Liu <jaliu@mozilla.com>
Wed, 16 Jul 2014 16:23:16 +0800
changeset 215228 81eedce71bbf9c471a6e047c9156ae3acee3566b
parent 215227 e2c0f207a23671c7d9902afb32846e4c7ff57d0a
child 215229 50fe2ac159dfc7e991cb01dbbc07385801a6484c
push id3857
push userraliiev@mozilla.com
push dateTue, 02 Sep 2014 16:39:23 +0000
treeherdermozilla-beta@5638b907b505 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbtian
bugs1035652
milestone33.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1035652 - Write a marionette test for verifying the discovery process based on bluetooth API v2. r=btian
dom/bluetooth2/tests/marionette/head.js
dom/bluetooth2/tests/marionette/manifest.ini
dom/bluetooth2/tests/marionette/test_dom_BluetoothAdapter_discovery_API2.js
--- a/dom/bluetooth2/tests/marionette/head.js
+++ b/dom/bluetooth2/tests/marionette/head.js
@@ -557,16 +557,53 @@ function waitForAdapterAttributeChanged(
       deferred.resolve(aEvent);
     }
   };
 
   return deferred.promise;
 }
 
 /**
+ * Wait for specified number of 'devicefound' events.
+ *
+ * Resolve if specified number of devices has been found. Never reject.
+ *
+ * Fulfill params: an array which contains BluetoothDeviceEvents that we
+ *                 received from the BluetoothDiscoveryHandle.
+ *
+ * @param aDiscoveryHandle
+ *        A BluetoothDiscoveryHandle which is used to notify application of
+ *        discovered remote bluetooth devices.
+ * @param aExpectedNumberOfDevices
+ *        The number of remote devices we expect to discovery.
+ *
+ * @return A deferred promise.
+ */
+function waitForDevicesFound(aDiscoveryHandle, aExpectedNumberOfDevices) {
+  let deferred = Promise.defer();
+
+  ok(aDiscoveryHandle instanceof BluetoothDiscoveryHandle,
+    "discoveryHandle should be a BluetoothDiscoveryHandle");
+
+  let devicesArray = [];
+  aDiscoveryHandle.ondevicefound = function onDeviceFound(aEvent) {
+    ok(aEvent instanceof BluetoothDeviceEvent,
+      "aEvent should be a BluetoothDeviceEvent");
+
+    devicesArray.push(aEvent);
+    if (devicesArray.length >= aExpectedNumberOfDevices) {
+      aDiscoveryHandle.ondevicefound = null;
+      deferred.resolve(devicesArray);
+    }
+  };
+
+  return deferred.promise;
+}
+
+/**
  * Flush permission settings and call |finish()|.
  */
 function cleanUp() {
   waitFor(function() {
     SpecialPowers.flushPermissions(function() {
       // Use ok here so that we have at least one test run.
       ok(true, "permissions flushed");
 
--- a/dom/bluetooth2/tests/marionette/manifest.ini
+++ b/dom/bluetooth2/tests/marionette/manifest.ini
@@ -1,8 +1,9 @@
 [DEFAULT]
 b2g = true
 browser = false
 qemu = false
 
 [test_dom_BluetoothManager_API2.js]
 [test_dom_BluetoothAdapter_enable_API2.js]
 [test_dom_BluetoothAdapter_setters_API2.js]
+[test_dom_BluetoothAdapter_discovery_API2.js]
new file mode 100644
--- /dev/null
+++ b/dom/bluetooth2/tests/marionette/test_dom_BluetoothAdapter_discovery_API2.js
@@ -0,0 +1,124 @@
+/* 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 the discovery process of BluetoothAdapter.
+//   Testers have to put the B2G devices in an environment which is surrounded
+//   by N discoverable remote devices. To pass this test, the number N has to be
+//   greater or equals than EXPECTED_NUMBER_OF_REMOTE_DEVICES.
+//
+// Test Procedure:
+//   [0] Set Bluetooth permission and enable default adapter.
+//   [1] Start discovery and verify the correctness.
+//   [2] Attach event handler for 'ondevicefound'.
+//   [3] Stop discovery and verify the correctness.
+//   [4] Mark the BluetoothDiscoveryHandle from [1] as expired.
+//   [5] Start discovery and verify the correctness.
+//   [6] Wait for 'devicefound' events.
+//   [7] Stop discovery and verify the correctness.
+//   [8] Call 'startDiscovery' twice continuously.
+//   [9] Call 'stopDiscovery' twice continuously.
+//   [10] Clean up the event handler of [2].
+//
+// Test Coverage:
+//   - BluetoothAdapter.discovering
+//   - BluetoothAdapter.startDiscovery()
+//   - BluetoothAdapter.stopDiscovery()
+//   - BluetoothAdapter.onattributechanged()
+//   - BluetoothDiscoveryHandle.ondevicefound()
+//
+///////////////////////////////////////////////////////////////////////////////
+
+MARIONETTE_TIMEOUT = 60000;
+MARIONETTE_HEAD_JS = 'head.js';
+
+const EXPECTED_NUMBER_OF_REMOTE_DEVICES = 2;
+
+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);
+
+  let discoveryHandle = null;
+  return Promise.resolve()
+    .then(function() {
+      log("[1] Start discovery and verify the correctness ... ");
+      let promises = [];
+      promises.push(waitForAdapterAttributeChanged(aAdapter, "discovering", true));
+      promises.push(aAdapter.startDiscovery());
+      return Promise.all(promises);
+    })
+    .then(function(aResults) {
+      log("[2] Attach event handler for 'ondevicefound' ... ");
+      discoveryHandle = aResults[1];
+      isHandleExpired = false;
+      discoveryHandle.ondevicefound = function onDeviceFound(aEvent) {
+        if (isHandleExpired) {
+          ok(false, "Expired BluetoothDiscoveryHandle received an event.");
+        }
+      };
+    })
+    .then(function() {
+      log("[3] Stop discovery and and verify the correctness ... ");
+      let promises = [];
+      if (aAdapter.discovering) {
+        promises.push(waitForAdapterAttributeChanged(aAdapter, "discovering", false));
+      }
+      promises.push(aAdapter.stopDiscovery());
+      return Promise.all(promises);
+    })
+    .then(function() {
+      log("[4] Mark the BluetoothDiscoveryHandle from [1] as expired ... ");
+      isHandleExpired = true;
+    })
+    .then(function() {
+      log("[5] Start discovery and verify the correctness ... ");
+      let promises = [];
+      promises.push(waitForAdapterAttributeChanged(aAdapter, "discovering", true));
+      promises.push(aAdapter.startDiscovery());
+      return Promise.all(promises);
+    })
+    .then(function(aResults) {
+      log("[6] Wait for 'devicefound' events ... ");
+      return waitForDevicesFound(aResults[1], EXPECTED_NUMBER_OF_REMOTE_DEVICES);
+    })
+    .then(function() {
+      log("[7] Stop discovery and and verify the correctness ... ");
+      let promises = [];
+      if (aAdapter.discovering) {
+        promises.push(waitForAdapterAttributeChanged(aAdapter, "discovering", false));
+      }
+      promises.push(aAdapter.stopDiscovery());
+      return Promise.all(promises);
+    })
+    .then(function() {
+      log("[8] Call 'startDiscovery' twice continuously ... ");
+      return aAdapter.startDiscovery()
+        .then(() => aAdapter.startDiscovery())
+        .then(() => ok(false, "Call startDiscovery() when adapter is discovering. - Fail"),
+              () => ok(true, "Call startDiscovery() when adapter is discovering. - Success"));
+    })
+    .then(function() {
+      log("[9] Call 'stopDiscovery' twice continuously ... ");
+      return aAdapter.stopDiscovery()
+        .then(() => aAdapter.stopDiscovery())
+        .then(() => ok(true, "Call stopDiscovery() when adapter isn't discovering. - Success"),
+              () => ok(false, "Call stopDiscovery() when adapter isn't discovering. - Fail"));
+    })
+    .then(function() {
+      log("[10] Clean up the event handler of [2] ... ");
+      if (discoveryHandle) {
+        discoveryHandle.ondevicefound = null;
+      }
+    });
+});