|
1 // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 "use strict"; |
|
7 |
|
8 const Ci = Components.interfaces; |
|
9 const Cm = Components.manager; |
|
10 const Cc = Components.classes; |
|
11 |
|
12 const CONTRACT_ID = "@mozilla.org/xre/runtime;1"; |
|
13 |
|
14 var gAppInfoClassID, gIncOldFactory, gOldCrashSubmit, gCrashesSubmitted; |
|
15 var gMockAppInfoQueried = false; |
|
16 |
|
17 function MockAppInfo() { |
|
18 } |
|
19 |
|
20 var newFactory = { |
|
21 createInstance: function(aOuter, aIID) { |
|
22 if (aOuter) |
|
23 throw Components.results.NS_ERROR_NO_AGGREGATION; |
|
24 return new MockAppInfo().QueryInterface(aIID); |
|
25 }, |
|
26 lockFactory: function(aLock) { |
|
27 throw Components.results.NS_ERROR_NOT_IMPLEMENTED; |
|
28 }, |
|
29 QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]) |
|
30 }; |
|
31 |
|
32 function initMockAppInfo() { |
|
33 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); |
|
34 gAppInfoClassID = registrar.contractIDToCID(CONTRACT_ID); |
|
35 gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); |
|
36 registrar.unregisterFactory(gAppInfoClassID, gIncOldFactory); |
|
37 var components = [MockAppInfo]; |
|
38 registrar.registerFactory(gAppInfoClassID, "", CONTRACT_ID, newFactory); |
|
39 gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); |
|
40 |
|
41 gCrashesSubmitted = 0; |
|
42 gOldCrashSubmit = BrowserUI.CrashSubmit; |
|
43 BrowserUI.CrashSubmit = { |
|
44 submit: function() { |
|
45 gCrashesSubmitted++; |
|
46 } |
|
47 }; |
|
48 } |
|
49 |
|
50 function cleanupMockAppInfo() { |
|
51 if (gIncOldFactory) { |
|
52 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); |
|
53 registrar.unregisterFactory(gAppInfoClassID, newFactory); |
|
54 registrar.registerFactory(gAppInfoClassID, "", CONTRACT_ID, gIncOldFactory); |
|
55 } |
|
56 gIncOldFactory = null; |
|
57 BrowserUI.CrashSubmit = gOldCrashSubmit; |
|
58 } |
|
59 |
|
60 MockAppInfo.prototype = { |
|
61 QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULRuntime]), |
|
62 get lastRunCrashID() { |
|
63 gMockAppInfoQueried = true; |
|
64 return this.crashid; |
|
65 }, |
|
66 } |
|
67 |
|
68 function GetAutoSubmitPref() { |
|
69 return Services.prefs.getBoolPref("app.crashreporter.autosubmit"); |
|
70 } |
|
71 |
|
72 function SetAutoSubmitPref(aValue) { |
|
73 Services.prefs.setBoolPref('app.crashreporter.autosubmit', aValue); |
|
74 } |
|
75 |
|
76 function GetPromptedPref() { |
|
77 return Services.prefs.getBoolPref("app.crashreporter.prompted"); |
|
78 } |
|
79 |
|
80 function SetPromptedPref(aValue) { |
|
81 Services.prefs.setBoolPref('app.crashreporter.prompted', aValue); |
|
82 } |
|
83 |
|
84 gTests.push({ |
|
85 desc: "Pressing 'cancel' button on the crash reporter prompt", |
|
86 setUp: initMockAppInfo, |
|
87 tearDown: cleanupMockAppInfo, |
|
88 |
|
89 run: function() { |
|
90 MockAppInfo.crashid = "testid"; |
|
91 SetAutoSubmitPref(false); |
|
92 SetPromptedPref(false); |
|
93 |
|
94 BrowserUI.startupCrashCheck(); |
|
95 yield waitForCondition2(function () { |
|
96 return Browser.selectedBrowser.currentURI.spec == "about:crashprompt"; |
|
97 }, "Loading crash prompt"); |
|
98 |
|
99 yield Browser.selectedTab.pageShowPromise; |
|
100 ok(true, "Loaded crash prompt"); |
|
101 |
|
102 EventUtils.sendMouseEvent({type: "click"}, |
|
103 "refuseButton", |
|
104 Browser.selectedBrowser |
|
105 .contentDocument |
|
106 .defaultView); |
|
107 yield waitForCondition2(function () { |
|
108 return Browser.selectedBrowser.currentURI.spec == "about:blank"; |
|
109 }, "Crash prompt dismissed"); |
|
110 |
|
111 ok(!GetAutoSubmitPref(), "auto-submit pref should still be false"); |
|
112 ok(GetPromptedPref(), "prompted pref should now be true"); |
|
113 is(gCrashesSubmitted, 0, "did not submit crash report"); |
|
114 } |
|
115 }); |
|
116 |
|
117 gTests.push({ |
|
118 desc: "Pressing 'Send Reports' button on the crash reporter prompt", |
|
119 setUp: initMockAppInfo, |
|
120 tearDown: cleanupMockAppInfo, |
|
121 |
|
122 run: function() { |
|
123 MockAppInfo.crashid = "testid"; |
|
124 SetAutoSubmitPref(false); |
|
125 SetPromptedPref(false); |
|
126 |
|
127 BrowserUI.startupCrashCheck(); |
|
128 yield waitForCondition2(function () { |
|
129 return Browser.selectedBrowser.currentURI.spec == "about:crashprompt"; |
|
130 }, "Loading crash prompt"); |
|
131 |
|
132 yield Browser.selectedTab.pageShowPromise; |
|
133 ok(true, "Loaded crash prompt"); |
|
134 |
|
135 EventUtils.sendMouseEvent({type: "click"}, |
|
136 "sendReportsButton", |
|
137 Browser.selectedBrowser |
|
138 .contentDocument |
|
139 .defaultView); |
|
140 yield waitForCondition2(function () { |
|
141 return Browser.selectedBrowser.currentURI.spec == "about:blank"; |
|
142 }, "Crash prompt dismissed"); |
|
143 |
|
144 ok(GetAutoSubmitPref(), "auto-submit pref should now be true"); |
|
145 ok(GetPromptedPref(), "prompted pref should now be true"); |
|
146 // TODO: We don't submit a crash report when the user selects |
|
147 // 'Send Reports' but eventually we want to. |
|
148 // is(gCrashesSubmitted, 1, "submitted 1 crash report"); |
|
149 } |
|
150 }); |
|
151 |
|
152 gTests.push({ |
|
153 desc: "Already prompted, crash reporting disabled", |
|
154 setUp: initMockAppInfo, |
|
155 tearDown: cleanupMockAppInfo, |
|
156 |
|
157 run: function() { |
|
158 MockAppInfo.crashid = "testid"; |
|
159 SetAutoSubmitPref(false); |
|
160 SetPromptedPref(true); |
|
161 |
|
162 BrowserUI.startupCrashCheck(); |
|
163 is(Browser.selectedBrowser.currentURI.spec, |
|
164 "about:blank", |
|
165 "Not loading crash prompt"); |
|
166 |
|
167 ok(!GetAutoSubmitPref(), "auto-submit pref should still be false"); |
|
168 ok(GetPromptedPref(), "prompted pref should still be true"); |
|
169 is(gCrashesSubmitted, 0, "did not submit crash report"); |
|
170 } |
|
171 }); |
|
172 |
|
173 gTests.push({ |
|
174 desc: "Already prompted, crash reporting enabled", |
|
175 setUp: initMockAppInfo, |
|
176 tearDown: cleanupMockAppInfo, |
|
177 |
|
178 run: function() { |
|
179 MockAppInfo.crashid = "testid"; |
|
180 SetAutoSubmitPref(true); |
|
181 SetPromptedPref(true); |
|
182 |
|
183 BrowserUI.startupCrashCheck(); |
|
184 is(Browser.selectedBrowser.currentURI.spec, |
|
185 "about:blank", |
|
186 "Not loading crash prompt"); |
|
187 |
|
188 ok(GetAutoSubmitPref(), "auto-submit pref should still be true"); |
|
189 ok(GetPromptedPref(), "prompted pref should still be true"); |
|
190 is(gCrashesSubmitted, 1, "submitted 1 crash report"); |
|
191 } |
|
192 }); |
|
193 |
|
194 gTests.push({ |
|
195 desc: "Crash reporting enabled, not prompted", |
|
196 setUp: initMockAppInfo, |
|
197 tearDown: cleanupMockAppInfo, |
|
198 |
|
199 run: function() { |
|
200 MockAppInfo.crashid = "testid"; |
|
201 SetAutoSubmitPref(true); |
|
202 SetPromptedPref(false); |
|
203 |
|
204 BrowserUI.startupCrashCheck(); |
|
205 is(Browser.selectedBrowser.currentURI.spec, |
|
206 "about:blank", |
|
207 "Not loading crash prompt"); |
|
208 |
|
209 ok(GetAutoSubmitPref(), "auto-submit pref should still be true"); |
|
210 // We don't check the "prompted" pref; it's equally correct for |
|
211 // the pref to be true or false at this point |
|
212 is(gCrashesSubmitted, 1, "submitted 1 crash report"); |
|
213 } |
|
214 }); |
|
215 |
|
216 function test() { |
|
217 if (!CrashReporter.enabled) { |
|
218 info("crash reporter prompt tests didn't run, CrashReporter.enabled is false."); |
|
219 return; |
|
220 } |
|
221 runTests(); |
|
222 } |