Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | let pendingEmulatorCmdCount = 0; |
michael@0 | 5 | |
michael@0 | 6 | SpecialPowers.addPermission("nfc-manager", true, document); |
michael@0 | 7 | |
michael@0 | 8 | /** |
michael@0 | 9 | * Emulator helper. |
michael@0 | 10 | */ |
michael@0 | 11 | let emulator = (function() { |
michael@0 | 12 | let pendingCmdCount = 0; |
michael@0 | 13 | let originalRunEmulatorCmd = runEmulatorCmd; |
michael@0 | 14 | |
michael@0 | 15 | // Overwritten it so people could not call this function directly. |
michael@0 | 16 | runEmulatorCmd = function() { |
michael@0 | 17 | throw "Use emulator.run(cmd, callback) instead of runEmulatorCmd"; |
michael@0 | 18 | }; |
michael@0 | 19 | |
michael@0 | 20 | function run(cmd, callback) { |
michael@0 | 21 | pendingCmdCount++; |
michael@0 | 22 | originalRunEmulatorCmd(cmd, function(result) { |
michael@0 | 23 | pendingCmdCount--; |
michael@0 | 24 | if (callback && typeof callback === "function") { |
michael@0 | 25 | callback(result); |
michael@0 | 26 | } |
michael@0 | 27 | }); |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | return { |
michael@0 | 31 | run: run |
michael@0 | 32 | }; |
michael@0 | 33 | }()); |
michael@0 | 34 | |
michael@0 | 35 | function toggleNFC(enabled, callback) { |
michael@0 | 36 | isnot(callback, null); |
michael@0 | 37 | |
michael@0 | 38 | let nfc = window.navigator.mozNfc; |
michael@0 | 39 | let req; |
michael@0 | 40 | if (enabled) { |
michael@0 | 41 | req = nfc.startPoll(); |
michael@0 | 42 | } else { |
michael@0 | 43 | req = nfc.powerOff(); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | req.onsuccess = function() { |
michael@0 | 47 | callback(); |
michael@0 | 48 | }; |
michael@0 | 49 | |
michael@0 | 50 | req.onerror = function() { |
michael@0 | 51 | ok(false, 'operation failed, error ' + req.error.name); |
michael@0 | 52 | finish(); |
michael@0 | 53 | }; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | function cleanUp() { |
michael@0 | 57 | log('Cleaning up'); |
michael@0 | 58 | waitFor(function() { |
michael@0 | 59 | SpecialPowers.removePermission("nfc-manager", document); |
michael@0 | 60 | finish() |
michael@0 | 61 | }, |
michael@0 | 62 | function() { |
michael@0 | 63 | return pendingEmulatorCmdCount === 0; |
michael@0 | 64 | }); |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | function runNextTest() { |
michael@0 | 68 | let test = tests.shift(); |
michael@0 | 69 | if (!test) { |
michael@0 | 70 | cleanUp(); |
michael@0 | 71 | return; |
michael@0 | 72 | } |
michael@0 | 73 | test(); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | // run this function to start tests |
michael@0 | 77 | function runTests() { |
michael@0 | 78 | if ('mozNfc' in window.navigator) { |
michael@0 | 79 | runNextTest(); |
michael@0 | 80 | } else { |
michael@0 | 81 | // succeed immediately on systems without NFC |
michael@0 | 82 | log('Skipping test on system without NFC'); |
michael@0 | 83 | ok(true, 'Skipping test on system without NFC'); |
michael@0 | 84 | finish(); |
michael@0 | 85 | } |
michael@0 | 86 | } |