Commit 7ea50a9f authored by Szu-Yu Chen [:aknow]'s avatar Szu-Yu Chen [:aknow]
Browse files

Bug 919414 - Part 1: Clear existing calls in test setup. r=hsinyi

parent 341f38bd
Loading
Loading
Loading
Loading
+88 −4
Original line number Diff line number Diff line
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
 * http://creativecommons.org/publicdomain/zero/1.0/ */

let Promise = SpecialPowers.Cu.import("resource://gre/modules/Promise.jsm").Promise;
let telephony;

/**
 * Emulator helper.
@@ -46,7 +47,71 @@ let emulator = (function() {
  };
}());

function startTest(test) {
/**
 * Telephony related helper functions.
 */
(function() {
  function checkInitialState() {
    log("Verify initial state.");
    ok(telephony, 'telephony');
    is(telephony.active, null, 'telephony.active');
    ok(telephony.calls, 'telephony.calls');
    is(telephony.calls.length, 0, 'telephony.calls.length');
  }

  /**
   * @return Promise
   */
  function clearCalls() {
    let deferred = Promise.defer();

    log("Clear existing calls.");
    emulator.run("gsm clear", function(result) {
      if (result[0] == "OK") {
        waitFor(function() {
          deferred.resolve();
        }, function() {
          return telephony.calls.length === 0;
        });
      } else {
        log("Failed to clear existing calls.");
        deferred.reject();
      }
    });

    return deferred.promise;
  }

  this.checkInitialState = checkInitialState;
  this.clearCalls = clearCalls;
}());

function _startTest(permissions, test) {
  function permissionSetUp() {
    SpecialPowers.setBoolPref("dom.mozSettings.enabled", true);
    SpecialPowers.setBoolPref("dom.promise.enabled", true);
    for (let per of permissions) {
      SpecialPowers.addPermission(per, true, document);
    }
  }

  function permissionTearDown() {
    SpecialPowers.clearUserPref("dom.mozSettings.enabled");
    SpecialPowers.clearUserPref("dom.promise.enabled");
    for (let per of permissions) {
      SpecialPowers.removePermission(per, document);
    }
  }

  function setUp() {
    log('== Test SetUp ==');
    permissionSetUp();
    // Make sure that we get the telephony after adding permission.
    telephony = window.navigator.mozTelephony;
    ok(telephony);
    return clearCalls().then(checkInitialState);
  }

  // Extend finish() with tear down.
  finish = (function() {
    let originalFinish = finish;
@@ -54,6 +119,7 @@ function startTest(test) {
    function tearDown() {
      log('== Test TearDown ==');
      emulator.waitFinish()
        .then(permissionTearDown)
        .then(function() {
          originalFinish.apply(this, arguments);
        });
@@ -62,6 +128,24 @@ function startTest(test) {
    return tearDown.bind(this);
  }());

  // Start the test.
  function mainTest() {
    setUp()
      .then(function onSuccess() {
        log('== Test Start ==');
        test();
      }, function onError(error) {
        SpecialPowers.Cu.reportError(error);
        ok(false, 'SetUp error');
      });
  }

  mainTest();
}

function startTest(test) {
  _startTest(['telephony'], test);
}

function startTestWithPermissions(permissions, test) {
  _startTest(permissions.concat('telephony'), test);
}