|
| 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 spec extends |
| 5 | +// 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("warns once however often and on however many instances the call repeats", function () { |
| 107 | + var ownRuns = 0; |
| 108 | + var C = java.lang.Object.extend("DispatchWarnRepeated", { |
| 109 | + interfaces: [java.lang.Runnable], |
| 110 | + run: function () {} |
| 111 | + }); |
| 112 | + for (var i = 0; i < 3; i++) { |
| 113 | + var instance = new C(); |
| 114 | + instance.run = function () { ownRuns++; }; |
| 115 | + runFromJava(instance); |
| 116 | + runFromJava(instance); |
| 117 | + } |
| 118 | + |
| 119 | + expect(ownRuns).toBe(6); |
| 120 | + expect(warnings("DispatchWarnRepeated")).toBe(1); |
| 121 | + }); |
| 122 | + |
| 123 | + it("calls an instance's own property on an instance Java created and warns", function () { |
| 124 | + var ownRuns = 0; |
| 125 | + var C = java.lang.Object.extend("DispatchWarnJavaCreated", { |
| 126 | + interfaces: [java.lang.Runnable], |
| 127 | + run: function () {} |
| 128 | + }); |
| 129 | + var instance = com.tns.tests.JavaCreated.newInstance(new C().getClass()); |
| 130 | + instance.run = function () { ownRuns++; }; |
| 131 | + |
| 132 | + runFromJava(instance); |
| 133 | + |
| 134 | + expect(ownRuns).toBe(1); |
| 135 | + expect(warnings("DispatchWarnJavaCreated")).toBe(1); |
| 136 | + }); |
| 137 | +}); |
0 commit comments