OLD | NEW |
(Empty) | |
| 1 <html> |
| 2 <head> |
| 3 <meta charset="utf-8"> |
| 4 </head> |
| 5 <body> |
| 6 <script type="text/javascript; version=1.8"> |
| 7 let exports = {}; |
| 8 let Cu = { |
| 9 reportError: function(e) |
| 10 { |
| 11 console.error(e); |
| 12 } |
| 13 }; |
| 14 </script> |
| 15 <script type="text/javascript; version=1.8" src="buildtools/lib/hooks.js"></sc
ript> |
| 16 <script type="text/javascript; version=1.8"> |
| 17 "use strict"; |
| 18 |
| 19 function test(testFunc) |
| 20 { |
| 21 let calls = []; |
| 22 let o = { |
| 23 "hookable": function(a, b) |
| 24 { |
| 25 calls.push(["orig", a, b]); |
| 26 return "origResult"; |
| 27 } |
| 28 }; |
| 29 |
| 30 function equals(actual, expected, message) |
| 31 { |
| 32 actual = JSON.stringify(actual); |
| 33 expected = JSON.stringify(expected); |
| 34 if (actual != expected) |
| 35 throw new Error(message + " (expected: " + expected + ", actual result
:" + actual + ")"); |
| 36 } |
| 37 |
| 38 function checkResult(args, resultExpected, callsExpected) |
| 39 { |
| 40 calls.splice(0, calls.length); |
| 41 let result = o.hookable.apply(o, args); |
| 42 |
| 43 equals(result, resultExpected, "Call result"); |
| 44 equals(calls, callsExpected, "Calls order"); |
| 45 } |
| 46 |
| 47 try |
| 48 { |
| 49 testFunc(o, calls, checkResult); |
| 50 } |
| 51 catch (e) |
| 52 { |
| 53 let pre = document.createElement("pre"); |
| 54 pre.style.backgroundColor = "#faa"; |
| 55 pre.textContent = e + "\n" + e.stack; |
| 56 document.body.appendChild(pre); |
| 57 } |
| 58 } |
| 59 |
| 60 test((o, calls, checkResult) => |
| 61 { |
| 62 checkResult([1, 2], "origResult", [["orig", 1, 2]]); |
| 63 }); |
| 64 |
| 65 // Simple hook |
| 66 test((o, calls, checkResult) => |
| 67 { |
| 68 let unhook = exports.hook(o, "hookable", function(a, b) |
| 69 { |
| 70 calls.push(["hook", a, b]); |
| 71 }); |
| 72 checkResult([3, 4], "origResult", [["hook", 3, 4], ["orig", 3, 4]]); |
| 73 |
| 74 unhook(); |
| 75 checkResult([5, 6], "origResult", [["orig", 5, 6]]); |
| 76 }); |
| 77 |
| 78 // Advanced hook: modifying parameters and adding cleanup |
| 79 test((o, calls, checkResult) => |
| 80 { |
| 81 let unhook = exports.hook(o, "hookable", function(a, b) |
| 82 { |
| 83 calls.push(["hook", a, b]); |
| 84 return [9, 10]; |
| 85 }, function() |
| 86 { |
| 87 calls.push("cleanup"); |
| 88 }); |
| 89 checkResult([7, 8], "origResult", [["hook", 7, 8], ["orig", 9, 10], "clean
up"]); |
| 90 |
| 91 unhook(); |
| 92 checkResult([11, 12], "origResult", [["orig", 11, 12]]); |
| 93 }); |
| 94 |
| 95 // Some other extension overriding the hook in a smart way |
| 96 test((o, calls, checkResult) => |
| 97 { |
| 98 let unhook = exports.hook(o, "hookable", function(a, b) |
| 99 { |
| 100 calls.push(["hook", a, b]); |
| 101 }); |
| 102 |
| 103 let origFunc = o.hookable; |
| 104 o.hookable = function(a, b) |
| 105 { |
| 106 calls.push(["override", a, b]); |
| 107 return origFunc.call(this, a, b); |
| 108 }; |
| 109 checkResult([13, 14], "origResult", [["override", 13, 14], ["hook", 13, 14
], ["orig", 13, 14]]); |
| 110 |
| 111 o.hookable = origFunc; |
| 112 unhook(); |
| 113 checkResult([15, 16], "origResult", [["orig", 15, 16]]); |
| 114 }); |
| 115 |
| 116 // Some other extension overriding the hook in a dumb way with toString() |
| 117 test((o, calls, checkResult) => |
| 118 { |
| 119 let unhook = exports.hook(o, "hookable", function(a, b) |
| 120 { |
| 121 calls.push(["hook", a, b]); |
| 122 }); |
| 123 |
| 124 let origFunc = o.hookable; |
| 125 o.hookable = eval("(" + String(origFunc).replace("{", "{calls.push(['overr
ide', a, b]);") + ")"); |
| 126 checkResult([17, 18], "origResult", [["hook", 17, 18], ["override", 17, 18
], ["orig", 17, 18]]); |
| 127 |
| 128 // This will currently fail - our hook is removed but other extension's ho
ok stays |
| 129 o.hookable = origFunc; |
| 130 unhook(); |
| 131 checkResult([19, 20], "origResult", [["orig", 19, 20]]); |
| 132 }); |
| 133 |
| 134 // Some other extension overriding the hook in a dumb way with toString() fo
r restoring as well |
| 135 test((o, calls, checkResult) => |
| 136 { |
| 137 let unhook = exports.hook(o, "hookable", function(a, b) |
| 138 { |
| 139 calls.push(["hook", a, b]); |
| 140 }); |
| 141 |
| 142 let origSource = o.hookable.toString(); |
| 143 o.hookable = eval("(" + origSource.replace("{", "{calls.push(['override',
a, b]);") + ")"); |
| 144 checkResult([21, 22], "origResult", [["hook", 21, 22], ["override", 21, 22
], ["orig", 21, 22]]); |
| 145 |
| 146 // This will currently fail - our hook is removed but other extension's ho
ok stays |
| 147 o.hookable = eval("(" + origSource + ")"); |
| 148 unhook(); |
| 149 checkResult([23, 24], "origResult", [["orig", 23, 24]]); |
| 150 }); |
| 151 |
| 152 // Some other extension overriding the hook in a dumb way with toSource() |
| 153 test((o, calls, checkResult) => |
| 154 { |
| 155 let unhook = exports.hook(o, "hookable", function(a, b) |
| 156 { |
| 157 calls.push(["hook", a, b]); |
| 158 }); |
| 159 |
| 160 let origFunc = o.hookable; |
| 161 o.hookable = eval(origFunc.toSource().replace("{", "{calls.push(['override
', a, b]);")); |
| 162 checkResult([25, 26], "origResult", [["hook", 25, 26], ["override", 25, 26
], ["orig", 25, 26]]); |
| 163 |
| 164 // This will currently fail - our hook is removed but other extension's ho
ok stays |
| 165 o.hookable = origFunc; |
| 166 unhook(); |
| 167 checkResult([27, 28], "origResult", [["orig", 27, 28]]); |
| 168 }); |
| 169 |
| 170 alert("All tests done"); |
| 171 </script> |
| 172 </body> |
| 173 </html> |
OLD | NEW |