modules/libpref/test/unit/test_warnings.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 Cu.import("resource://gre/modules/Promise.jsm");
     7 let cs = Cc["@mozilla.org/consoleservice;1"].
     8   getService(Ci.nsIConsoleService);
     9 let ps = Cc["@mozilla.org/preferences-service;1"].
    10   getService(Ci.nsIPrefService);
    12 function makeBuffer(length) {
    13   return new Array(length + 1).join('x');
    14 }
    16 /**
    17  * @resolves |true| if execution proceeded without warning,
    18  * |false| if there was a warning.
    19  */
    20 function checkWarning(pref, buffer) {
    21   let deferred = Promise.defer();
    22   let complete = false;
    23   let listener = {
    24     observe: function(event) {
    25       let message = event.message;
    26       if (!(message.startsWith("Warning: attempting to write")
    27             && message.contains(pref))) {
    28         return;
    29       }
    30       if (complete) {
    31         return;
    32       }
    33       complete = true;
    34       do_print("Warning while setting " + pref);
    35       cs.unregisterListener(listener);
    36       deferred.resolve(true);
    37     }
    38   };
    39   do_timeout(1000, function() {
    40     if (complete) {
    41       return;
    42     }
    43     complete = true;
    44     do_print("No warning while setting " + pref);
    45     cs.unregisterListener(listener);
    46     deferred.resolve(false);
    47   });
    48   cs.registerListener(listener);
    49   ps.setCharPref(pref, buffer);
    50   return deferred.promise;
    51 }
    53 function run_test() {
    54   run_next_test();
    55 }
    57 add_task(function() {
    58   // Simple change, shouldn't cause a warning
    59   do_print("Checking that a simple change doesn't cause a warning");
    60   let buf = makeBuffer(100);
    61   let warned = yield checkWarning("string.accept", buf);
    62   do_check_false(warned);
    64   // Large change, should cause a warning
    65   do_print("Checking that a large change causes a warning");
    66   buf = makeBuffer(32 * 1024);
    67   warned = yield checkWarning("string.warn", buf);
    68   do_check_true(warned);
    69 });

mercurial