1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/plugins/browser_pluginCrashCommentAndURL.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,155 @@ 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 CRASH_URL = "http://example.com/browser/browser/base/content/test/plugins/plugin_crashCommentAndURL.html"; 1.11 + 1.12 +const SERVER_URL = "http://example.com/browser/toolkit/crashreporter/test/browser/crashreport.sjs"; 1.13 + 1.14 +function test() { 1.15 + // Crashing the plugin takes up a lot of time, so extend the test timeout. 1.16 + requestLongerTimeout(runs.length); 1.17 + waitForExplicitFinish(); 1.18 + setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED); 1.19 + 1.20 + // The test harness sets MOZ_CRASHREPORTER_NO_REPORT, which disables plugin 1.21 + // crash reports. This test needs them enabled. The test also needs a mock 1.22 + // report server, and fortunately one is already set up by toolkit/ 1.23 + // crashreporter/test/Makefile.in. Assign its URL to MOZ_CRASHREPORTER_URL, 1.24 + // which CrashSubmit.jsm uses as a server override. 1.25 + let env = Cc["@mozilla.org/process/environment;1"]. 1.26 + getService(Components.interfaces.nsIEnvironment); 1.27 + let noReport = env.get("MOZ_CRASHREPORTER_NO_REPORT"); 1.28 + let serverURL = env.get("MOZ_CRASHREPORTER_URL"); 1.29 + env.set("MOZ_CRASHREPORTER_NO_REPORT", ""); 1.30 + env.set("MOZ_CRASHREPORTER_URL", SERVER_URL); 1.31 + 1.32 + let tab = gBrowser.loadOneTab("about:blank", { inBackground: false }); 1.33 + let browser = gBrowser.getBrowserForTab(tab); 1.34 + browser.addEventListener("PluginCrashed", onCrash, false); 1.35 + Services.obs.addObserver(onSubmitStatus, "crash-report-status", false); 1.36 + 1.37 + registerCleanupFunction(function cleanUp() { 1.38 + env.set("MOZ_CRASHREPORTER_NO_REPORT", noReport); 1.39 + env.set("MOZ_CRASHREPORTER_URL", serverURL); 1.40 + gBrowser.selectedBrowser.removeEventListener("PluginCrashed", onCrash, 1.41 + false); 1.42 + Services.obs.removeObserver(onSubmitStatus, "crash-report-status"); 1.43 + gBrowser.removeCurrentTab(); 1.44 + }); 1.45 + 1.46 + doNextRun(); 1.47 +} 1.48 + 1.49 +let runs = [ 1.50 + { 1.51 + shouldSubmissionUIBeVisible: true, 1.52 + comment: "", 1.53 + urlOptIn: false, 1.54 + }, 1.55 + { 1.56 + shouldSubmissionUIBeVisible: true, 1.57 + comment: "a test comment", 1.58 + urlOptIn: true, 1.59 + }, 1.60 + { 1.61 + width: 300, 1.62 + height: 300, 1.63 + shouldSubmissionUIBeVisible: false, 1.64 + }, 1.65 +]; 1.66 + 1.67 +let currentRun = null; 1.68 + 1.69 +function doNextRun() { 1.70 + try { 1.71 + if (!runs.length) { 1.72 + finish(); 1.73 + return; 1.74 + } 1.75 + currentRun = runs.shift(); 1.76 + let args = ["width", "height"].reduce(function (memo, arg) { 1.77 + if (arg in currentRun) 1.78 + memo[arg] = currentRun[arg]; 1.79 + return memo; 1.80 + }, {}); 1.81 + gBrowser.loadURI(CRASH_URL + "?" + 1.82 + encodeURIComponent(JSON.stringify(args))); 1.83 + // And now wait for the crash. 1.84 + } 1.85 + catch (err) { 1.86 + failWithException(err); 1.87 + finish(); 1.88 + } 1.89 +} 1.90 + 1.91 +function onCrash() { 1.92 + try { 1.93 + let plugin = gBrowser.contentDocument.getElementById("plugin"); 1.94 + let elt = gPluginHandler.getPluginUI.bind(gPluginHandler, plugin); 1.95 + let style = 1.96 + gBrowser.contentWindow.getComputedStyle(elt("pleaseSubmit")); 1.97 + is(style.display, 1.98 + currentRun.shouldSubmissionUIBeVisible ? "block" : "none", 1.99 + "Submission UI visibility should be correct"); 1.100 + if (!currentRun.shouldSubmissionUIBeVisible) { 1.101 + // Done with this run. 1.102 + doNextRun(); 1.103 + return; 1.104 + } 1.105 + elt("submitComment").value = currentRun.comment; 1.106 + elt("submitURLOptIn").checked = currentRun.urlOptIn; 1.107 + elt("submitButton").click(); 1.108 + // And now wait for the submission status notification. 1.109 + } 1.110 + catch (err) { 1.111 + failWithException(err); 1.112 + doNextRun(); 1.113 + } 1.114 +} 1.115 + 1.116 +function onSubmitStatus(subj, topic, data) { 1.117 + try { 1.118 + // Wait for success or failed, doesn't matter which. 1.119 + if (data != "success" && data != "failed") 1.120 + return; 1.121 + 1.122 + let extra = getPropertyBagValue(subj.QueryInterface(Ci.nsIPropertyBag), 1.123 + "extra"); 1.124 + ok(extra instanceof Ci.nsIPropertyBag, "Extra data should be property bag"); 1.125 + 1.126 + let val = getPropertyBagValue(extra, "PluginUserComment"); 1.127 + if (currentRun.comment) 1.128 + is(val, currentRun.comment, 1.129 + "Comment in extra data should match comment in textbox"); 1.130 + else 1.131 + ok(val === undefined, 1.132 + "Comment should be absent from extra data when textbox is empty"); 1.133 + 1.134 + val = getPropertyBagValue(extra, "PluginContentURL"); 1.135 + if (currentRun.urlOptIn) 1.136 + is(val, gBrowser.currentURI.spec, 1.137 + "URL in extra data should match browser URL when opt-in checked"); 1.138 + else 1.139 + ok(val === undefined, 1.140 + "URL should be absent from extra data when opt-in not checked"); 1.141 + } 1.142 + catch (err) { 1.143 + failWithException(err); 1.144 + } 1.145 + doNextRun(); 1.146 +} 1.147 + 1.148 +function getPropertyBagValue(bag, key) { 1.149 + try { 1.150 + var val = bag.getProperty(key); 1.151 + } 1.152 + catch (e if e.result == Cr.NS_ERROR_FAILURE) {} 1.153 + return val; 1.154 +} 1.155 + 1.156 +function failWithException(err) { 1.157 + ok(false, "Uncaught exception: " + err + "\n" + err.stack); 1.158 +}