Skip to content

Commit 952a7fd

Browse files
authored
Merge pull request #1042 from NativeScript/trifonov/handle-SIGABRT
handle SIGABRT throwing NativeScriptException
2 parents 7ad56a6 + e3ad2a9 commit 952a7fd

File tree

4 files changed

+120
-78
lines changed

4 files changed

+120
-78
lines changed

test-app/app/src/main/assets/app/tests/exceptionHandlingTests.js

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
describe("Tests exception handling ", function () {
2-
2+
33
var myCustomEquality = function(first, second) {
44
return first == second;
55
};
6-
6+
77
beforeEach(function() {
88
jasmine.addCustomEqualityTester(myCustomEquality);
99
});
10-
10+
1111
it("TestThrowJSExceptionThroughJavaAndCatchInJS", function () {
12-
12+
1313
__log("TEST: TestThrowJSExceptionThroughJavaAndCatchInJS");
14-
14+
1515
var exceptionThrown = false;
1616
var exceptionCaught = false;
1717
var sameExObject = false;
18-
18+
1919
var ex = { myProp: "SomeValue" };
20-
20+
2121
var EH = com.tns.tests.ExceptionHandlingTest.extend("ExceptionHandlingTest", {
2222
onEvent1: function(s, n) {
2323
if (n === 0) {
@@ -28,9 +28,9 @@ describe("Tests exception handling ", function () {
2828
}
2929
}
3030
});
31-
31+
3232
var eh = new EH();
33-
33+
3434
try
3535
{
3636
eh.triggerEvent1("test", 5);
@@ -46,18 +46,18 @@ describe("Tests exception handling ", function () {
4646
expect(exceptionCaught).toBe(true);
4747
expect(sameExObject).toBe(true);
4848
});
49-
49+
5050
it("TestThrowJavaExceptionFromJsThroughJavaAndCatchInJS", function () {
5151

5252
__log("TEST: TestThrowJavaExceptionFromJsThroughJavaAndCatchInJS");
53-
53+
5454
var exceptionThrown = false;
5555
var exceptionCaught = false;
5656
var nativeExceptionFound = false;
5757
var exMsg = "";
58-
58+
5959
var ex = new java.lang.Exception("This exception is thrown from JavaScript!");
60-
60+
6161
var EH = com.tns.tests.ExceptionHandlingTest.extend("ExceptionHandlingTest53", {
6262
onEvent1: function(s, n) {
6363
if (n === 0) {
@@ -68,9 +68,9 @@ describe("Tests exception handling ", function () {
6868
}
6969
}
7070
});
71-
71+
7272
var eh = new EH();
73-
73+
7474
try
7575
{
7676
eh.triggerEvent1("test", 5);
@@ -90,14 +90,14 @@ describe("Tests exception handling ", function () {
9090
expect(nativeExceptionFound).toBe(true);
9191
expect(exMsg.indexOf("onEvent1")).not.toEqual(-1);
9292
});
93-
93+
9494
it("TestThrowJSExceptionAndCatchInJava", function () {
95-
95+
9696
__log("TEST: TestThrowJSExceptionAndCatchInJava");
97-
97+
9898
var exceptionThrown = false;
9999
var exceptionCaught = true;
100-
100+
101101
var EH = com.tns.tests.ExceptionHandlingTest.extend("ExceptionHandlingTest89", {
102102
onEvent1: function(s, n) {
103103
if (n === 0) {
@@ -108,9 +108,9 @@ describe("Tests exception handling ", function () {
108108
}
109109
}
110110
});
111-
111+
112112
var eh = new EH();
113-
113+
114114
try
115115
{
116116
eh.triggerEvent1WithCatchClause("test", 5);
@@ -124,16 +124,16 @@ describe("Tests exception handling ", function () {
124124
expect(exceptionThrown).toBe(true);
125125
expect(exceptionCaught).toBe(false);
126126
});
127-
127+
128128
it("TestThrowJavaExceptionFromJsAndCatchInJava", function () {
129-
129+
130130
__log("TEST: TestThrowJavaExceptionFromJsAndCatchInJava");
131-
131+
132132
var exceptionThrown = false;
133133
var exceptionCaught = true;
134-
134+
135135
var ex = new java.lang.Exception("This exception is thrown from JavaScript!");
136-
136+
137137
var EH = com.tns.tests.ExceptionHandlingTest.extend("ExceptionHandlingTest121", {
138138
onEvent1: function(s, n) {
139139
if (n === 0) {
@@ -144,9 +144,9 @@ describe("Tests exception handling ", function () {
144144
}
145145
}
146146
});
147-
147+
148148
var eh = new EH();
149-
149+
150150
try
151151
{
152152
eh.triggerEvent1WithCatchClause("test", 5);
@@ -160,15 +160,15 @@ describe("Tests exception handling ", function () {
160160
expect(exceptionThrown).toBe(true);
161161
expect(exceptionCaught).toBe(false);
162162
});
163-
163+
164164
it("TestMethodThatThrowsException", function () {
165-
165+
166166
__log("TEST: TestMethodThatThrowsException");
167-
167+
168168
var exceptionCaught = false;
169-
169+
170170
var dummy = new com.tns.tests.DummyClass();
171-
171+
172172
try
173173
{
174174
dummy.methodThatThrowsException();
@@ -177,19 +177,19 @@ describe("Tests exception handling ", function () {
177177
{
178178
exceptionCaught = true;
179179
}
180-
180+
181181
expect(exceptionCaught).toBe(true);
182182
});
183-
183+
184184
it("TestErrorObjectContainsJavaNativeException", function () {
185-
186-
185+
186+
187187
__log("TEST: TestErrorObjectContainsJavaNativeException");
188-
188+
189189
var nativeException = undefined;
190-
190+
191191
var dummy = new com.tns.tests.DummyClass();
192-
192+
193193
try
194194
{
195195
dummy.methodThatThrowsException();
@@ -200,20 +200,20 @@ describe("Tests exception handling ", function () {
200200
}
201201

202202
__log("nativeException=" + nativeException);
203-
203+
204204
expect(nativeException).not.toEqual(undefined);
205-
205+
206206
var nativeExceptionFrameCount = nativeException.getStackTrace().length;
207-
207+
208208
expect(nativeExceptionFrameCount).toBeGreaterThan(0);
209209
});
210-
210+
211211
it("TestConstructorThatThrowsException", function () {
212-
212+
213213
__log("TEST: TestConstructorThatThrowsException");
214-
214+
215215
var exceptionCaught = false;
216-
216+
217217
try
218218
{
219219
var dummy = new com.tns.tests.DummyClass(true /* throwsException */);
@@ -222,54 +222,54 @@ describe("Tests exception handling ", function () {
222222
{
223223
exceptionCaught = true;
224224
}
225-
225+
226226
expect(exceptionCaught).toBe(true);
227227
});
228-
228+
229229
it("TestArrayElementGetAccessThatThrowsException", function () {
230-
230+
231231
__log("TEST: TestArrayElementGetAccessThatThrowsException");
232-
232+
233233
var exceptionCaught = false;
234234

235235
var d = new com.tns.tests.DummyClass();
236-
236+
237237
var arr = d.getDummyClassArrayAsObject();
238-
238+
239239
var arrLength = arr.length;
240-
240+
241241
expect(arrLength).toEqual(1);
242-
242+
243243
try
244244
{
245245
var dummy = arr[arrLength];
246-
246+
247247
var name = dummy.getName();
248248
}
249249
catch (e)
250250
{
251251
exceptionCaught = true;
252252
}
253-
253+
254254
expect(exceptionCaught).toBe(true);
255255
});
256-
256+
257257
it("TestArrayElementSetAccessThatThrowsException", function () {
258-
258+
259259
__log("TEST: TestArrayElementSetAccessThatThrowsException");
260-
260+
261261
var exceptionCaught = false;
262262

263263
var d = new com.tns.tests.DummyClass();
264-
264+
265265
var arr = d.getDummyClassArrayAsObject();
266-
266+
267267
var arrLength = arr.length;
268-
268+
269269
expect(arrLength).toEqual(1);
270-
270+
271271
var last = arr[arrLength - 1];
272-
272+
273273
try
274274
{
275275
arr[arrLength] = last;
@@ -278,15 +278,15 @@ describe("Tests exception handling ", function () {
278278
{
279279
exceptionCaught = true;
280280
}
281-
281+
282282
expect(exceptionCaught).toBe(true);
283283
});
284-
284+
285285
it("should not wrap the thrown exception into NativeScriptException", function () {
286286
var MyTest1 = com.tns.tests.ExceptionHandlingTest.extend({
287287
onGetFile: function(s, n) {
288288
if (n === 0) {
289-
new java.io.File("/blah/blah").createNewFile();
289+
new java.io.File("/blah/blah").createNewFile();
290290
} else {
291291
this.getExceptionRec(s, n-1)
292292
}
@@ -299,7 +299,7 @@ describe("Tests exception handling ", function () {
299299
var MyTest2 = com.tns.tests.ExceptionHandlingTest.extend({
300300
onGetFile: function(s, n) {
301301
if (n === 0) {
302-
throw new java.io.IOException(s);
302+
throw new java.io.IOException(s);
303303
} else {
304304
this.getExceptionRec(s, n-1)
305305
}
@@ -337,8 +337,20 @@ describe("Tests exception handling ", function () {
337337
expect(errMsg).toContain("Cannot compile /data/data/com.tns.testapplication/files/app/tests/syntaxErrors.js");
338338
expect(errMsg).toContain("SyntaxError: Unexpected token =");
339339
expect(errMsg).toContain("File: \"file:///data/data/com.tns.testapplication/files/app/tests/syntaxErrors.js, line: 3, column: 10");
340+
});
340341

342+
// run this test only for API level bigger than 25 as we have handling there
343+
if(android.os.Build.VERSION.SDK_INT > 25) {
344+
it("Should handle SIGABRT and throw a NativeScript exception when incorrectly calling JNI methods", function () {
345+
let myClassInstance = new com.tns.tests.MyTestBaseClass3();
346+
// public void callMeWithAString(java.lang.String[] stringArr, Runnable arbitraryInterface)
347+
try {
348+
myClassInstance.callMeWithAString("stringVal", new java.lang.Runnable({ run: () => {} }))
349+
} catch (e) {
350+
android.util.Log.d("~~~~~", "~~~~~~~~ " + e.toString());
341351

342-
343-
});
352+
expect(e.toString()).toContain("SIGABRT");
353+
}
354+
});
355+
}
344356
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.tns.tests;
2+
3+
public class MyTestBaseClass3 {
4+
public void callMeWithAString(java.lang.String[] stringArr, Runnable arbitraryInterface) {
5+
String res = "I lied, call me with an array!";
6+
android.util.Log.d("Log", "callMeWithAString called.");
7+
}
8+
}

0 commit comments

Comments
 (0)