1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/metro/base/tests/mochitest/browser_crashprompt.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,222 @@ 1.4 +// -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +"use strict"; 1.10 + 1.11 +const Ci = Components.interfaces; 1.12 +const Cm = Components.manager; 1.13 +const Cc = Components.classes; 1.14 + 1.15 +const CONTRACT_ID = "@mozilla.org/xre/runtime;1"; 1.16 + 1.17 +var gAppInfoClassID, gIncOldFactory, gOldCrashSubmit, gCrashesSubmitted; 1.18 +var gMockAppInfoQueried = false; 1.19 + 1.20 +function MockAppInfo() { 1.21 +} 1.22 + 1.23 +var newFactory = { 1.24 + createInstance: function(aOuter, aIID) { 1.25 + if (aOuter) 1.26 + throw Components.results.NS_ERROR_NO_AGGREGATION; 1.27 + return new MockAppInfo().QueryInterface(aIID); 1.28 + }, 1.29 + lockFactory: function(aLock) { 1.30 + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; 1.31 + }, 1.32 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]) 1.33 +}; 1.34 + 1.35 +function initMockAppInfo() { 1.36 + var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); 1.37 + gAppInfoClassID = registrar.contractIDToCID(CONTRACT_ID); 1.38 + gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); 1.39 + registrar.unregisterFactory(gAppInfoClassID, gIncOldFactory); 1.40 + var components = [MockAppInfo]; 1.41 + registrar.registerFactory(gAppInfoClassID, "", CONTRACT_ID, newFactory); 1.42 + gIncOldFactory = Cm.getClassObject(Cc[CONTRACT_ID], Ci.nsIFactory); 1.43 + 1.44 + gCrashesSubmitted = 0; 1.45 + gOldCrashSubmit = BrowserUI.CrashSubmit; 1.46 + BrowserUI.CrashSubmit = { 1.47 + submit: function() { 1.48 + gCrashesSubmitted++; 1.49 + } 1.50 + }; 1.51 +} 1.52 + 1.53 +function cleanupMockAppInfo() { 1.54 + if (gIncOldFactory) { 1.55 + var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); 1.56 + registrar.unregisterFactory(gAppInfoClassID, newFactory); 1.57 + registrar.registerFactory(gAppInfoClassID, "", CONTRACT_ID, gIncOldFactory); 1.58 + } 1.59 + gIncOldFactory = null; 1.60 + BrowserUI.CrashSubmit = gOldCrashSubmit; 1.61 +} 1.62 + 1.63 +MockAppInfo.prototype = { 1.64 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULRuntime]), 1.65 + get lastRunCrashID() { 1.66 + gMockAppInfoQueried = true; 1.67 + return this.crashid; 1.68 + }, 1.69 +} 1.70 + 1.71 +function GetAutoSubmitPref() { 1.72 + return Services.prefs.getBoolPref("app.crashreporter.autosubmit"); 1.73 +} 1.74 + 1.75 +function SetAutoSubmitPref(aValue) { 1.76 + Services.prefs.setBoolPref('app.crashreporter.autosubmit', aValue); 1.77 +} 1.78 + 1.79 +function GetPromptedPref() { 1.80 + return Services.prefs.getBoolPref("app.crashreporter.prompted"); 1.81 +} 1.82 + 1.83 +function SetPromptedPref(aValue) { 1.84 + Services.prefs.setBoolPref('app.crashreporter.prompted', aValue); 1.85 +} 1.86 + 1.87 +gTests.push({ 1.88 + desc: "Pressing 'cancel' button on the crash reporter prompt", 1.89 + setUp: initMockAppInfo, 1.90 + tearDown: cleanupMockAppInfo, 1.91 + 1.92 + run: function() { 1.93 + MockAppInfo.crashid = "testid"; 1.94 + SetAutoSubmitPref(false); 1.95 + SetPromptedPref(false); 1.96 + 1.97 + BrowserUI.startupCrashCheck(); 1.98 + yield waitForCondition2(function () { 1.99 + return Browser.selectedBrowser.currentURI.spec == "about:crashprompt"; 1.100 + }, "Loading crash prompt"); 1.101 + 1.102 + yield Browser.selectedTab.pageShowPromise; 1.103 + ok(true, "Loaded crash prompt"); 1.104 + 1.105 + EventUtils.sendMouseEvent({type: "click"}, 1.106 + "refuseButton", 1.107 + Browser.selectedBrowser 1.108 + .contentDocument 1.109 + .defaultView); 1.110 + yield waitForCondition2(function () { 1.111 + return Browser.selectedBrowser.currentURI.spec == "about:blank"; 1.112 + }, "Crash prompt dismissed"); 1.113 + 1.114 + ok(!GetAutoSubmitPref(), "auto-submit pref should still be false"); 1.115 + ok(GetPromptedPref(), "prompted pref should now be true"); 1.116 + is(gCrashesSubmitted, 0, "did not submit crash report"); 1.117 + } 1.118 +}); 1.119 + 1.120 +gTests.push({ 1.121 + desc: "Pressing 'Send Reports' button on the crash reporter prompt", 1.122 + setUp: initMockAppInfo, 1.123 + tearDown: cleanupMockAppInfo, 1.124 + 1.125 + run: function() { 1.126 + MockAppInfo.crashid = "testid"; 1.127 + SetAutoSubmitPref(false); 1.128 + SetPromptedPref(false); 1.129 + 1.130 + BrowserUI.startupCrashCheck(); 1.131 + yield waitForCondition2(function () { 1.132 + return Browser.selectedBrowser.currentURI.spec == "about:crashprompt"; 1.133 + }, "Loading crash prompt"); 1.134 + 1.135 + yield Browser.selectedTab.pageShowPromise; 1.136 + ok(true, "Loaded crash prompt"); 1.137 + 1.138 + EventUtils.sendMouseEvent({type: "click"}, 1.139 + "sendReportsButton", 1.140 + Browser.selectedBrowser 1.141 + .contentDocument 1.142 + .defaultView); 1.143 + yield waitForCondition2(function () { 1.144 + return Browser.selectedBrowser.currentURI.spec == "about:blank"; 1.145 + }, "Crash prompt dismissed"); 1.146 + 1.147 + ok(GetAutoSubmitPref(), "auto-submit pref should now be true"); 1.148 + ok(GetPromptedPref(), "prompted pref should now be true"); 1.149 + // TODO: We don't submit a crash report when the user selects 1.150 + // 'Send Reports' but eventually we want to. 1.151 + // is(gCrashesSubmitted, 1, "submitted 1 crash report"); 1.152 + } 1.153 +}); 1.154 + 1.155 +gTests.push({ 1.156 + desc: "Already prompted, crash reporting disabled", 1.157 + setUp: initMockAppInfo, 1.158 + tearDown: cleanupMockAppInfo, 1.159 + 1.160 + run: function() { 1.161 + MockAppInfo.crashid = "testid"; 1.162 + SetAutoSubmitPref(false); 1.163 + SetPromptedPref(true); 1.164 + 1.165 + BrowserUI.startupCrashCheck(); 1.166 + is(Browser.selectedBrowser.currentURI.spec, 1.167 + "about:blank", 1.168 + "Not loading crash prompt"); 1.169 + 1.170 + ok(!GetAutoSubmitPref(), "auto-submit pref should still be false"); 1.171 + ok(GetPromptedPref(), "prompted pref should still be true"); 1.172 + is(gCrashesSubmitted, 0, "did not submit crash report"); 1.173 + } 1.174 +}); 1.175 + 1.176 +gTests.push({ 1.177 + desc: "Already prompted, crash reporting enabled", 1.178 + setUp: initMockAppInfo, 1.179 + tearDown: cleanupMockAppInfo, 1.180 + 1.181 + run: function() { 1.182 + MockAppInfo.crashid = "testid"; 1.183 + SetAutoSubmitPref(true); 1.184 + SetPromptedPref(true); 1.185 + 1.186 + BrowserUI.startupCrashCheck(); 1.187 + is(Browser.selectedBrowser.currentURI.spec, 1.188 + "about:blank", 1.189 + "Not loading crash prompt"); 1.190 + 1.191 + ok(GetAutoSubmitPref(), "auto-submit pref should still be true"); 1.192 + ok(GetPromptedPref(), "prompted pref should still be true"); 1.193 + is(gCrashesSubmitted, 1, "submitted 1 crash report"); 1.194 + } 1.195 +}); 1.196 + 1.197 +gTests.push({ 1.198 + desc: "Crash reporting enabled, not prompted", 1.199 + setUp: initMockAppInfo, 1.200 + tearDown: cleanupMockAppInfo, 1.201 + 1.202 + run: function() { 1.203 + MockAppInfo.crashid = "testid"; 1.204 + SetAutoSubmitPref(true); 1.205 + SetPromptedPref(false); 1.206 + 1.207 + BrowserUI.startupCrashCheck(); 1.208 + is(Browser.selectedBrowser.currentURI.spec, 1.209 + "about:blank", 1.210 + "Not loading crash prompt"); 1.211 + 1.212 + ok(GetAutoSubmitPref(), "auto-submit pref should still be true"); 1.213 + // We don't check the "prompted" pref; it's equally correct for 1.214 + // the pref to be true or false at this point 1.215 + is(gCrashesSubmitted, 1, "submitted 1 crash report"); 1.216 + } 1.217 +}); 1.218 + 1.219 +function test() { 1.220 + if (!CrashReporter.enabled) { 1.221 + info("crash reporter prompt tests didn't run, CrashReporter.enabled is false."); 1.222 + return; 1.223 + } 1.224 + runTests(); 1.225 +}