services/sync/tests/unit/test_utils_catch.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_utils_catch.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +Cu.import("resource://services-sync/util.js");
     1.5 +Cu.import("resource://services-sync/service.js");
     1.6 +
     1.7 +function run_test() {
     1.8 +  _("Make sure catch when copied to an object will correctly catch stuff");
     1.9 +  let ret, rightThis, didCall, didThrow, wasTen, wasLocked;
    1.10 +  let obj = {
    1.11 +    catch: Utils.catch,
    1.12 +    _log: {
    1.13 +      debug: function(str) {
    1.14 +        didThrow = str.search(/^Exception: /) == 0;
    1.15 +      },
    1.16 +      info: function(str) {
    1.17 +        wasLocked = str.indexOf("Cannot start sync: already syncing?") == 0;
    1.18 +      }
    1.19 +    },
    1.20 +
    1.21 +    func: function() this.catch(function() {
    1.22 +      rightThis = this == obj;
    1.23 +      didCall = true;
    1.24 +      return 5;
    1.25 +    })(),
    1.26 +
    1.27 +    throwy: function() this.catch(function() {
    1.28 +      rightThis = this == obj;
    1.29 +      didCall = true;
    1.30 +      throw 10;
    1.31 +    })(),
    1.32 +
    1.33 +    callbacky: function() this.catch(function() {
    1.34 +      rightThis = this == obj;
    1.35 +      didCall = true;
    1.36 +      throw 10;
    1.37 +    }, function(ex) {
    1.38 +      wasTen = (ex == 10)
    1.39 +    })(),
    1.40 +
    1.41 +    lockedy: function() this.catch(function() {
    1.42 +      rightThis = this == obj;
    1.43 +      didCall = true;
    1.44 +      throw("Could not acquire lock.");
    1.45 +    })()
    1.46 +  };
    1.47 +
    1.48 +  _("Make sure a normal call will call and return");
    1.49 +  rightThis = didCall = didThrow = wasLocked = false;
    1.50 +  ret = obj.func();
    1.51 +  do_check_eq(ret, 5);
    1.52 +  do_check_true(rightThis);
    1.53 +  do_check_true(didCall);
    1.54 +  do_check_false(didThrow);
    1.55 +  do_check_eq(wasTen, undefined);
    1.56 +  do_check_false(wasLocked);
    1.57 +
    1.58 +  _("Make sure catch/throw results in debug call and caller doesn't need to handle exception");
    1.59 +  rightThis = didCall = didThrow = wasLocked = false;
    1.60 +  ret = obj.throwy();
    1.61 +  do_check_eq(ret, undefined);
    1.62 +  do_check_true(rightThis);
    1.63 +  do_check_true(didCall);
    1.64 +  do_check_true(didThrow);
    1.65 +  do_check_eq(wasTen, undefined);
    1.66 +  do_check_false(wasLocked);
    1.67 +
    1.68 +  _("Test callback for exception testing.");
    1.69 +  rightThis = didCall = didThrow = wasLocked = false;
    1.70 +  ret = obj.callbacky();
    1.71 +  do_check_eq(ret, undefined);
    1.72 +  do_check_true(rightThis);
    1.73 +  do_check_true(didCall);
    1.74 +  do_check_true(didThrow);
    1.75 +  do_check_true(wasTen);
    1.76 +  do_check_false(wasLocked);
    1.77 +
    1.78 +  _("Test the lock-aware catch that Service uses.");
    1.79 +  obj.catch = Service._catch;
    1.80 +  rightThis = didCall = didThrow = wasLocked = false;
    1.81 +  wasTen = undefined;
    1.82 +  ret = obj.lockedy();
    1.83 +  do_check_eq(ret, undefined);
    1.84 +  do_check_true(rightThis);
    1.85 +  do_check_true(didCall);
    1.86 +  do_check_true(didThrow);
    1.87 +  do_check_eq(wasTen, undefined);
    1.88 +  do_check_true(wasLocked);
    1.89 +}

mercurial