Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: chrome/content/tests/qunit.html

Issue 8433028: added hook function to appIntegration to handle function-overwrites from other extensions (Closed)
Patch Set: Created Sept. 25, 2012, 4:30 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/content/tests/qunit.html
===================================================================
new file mode 100644
--- /dev/null
+++ b/chrome/content/tests/qunit.html
@@ -0,0 +1,208 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <link rel="stylesheet" href="qunit.css"/>
+ <script src="qunit.js"></script>
+ <script type="text/javascript">
+ (function()
+ {
+ var results = [];
+ var gBrowser = null;
+
+ var URLFixer = new (function()
+ {
+ this.hook = function(obj, name, func)
+ {
+ var orig = obj[name];
+ var origGet = obj.__lookupGetter__(name);
+ var origSet = obj.__lookupSetter__(name);
+ var dumbOverrideAttempt = false;
+
+ var newFunc = function()
+ {
+ try
+ {
+ func.apply(this, arguments);
+ }
+ catch(e)
+ {
+ Cu.reportError(e);
+ }
+ orig.apply(this, arguments);
+ }
+ newFunc.toString = function()
+ {
+ dumbOverrideAttempt = true;
+ return orig.toString();
+ }
+
+ obj.__defineGetter__(name, function()
+ {
+ dumbOverrideAttempt = false;
+ return newFunc;
+ }
+ );
+
+ obj.__defineSetter__(name, function(value)
+ {
+ if (dumbOverrideAttempt)
+ {
+ orig = value;
+ }
+ else
+ {
+ delete obj[name];
+ obj[name] = value;
+ }
+ }
+ );
+
+ return function() {
+ delete obj[name];
+ obj[name] = orig;
+ if (origGet) {
+ obj.__defineGetter__(name, origGet);
+ }
+ if (origSet) {
+ obj.__defineSetter__(name, origSet);
+ }
+ }
+ }
+ }
+ )();
+
+ module("function-overwrites",
+ {
+ setup: function()
+ {
+ gBrowser = new (function()
+ {
+ this.foo = function(value)
+ {
+ results.push(value);
+ }
+ }
+ )();
+ },
+ teardown: function()
+ {
+ results = [];
+ gBrowser = null;
+ }
+ }
+ );
+
+ test("no overwrite", function()
+ {
+ var unhook = URLFixer.hook(gBrowser, "foo", function()
+ {
+ results.push(-1);
+ }
+ );
+
+ gBrowser.foo(0);
+ deepEqual(results, [-1, 0]);
+
+ results = [];
+ unhook();
+ gBrowser.foo(0);
+ deepEqual(results, [0]);
+ }
+ );
+
+ test("well implemented overwrite", function()
+ {
+ var unhook = URLFixer.hook(gBrowser, "foo", function()
+ {
+ results.push(-1);
+ }
+ );
+
+ var orig = gBrowser.foo;
+ gBrowser.foo = function()
+ {
+ results.push(1);
+ orig.apply(this, arguments);
+ }
+
+ gBrowser.foo(0);
+ deepEqual(results, [1, -1, 0]);
+
+ results = [];
+ gBrowser.foo = orig;
+ gBrowser.foo(0);
+ deepEqual(results, [-1, 0]);
+
+ results = [];
+ unhook();
+ gBrowser.foo(0);
+ deepEqual(results, [0]);
+ }
+ );
+
+ test("poorly implemented overwrite", function()
+ {
+ var unhook = URLFixer.hook(gBrowser, "foo", function()
+ {
+ results.push(-1);
+ }
+ );
+
+ var func = "results.push(1);";
+ eval("gBrowser.foo = " + gBrowser.foo.toString().replace("{", "{" + func));
+
+ gBrowser.foo(0);
+ deepEqual(results, [-1, 1, 0]);
+
+ results = [];
+ eval("gBrowser.foo = "+gBrowser.foo.toString().replace(func, ""));
+ gBrowser.foo(0);
+ deepEqual(results, [-1, 0]);
+
+ results = [];
+ unhook();
+ gBrowser.foo(0);
+ deepEqual(results, [0]);
+ }
+ );
+
+ test("best implemented overwrite", function()
+ {
+ var unhook = URLFixer.hook(gBrowser, "foo", function()
+ {
+ results.push(-1);
+ }
+ );
+
+ var unhook2 = URLFixer.hook(gBrowser, "foo", function()
+ {
+ results.push(1);
+ }
+ );
+ gBrowser.foo(0);
+ deepEqual(results, [1, -1, 0]);
+
+ results = [];
+ unhook2();
+ gBrowser.foo(0);
+ deepEqual(results, [-1, 0]);
+
+ results = [];
+ unhook();
+ gBrowser.foo(0);
+ deepEqual(results, [0]);
+ }
+ );
+
+ }
+ )();
+ </script>
+ </head>
+ <body>
+ <h1 id="qunit-header">URL Fixer unit tests</h1>
+ <h2 id="qunit-banner"></h2>
+ <div id="qunit-testrunner-toolbar"></div>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ </body>
+</html>

Powered by Google App Engine
This is Rietveld