OLD | NEW |
(Empty) | |
| 1 (function() |
| 2 { |
| 3 let {hook} = require("hooks"); |
| 4 |
| 5 let results = []; |
| 6 let gBrowser = null; |
| 7 |
| 8 module("Function hooks", |
| 9 { |
| 10 setup: function() |
| 11 { |
| 12 gBrowser = new (function() |
| 13 { |
| 14 this.foo = function(value) |
| 15 { |
| 16 results.push(value); |
| 17 } |
| 18 })(); |
| 19 }, |
| 20 teardown: function() |
| 21 { |
| 22 results = []; |
| 23 gBrowser = null; |
| 24 } |
| 25 }); |
| 26 |
| 27 test("No overwrite", function() |
| 28 { |
| 29 let unhook = hook(gBrowser, "foo", function() |
| 30 { |
| 31 results.push(-1); |
| 32 }); |
| 33 |
| 34 gBrowser.foo(0); |
| 35 deepEqual(results, [-1, 0], "Registered our hook"); |
| 36 |
| 37 results = []; |
| 38 unhook(); |
| 39 gBrowser.foo(0); |
| 40 deepEqual(results, [0], "Unregistered our hook"); |
| 41 }); |
| 42 |
| 43 test("Well-implemented overwrite", function() |
| 44 { |
| 45 let unhook = hook(gBrowser, "foo", function() |
| 46 { |
| 47 results.push(-1); |
| 48 }); |
| 49 |
| 50 let orig = gBrowser.foo; |
| 51 gBrowser.foo = function() |
| 52 { |
| 53 results.push(1); |
| 54 orig.apply(this, arguments); |
| 55 }; |
| 56 |
| 57 gBrowser.foo(0); |
| 58 deepEqual(results, [1, -1, 0], "Registered our hook and third-party hook"); |
| 59 |
| 60 results = []; |
| 61 gBrowser.foo = orig; |
| 62 gBrowser.foo(0); |
| 63 deepEqual(results, [-1, 0], "Unregistered third-party hook"); |
| 64 |
| 65 results = []; |
| 66 unhook(); |
| 67 gBrowser.foo(0); |
| 68 deepEqual(results, [0], "Unregistered our hook"); |
| 69 }); |
| 70 |
| 71 test("Poorly implemented overwrite", function() |
| 72 { |
| 73 let unhook = hook(gBrowser, "foo", function() |
| 74 { |
| 75 results.push(-1); |
| 76 }); |
| 77 |
| 78 let func = "results.push(1);"; |
| 79 eval("gBrowser.foo = " + gBrowser.foo.toString().replace("{", "{" + func)); |
| 80 |
| 81 gBrowser.foo(0); |
| 82 deepEqual(results, [-1, 1, 0], "Registered our hook and third-party hook"); |
| 83 |
| 84 results = []; |
| 85 eval("gBrowser.foo = "+gBrowser.foo.toString().replace(func, "")); |
| 86 gBrowser.foo(0); |
| 87 deepEqual(results, [-1, 0], "Unregistered third-party hook"); |
| 88 |
| 89 results = []; |
| 90 unhook(); |
| 91 gBrowser.foo(0); |
| 92 deepEqual(results, [0], "Unregistered our hook"); |
| 93 }); |
| 94 |
| 95 test("Best implemented overwrite", function() |
| 96 { |
| 97 let unhook = hook(gBrowser, "foo", function() |
| 98 { |
| 99 results.push(-1); |
| 100 }); |
| 101 |
| 102 let unhook2 = hook(gBrowser, "foo", function() |
| 103 { |
| 104 results.push(1); |
| 105 }); |
| 106 gBrowser.foo(0); |
| 107 deepEqual(results, [1, -1, 0], "Registered our hook and third-party hook"); |
| 108 |
| 109 results = []; |
| 110 unhook2(); |
| 111 gBrowser.foo(0); |
| 112 deepEqual(results, [-1, 0], "Unregistered third-party hook"); |
| 113 |
| 114 results = []; |
| 115 unhook(); |
| 116 gBrowser.foo(0); |
| 117 deepEqual(results, [0], "Unregistered our hook"); |
| 118 }); |
| 119 |
| 120 })(); |
OLD | NEW |