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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     6 "use strict";
     8 const Ci = Components.interfaces;
     9 const Cm = Components.manager;
    10 const Cc = Components.classes;
    12 const CONTRACT_ID = "@mozilla.org/xre/runtime;1";
    14 var gAppInfoClassID, gIncOldFactory, gOldCrashSubmit, gCrashesSubmitted;
    15 var gMockAppInfoQueried = false;
    17 function MockAppInfo() {
    18 }
    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 };
    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);
    41   gCrashesSubmitted = 0;
    42   gOldCrashSubmit = BrowserUI.CrashSubmit;
    43   BrowserUI.CrashSubmit = {
    44     submit: function() {
    45       gCrashesSubmitted++;
    46     }
    47   };
    48 }
    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 }
    60 MockAppInfo.prototype = {
    61   QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULRuntime]),
    62   get lastRunCrashID() {
    63     gMockAppInfoQueried = true;
    64     return this.crashid;
    65   },
    66 }
    68 function GetAutoSubmitPref() {
    69   return Services.prefs.getBoolPref("app.crashreporter.autosubmit");
    70 }
    72 function SetAutoSubmitPref(aValue) {
    73   Services.prefs.setBoolPref('app.crashreporter.autosubmit', aValue);
    74 }
    76 function GetPromptedPref() {
    77   return Services.prefs.getBoolPref("app.crashreporter.prompted");
    78 }
    80 function SetPromptedPref(aValue) {
    81   Services.prefs.setBoolPref('app.crashreporter.prompted', aValue);
    82 }
    84 gTests.push({
    85   desc: "Pressing 'cancel' button on the crash reporter prompt",
    86   setUp: initMockAppInfo,
    87   tearDown: cleanupMockAppInfo,
    89   run: function() {
    90     MockAppInfo.crashid = "testid";
    91     SetAutoSubmitPref(false);
    92     SetPromptedPref(false);
    94     BrowserUI.startupCrashCheck();
    95     yield waitForCondition2(function () {
    96       return Browser.selectedBrowser.currentURI.spec == "about:crashprompt";
    97     }, "Loading crash prompt");
    99     yield Browser.selectedTab.pageShowPromise;
   100     ok(true, "Loaded crash prompt");
   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");
   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 });
   117 gTests.push({
   118   desc: "Pressing 'Send Reports' button on the crash reporter prompt",
   119   setUp: initMockAppInfo,
   120   tearDown: cleanupMockAppInfo,
   122   run: function() {
   123     MockAppInfo.crashid = "testid";
   124     SetAutoSubmitPref(false);
   125     SetPromptedPref(false);
   127     BrowserUI.startupCrashCheck();
   128     yield waitForCondition2(function () {
   129       return Browser.selectedBrowser.currentURI.spec == "about:crashprompt";
   130     }, "Loading crash prompt");
   132     yield Browser.selectedTab.pageShowPromise;
   133     ok(true, "Loaded crash prompt");
   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");
   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 });
   152 gTests.push({
   153   desc: "Already prompted, crash reporting disabled",
   154   setUp: initMockAppInfo,
   155   tearDown: cleanupMockAppInfo,
   157   run: function() {
   158     MockAppInfo.crashid = "testid";
   159     SetAutoSubmitPref(false);
   160     SetPromptedPref(true);
   162     BrowserUI.startupCrashCheck();
   163     is(Browser.selectedBrowser.currentURI.spec,
   164        "about:blank",
   165        "Not loading crash prompt");
   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 });
   173 gTests.push({
   174   desc: "Already prompted, crash reporting enabled",
   175   setUp: initMockAppInfo,
   176   tearDown: cleanupMockAppInfo,
   178   run: function() {
   179     MockAppInfo.crashid = "testid";
   180     SetAutoSubmitPref(true);
   181     SetPromptedPref(true);
   183     BrowserUI.startupCrashCheck();
   184     is(Browser.selectedBrowser.currentURI.spec,
   185        "about:blank",
   186        "Not loading crash prompt");
   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 });
   194 gTests.push({
   195   desc: "Crash reporting enabled, not prompted",
   196   setUp: initMockAppInfo,
   197   tearDown: cleanupMockAppInfo,
   199   run: function() {
   200     MockAppInfo.crashid = "testid";
   201     SetAutoSubmitPref(true);
   202     SetPromptedPref(false);
   204     BrowserUI.startupCrashCheck();
   205     is(Browser.selectedBrowser.currentURI.spec,
   206        "about:blank",
   207        "Not loading crash prompt");
   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 });
   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 }

mercurial