Bug 1000935: Added Marionette test for NfcPeer.sendNDEF, r=dlee
authorThomas Zimmermann <tdz@users.sourceforge.net>
Mon, 12 May 2014 15:19:13 +0800
changeset 182580 1548cd4a589562e7ae9f14dd3bc0de8ba84307ee
parent 182579 bf6874a80bbf7297bf881c88c289830d177ad60c
child 182581 08cbbdb5a1347b18169635c93420424c20f8dce5
push id26766
push userryanvm@gmail.com
push dateMon, 12 May 2014 20:06:14 +0000
treeherdermozilla-central@b1c9fcb5450b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdlee
bugs1000935
milestone32.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 1000935: Added Marionette test for NfcPeer.sendNDEF, r=dlee
dom/nfc/tests/marionette/manifest.ini
dom/nfc/tests/marionette/test_nfc_peer_sendndef.js
--- a/dom/nfc/tests/marionette/manifest.ini
+++ b/dom/nfc/tests/marionette/manifest.ini
@@ -2,8 +2,9 @@
 b2g=true
 browser=false
 qemu=true
 
 [test_ndef.js]
 [test_nfc_enabled.js]
 [test_nfc_manager_tech_discovered.js]
 [test_nfc_peer.js]
+[test_nfc_peer_sendndef.js]
new file mode 100644
--- /dev/null
+++ b/dom/nfc/tests/marionette/test_nfc_peer_sendndef.js
@@ -0,0 +1,106 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+MARIONETTE_TIMEOUT = 30000;
+MARIONETTE_HEAD_JS = "head.js";
+
+let url = "https://www.example.com";
+
+function compareNDEFs(ndef1, ndef2) {
+  is(ndef1.length, ndef2.length,
+     "NDEF messages have the same number of records");
+  ndef1.forEach(function(record1, index) {
+    let record2 = this[index];
+    is(record1.tnf, record2.tnf, "test for equal TNF fields");
+    let fields = ["type", "id", "payload"];
+    fields.forEach(function(value) {
+        let field1 = record1[value];
+        let field2 = record2[value];
+        is(field1.length, field2.length,
+           value + " fields have the same length");
+        let eq = true;
+        for (let i = 0; eq && i < field1.length; ++i) {
+          eq = (field1[i] === field2[i]);
+        }
+        ok(eq, value + " fields contain the same data");
+      });
+  }, ndef2);
+}
+
+function parseNDEFString(str) {
+  /* make it an object */
+  let arr = null;
+  try {
+    arr = JSON.parse(str);
+  } catch (e) {
+    ok(false, "Parser error: " + e.message);
+    return null;
+  }
+  /* and build NDEF array */
+  let ndef = arr.map(function(value) {
+      let type = new Uint8Array(NfcUtils.fromUTF8(this.atob(value.type)));
+      let id = new Uint8Array(NfcUtils.fromUTF8(this.atob(value.id)));
+      let payload = new Uint8Array(NfcUtils.fromUTF8(this.atob(value.payload)));
+      return new MozNDEFRecord(value.tnf, type, id, payload);
+    }, window);
+  return ndef;
+}
+
+function sendNDEF(techType, sessionToken) {
+  let tnf = NDEF.TNF_WELL_KNOWN;
+  let type = new Uint8Array(NfcUtils.fromUTF8("U"));
+  let id = new Uint8Array(NfcUtils.fromUTF8(""));
+  let payload = new Uint8Array(NfcUtils.fromUTF8(url));
+  let ndef = [new MozNDEFRecord(tnf, type, id, payload)];
+
+  let peer = window.navigator.mozNfc.getNFCPeer(sessionToken);
+  let req = peer.sendNDEF(ndef);
+  req.onsuccess = function() {
+    log("Successfully sent NDEF message");
+
+    let cmd = "nfc snep put -1 -1"; /* read last SNEP PUT from emulator */
+    log("Executing \'" + cmd + "\'");
+    emulator.run(cmd, function(result) {
+      is(result.pop(), "OK", "check SNEP PUT result");
+      let ndef2 = parseNDEFString(result.pop());
+      compareNDEFs(ndef, ndef2);
+      toggleNFC(false, runNextTest);
+    });
+  };
+  req.onerror = function() {
+    ok(false, "Failed to send NDEF message, error \'" + this.error + "\'");
+    toggleNFC(false, runNextTest);
+  };
+}
+
+function handleTechnologyDiscoveredRE0(msg) {
+  log("Received \'nfc-manager-tech-discovered\' " + JSON.stringify(msg));
+  is(msg.type, "techDiscovered", "check for correct message type");
+  let index = msg.techList.indexOf("P2P");
+  isnot(index, -1, "check for \'P2P\' in tech list");
+  sendNDEF(msg.techList[index], msg.sessionToken);
+}
+
+function activateRE0() {
+  let cmd = "nfc nci rf_intf_activated_ntf 0";
+  log("Executing \'" + cmd + "\'");
+  emulator.run(cmd, function(result) {
+    is(result.pop(), "OK", "check activation of RE0");
+  });
+}
+
+function testOnPeerReadyRE0() {
+  log("Running \'testOnPeerReadyRE0\'");
+  window.navigator.mozSetMessageHandler(
+    "nfc-manager-tech-discovered", handleTechnologyDiscoveredRE0);
+  toggleNFC(true, activateRE0);
+}
+
+let tests = [
+  testOnPeerReadyRE0
+];
+
+SpecialPowers.pushPermissions(
+  [{"type": "nfc", "allow": true,
+                   "read": true, 'write': true, context: document},
+   {"type": "nfc-manager", 'allow': true, context: document}], runTests);