browser/base/content/test/plugins/browser_CTP_crashreporting.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/plugins/browser_CTP_crashreporting.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +Cu.import("resource://gre/modules/Services.jsm");
     1.9 +
    1.10 +const SERVER_URL = "http://example.com/browser/toolkit/crashreporter/test/browser/crashreport.sjs";
    1.11 +const gTestRoot = getRootDirectory(gTestPath);
    1.12 +var gTestBrowser = null;
    1.13 +
    1.14 +// Test that plugin crash submissions still work properly after
    1.15 +// click-to-play activation.
    1.16 +
    1.17 +function test() {
    1.18 +  // Crashing the plugin takes up a lot of time, so extend the test timeout.
    1.19 +  requestLongerTimeout(2);
    1.20 +  waitForExplicitFinish();
    1.21 +  setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY);
    1.22 +
    1.23 +  // The test harness sets MOZ_CRASHREPORTER_NO_REPORT, which disables plugin
    1.24 +  // crash reports.  This test needs them enabled.  The test also needs a mock
    1.25 +  // report server, and fortunately one is already set up by toolkit/
    1.26 +  // crashreporter/test/Makefile.in.  Assign its URL to MOZ_CRASHREPORTER_URL,
    1.27 +  // which CrashSubmit.jsm uses as a server override.
    1.28 +  let env = Cc["@mozilla.org/process/environment;1"].
    1.29 +            getService(Components.interfaces.nsIEnvironment);
    1.30 +  let noReport = env.get("MOZ_CRASHREPORTER_NO_REPORT");
    1.31 +  let serverURL = env.get("MOZ_CRASHREPORTER_URL");
    1.32 +  env.set("MOZ_CRASHREPORTER_NO_REPORT", "");
    1.33 +  env.set("MOZ_CRASHREPORTER_URL", SERVER_URL);
    1.34 +
    1.35 +  let tab = gBrowser.loadOneTab("about:blank", { inBackground: false });
    1.36 +  gTestBrowser = gBrowser.getBrowserForTab(tab);
    1.37 +  gTestBrowser.addEventListener("PluginCrashed", onCrash, false);
    1.38 +  gTestBrowser.addEventListener("load", onPageLoad, true);
    1.39 +  Services.obs.addObserver(onSubmitStatus, "crash-report-status", false);
    1.40 +
    1.41 +  registerCleanupFunction(function cleanUp() {
    1.42 +    env.set("MOZ_CRASHREPORTER_NO_REPORT", noReport);
    1.43 +    env.set("MOZ_CRASHREPORTER_URL", serverURL);
    1.44 +    gTestBrowser.removeEventListener("PluginCrashed", onCrash, false);
    1.45 +    gTestBrowser.removeEventListener("load", onPageLoad, true);
    1.46 +    Services.obs.removeObserver(onSubmitStatus, "crash-report-status");
    1.47 +    gBrowser.removeCurrentTab();
    1.48 +  });
    1.49 +
    1.50 +  gTestBrowser.contentWindow.location = gTestRoot + "plugin_big.html";
    1.51 +}
    1.52 +function onPageLoad() {
    1.53 +  // Force the plugins binding to attach as layout is async.
    1.54 +  let plugin = gTestBrowser.contentDocument.getElementById("test");
    1.55 +  plugin.clientTop;
    1.56 +  executeSoon(afterBindingAttached);
    1.57 +}
    1.58 +
    1.59 +function afterBindingAttached() {
    1.60 +  let popupNotification = PopupNotifications.getNotification("click-to-play-plugins", gTestBrowser);
    1.61 +  ok(popupNotification, "Should have a click-to-play notification");
    1.62 +
    1.63 +  let plugin = gTestBrowser.contentDocument.getElementById("test");
    1.64 +  let objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
    1.65 +  ok(!objLoadingContent.activated, "Plugin should not be activated");
    1.66 +
    1.67 +  // Simulate clicking the "Allow Always" button.
    1.68 +  popupNotification.reshow();
    1.69 +  PopupNotifications.panel.firstChild._primaryButton.click();
    1.70 +
    1.71 +  let condition = function() objLoadingContent.activated;
    1.72 +  waitForCondition(condition, pluginActivated, "Waited too long for plugin to activate");
    1.73 +}
    1.74 +
    1.75 +function pluginActivated() {
    1.76 +  let plugin = gTestBrowser.contentDocument.getElementById("test");
    1.77 +  try {
    1.78 +    plugin.crash();
    1.79 +  } catch (e) {
    1.80 +    // The plugin crashed in the above call, an exception is expected.
    1.81 +  }
    1.82 +}
    1.83 +
    1.84 +function onCrash() {
    1.85 +  try {
    1.86 +    let plugin = gBrowser.contentDocument.getElementById("test");
    1.87 +    let elt = gPluginHandler.getPluginUI.bind(gPluginHandler, plugin);
    1.88 +    let style =
    1.89 +      gBrowser.contentWindow.getComputedStyle(elt("pleaseSubmit"));
    1.90 +    is(style.display, "block", "Submission UI visibility should be correct");
    1.91 +
    1.92 +    elt("submitComment").value = "a test comment";
    1.93 +    is(elt("submitURLOptIn").checked, true, "URL opt-in should default to true");
    1.94 +    EventUtils.synthesizeMouseAtCenter(elt("submitURLOptIn"), {}, gTestBrowser.contentWindow);
    1.95 +    EventUtils.synthesizeMouseAtCenter(elt("submitButton"), {}, gTestBrowser.contentWindow);
    1.96 +    // And now wait for the submission status notification.
    1.97 +  }
    1.98 +  catch (err) {
    1.99 +    failWithException(err);
   1.100 +  }
   1.101 +}
   1.102 +
   1.103 +function onSubmitStatus(subj, topic, data) {
   1.104 +  try {
   1.105 +    // Wait for success or failed, doesn't matter which.
   1.106 +    if (data != "success" && data != "failed")
   1.107 +      return;
   1.108 +
   1.109 +    let extra = getPropertyBagValue(subj.QueryInterface(Ci.nsIPropertyBag),
   1.110 +                                    "extra");
   1.111 +    ok(extra instanceof Ci.nsIPropertyBag, "Extra data should be property bag");
   1.112 +
   1.113 +    let val = getPropertyBagValue(extra, "PluginUserComment");
   1.114 +    is(val, "a test comment",
   1.115 +       "Comment in extra data should match comment in textbox");
   1.116 +
   1.117 +    val = getPropertyBagValue(extra, "PluginContentURL");
   1.118 +    ok(val === undefined,
   1.119 +       "URL should be absent from extra data when opt-in not checked");
   1.120 +  }
   1.121 +  catch (err) {
   1.122 +    failWithException(err);
   1.123 +  }
   1.124 +  finish();
   1.125 +}
   1.126 +
   1.127 +function getPropertyBagValue(bag, key) {
   1.128 +  try {
   1.129 +    var val = bag.getProperty(key);
   1.130 +  }
   1.131 +  catch (e if e.result == Cr.NS_ERROR_FAILURE) {}
   1.132 +  return val;
   1.133 +}
   1.134 +
   1.135 +function failWithException(err) {
   1.136 +  ok(false, "Uncaught exception: " + err + "\n" + err.stack);
   1.137 +}

mercurial