Bug 996452 - [NFC] Add emulator rf_discover test case. r=tzimmermann
authorDimi Lee <dlee@mozilla.com>
Wed, 23 Apr 2014 18:32:02 +0800
changeset 180199 509937940bc637561bacff9822516a557b113d7a
parent 180198 276eb640a3fe822313f16c48b37af731793a4971
child 180200 5f6a80d4a7faa7090570fb1293ad76b8ef40ebc9
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewerstzimmermann
bugs996452
milestone31.0a1
Bug 996452 - [NFC] Add emulator rf_discover test case. r=tzimmermann
dom/nfc/tests/marionette/head.js
dom/nfc/tests/marionette/test_nfc_manager_tech_discovered.js
--- a/dom/nfc/tests/marionette/head.js
+++ b/dom/nfc/tests/marionette/head.js
@@ -1,15 +1,42 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 let pendingEmulatorCmdCount = 0;
 
 SpecialPowers.addPermission("nfc-manager", true, document);
 
+/**
+ * Emulator helper.
+ */
+let emulator = (function() {
+  let pendingCmdCount = 0;
+  let originalRunEmulatorCmd = runEmulatorCmd;
+
+  // Overwritten it so people could not call this function directly.
+  runEmulatorCmd = function() {
+    throw "Use emulator.run(cmd, callback) instead of runEmulatorCmd";
+  };
+
+  function run(cmd, callback) {
+    pendingCmdCount++;
+    originalRunEmulatorCmd(cmd, function(result) {
+      pendingCmdCount--;
+      if (callback && typeof callback === "function") {
+        callback(result);
+      }
+    });
+  }
+
+  return {
+    run: run
+  };
+}());
+
 function toggleNFC(enabled, callback) {
   isnot(callback, null);
 
   let nfc = window.navigator.mozNfc;
   let req;
   if (enabled) {
     req = nfc.startPoll();
   } else {
--- a/dom/nfc/tests/marionette/test_nfc_manager_tech_discovered.js
+++ b/dom/nfc/tests/marionette/test_nfc_manager_tech_discovered.js
@@ -1,36 +1,76 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 MARIONETTE_TIMEOUT = 30000;
 MARIONETTE_HEAD_JS = 'head.js';
 
+let Promise =
+  SpecialPowers.Cu.import("resource://gre/modules/Promise.jsm").Promise;
+
+// See nfc-nci.h.
+const NCI_LAST_NOTIFICATION  = 0;
+const NCI_LIMIT_NOTIFICATION = 1;
+const NCI_MORE_NOTIFICATIONS = 2;
+
 function handleTechnologyDiscoveredRE0(msg) {
   log('Received \'nfc-manager-tech-discovered\'');
   is(msg.type, 'techDiscovered', 'check for correct message type');
   is(msg.techList[0], 'P2P', 'check for correct tech type');
   toggleNFC(false, runNextTest);
 }
 
-function activateRE0() {
-  var cmd = 'nfc ntf rf_intf_activated 0';
-  log('Executing \'' + cmd + '\'');
-  ++pendingEmulatorCmdCount;
-  runEmulatorCmd(cmd, function(result) {
-    --pendingEmulatorCmdCount;
+function activateRE(re) {
+  let deferred = Promise.defer();
+  let cmd = 'nfc ntf rf_intf_activated ' + re;
+
+  emulator.run(cmd, function(result) {
     is(result.pop(), 'OK', 'check activation of RE0');
+    deferred.resolve();
   });
+
+  return deferred.promise;
+}
+
+function notifyDiscoverRE(re, type) {
+  let deferred = Promise.defer();
+  let cmd = 'nfc ntf rf_discover ' + re + ' ' + type;
+
+  emulator.run(cmd, function(result) {
+    is(result.pop(), 'OK', 'check discover of RE' + re);
+    deferred.resolve();
+  });
+
+  return deferred.promise;
 }
 
 function testActivateRE0() {
   log('Running \'testActivateRE0\'');
   window.navigator.mozSetMessageHandler(
     'nfc-manager-tech-discovered', handleTechnologyDiscoveredRE0);
-  toggleNFC(true, activateRE0);
+
+  toggleNFC(true, function() {
+    activateRE(0);
+  });
+}
+
+// Check NCI Spec 5.2, this will change NCI state from
+// DISCOVERY -> W4_ALL_DISCOVERIES -> W4_HOST_SELECT -> POLL_ACTIVE
+function testRfDiscover() {
+  log('Running \'testRfDiscover\'');
+  window.navigator.mozSetMessageHandler(
+    'nfc-manager-tech-discovered', handleTechnologyDiscoveredRE0);
+
+  toggleNFC(true, function() {
+    notifyDiscoverRE(0, NCI_MORE_NOTIFICATIONS)
+    .then(() => notifyDiscoverRE(1, NCI_LAST_NOTIFICATION))
+    .then(() => activateRE(0));
+  });
 }
 
 let tests = [
-  testActivateRE0
+  testActivateRE0,
+  testRfDiscover
 ];
 
 SpecialPowers.pushPermissions(
   [{'type': 'nfc-manager', 'allow': true, context: document}], runTests);