michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: let pendingEmulatorCmdCount = 0; michael@0: michael@0: SpecialPowers.addPermission("nfc-manager", true, document); michael@0: michael@0: /** michael@0: * Emulator helper. michael@0: */ michael@0: let emulator = (function() { michael@0: let pendingCmdCount = 0; michael@0: let originalRunEmulatorCmd = runEmulatorCmd; michael@0: michael@0: // Overwritten it so people could not call this function directly. michael@0: runEmulatorCmd = function() { michael@0: throw "Use emulator.run(cmd, callback) instead of runEmulatorCmd"; michael@0: }; michael@0: michael@0: function run(cmd, callback) { michael@0: pendingCmdCount++; michael@0: originalRunEmulatorCmd(cmd, function(result) { michael@0: pendingCmdCount--; michael@0: if (callback && typeof callback === "function") { michael@0: callback(result); michael@0: } michael@0: }); michael@0: } michael@0: michael@0: return { michael@0: run: run michael@0: }; michael@0: }()); michael@0: michael@0: function toggleNFC(enabled, callback) { michael@0: isnot(callback, null); michael@0: michael@0: let nfc = window.navigator.mozNfc; michael@0: let req; michael@0: if (enabled) { michael@0: req = nfc.startPoll(); michael@0: } else { michael@0: req = nfc.powerOff(); michael@0: } michael@0: michael@0: req.onsuccess = function() { michael@0: callback(); michael@0: }; michael@0: michael@0: req.onerror = function() { michael@0: ok(false, 'operation failed, error ' + req.error.name); michael@0: finish(); michael@0: }; michael@0: } michael@0: michael@0: function cleanUp() { michael@0: log('Cleaning up'); michael@0: waitFor(function() { michael@0: SpecialPowers.removePermission("nfc-manager", document); michael@0: finish() michael@0: }, michael@0: function() { michael@0: return pendingEmulatorCmdCount === 0; michael@0: }); michael@0: } michael@0: michael@0: function runNextTest() { michael@0: let test = tests.shift(); michael@0: if (!test) { michael@0: cleanUp(); michael@0: return; michael@0: } michael@0: test(); michael@0: } michael@0: michael@0: // run this function to start tests michael@0: function runTests() { michael@0: if ('mozNfc' in window.navigator) { michael@0: runNextTest(); michael@0: } else { michael@0: // succeed immediately on systems without NFC michael@0: log('Skipping test on system without NFC'); michael@0: ok(true, 'Skipping test on system without NFC'); michael@0: finish(); michael@0: } michael@0: }