dom/nfc/tests/marionette/head.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/nfc/tests/marionette/head.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +let pendingEmulatorCmdCount = 0;
     1.8 +
     1.9 +SpecialPowers.addPermission("nfc-manager", true, document);
    1.10 +
    1.11 +/**
    1.12 + * Emulator helper.
    1.13 + */
    1.14 +let emulator = (function() {
    1.15 +  let pendingCmdCount = 0;
    1.16 +  let originalRunEmulatorCmd = runEmulatorCmd;
    1.17 +
    1.18 +  // Overwritten it so people could not call this function directly.
    1.19 +  runEmulatorCmd = function() {
    1.20 +    throw "Use emulator.run(cmd, callback) instead of runEmulatorCmd";
    1.21 +  };
    1.22 +
    1.23 +  function run(cmd, callback) {
    1.24 +    pendingCmdCount++;
    1.25 +    originalRunEmulatorCmd(cmd, function(result) {
    1.26 +      pendingCmdCount--;
    1.27 +      if (callback && typeof callback === "function") {
    1.28 +        callback(result);
    1.29 +      }
    1.30 +    });
    1.31 +  }
    1.32 +
    1.33 +  return {
    1.34 +    run: run
    1.35 +  };
    1.36 +}());
    1.37 +
    1.38 +function toggleNFC(enabled, callback) {
    1.39 +  isnot(callback, null);
    1.40 +
    1.41 +  let nfc = window.navigator.mozNfc;
    1.42 +  let req;
    1.43 +  if (enabled) {
    1.44 +    req = nfc.startPoll();
    1.45 +  } else {
    1.46 +    req = nfc.powerOff();
    1.47 +  }
    1.48 +
    1.49 +  req.onsuccess = function() {
    1.50 +    callback();
    1.51 +  };
    1.52 +
    1.53 +  req.onerror = function() {
    1.54 +    ok(false, 'operation failed, error ' + req.error.name);
    1.55 +    finish();
    1.56 +  };
    1.57 +}
    1.58 +
    1.59 +function cleanUp() {
    1.60 +  log('Cleaning up');
    1.61 +  waitFor(function() {
    1.62 +            SpecialPowers.removePermission("nfc-manager", document);
    1.63 +            finish()
    1.64 +          },
    1.65 +          function() {
    1.66 +            return pendingEmulatorCmdCount === 0;
    1.67 +          });
    1.68 +}
    1.69 +
    1.70 +function runNextTest() {
    1.71 +  let test = tests.shift();
    1.72 +  if (!test) {
    1.73 +    cleanUp();
    1.74 +    return;
    1.75 +  }
    1.76 +  test();
    1.77 +}
    1.78 +
    1.79 +// run this function to start tests
    1.80 +function runTests() {
    1.81 +  if ('mozNfc' in window.navigator) {
    1.82 +    runNextTest();
    1.83 +  } else {
    1.84 +    // succeed immediately on systems without NFC
    1.85 +    log('Skipping test on system without NFC');
    1.86 +    ok(true, 'Skipping test on system without NFC');
    1.87 +    finish();
    1.88 +  }
    1.89 +}

mercurial