modules/libpref/test/unit/test_warnings.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/libpref/test/unit/test_warnings.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,69 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +Cu.import("resource://gre/modules/Promise.jsm");
     1.9 +
    1.10 +let cs = Cc["@mozilla.org/consoleservice;1"].
    1.11 +  getService(Ci.nsIConsoleService);
    1.12 +let ps = Cc["@mozilla.org/preferences-service;1"].
    1.13 +  getService(Ci.nsIPrefService);
    1.14 +
    1.15 +function makeBuffer(length) {
    1.16 +  return new Array(length + 1).join('x');
    1.17 +}
    1.18 +
    1.19 +/**
    1.20 + * @resolves |true| if execution proceeded without warning,
    1.21 + * |false| if there was a warning.
    1.22 + */
    1.23 +function checkWarning(pref, buffer) {
    1.24 +  let deferred = Promise.defer();
    1.25 +  let complete = false;
    1.26 +  let listener = {
    1.27 +    observe: function(event) {
    1.28 +      let message = event.message;
    1.29 +      if (!(message.startsWith("Warning: attempting to write")
    1.30 +            && message.contains(pref))) {
    1.31 +        return;
    1.32 +      }
    1.33 +      if (complete) {
    1.34 +        return;
    1.35 +      }
    1.36 +      complete = true;
    1.37 +      do_print("Warning while setting " + pref);
    1.38 +      cs.unregisterListener(listener);
    1.39 +      deferred.resolve(true);
    1.40 +    }
    1.41 +  };
    1.42 +  do_timeout(1000, function() {
    1.43 +    if (complete) {
    1.44 +      return;
    1.45 +    }
    1.46 +    complete = true;
    1.47 +    do_print("No warning while setting " + pref);
    1.48 +    cs.unregisterListener(listener);
    1.49 +    deferred.resolve(false);
    1.50 +  });
    1.51 +  cs.registerListener(listener);
    1.52 +  ps.setCharPref(pref, buffer);
    1.53 +  return deferred.promise;
    1.54 +}
    1.55 +
    1.56 +function run_test() {
    1.57 +  run_next_test();
    1.58 +}
    1.59 +
    1.60 +add_task(function() {
    1.61 +  // Simple change, shouldn't cause a warning
    1.62 +  do_print("Checking that a simple change doesn't cause a warning");
    1.63 +  let buf = makeBuffer(100);
    1.64 +  let warned = yield checkWarning("string.accept", buf);
    1.65 +  do_check_false(warned);
    1.66 +
    1.67 +  // Large change, should cause a warning
    1.68 +  do_print("Checking that a large change causes a warning");
    1.69 +  buf = makeBuffer(32 * 1024);
    1.70 +  warned = yield checkWarning("string.warn", buf);
    1.71 +  do_check_true(warned);
    1.72 +});

mercurial