|
| 1 | +// Java->JS calls look the method up on the instance on every call, so an own |
| 2 | +// property of the instance or a method replaced after extend() is what runs. Debug |
| 3 | +// builds keep calling it but log a warning once per class and method, since the |
| 4 | +// iOS runtime binds the class's methods at extend time instead. Every extend() spec |
| 5 | +// uses a class of its own: the warning names the class, and each is logged only once. |
| 6 | +// Interfaces are named in full so the static binding generator implements them. |
| 7 | +describe("Java->JS calls through a function other than the class's own", function () { |
| 8 | + var LogcatUtil = com.tns.tests.LogcatUtil; |
| 9 | + |
| 10 | + function runFromJava(runnable) { |
| 11 | + new java.util.concurrent.FutureTask(runnable, null).run(); |
| 12 | + } |
| 13 | + |
| 14 | + function warnings(className) { |
| 15 | + return LogcatUtil.countJsWarnings(className, 'Java called "run" on '); |
| 16 | + } |
| 17 | + |
| 18 | + it("calls the class's own method without a warning", function () { |
| 19 | + var runs = 0; |
| 20 | + var C = java.lang.Object.extend("DispatchWarnOwnMethod", { |
| 21 | + interfaces: [java.lang.Runnable], |
| 22 | + run: function () { runs++; } |
| 23 | + }); |
| 24 | + var instance = new C(); |
| 25 | + |
| 26 | + runFromJava(instance); |
| 27 | + runFromJava(new C()); |
| 28 | + |
| 29 | + expect(runs).toBe(2); |
| 30 | + expect(warnings("DispatchWarnOwnMethod")).toBe(0); |
| 31 | + }); |
| 32 | + |
| 33 | + it("calls an instance's own property and warns", function () { |
| 34 | + var classRuns = 0; |
| 35 | + var ownRuns = 0; |
| 36 | + var C = java.lang.Object.extend("DispatchWarnInstanceProperty", { |
| 37 | + interfaces: [java.lang.Runnable], |
| 38 | + run: function () { classRuns++; } |
| 39 | + }); |
| 40 | + var instance = new C(); |
| 41 | + instance.run = function () { ownRuns++; }; |
| 42 | + |
| 43 | + runFromJava(instance); |
| 44 | + |
| 45 | + expect(ownRuns).toBe(1); |
| 46 | + expect(classRuns).toBe(0); |
| 47 | + expect(warnings("DispatchWarnInstanceProperty")).toBe(1); |
| 48 | + }); |
| 49 | + |
| 50 | + it("calls a TypeScript class field and warns", function () { |
| 51 | + var fieldRuns = 0; |
| 52 | + var FieldClass = (function (_super) { |
| 53 | + __extends(DispatchWarnClassField, _super); |
| 54 | + function DispatchWarnClassField() { |
| 55 | + var _this = _super.call(this) || this; |
| 56 | + _this.run = function () { fieldRuns++; }; |
| 57 | + return _this; |
| 58 | + } |
| 59 | + DispatchWarnClassField = __decorate([ |
| 60 | + Interfaces([java.lang.Runnable]) |
| 61 | + ], DispatchWarnClassField); |
| 62 | + return DispatchWarnClassField; |
| 63 | + }(java.lang.Object)); |
| 64 | + var instance = new FieldClass(); |
| 65 | + |
| 66 | + runFromJava(instance); |
| 67 | + |
| 68 | + expect(fieldRuns).toBe(1); |
| 69 | + expect(warnings("DispatchWarnClassField")).toBe(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it("calls a method replaced after extend and warns", function () { |
| 73 | + var originalRuns = 0; |
| 74 | + var replacedRuns = 0; |
| 75 | + var implementation = { |
| 76 | + interfaces: [java.lang.Runnable], |
| 77 | + run: function () { originalRuns++; } |
| 78 | + }; |
| 79 | + var C = java.lang.Object.extend("DispatchWarnReplacedMethod", implementation); |
| 80 | + var instance = new C(); |
| 81 | + implementation.run = function () { replacedRuns++; }; |
| 82 | + |
| 83 | + runFromJava(instance); |
| 84 | + |
| 85 | + expect(replacedRuns).toBe(1); |
| 86 | + expect(originalRuns).toBe(0); |
| 87 | + expect(warnings("DispatchWarnReplacedMethod")).toBe(1); |
| 88 | + }); |
| 89 | + |
| 90 | + it("calls a method added to the class's prototype after extend and warns", function () { |
| 91 | + var originalRuns = 0; |
| 92 | + var addedRuns = 0; |
| 93 | + var C = java.lang.Object.extend("DispatchWarnPrototypeMethod", { |
| 94 | + interfaces: [java.lang.Runnable], |
| 95 | + run: function () { originalRuns++; } |
| 96 | + }); |
| 97 | + C.prototype.run = function () { addedRuns++; }; |
| 98 | + |
| 99 | + runFromJava(new C()); |
| 100 | + |
| 101 | + expect(addedRuns).toBe(1); |
| 102 | + expect(originalRuns).toBe(0); |
| 103 | + expect(warnings("DispatchWarnPrototypeMethod")).toBe(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it("calls an instance's function for a member the class defined as null and warns", function () { |
| 107 | + var ownRuns = 0; |
| 108 | + var C = java.lang.Object.extend("DispatchWarnNullMember", { |
| 109 | + interfaces: [java.lang.Runnable], |
| 110 | + run: null |
| 111 | + }); |
| 112 | + var instance = new C(); |
| 113 | + instance.run = function () { ownRuns++; }; |
| 114 | + |
| 115 | + runFromJava(instance); |
| 116 | + runFromJava(instance); |
| 117 | + |
| 118 | + expect(ownRuns).toBe(2); |
| 119 | + expect(warnings("DispatchWarnNullMember")).toBe(1); |
| 120 | + }); |
| 121 | + |
| 122 | + it("warns once however often and on however many instances the call repeats", function () { |
| 123 | + var ownRuns = 0; |
| 124 | + var C = java.lang.Object.extend("DispatchWarnRepeated", { |
| 125 | + interfaces: [java.lang.Runnable], |
| 126 | + run: function () {} |
| 127 | + }); |
| 128 | + for (var i = 0; i < 3; i++) { |
| 129 | + var instance = new C(); |
| 130 | + instance.run = function () { ownRuns++; }; |
| 131 | + runFromJava(instance); |
| 132 | + runFromJava(instance); |
| 133 | + } |
| 134 | + |
| 135 | + expect(ownRuns).toBe(6); |
| 136 | + expect(warnings("DispatchWarnRepeated")).toBe(1); |
| 137 | + }); |
| 138 | + |
| 139 | + // Every `new Runnable({...})` shares one Java class, so the spec without an override |
| 140 | + // runs first and both count only the warnings their own calls add. |
| 141 | + it("calls an interface implementation's method without a warning", function () { |
| 142 | + var runs = 0; |
| 143 | + var runnable = new java.lang.Runnable({ run: function () { runs++; } }); |
| 144 | + var className = String(runnable.getClass().getName()); |
| 145 | + var before = warnings(className); |
| 146 | + |
| 147 | + runFromJava(runnable); |
| 148 | + |
| 149 | + expect(runs).toBe(1); |
| 150 | + expect(warnings(className)).toBe(before); |
| 151 | + }); |
| 152 | + |
| 153 | + it("calls an own property of an interface implementation and warns once", function () { |
| 154 | + var implementationRuns = 0; |
| 155 | + var ownRuns = 0; |
| 156 | + var runnable = new java.lang.Runnable({ run: function () { implementationRuns++; } }); |
| 157 | + var className = String(runnable.getClass().getName()); |
| 158 | + var before = warnings(className); |
| 159 | + runnable.run = function () { ownRuns++; }; |
| 160 | + |
| 161 | + runFromJava(runnable); |
| 162 | + runFromJava(runnable); |
| 163 | + |
| 164 | + expect(ownRuns).toBe(2); |
| 165 | + expect(implementationRuns).toBe(0); |
| 166 | + expect(warnings(className)).toBe(before + 1); |
| 167 | + }); |
| 168 | + |
| 169 | + it("calls an instance's own property on an instance Java created and warns", function () { |
| 170 | + var ownRuns = 0; |
| 171 | + var C = java.lang.Object.extend("DispatchWarnJavaCreated", { |
| 172 | + interfaces: [java.lang.Runnable], |
| 173 | + run: function () {} |
| 174 | + }); |
| 175 | + var instance = com.tns.tests.JavaCreated.newInstance(new C().getClass()); |
| 176 | + instance.run = function () { ownRuns++; }; |
| 177 | + |
| 178 | + runFromJava(instance); |
| 179 | + |
| 180 | + expect(ownRuns).toBe(1); |
| 181 | + expect(warnings("DispatchWarnJavaCreated")).toBe(1); |
| 182 | + }); |
| 183 | + |
| 184 | + // The getter on Object.prototype.value is installed only around extend(): while it |
| 185 | + // is there, Object.defineProperty reads every accessor descriptor as having a value. |
| 186 | + it("runs no inherited getter while extend() captures an accessor", function () { |
| 187 | + var inheritedReads = 0; |
| 188 | + var implementation = { |
| 189 | + interfaces: [java.lang.Runnable], |
| 190 | + run: function () {} |
| 191 | + }; |
| 192 | + Object.defineProperty(implementation, "state", { |
| 193 | + get: function () { return 1; }, |
| 194 | + enumerable: true, |
| 195 | + configurable: true |
| 196 | + }); |
| 197 | + |
| 198 | + Object.defineProperty(Object.prototype, "value", { |
| 199 | + get: function () { inheritedReads++; return undefined; }, |
| 200 | + configurable: true |
| 201 | + }); |
| 202 | + try { |
| 203 | + java.lang.Object.extend("DispatchWarnAccessorCapture", implementation); |
| 204 | + } finally { |
| 205 | + delete Object.prototype.value; |
| 206 | + } |
| 207 | + |
| 208 | + expect(inheritedReads).toBe(0); |
| 209 | + }); |
| 210 | +}); |
0 commit comments