browser/metro/base/tests/mochitest/browser_crashprompt.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial