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