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

Unified Diff: chrome/content/tests/tests/hooks.js

Issue 8433028: added hook function to appIntegration to handle function-overwrites from other extensions (Closed)
Patch Set: applied code review changes Created Sept. 28, 2012, 8:01 a.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/tests/hooks.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/chrome/content/tests/tests/hooks.js
@@ -0,0 +1,120 @@
+(function()
+{
+ let {hook} = require("hooks");
+
+ let results = [];
+ let gBrowser = null;
+
+ module("Function hooks",
+ {
+ setup: function()
+ {
+ gBrowser = new (function()
+ {
+ this.foo = function(value)
+ {
+ results.push(value);
+ }
+ })();
+ },
+ teardown: function()
+ {
+ results = [];
+ gBrowser = null;
+ }
+ });
+
+ test("No overwrite", function()
+ {
+ let unhook = hook(gBrowser, "foo", function()
+ {
+ results.push(-1);
+ });
+
+ gBrowser.foo(0);
+ deepEqual(results, [-1, 0], "Registered our hook");
+
+ results = [];
+ unhook();
+ gBrowser.foo(0);
+ deepEqual(results, [0], "Unregistered our hook");
+ });
+
+ test("Well-implemented overwrite", function()
+ {
+ let unhook = hook(gBrowser, "foo", function()
+ {
+ results.push(-1);
+ });
+
+ let orig = gBrowser.foo;
+ gBrowser.foo = function()
+ {
+ results.push(1);
+ orig.apply(this, arguments);
+ };
+
+ gBrowser.foo(0);
+ deepEqual(results, [1, -1, 0], "Registered our hook and third-party hook");
+
+ results = [];
+ gBrowser.foo = orig;
+ gBrowser.foo(0);
+ deepEqual(results, [-1, 0], "Unregistered third-party hook");
+
+ results = [];
+ unhook();
+ gBrowser.foo(0);
+ deepEqual(results, [0], "Unregistered our hook");
+ });
+
+ test("Poorly implemented overwrite", function()
+ {
+ let unhook = hook(gBrowser, "foo", function()
+ {
+ results.push(-1);
+ });
+
+ let func = "results.push(1);";
+ eval("gBrowser.foo = " + gBrowser.foo.toString().replace("{", "{" + func));
+
+ gBrowser.foo(0);
+ deepEqual(results, [-1, 1, 0], "Registered our hook and third-party hook");
+
+ results = [];
+ eval("gBrowser.foo = "+gBrowser.foo.toString().replace(func, ""));
+ gBrowser.foo(0);
+ deepEqual(results, [-1, 0], "Unregistered third-party hook");
+
+ results = [];
+ unhook();
+ gBrowser.foo(0);
+ deepEqual(results, [0], "Unregistered our hook");
+ });
+
+ test("Best implemented overwrite", function()
+ {
+ let unhook = hook(gBrowser, "foo", function()
+ {
+ results.push(-1);
+ });
+
+ let unhook2 = hook(gBrowser, "foo", function()
+ {
+ results.push(1);
+ });
+ gBrowser.foo(0);
+ deepEqual(results, [1, -1, 0], "Registered our hook and third-party hook");
+
+ results = [];
+ unhook2();
+ gBrowser.foo(0);
+ deepEqual(results, [-1, 0], "Unregistered third-party hook");
+
+ results = [];
+ unhook();
+ gBrowser.foo(0);
+ deepEqual(results, [0], "Unregistered our hook");
+ });
+
+})();

Powered by Google App Engine
This is Rietveld