|
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/. */ |
|
4 |
|
5 const Ci = Components.interfaces; |
|
6 const Cu = Components.utils; |
|
7 const PREF_DEPRECATION_WARNINGS = "devtools.errorconsole.deprecation_warnings"; |
|
8 |
|
9 Cu.import("resource://gre/modules/Services.jsm", this); |
|
10 Cu.import("resource://gre/modules/Deprecated.jsm", this); |
|
11 |
|
12 // Using this named functions to test deprecation and the properly logged |
|
13 // callstacks. |
|
14 function basicDeprecatedFunction () { |
|
15 Deprecated.warning("this method is deprecated.", "http://example.com"); |
|
16 return true; |
|
17 } |
|
18 |
|
19 function deprecationFunctionBogusCallstack () { |
|
20 Deprecated.warning("this method is deprecated.", "http://example.com", { |
|
21 caller: {} |
|
22 }); |
|
23 return true; |
|
24 } |
|
25 |
|
26 function deprecationFunctionCustomCallstack () { |
|
27 // Get the nsIStackFrame that will contain the name of this function. |
|
28 function getStack () { |
|
29 return Components.stack; |
|
30 } |
|
31 Deprecated.warning("this method is deprecated.", "http://example.com", |
|
32 getStack()); |
|
33 return true; |
|
34 } |
|
35 |
|
36 let tests = [ |
|
37 // Test deprecation warning without passing the callstack. |
|
38 { |
|
39 deprecatedFunction: basicDeprecatedFunction, |
|
40 expectedObservation: function (aMessage) { |
|
41 testAMessage(aMessage); |
|
42 ok(aMessage.errorMessage.indexOf("basicDeprecatedFunction") > 0, |
|
43 "Callstack is correctly logged."); |
|
44 } |
|
45 }, |
|
46 // Test a reported error when URL to documentation is not passed. |
|
47 { |
|
48 deprecatedFunction: function () { |
|
49 Deprecated.warning("this method is deprecated."); |
|
50 return true; |
|
51 }, |
|
52 expectedObservation: function (aMessage) { |
|
53 ok(aMessage.errorMessage.indexOf("must provide a URL") > 0, |
|
54 "Deprecation warning logged an empty URL argument."); |
|
55 } |
|
56 }, |
|
57 // Test deprecation with a bogus callstack passed as an argument (it will be |
|
58 // replaced with the current call stack). |
|
59 { |
|
60 deprecatedFunction: deprecationFunctionBogusCallstack, |
|
61 expectedObservation: function (aMessage) { |
|
62 testAMessage(aMessage); |
|
63 ok(aMessage.errorMessage.indexOf("deprecationFunctionBogusCallstack") > 0, |
|
64 "Callstack is correctly logged."); |
|
65 } |
|
66 }, |
|
67 // When pref is unset Deprecated.warning should not log anything. |
|
68 { |
|
69 deprecatedFunction: basicDeprecatedFunction, |
|
70 expectedObservation: null, |
|
71 // Set pref to false. |
|
72 logWarnings: false |
|
73 }, |
|
74 // Test deprecation with a valid custom callstack passed as an argument. |
|
75 { |
|
76 deprecatedFunction: deprecationFunctionCustomCallstack, |
|
77 expectedObservation: function (aMessage) { |
|
78 testAMessage(aMessage); |
|
79 ok(aMessage.errorMessage.indexOf("deprecationFunctionCustomCallstack") > 0, |
|
80 "Callstack is correctly logged."); |
|
81 }, |
|
82 // Set pref to true. |
|
83 logWarnings: true |
|
84 }]; |
|
85 |
|
86 // Which test are we running now? |
|
87 let idx = -1; |
|
88 |
|
89 function test() { |
|
90 waitForExplicitFinish(); |
|
91 |
|
92 // Check if Deprecated is loaded. |
|
93 ok(Deprecated, "Deprecated object exists"); |
|
94 |
|
95 nextTest(); |
|
96 } |
|
97 |
|
98 // Test Consle Message attributes. |
|
99 function testAMessage (aMessage) { |
|
100 ok(aMessage.errorMessage.indexOf("DEPRECATION WARNING: " + |
|
101 "this method is deprecated.") === 0, |
|
102 "Deprecation is correctly logged."); |
|
103 ok(aMessage.errorMessage.indexOf("http://example.com") > 0, |
|
104 "URL is correctly logged."); |
|
105 } |
|
106 |
|
107 function nextTest() { |
|
108 idx++; |
|
109 |
|
110 if (idx == tests.length) { |
|
111 finish(); |
|
112 return; |
|
113 } |
|
114 |
|
115 info("Running test #" + idx); |
|
116 let test = tests[idx]; |
|
117 |
|
118 // Deprecation warnings will be logged only when the preference is set. |
|
119 if (typeof test.logWarnings !== "undefined") { |
|
120 Services.prefs.setBoolPref(PREF_DEPRECATION_WARNINGS, test.logWarnings); |
|
121 } |
|
122 |
|
123 // Create a console listener. |
|
124 let consoleListener = { |
|
125 observe: function (aMessage) { |
|
126 // Ignore unexpected messages. |
|
127 if (!(aMessage instanceof Ci.nsIScriptError)) { |
|
128 return; |
|
129 } |
|
130 if (aMessage.errorMessage.indexOf("DEPRECATION WARNING: ") < 0 && |
|
131 aMessage.errorMessage.indexOf("must provide a URL") < 0) { |
|
132 return; |
|
133 } |
|
134 ok(aMessage instanceof Ci.nsIScriptError, |
|
135 "Deprecation log message is an instance of type nsIScriptError."); |
|
136 |
|
137 |
|
138 if (test.expectedObservation === null) { |
|
139 ok(false, "Deprecated warning not expected"); |
|
140 } |
|
141 else { |
|
142 test.expectedObservation(aMessage); |
|
143 } |
|
144 |
|
145 Services.console.unregisterListener(consoleListener); |
|
146 executeSoon(nextTest); |
|
147 } |
|
148 }; |
|
149 Services.console.registerListener(consoleListener); |
|
150 test.deprecatedFunction(); |
|
151 if (test.expectedObservation === null) { |
|
152 executeSoon(function() { |
|
153 Services.console.unregisterListener(consoleListener); |
|
154 executeSoon(nextTest); |
|
155 }); |
|
156 } |
|
157 } |