From f0fefa8766cb3cec38f39c31bb01860bea88dcbc Mon Sep 17 00:00:00 2001 From: BogDan Vatra Date: Thu, 4 Feb 2016 17:16:05 +0200 Subject: Helper functions needed to run Runnables on Android UI thread easily. Add two function to allow the users to easily run (a)synchronously Runnables from any thread directly to Andoroid UI thread. These functions are useful to create java controls and to access their methods, which must done on Android UI thread. Change-Id: Iec5437321e6136cc90268cc7ecf091f82fc4cdd3 Reviewed-by: Christian Stromme --- .../qandroidfunctions/tst_qandroidfunctions.cpp | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'tests') diff --git a/tests/auto/qandroidfunctions/tst_qandroidfunctions.cpp b/tests/auto/qandroidfunctions/tst_qandroidfunctions.cpp index 36c86df..f6bfc2b 100644 --- a/tests/auto/qandroidfunctions/tst_qandroidfunctions.cpp +++ b/tests/auto/qandroidfunctions/tst_qandroidfunctions.cpp @@ -35,6 +35,7 @@ class tst_QAndroidFunctions : public QObject private slots: void testAndroidSdkVersion(); void testAndroidActivity(); + void testRunOnAndroidThread(); }; void tst_QAndroidFunctions::testAndroidSdkVersion() @@ -49,6 +50,56 @@ void tst_QAndroidFunctions::testAndroidActivity() QVERIFY(activity.callMethod("isTaskRoot")); } +void tst_QAndroidFunctions::testRunOnAndroidThread() +{ + int a = 0; + + // test async operation + QtAndroid::runOnAndroidThread([&a]{ + a = 1; + }); + QTRY_COMPARE(a, 1); // wait for async op. to finish + + // test sync operation + QtAndroid::runOnAndroidThreadSync([&a]{ + a = 2; + }); + QCOMPARE(a, 2); + + // test async/async lock + QtAndroid::runOnAndroidThread([&a]{ + QtAndroid::runOnAndroidThread([&a]{ + a = 3; + }); + }); + QTRY_COMPARE(a, 3); // wait for async op. to finish + + // test async/sync lock + QtAndroid::runOnAndroidThread([&a]{ + QtAndroid::runOnAndroidThreadSync([&a]{ + a = 5; + }); + }); + QTRY_COMPARE(a, 5); + + // test sync/sync lock + QtAndroid::runOnAndroidThreadSync([&a]{ + QtAndroid::runOnAndroidThreadSync([&a]{ + a = 4; + }); + }); + QCOMPARE(a, 4); + + + // test sync/async lock + QtAndroid::runOnAndroidThreadSync([&a]{ + QtAndroid::runOnAndroidThread([&a]{ + a = 6; + }); + }); + QCOMPARE(a, 6); +} + QTEST_APPLESS_MAIN(tst_QAndroidFunctions) #include "tst_qandroidfunctions.moc" -- cgit v1.2.3