Files
NativeScript/tests/app/utils/utils-tests.ts
Martin Bektchiev c60f74d4eb fix(devtools-ios): Ensure UI modifications run on main thread
Modifications to the UI can only be made from the main thread.
Since {N} 5.3.0 all debugger protocol messages are processed
by the worker thread that receives them in iOS.

refs #7219, https://github.com/NativeScript/ios-runtime/pull/1101
2019-05-08 17:52:00 +03:00

77 lines
2.4 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();
}
};
export function test_executeOnMainThread_Works(done: Function) {
utils.executeOnMainThread(() => {
try {
TKUnit.assertTrue(utils.isMainThread());
done();
} catch (e) {
done(e);
}
});
}
export function test_mainThreadify_PassesArgs(done: Function) {
const expectedN = 434;
const expectedB = true;
const expectedS = "string";
const f = utils.mainThreadify(function (n: number, b: boolean, s: string) {
try {
TKUnit.assertTrue(utils.isMainThread());
TKUnit.assertEqual(n, expectedN);
TKUnit.assertEqual(b, expectedB);
TKUnit.assertEqual(s, expectedS);
done();
} catch (e) {
done(e);
}
});
f(expectedN, expectedB, expectedS);
}
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/);
}