michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const Ci = Components.interfaces; michael@0: const Cm = Components.manager; michael@0: const Cc = Components.classes; michael@0: michael@0: const CONTRACT_ID = "@mozilla.org/xre/runtime;1"; michael@0: michael@0: var gAppInfoClassID, gIncOldFactory, gOldCrashSubmit, gCrashesSubmitted; michael@0: var gMockAppInfoQueried = false; michael@0: michael@0: function MockAppInfo() { michael@0: } michael@0: michael@0: var newFactory = { michael@0: createInstance: function(aOuter, aIID) { michael@0: if (aOuter) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: return new MockAppInfo().QueryInterface(aIID); michael@0: }, michael@0: lockFactory: function(aLock) { michael@0: throw Components.results.NS_ERROR_NOT_IMPLEMENTED; michael@0: }, michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]) michael@0: }; michael@0: michael@0: function initMockAppInfo() { michael@0: var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); michael@0: gAppInfoClassID = registrar.contractIDToCID(CONTRACT_ID); michael@0: gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); michael@0: registrar.unregisterFactory(gAppInfoClassID, gIncOldFactory); michael@0: var components = [MockAppInfo]; michael@0: registrar.registerFactory(gAppInfoClassID, "", CONTRACT_ID, newFactory); michael@0: gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); michael@0: michael@0: gCrashesSubmitted = 0; michael@0: gOldCrashSubmit = BrowserUI.CrashSubmit; michael@0: BrowserUI.CrashSubmit = { michael@0: submit: function() { michael@0: gCrashesSubmitted++; michael@0: } michael@0: }; michael@0: } michael@0: michael@0: function cleanupMockAppInfo() { michael@0: if (gIncOldFactory) { michael@0: var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); michael@0: registrar.unregisterFactory(gAppInfoClassID, newFactory); michael@0: registrar.registerFactory(gAppInfoClassID, "", CONTRACT_ID, gIncOldFactory); michael@0: } michael@0: gIncOldFactory = null; michael@0: BrowserUI.CrashSubmit = gOldCrashSubmit; michael@0: } michael@0: michael@0: MockAppInfo.prototype = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULRuntime]), michael@0: get lastRunCrashID() { michael@0: gMockAppInfoQueried = true; michael@0: return this.crashid; michael@0: }, michael@0: } michael@0: michael@0: function GetAutoSubmitPref() { michael@0: return Services.prefs.getBoolPref("app.crashreporter.autosubmit"); michael@0: } michael@0: michael@0: function SetAutoSubmitPref(aValue) { michael@0: Services.prefs.setBoolPref('app.crashreporter.autosubmit', aValue); michael@0: } michael@0: michael@0: function GetPromptedPref() { michael@0: return Services.prefs.getBoolPref("app.crashreporter.prompted"); michael@0: } michael@0: michael@0: function SetPromptedPref(aValue) { michael@0: Services.prefs.setBoolPref('app.crashreporter.prompted', aValue); michael@0: } michael@0: michael@0: gTests.push({ michael@0: desc: "Pressing 'cancel' button on the crash reporter prompt", michael@0: setUp: initMockAppInfo, michael@0: tearDown: cleanupMockAppInfo, michael@0: michael@0: run: function() { michael@0: MockAppInfo.crashid = "testid"; michael@0: SetAutoSubmitPref(false); michael@0: SetPromptedPref(false); michael@0: michael@0: BrowserUI.startupCrashCheck(); michael@0: yield waitForCondition2(function () { michael@0: return Browser.selectedBrowser.currentURI.spec == "about:crashprompt"; michael@0: }, "Loading crash prompt"); michael@0: michael@0: yield Browser.selectedTab.pageShowPromise; michael@0: ok(true, "Loaded crash prompt"); michael@0: michael@0: EventUtils.sendMouseEvent({type: "click"}, michael@0: "refuseButton", michael@0: Browser.selectedBrowser michael@0: .contentDocument michael@0: .defaultView); michael@0: yield waitForCondition2(function () { michael@0: return Browser.selectedBrowser.currentURI.spec == "about:blank"; michael@0: }, "Crash prompt dismissed"); michael@0: michael@0: ok(!GetAutoSubmitPref(), "auto-submit pref should still be false"); michael@0: ok(GetPromptedPref(), "prompted pref should now be true"); michael@0: is(gCrashesSubmitted, 0, "did not submit crash report"); michael@0: } michael@0: }); michael@0: michael@0: gTests.push({ michael@0: desc: "Pressing 'Send Reports' button on the crash reporter prompt", michael@0: setUp: initMockAppInfo, michael@0: tearDown: cleanupMockAppInfo, michael@0: michael@0: run: function() { michael@0: MockAppInfo.crashid = "testid"; michael@0: SetAutoSubmitPref(false); michael@0: SetPromptedPref(false); michael@0: michael@0: BrowserUI.startupCrashCheck(); michael@0: yield waitForCondition2(function () { michael@0: return Browser.selectedBrowser.currentURI.spec == "about:crashprompt"; michael@0: }, "Loading crash prompt"); michael@0: michael@0: yield Browser.selectedTab.pageShowPromise; michael@0: ok(true, "Loaded crash prompt"); michael@0: michael@0: EventUtils.sendMouseEvent({type: "click"}, michael@0: "sendReportsButton", michael@0: Browser.selectedBrowser michael@0: .contentDocument michael@0: .defaultView); michael@0: yield waitForCondition2(function () { michael@0: return Browser.selectedBrowser.currentURI.spec == "about:blank"; michael@0: }, "Crash prompt dismissed"); michael@0: michael@0: ok(GetAutoSubmitPref(), "auto-submit pref should now be true"); michael@0: ok(GetPromptedPref(), "prompted pref should now be true"); michael@0: // TODO: We don't submit a crash report when the user selects michael@0: // 'Send Reports' but eventually we want to. michael@0: // is(gCrashesSubmitted, 1, "submitted 1 crash report"); michael@0: } michael@0: }); michael@0: michael@0: gTests.push({ michael@0: desc: "Already prompted, crash reporting disabled", michael@0: setUp: initMockAppInfo, michael@0: tearDown: cleanupMockAppInfo, michael@0: michael@0: run: function() { michael@0: MockAppInfo.crashid = "testid"; michael@0: SetAutoSubmitPref(false); michael@0: SetPromptedPref(true); michael@0: michael@0: BrowserUI.startupCrashCheck(); michael@0: is(Browser.selectedBrowser.currentURI.spec, michael@0: "about:blank", michael@0: "Not loading crash prompt"); michael@0: michael@0: ok(!GetAutoSubmitPref(), "auto-submit pref should still be false"); michael@0: ok(GetPromptedPref(), "prompted pref should still be true"); michael@0: is(gCrashesSubmitted, 0, "did not submit crash report"); michael@0: } michael@0: }); michael@0: michael@0: gTests.push({ michael@0: desc: "Already prompted, crash reporting enabled", michael@0: setUp: initMockAppInfo, michael@0: tearDown: cleanupMockAppInfo, michael@0: michael@0: run: function() { michael@0: MockAppInfo.crashid = "testid"; michael@0: SetAutoSubmitPref(true); michael@0: SetPromptedPref(true); michael@0: michael@0: BrowserUI.startupCrashCheck(); michael@0: is(Browser.selectedBrowser.currentURI.spec, michael@0: "about:blank", michael@0: "Not loading crash prompt"); michael@0: michael@0: ok(GetAutoSubmitPref(), "auto-submit pref should still be true"); michael@0: ok(GetPromptedPref(), "prompted pref should still be true"); michael@0: is(gCrashesSubmitted, 1, "submitted 1 crash report"); michael@0: } michael@0: }); michael@0: michael@0: gTests.push({ michael@0: desc: "Crash reporting enabled, not prompted", michael@0: setUp: initMockAppInfo, michael@0: tearDown: cleanupMockAppInfo, michael@0: michael@0: run: function() { michael@0: MockAppInfo.crashid = "testid"; michael@0: SetAutoSubmitPref(true); michael@0: SetPromptedPref(false); michael@0: michael@0: BrowserUI.startupCrashCheck(); michael@0: is(Browser.selectedBrowser.currentURI.spec, michael@0: "about:blank", michael@0: "Not loading crash prompt"); michael@0: michael@0: ok(GetAutoSubmitPref(), "auto-submit pref should still be true"); michael@0: // We don't check the "prompted" pref; it's equally correct for michael@0: // the pref to be true or false at this point michael@0: is(gCrashesSubmitted, 1, "submitted 1 crash report"); michael@0: } michael@0: }); michael@0: michael@0: function test() { michael@0: if (!CrashReporter.enabled) { michael@0: info("crash reporter prompt tests didn't run, CrashReporter.enabled is false."); michael@0: return; michael@0: } michael@0: runTests(); michael@0: }