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"); |
+ }); |
+ |
+})(); |