Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 83 additions & 71 deletions test-app/app/src/main/assets/app/tests/exceptionHandlingTests.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
describe("Tests exception handling ", function () {

var myCustomEquality = function(first, second) {
return first == second;
};

beforeEach(function() {
jasmine.addCustomEqualityTester(myCustomEquality);
});

it("TestThrowJSExceptionThroughJavaAndCatchInJS", function () {

__log("TEST: TestThrowJSExceptionThroughJavaAndCatchInJS");

var exceptionThrown = false;
var exceptionCaught = false;
var sameExObject = false;

var ex = { myProp: "SomeValue" };

var EH = com.tns.tests.ExceptionHandlingTest.extend("ExceptionHandlingTest", {
onEvent1: function(s, n) {
if (n === 0) {
Expand All @@ -28,9 +28,9 @@ describe("Tests exception handling ", function () {
}
}
});

var eh = new EH();

try
{
eh.triggerEvent1("test", 5);
Expand All @@ -46,18 +46,18 @@ describe("Tests exception handling ", function () {
expect(exceptionCaught).toBe(true);
expect(sameExObject).toBe(true);
});

it("TestThrowJavaExceptionFromJsThroughJavaAndCatchInJS", function () {

__log("TEST: TestThrowJavaExceptionFromJsThroughJavaAndCatchInJS");

var exceptionThrown = false;
var exceptionCaught = false;
var nativeExceptionFound = false;
var exMsg = "";

var ex = new java.lang.Exception("This exception is thrown from JavaScript!");

var EH = com.tns.tests.ExceptionHandlingTest.extend("ExceptionHandlingTest53", {
onEvent1: function(s, n) {
if (n === 0) {
Expand All @@ -68,9 +68,9 @@ describe("Tests exception handling ", function () {
}
}
});

var eh = new EH();

try
{
eh.triggerEvent1("test", 5);
Expand All @@ -90,14 +90,14 @@ describe("Tests exception handling ", function () {
expect(nativeExceptionFound).toBe(true);
expect(exMsg.indexOf("onEvent1")).not.toEqual(-1);
});

it("TestThrowJSExceptionAndCatchInJava", function () {

__log("TEST: TestThrowJSExceptionAndCatchInJava");

var exceptionThrown = false;
var exceptionCaught = true;

var EH = com.tns.tests.ExceptionHandlingTest.extend("ExceptionHandlingTest89", {
onEvent1: function(s, n) {
if (n === 0) {
Expand All @@ -108,9 +108,9 @@ describe("Tests exception handling ", function () {
}
}
});

var eh = new EH();

try
{
eh.triggerEvent1WithCatchClause("test", 5);
Expand All @@ -124,16 +124,16 @@ describe("Tests exception handling ", function () {
expect(exceptionThrown).toBe(true);
expect(exceptionCaught).toBe(false);
});

it("TestThrowJavaExceptionFromJsAndCatchInJava", function () {

__log("TEST: TestThrowJavaExceptionFromJsAndCatchInJava");

var exceptionThrown = false;
var exceptionCaught = true;

var ex = new java.lang.Exception("This exception is thrown from JavaScript!");

var EH = com.tns.tests.ExceptionHandlingTest.extend("ExceptionHandlingTest121", {
onEvent1: function(s, n) {
if (n === 0) {
Expand All @@ -144,9 +144,9 @@ describe("Tests exception handling ", function () {
}
}
});

var eh = new EH();

try
{
eh.triggerEvent1WithCatchClause("test", 5);
Expand All @@ -160,15 +160,15 @@ describe("Tests exception handling ", function () {
expect(exceptionThrown).toBe(true);
expect(exceptionCaught).toBe(false);
});

it("TestMethodThatThrowsException", function () {

__log("TEST: TestMethodThatThrowsException");

var exceptionCaught = false;

var dummy = new com.tns.tests.DummyClass();

try
{
dummy.methodThatThrowsException();
Expand All @@ -177,19 +177,19 @@ describe("Tests exception handling ", function () {
{
exceptionCaught = true;
}

expect(exceptionCaught).toBe(true);
});

it("TestErrorObjectContainsJavaNativeException", function () {


__log("TEST: TestErrorObjectContainsJavaNativeException");

var nativeException = undefined;

var dummy = new com.tns.tests.DummyClass();

try
{
dummy.methodThatThrowsException();
Expand All @@ -200,20 +200,20 @@ describe("Tests exception handling ", function () {
}

__log("nativeException=" + nativeException);

expect(nativeException).not.toEqual(undefined);

var nativeExceptionFrameCount = nativeException.getStackTrace().length;

expect(nativeExceptionFrameCount).toBeGreaterThan(0);
});

it("TestConstructorThatThrowsException", function () {

__log("TEST: TestConstructorThatThrowsException");

var exceptionCaught = false;

try
{
var dummy = new com.tns.tests.DummyClass(true /* throwsException */);
Expand All @@ -222,54 +222,54 @@ describe("Tests exception handling ", function () {
{
exceptionCaught = true;
}

expect(exceptionCaught).toBe(true);
});

it("TestArrayElementGetAccessThatThrowsException", function () {

__log("TEST: TestArrayElementGetAccessThatThrowsException");

var exceptionCaught = false;

var d = new com.tns.tests.DummyClass();

var arr = d.getDummyClassArrayAsObject();

var arrLength = arr.length;

expect(arrLength).toEqual(1);

try
{
var dummy = arr[arrLength];

var name = dummy.getName();
}
catch (e)
{
exceptionCaught = true;
}

expect(exceptionCaught).toBe(true);
});

it("TestArrayElementSetAccessThatThrowsException", function () {

__log("TEST: TestArrayElementSetAccessThatThrowsException");

var exceptionCaught = false;

var d = new com.tns.tests.DummyClass();

var arr = d.getDummyClassArrayAsObject();

var arrLength = arr.length;

expect(arrLength).toEqual(1);

var last = arr[arrLength - 1];

try
{
arr[arrLength] = last;
Expand All @@ -278,15 +278,15 @@ describe("Tests exception handling ", function () {
{
exceptionCaught = true;
}

expect(exceptionCaught).toBe(true);
});

it("should not wrap the thrown exception into NativeScriptException", function () {
var MyTest1 = com.tns.tests.ExceptionHandlingTest.extend({
onGetFile: function(s, n) {
if (n === 0) {
new java.io.File("/blah/blah").createNewFile();
new java.io.File("/blah/blah").createNewFile();
} else {
this.getExceptionRec(s, n-1)
}
Expand All @@ -299,7 +299,7 @@ describe("Tests exception handling ", function () {
var MyTest2 = com.tns.tests.ExceptionHandlingTest.extend({
onGetFile: function(s, n) {
if (n === 0) {
throw new java.io.IOException(s);
throw new java.io.IOException(s);
} else {
this.getExceptionRec(s, n-1)
}
Expand Down Expand Up @@ -337,8 +337,20 @@ describe("Tests exception handling ", function () {
expect(errMsg).toContain("Cannot compile /data/data/com.tns.testapplication/files/app/tests/syntaxErrors.js");
expect(errMsg).toContain("SyntaxError: Unexpected token =");
expect(errMsg).toContain("File: \"file:///data/data/com.tns.testapplication/files/app/tests/syntaxErrors.js, line: 3, column: 10");
});

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


});
expect(e.toString()).toContain("SIGABRT");
}
});
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.tns.tests;

public class MyTestBaseClass3 {
public void callMeWithAString(java.lang.String[] stringArr, Runnable arbitraryInterface) {
String res = "I lied, call me with an array!";
android.util.Log.d("Log", "callMeWithAString called.");
}
}
Loading