Bug 612405: use a more reliable trick to detect the console API being replaced, r=ddahl, a=dolske

--HG--
extra : rebase_source : 7c7753855472f3717a3f7e2fd5ecd5d14a736f27
This commit is contained in:
Gavin Sharp 2010-11-17 17:29:42 -05:00
Родитель 2ef44d13c5
Коммит 4ced50fc06
10 изменённых файлов: 60 добавлений и 88 удалений

Просмотреть файл

@ -73,9 +73,7 @@ ConsoleAPI.prototype = {
},
error: function CA_error() {
self.notifyObservers(id, "error", arguments);
},
// TODO: remove this once bug 612405 is fixed
classID: self.classID
}
};
},

Просмотреть файл

@ -24,8 +24,7 @@ function doTest() {
"log": "function",
"info": "function",
"warn": "function",
"error": "function",
"classID": "object" // XXX Bug 612405
"error": "function"
};
var foundProps = 0;

Просмотреть файл

@ -2732,12 +2732,21 @@ HUD_SERVICE.prototype =
}
}
// need to detect that the console component has been paved over
// TODO: change how we detect our console: bug 612405
let consoleObject = aContentWindow.wrappedJSObject.console;
if (consoleObject && consoleObject.classID != CONSOLEAPI_CLASS_ID) {
// Need to detect that the console component has been paved over. Do this by
// checking whether its global object is equal to that of an object
// returned by our native ConsoleAPI nsIDOMGlobalPropertyInitializer.
let consoleObject = unwrap(aContentWindow).console;
let consoleGlobal = Cu.getGlobalForObject(consoleObject);
let nativeConsoleObj = Cc["@mozilla.org/console-api;1"].
createInstance(Ci.nsIDOMGlobalPropertyInitializer).
init(aContentWindow);
let nativeConsoleGlobal = Cu.getGlobalForObject(nativeConsoleObj);
// Need a "===" comparison because backstagepass objects have strange
// behavior with ==
if (consoleGlobal !== nativeConsoleGlobal)
this.logWarningAboutReplacedAPI(hudId);
}
// register the controller to handle "select all" properly
this.createController(xulWindow);

Просмотреть файл

@ -39,36 +39,35 @@
const TEST_REPLACED_API_URI = "http://example.com/browser/toolkit/components/console/hudservice/tests/browser/test-console-replaced-api.html";
function test()
{
addTab(TEST_REPLACED_API_URI);
function test() {
waitForExplicitFinish();
// First test that the warning does not appear on a normal page (about:blank)
addTab("about:blank");
browser.addEventListener("load", function() {
browser.removeEventListener("load", arguments.callee,
true);
testOpenWebConsole();
browser.removeEventListener("load", arguments.callee, true);
testOpenWebConsole(false);
executeSoon(testWarningPresent);
}, true);
}
function testOpenWebConsole()
{
function testWarningPresent() {
// Then test that the warning does appear on a page that replaces the API
browser.addEventListener("load", function() {
browser.removeEventListener("load", arguments.callee, true);
testOpenWebConsole(true);
finishTest();
}, true);
browser.contentWindow.location = TEST_REPLACED_API_URI;
}
function testOpenWebConsole(shouldWarn) {
openConsole();
is(HUDService.displaysIndex().length, 1, "WebConsole was opened");
hudId = HUDService.displaysIndex()[0];
hud = HUDService.getHeadsUpDisplay(hudId);
HUDService.logWarningAboutReplacedAPI(hudId);
testWarning();
}
function testWarning()
{
const successMsg = "Found the warning message";
const errMsg = "Could not find the warning message about the replaced API";
testLogEntry(hud, "disabled",
{ success: "Found disabled console error message",
err: "disable msg not found"});
finishTest();
let msg = (shouldWarn ? "found" : "didn't find") + " API replacement warning";
testLogEntry(hud, "disabled", msg, false, !shouldWarn);
}

Просмотреть файл

@ -92,16 +92,13 @@ var consoleObserver = {
Services.console.unregisterListener(this);
const successMsg = "Found the error message after page reload";
const errMsg = "Could not get the error message after page reload";
hudId = HUDService.displaysIndex()[0];
hud = HUDService.hudReferences[hudId];
outputNode = hud.outputNode;
executeSoon(function() {
testLogEntry(outputNode, "fooBazBaz",
{ success: successMsg, err: errMsg });
let msg = "Found the error message after page reload";
testLogEntry(outputNode, "fooBazBaz", msg);
finishTest();
});
}

Просмотреть файл

@ -21,11 +21,8 @@ function onContentLoaded()
let filterBox = HUD.querySelector(".hud-filter-box");
let outputNode = HUD.querySelector(".hud-output-node");
let warningFound = "the unknown CSS property warning is displayed";
let warningNotFound = "could not find the unknown CSS property warning";
testLogEntry(outputNode, "foobarCssParser",
{ success: warningFound, err: warningNotFound }, true);
let msg = "the unknown CSS property warning is displayed";
testLogEntry(outputNode, "foobarCssParser", msg, true);
HUDService.setFilterState(hudId, "cssparser", false);
let nodes = HUD.querySelectorAll(".hud-msg-node");
@ -34,13 +31,9 @@ function onContentLoaded()
function (){
HUDService.setFilterState(hudId, "cssparser", false);
warningNotFound = "the unknown CSS property warning is not displayed, " +
"after filtering";
warningFound = "the unknown CSS property warning is still displayed, " +
"after filtering";
testLogEntry(outputNode, "foobarCssParser",
{ success: warningNotFound, err: warningFound }, true, true);
let msg = "the unknown CSS property warning is not displayed, " +
"after filtering";
testLogEntry(outputNode, "foobarCssParser", msg, true, true);
}
);

Просмотреть файл

@ -94,11 +94,8 @@ function tab1Reloaded(aEvent) {
let display1 = HUDService.getOutputNodeById(hudId1);
let outputNode1 = display1.querySelector(".hud-output-node");
const successMsg1 = "Found the iframe network request in tab1";
const errorMsg1 = "Failed to find the iframe network request in tab1";
testLogEntry(outputNode1, TEST_IFRAME_URI,
{ success: successMsg1, err: errorMsg1}, true);
let msg = "Found the iframe network request in tab1";
testLogEntry(outputNode1, TEST_IFRAME_URI, msg, true);
let hudId2 = HUDService.getHudIdByWindow(tab2.linkedBrowser.contentWindow);
let display2 = HUDService.getOutputNodeById(hudId2);
@ -108,16 +105,11 @@ function tab1Reloaded(aEvent) {
isnot(outputNode1, outputNode2,
"the two HUD outputNodes must be different");
const successMsg2 = "The iframe network request is not in tab2";
const errorMsg2 = "Found the iframe network request in tab2";
testLogEntry(outputNode2, TEST_IFRAME_URI,
{ success: successMsg2, err: errorMsg2}, true, true);
msg = "Didn't find the iframe network request in tab2";
testLogEntry(outputNode2, TEST_IFRAME_URI, msg, true, true);
HUDService.deactivateHUDForContext(tab2);
gBrowser.removeTab(tab2);
tab1 = tab2 = null;
finishTest();
}

Просмотреть файл

@ -37,10 +37,8 @@ function buttonClicked() {
let hudId = HUDService.getHudIdByWindow(content);
let outputNode = HUDService.getOutputNodeById(hudId);
const successMsg = "the error from the external script was logged";
const errorMsg = "the error from the external script was not logged";
testLogEntry(outputNode, "bogus", { success: successMsg, err: errorMsg });
let msg = "the error from the external script was logged";
testLogEntry(outputNode, "bogus", msg);
finishTest();
}

Просмотреть файл

@ -89,26 +89,16 @@ function addTab(aURL)
* the HUD output node.
* @param {string} aMatchString
* the string you want to check if it exists in the output node.
* @param {string} aMsg
* the message describing the test
* @param {boolean} [aOnlyVisible=false]
* find only messages that are visible, not hidden by the filter.
* @param {boolean} [aFailIfFound=false]
* fail the test if the string is found in the output node.
*/
function testLogEntry(aOutputNode, aMatchString, aSuccessErrObj, aOnlyVisible,
function testLogEntry(aOutputNode, aMatchString, aMsg, aOnlyVisible,
aFailIfFound)
{
let found = true;
let notfound = false;
let foundMsg = aSuccessErrObj.success;
let notfoundMsg = aSuccessErrObj.err;
if (aFailIfFound) {
found = false;
notfound = true;
foundMsg = aSuccessErrObj.success;
notfoundMsg = aSuccessErrObj.err;
}
let selector = ".hud-msg-node";
// Skip entries that are hidden by the filter.
if (aOnlyVisible) {
@ -116,14 +106,15 @@ function testLogEntry(aOutputNode, aMatchString, aSuccessErrObj, aOnlyVisible,
}
let msgs = aOutputNode.querySelectorAll(selector);
let found = false;
for (let i = 0, n = msgs.length; i < n; i++) {
let message = msgs[i].textContent.indexOf(aMatchString);
if (message > -1) {
ok(found, foundMsg);
return;
found = true;
break;
}
}
ok(notfound, notfoundMsg);
is(found, !aFailIfFound, aMsg);
}
function openConsole()

Просмотреть файл

@ -1,15 +1,11 @@
<!DOCTYPE HTML>
<html dir="ltr" xml:lang="en-US" lang="en-US"><head>
<title>Console test replaced API</title>
<script type="text/javascript">
function replaceAPI() {
var console = {log: function (msg){}, info: function (msg){}, warn: function (msg){}, error: function (msg){}};
window.console = console;
}
</script>
</head>
<body>
<h1 id="header">Web Console Replace API Test</h1>
<button onclick="replaceAPI();">ReplaceAPI</button>
<script type="text/javascript">
window.console = {log: function (msg){}, info: function (msg){}, warn: function (msg){}, error: function (msg){}};
</script>
</body>
</html>