michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: MARIONETTE_TIMEOUT = 30000; michael@0: MARIONETTE_HEAD_JS = 'head.js'; michael@0: michael@0: let Promise = michael@0: SpecialPowers.Cu.import("resource://gre/modules/Promise.jsm").Promise; michael@0: michael@0: // See nfc-nci.h. michael@0: const NCI_LAST_NOTIFICATION = 0; michael@0: const NCI_LIMIT_NOTIFICATION = 1; michael@0: const NCI_MORE_NOTIFICATIONS = 2; michael@0: michael@0: function handleTechnologyDiscoveredRE0(msg) { michael@0: log('Received \'nfc-manager-tech-discovered\''); michael@0: is(msg.type, 'techDiscovered', 'check for correct message type'); michael@0: is(msg.techList[0], 'P2P', 'check for correct tech type'); michael@0: toggleNFC(false, runNextTest); michael@0: } michael@0: michael@0: function activateRE(re) { michael@0: let deferred = Promise.defer(); michael@0: let cmd = 'nfc ntf rf_intf_activated ' + re; michael@0: michael@0: emulator.run(cmd, function(result) { michael@0: is(result.pop(), 'OK', 'check activation of RE0'); michael@0: deferred.resolve(); michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function notifyDiscoverRE(re, type) { michael@0: let deferred = Promise.defer(); michael@0: let cmd = 'nfc ntf rf_discover ' + re + ' ' + type; michael@0: michael@0: emulator.run(cmd, function(result) { michael@0: is(result.pop(), 'OK', 'check discover of RE' + re); michael@0: deferred.resolve(); michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function testActivateRE0() { michael@0: log('Running \'testActivateRE0\''); michael@0: window.navigator.mozSetMessageHandler( michael@0: 'nfc-manager-tech-discovered', handleTechnologyDiscoveredRE0); michael@0: michael@0: toggleNFC(true, function() { michael@0: activateRE(0); michael@0: }); michael@0: } michael@0: michael@0: // Check NCI Spec 5.2, this will change NCI state from michael@0: // DISCOVERY -> W4_ALL_DISCOVERIES -> W4_HOST_SELECT -> POLL_ACTIVE michael@0: function testRfDiscover() { michael@0: log('Running \'testRfDiscover\''); michael@0: window.navigator.mozSetMessageHandler( michael@0: 'nfc-manager-tech-discovered', handleTechnologyDiscoveredRE0); michael@0: michael@0: toggleNFC(true, function() { michael@0: notifyDiscoverRE(0, NCI_MORE_NOTIFICATIONS) michael@0: .then(() => notifyDiscoverRE(1, NCI_LAST_NOTIFICATION)) michael@0: .then(() => activateRE(0)); michael@0: }); michael@0: } michael@0: michael@0: let tests = [ michael@0: testActivateRE0, michael@0: testRfDiscover michael@0: ]; michael@0: michael@0: SpecialPowers.pushPermissions( michael@0: [{'type': 'nfc-manager', 'allow': true, context: document}], runTests);