1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/osfile/tests/xpcshell/test_logging.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,74 @@ 1.4 +"use strict"; 1.5 + 1.6 +Components.utils.import("resource://gre/modules/osfile.jsm"); 1.7 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.8 + 1.9 +/** 1.10 + * Tests logging by passing OS.Shared.LOG both an object with its own 1.11 + * toString method, and one with the default. 1.12 + */ 1.13 +function run_test() { 1.14 + do_test_pending(); 1.15 + let messageCount = 0; 1.16 + 1.17 + do_print("Test starting"); 1.18 + 1.19 + // Create a console listener. 1.20 + let consoleListener = { 1.21 + observe: function (aMessage) { 1.22 + //Ignore unexpected messages. 1.23 + if (!(aMessage instanceof Components.interfaces.nsIConsoleMessage)) { 1.24 + return; 1.25 + } 1.26 + // This is required, as printing to the |Services.console| 1.27 + // while in the observe function causes an exception. 1.28 + do_execute_soon(function() { 1.29 + do_print("Observing message " + aMessage.message); 1.30 + if (aMessage.message.indexOf("TEST OS") < 0) { 1.31 + return; 1.32 + } 1.33 + 1.34 + ++messageCount; 1.35 + if(messageCount == 1) { 1.36 + do_check_eq(aMessage.message, "TEST OS {\"name\":\"test\"}\n"); 1.37 + } 1.38 + if(messageCount == 2) { 1.39 + do_check_eq(aMessage.message, "TEST OS name is test\n"); 1.40 + toggleConsoleListener(false); 1.41 + do_test_finished(); 1.42 + } 1.43 + }); 1.44 + } 1.45 + }; 1.46 + 1.47 + // Set/Unset the console listener. 1.48 + function toggleConsoleListener (pref) { 1.49 + do_print("Setting console listener: " + pref); 1.50 + Services.prefs.setBoolPref("toolkit.osfile.log", pref); 1.51 + Services.prefs.setBoolPref("toolkit.osfile.log.redirect", pref); 1.52 + Services.console[pref ? "registerListener" : "unregisterListener"]( 1.53 + consoleListener); 1.54 + } 1.55 + 1.56 + toggleConsoleListener(true); 1.57 + 1.58 + let objectDefault = {name: "test"}; 1.59 + let CustomToString = function() { 1.60 + this.name = "test"; 1.61 + }; 1.62 + CustomToString.prototype.toString = function() { 1.63 + return "name is " + this.name; 1.64 + }; 1.65 + let objectCustom = new CustomToString(); 1.66 + 1.67 + do_print(OS.Shared.LOG.toSource()); 1.68 + 1.69 + do_print("Logging 1"); 1.70 + OS.Shared.LOG(objectDefault); 1.71 + 1.72 + do_print("Logging 2"); 1.73 + OS.Shared.LOG(objectCustom); 1.74 + // Once both messages are observed OS.Shared.DEBUG, and OS.Shared.TEST 1.75 + // are reset to false. 1.76 +} 1.77 +