OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 4 <link rel="stylesheet" href="qunit.css"/> |
| 5 <script src="qunit.js"></script> |
| 6 <script type="text/javascript"> |
| 7 (function() |
| 8 { |
| 9 var results = []; |
| 10 var gBrowser = null; |
| 11 |
| 12 var URLFixer = new (function() |
| 13 { |
| 14 this.hook = function(obj, name, func) |
| 15 { |
| 16 var orig = obj[name]; |
| 17 var origGet = obj.__lookupGetter__(name); |
| 18 var origSet = obj.__lookupSetter__(name); |
| 19 var dumbOverrideAttempt = false; |
| 20 |
| 21 var newFunc = function() |
| 22 { |
| 23 try |
| 24 { |
| 25 func.apply(this, arguments); |
| 26 } |
| 27 catch(e) |
| 28 { |
| 29 Cu.reportError(e); |
| 30 } |
| 31 orig.apply(this, arguments); |
| 32 } |
| 33 newFunc.toString = function() |
| 34 { |
| 35 dumbOverrideAttempt = true; |
| 36 return orig.toString(); |
| 37 } |
| 38 |
| 39 obj.__defineGetter__(name, function() |
| 40 { |
| 41 dumbOverrideAttempt = false; |
| 42 return newFunc; |
| 43 } |
| 44 ); |
| 45 |
| 46 obj.__defineSetter__(name, function(value) |
| 47 { |
| 48 if (dumbOverrideAttempt) |
| 49 { |
| 50 orig = value; |
| 51 } |
| 52 else |
| 53 { |
| 54 delete obj[name]; |
| 55 obj[name] = value; |
| 56 } |
| 57 } |
| 58 ); |
| 59 |
| 60 return function() { |
| 61 delete obj[name]; |
| 62 obj[name] = orig; |
| 63 if (origGet) { |
| 64 obj.__defineGetter__(name, origGet); |
| 65 } |
| 66 if (origSet) { |
| 67 obj.__defineSetter__(name, origSet); |
| 68 } |
| 69 } |
| 70 } |
| 71 } |
| 72 )(); |
| 73 |
| 74 module("function-overwrites", |
| 75 { |
| 76 setup: function() |
| 77 { |
| 78 gBrowser = new (function() |
| 79 { |
| 80 this.foo = function(value) |
| 81 { |
| 82 results.push(value); |
| 83 } |
| 84 } |
| 85 )(); |
| 86 }, |
| 87 teardown: function() |
| 88 { |
| 89 results = []; |
| 90 gBrowser = null; |
| 91 } |
| 92 } |
| 93 ); |
| 94 |
| 95 test("no overwrite", function() |
| 96 { |
| 97 var unhook = URLFixer.hook(gBrowser, "foo", function() |
| 98 { |
| 99 results.push(-1); |
| 100 } |
| 101 ); |
| 102 |
| 103 gBrowser.foo(0); |
| 104 deepEqual(results, [-1, 0]); |
| 105 |
| 106 results = []; |
| 107 unhook(); |
| 108 gBrowser.foo(0); |
| 109 deepEqual(results, [0]); |
| 110 } |
| 111 ); |
| 112 |
| 113 test("well implemented overwrite", function() |
| 114 { |
| 115 var unhook = URLFixer.hook(gBrowser, "foo", function() |
| 116 { |
| 117 results.push(-1); |
| 118 } |
| 119 ); |
| 120 |
| 121 var orig = gBrowser.foo; |
| 122 gBrowser.foo = function() |
| 123 { |
| 124 results.push(1); |
| 125 orig.apply(this, arguments); |
| 126 } |
| 127 |
| 128 gBrowser.foo(0); |
| 129 deepEqual(results, [1, -1, 0]); |
| 130 |
| 131 results = []; |
| 132 gBrowser.foo = orig; |
| 133 gBrowser.foo(0); |
| 134 deepEqual(results, [-1, 0]); |
| 135 |
| 136 results = []; |
| 137 unhook(); |
| 138 gBrowser.foo(0); |
| 139 deepEqual(results, [0]); |
| 140 } |
| 141 ); |
| 142 |
| 143 test("poorly implemented overwrite", function() |
| 144 { |
| 145 var unhook = URLFixer.hook(gBrowser, "foo", function() |
| 146 { |
| 147 results.push(-1); |
| 148 } |
| 149 ); |
| 150 |
| 151 var func = "results.push(1);"; |
| 152 eval("gBrowser.foo = " + gBrowser.foo.toString().replace("{", "{"
+ func)); |
| 153 |
| 154 gBrowser.foo(0); |
| 155 deepEqual(results, [-1, 1, 0]); |
| 156 |
| 157 results = []; |
| 158 eval("gBrowser.foo = "+gBrowser.foo.toString().replace(func, "")); |
| 159 gBrowser.foo(0); |
| 160 deepEqual(results, [-1, 0]); |
| 161 |
| 162 results = []; |
| 163 unhook(); |
| 164 gBrowser.foo(0); |
| 165 deepEqual(results, [0]); |
| 166 } |
| 167 ); |
| 168 |
| 169 test("best implemented overwrite", function() |
| 170 { |
| 171 var unhook = URLFixer.hook(gBrowser, "foo", function() |
| 172 { |
| 173 results.push(-1); |
| 174 } |
| 175 ); |
| 176 |
| 177 var unhook2 = URLFixer.hook(gBrowser, "foo", function() |
| 178 { |
| 179 results.push(1); |
| 180 } |
| 181 ); |
| 182 gBrowser.foo(0); |
| 183 deepEqual(results, [1, -1, 0]); |
| 184 |
| 185 results = []; |
| 186 unhook2(); |
| 187 gBrowser.foo(0); |
| 188 deepEqual(results, [-1, 0]); |
| 189 |
| 190 results = []; |
| 191 unhook(); |
| 192 gBrowser.foo(0); |
| 193 deepEqual(results, [0]); |
| 194 } |
| 195 ); |
| 196 |
| 197 } |
| 198 )(); |
| 199 </script> |
| 200 </head> |
| 201 <body> |
| 202 <h1 id="qunit-header">URL Fixer unit tests</h1> |
| 203 <h2 id="qunit-banner"></h2> |
| 204 <div id="qunit-testrunner-toolbar"></div> |
| 205 <h2 id="qunit-userAgent"></h2> |
| 206 <ol id="qunit-tests"></ol> |
| 207 </body> |
| 208 </html> |
OLD | NEW |