Files
NativeScript/tests/app/utils/utils-tests.ts
Martin Bektchiev 8b4a9b3c6b feat: Provide API to release the native object wrapped by a JS one (#6873)
* Add `releaseNativeObject` function in `utils`
* Add tests
* Add typings for the global `__releaseNativeCounterpart` functions
provided by Android and iOS runtimes

refs https://github.com/NativeScript/ios-runtime/issues/1062 and https://github.com/NativeScript/android-runtime/issues/1254
2019-02-08 10:42:51 +02:00

45 lines
1.6 KiB
TypeScript

import * as TKUnit from "../TKUnit";
import * as utils from "tns-core-modules/utils/utils";
import { isIOS } from "tns-core-modules/platform";
export function test_GC_isDefined() {
TKUnit.assertNotEqual(utils.GC, undefined, "Method utils.GC() should be defined!");
};
export function test_releaseNativeObject_isDefined() {
TKUnit.assertNotEqual(utils.releaseNativeObject, undefined, "Method utils.releaseNativeObject() should be defined!");
};
export function test_releaseNativeObject_canBeCalledWithNativeObject() {
if (isIOS) {
test_releaseNativeObject_canBeCalledWithNativeObject_iOS();
} else {
test_releaseNativeObject_canBeCalledWithNativeObject_Android();
}
};
function test_releaseNativeObject_canBeCalledWithNativeObject_iOS() {
let deallocated = false;
const obj = new ((<any>NSObject).extend({
dealloc: function () {
deallocated = true;
}
}));
TKUnit.assertMatches(obj.description, /NSObject/, "Object description should match!")
utils.releaseNativeObject(obj);
// Need to sleep to make the delayed release get executed
NSThread.sleepForTimeInterval(0);
TKUnit.assertTrue(deallocated, "NativeObject must have been deallocated!");
}
function test_releaseNativeObject_canBeCalledWithNativeObject_Android() {
const obj = new java.lang.Object();
TKUnit.assertMatches(obj.toString(), /java.lang.Object/, "Object description should match!")
utils.releaseNativeObject(obj);
TKUnit.assertThrowsRegExp(obj.toString.bind(obj), "Should throw an error!", /Failed calling toString on a java\/lang\/Object instance/);
}