toolkit/components/osfile/tests/xpcshell/test_logging.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 "use strict";
     3 Components.utils.import("resource://gre/modules/osfile.jsm");
     4 Components.utils.import("resource://gre/modules/Services.jsm");
     6 /**
     7  * Tests logging by passing OS.Shared.LOG both an object with its own
     8  * toString method, and one with the default.
     9  */
    10 function run_test() {
    11   do_test_pending();
    12   let messageCount = 0;
    14   do_print("Test starting");
    16   // Create a console listener.
    17   let consoleListener = {
    18     observe: function (aMessage) {
    19       //Ignore unexpected messages.
    20       if (!(aMessage instanceof Components.interfaces.nsIConsoleMessage)) {
    21         return;
    22       }
    23       // This is required, as printing to the |Services.console|
    24       // while in the observe function causes an exception.
    25       do_execute_soon(function() {
    26         do_print("Observing message " + aMessage.message);
    27         if (aMessage.message.indexOf("TEST OS") < 0) {
    28           return;
    29         }
    31         ++messageCount;
    32         if(messageCount == 1) {
    33           do_check_eq(aMessage.message, "TEST OS {\"name\":\"test\"}\n");
    34         }
    35         if(messageCount == 2) {
    36           do_check_eq(aMessage.message, "TEST OS name is test\n");
    37           toggleConsoleListener(false);
    38           do_test_finished();
    39         }
    40       });
    41     }
    42   };
    44   // Set/Unset the console listener.
    45   function toggleConsoleListener (pref) {
    46     do_print("Setting console listener: " + pref);
    47     Services.prefs.setBoolPref("toolkit.osfile.log", pref);
    48     Services.prefs.setBoolPref("toolkit.osfile.log.redirect", pref);
    49     Services.console[pref ? "registerListener" : "unregisterListener"](
    50       consoleListener);
    51   }
    53   toggleConsoleListener(true);
    55   let objectDefault = {name: "test"};
    56   let CustomToString = function() {
    57     this.name = "test";
    58   };
    59   CustomToString.prototype.toString = function() {
    60     return "name is " + this.name;
    61   };
    62   let objectCustom = new CustomToString();
    64   do_print(OS.Shared.LOG.toSource());
    66   do_print("Logging 1");
    67   OS.Shared.LOG(objectDefault);
    69   do_print("Logging 2");
    70   OS.Shared.LOG(objectCustom);
    71   // Once both messages are observed OS.Shared.DEBUG, and OS.Shared.TEST
    72   // are reset to false.
    73 }

mercurial