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

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:f2363ecd81a6
1 "use strict";
2
3 Components.utils.import("resource://gre/modules/osfile.jsm");
4 Components.utils.import("resource://gre/modules/Services.jsm");
5
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;
13
14 do_print("Test starting");
15
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 }
30
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 };
43
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 }
52
53 toggleConsoleListener(true);
54
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();
63
64 do_print(OS.Shared.LOG.toSource());
65
66 do_print("Logging 1");
67 OS.Shared.LOG(objectDefault);
68
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 }
74

mercurial