Bug 987596 - B2G NFC: Marionette tests for onpeerready. r=dimi
authorYoshi Huang <allstars.chh@mozilla.com>
Fri, 25 Apr 2014 19:22:09 +0800
changeset 181002 d5d2904ed31c72c15e574aa4522af72b0b37b64d
parent 181001 12b431e1eaaa631a735ff914cf9422130139bb37
child 181003 4f3bcc6a80fd527cbcc155251c4b1af12be60b77
push id272
push userpvanderbeken@mozilla.com
push dateMon, 05 May 2014 16:31:18 +0000
reviewersdimi
bugs987596, 100644
milestone31.0a1
Bug 987596 - B2G NFC: Marionette tests for onpeerready. r=dimi From bfece1daa040d5e7e4ff965adea343831b2484c4 Mon Sep 17 00:00:00 2001 --- dom/nfc/tests/marionette/head.js | 5 +- dom/nfc/tests/marionette/manifest.ini | 1 + dom/nfc/tests/marionette/test_nfc_enabled.js | 1 - .../marionette/test_nfc_manager_tech_discovered.js | 5 +- dom/nfc/tests/marionette/test_nfc_peer.js | 64 ++++++++++++++++++++++ 5 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 dom/nfc/tests/marionette/test_nfc_peer.js
dom/nfc/tests/marionette/head.js
dom/nfc/tests/marionette/manifest.ini
dom/nfc/tests/marionette/test_nfc_enabled.js
dom/nfc/tests/marionette/test_nfc_manager_tech_discovered.js
dom/nfc/tests/marionette/test_nfc_peer.js
--- a/dom/nfc/tests/marionette/head.js
+++ b/dom/nfc/tests/marionette/head.js
@@ -1,13 +1,17 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 let pendingEmulatorCmdCount = 0;
 
+let Promise =
+  SpecialPowers.Cu.import("resource://gre/modules/Promise.jsm").Promise;
+let nfc = window.navigator.mozNfc;
+
 SpecialPowers.addPermission("nfc-manager", true, document);
 
 /**
  * Emulator helper.
  */
 let emulator = (function() {
   let pendingCmdCount = 0;
   let originalRunEmulatorCmd = runEmulatorCmd;
@@ -30,17 +34,16 @@ let emulator = (function() {
   return {
     run: run
   };
 }());
 
 function toggleNFC(enabled, callback) {
   isnot(callback, null);
 
-  let nfc = window.navigator.mozNfc;
   let req;
   if (enabled) {
     req = nfc.startPoll();
   } else {
     req = nfc.powerOff();
   }
 
   req.onsuccess = function() {
--- a/dom/nfc/tests/marionette/manifest.ini
+++ b/dom/nfc/tests/marionette/manifest.ini
@@ -1,8 +1,9 @@
 [DEFAULT]
 b2g=true
 browser=false
 qemu=true
 
 [test_ndef.js]
 [test_nfc_enabled.js]
 [test_nfc_manager_tech_discovered.js]
+[test_nfc_peer.js]
--- a/dom/nfc/tests/marionette/test_nfc_enabled.js
+++ b/dom/nfc/tests/marionette/test_nfc_enabled.js
@@ -1,15 +1,14 @@
 /* Any copyright is dedicated to the Public Domain.
    http://creativecommons.org/publicdomain/zero/1.0/ */
 
 MARIONETTE_TIMEOUT = 30000;
 MARIONETTE_HEAD_JS = 'head.js';
 
-let nfc = window.navigator.mozNfc;
 function testEnableNFC() {
   log('Running \'testEnableNFC\'');
   let req = nfc.startPoll();
   req.onsuccess = function () {
     ok(true);
     runNextTest();
   };
   req.onerror = function () {
--- a/dom/nfc/tests/marionette/test_nfc_manager_tech_discovered.js
+++ b/dom/nfc/tests/marionette/test_nfc_manager_tech_discovered.js
@@ -1,17 +1,14 @@
 /* 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');
@@ -19,17 +16,17 @@ function handleTechnologyDiscoveredRE0(m
   toggleNFC(false, runNextTest);
 }
 
 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');
+    is(result.pop(), 'OK', 'check activation of RE' + re);
     deferred.resolve();
   });
 
   return deferred.promise;
 }
 
 function notifyDiscoverRE(re, type) {
   let deferred = Promise.defer();
new file mode 100644
--- /dev/null
+++ b/dom/nfc/tests/marionette/test_nfc_peer.js
@@ -0,0 +1,64 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+MARIONETTE_TIMEOUT = 30000;
+MARIONETTE_HEAD_JS = 'head.js';
+
+let MANIFEST_URL = "app://system.gaiamobile.org/manifest.webapp";
+
+function peerReadyCb(evt) {
+  log("peerReadyCb called");
+  let peer = nfc.getNFCPeer(evt.detail);
+  ok(peer instanceof MozNFCPeer, "Should get a NFCPeer object.");
+
+  // reset callback and NFC Hardware.
+  nfc.onpeerready = null;
+  toggleNFC(false, runNextTest);
+}
+
+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");
+
+  nfc.onpeerready = peerReadyCb;
+
+  let request = nfc.checkP2PRegistration(MANIFEST_URL);
+  request.onsuccess = function (evt) {
+    nfc.notifyUserAcceptedP2P(MANIFEST_URL);
+  }
+
+  request.onerror = function () {
+    ok(false, "checkP2PRegistration failed.");
+    toggleNFC(false, runNextTest);
+  }
+}
+
+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 RE" + re);
+    deferred.resolve();
+  });
+
+  return deferred.promise;
+}
+
+function testPeerReady() {
+  window.navigator.mozSetMessageHandler(
+    "nfc-manager-tech-discovered", handleTechnologyDiscoveredRE0);
+
+  toggleNFC(true, function() {
+    activateRE(0);
+  });
+}
+
+let tests = [
+  testPeerReady
+];
+
+SpecialPowers.pushPermissions(
+  [{"type": "nfc-manager", "allow": true, context: document},
+   {"type": "nfc-write", "allow": true, context: document}], runTests);