Skip to content

Commit b8dfe7c

Browse files
committed
test(runtime): cover the dispatch-override warning
Specs for a class's own method (no warning), an instance own property, a TypeScript class field, a method replaced on the implementation object and one added to the class prototype after extend(), a Java-created instance, and repeated calls on several instances (one warning). Each spec checks the JS function Java reached and counts the warning in logcat through a LogcatUtil helper that leaves the log in place.
1 parent 2dc604f commit b8dfe7c

3 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎test-app/app/src/main/assets/app/mainpage.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ require("./tests/testGC");
6363
require("./tests/testsMemoryManagement");
6464
require("./tests/testFieldGetSet");
6565
require("./tests/extendedClassesTests");
66+
require("./tests/testDispatchOverrideWarnings");
6667
require("./tests/extendClassNameTests");
6768
require("./tests/testJniReferenceLeak");
6869
require("./tests/testNativeModules");
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
});

‎test-app/app/src/main/java/com/tns/tests/LogcatUtil.java‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,20 @@ public static boolean logcatContainsString(String str) throws Exception {
2121

2222
return output.contains(str);
2323
}
24+
25+
// Lines this app logged under the JS tag at warning level or above that contain both
26+
// strings. Leaves the log in place, so earlier lines count too.
27+
public static int countJsWarnings(String needle, String otherNeedle) throws Exception {
28+
Process process = Runtime.getRuntime().exec(new String[]{"logcat", "-d", "-v", "raw", "-s", "JS:W"});
29+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
30+
int count = 0;
31+
String line;
32+
while ((line = bufferedReader.readLine()) != null) {
33+
if (line.contains(needle) && line.contains(otherNeedle)) {
34+
count++;
35+
}
36+
}
37+
process.waitFor();
38+
return count;
39+
}
2440
}

0 commit comments

Comments
 (0)