dom/fmradio/test/marionette/test_enable_disable.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/fmradio/test/marionette/test_enable_disable.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,85 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +MARIONETTE_TIMEOUT = 10000;
     1.8 +
     1.9 +SpecialPowers.addPermission("fmradio", true, document);
    1.10 +
    1.11 +let FMRadio = window.navigator.mozFMRadio;
    1.12 +
    1.13 +function verifyInitialState() {
    1.14 +  log("Verifying initial state.");
    1.15 +  ok(FMRadio);
    1.16 +  is(FMRadio.enabled, false);
    1.17 +
    1.18 +  log("Verifying attributes when disabled.");
    1.19 +  is(FMRadio.frequency, null);
    1.20 +  ok(FMRadio.frequencyLowerBound);
    1.21 +  ok(FMRadio.frequencyUpperBound);
    1.22 +  ok(FMRadio.frequencyUpperBound > FMRadio.frequencyLowerBound);
    1.23 +  ok(FMRadio.channelWidth);
    1.24 +
    1.25 +  enableFMRadio();
    1.26 +}
    1.27 +
    1.28 +function enableFMRadio() {
    1.29 +  log("Verifying behaviors when enabled.");
    1.30 +  var frequency = FMRadio.frequencyLowerBound + FMRadio.channelWidth;
    1.31 +  var request = FMRadio.enable(frequency);
    1.32 +  ok(request, "FMRadio.enable(r" + frequency + ") returns request");
    1.33 +
    1.34 +  request.onsuccess = function() {
    1.35 +    ok(FMRadio.enabled);
    1.36 +    ok(typeof FMRadio.frequency == "number");
    1.37 +    ok(FMRadio.frequency > FMRadio.frequencyLowerBound);
    1.38 +  };
    1.39 +
    1.40 +  request.onerror = function() {
    1.41 +    ok(null, "Failed to enable");
    1.42 +  };
    1.43 +
    1.44 +  var enabled = false;
    1.45 +  FMRadio.onenabled = function() {
    1.46 +    FMRadio.onenabled = null;
    1.47 +    enabled = FMRadio.enabled;
    1.48 +  };
    1.49 +
    1.50 +  FMRadio.onfrequencychange = function() {
    1.51 +    log("Check if 'onfrequencychange' event is fired after the 'enabled' event");
    1.52 +    FMRadio.onfrequencychange = null;
    1.53 +    ok(enabled, "FMRadio is enabled when handling `onfrequencychange`");
    1.54 +    disableFMRadio();
    1.55 +  };
    1.56 +}
    1.57 +
    1.58 +function disableFMRadio() {
    1.59 +  log("Verify behaviors when disabled");
    1.60 +
    1.61 +  // There are two possibilities which depends on the system
    1.62 +  // process scheduling (bug 911063 comment 0):
    1.63 +  //   - seek fails
    1.64 +  //   - seek's onsuccess fires before disable's onsucess
    1.65 +  var seekRequest = FMRadio.seekUp();
    1.66 +  var seekCompletes = false;
    1.67 +  var failedToSeek = false;
    1.68 +  seekRequest.onerror = function() {
    1.69 +    ok(!seekCompletes);
    1.70 +    failedToSeek = true;
    1.71 +  };
    1.72 +
    1.73 +  seekRequest.onsuccess = function() {
    1.74 +    ok(!failedToSeek);
    1.75 +    seekCompletes = true;
    1.76 +  };
    1.77 +
    1.78 +  FMRadio.disable();
    1.79 +  FMRadio.ondisabled = function() {
    1.80 +    FMRadio.ondisabled = null;
    1.81 +    ok(seekCompletes || failedToSeek);
    1.82 +    ok(!FMRadio.enabled);
    1.83 +    finish();
    1.84 +  };
    1.85 +}
    1.86 +
    1.87 +verifyInitialState();
    1.88 +

mercurial