From 0dc31e667fc66ed98e855a0553958555e2fab2d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Fri, 31 Jan 2020 18:59:19 +0100 Subject: [PATCH] testlib: Simplify macOS setup/teardown code Change-Id: If92845ccb044becee136c7791fdb3cfe3641b28d Reviewed-by: Simon Hausmann --- src/testlib/qtestcase.cpp | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 2f62d51c583..76a1b36a617 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -302,8 +302,7 @@ static QObject *currentTestObject = nullptr; static QString mainSourcePath; #if defined(Q_OS_MACOS) -bool macNeedsActivate = false; -IOPMAssertionID powerID; +static IOPMAssertionID macPowerSavingDisabled = 0; #endif class TestMethods { @@ -1854,23 +1853,17 @@ void QTest::qInit(QObject *testObject, int argc, char **argv) initEnvironment(); QBenchmarkGlobalData::current = new QBenchmarkGlobalData; -#if defined(Q_OS_MACX) - macNeedsActivate = qApp && (qstrcmp(qApp->metaObject()->className(), "QApplication") == 0); - - // Don't restore saved window state for auto tests. +#if defined(Q_OS_MACOS) + // Don't restore saved window state for auto tests QTestPrivate::disableWindowRestore(); - // Disable App Nap which may cause tests to stall. + // Disable App Nap which may cause tests to stall QTestPrivate::AppNapDisabler appNapDisabler; -#endif -#if defined(Q_OS_MACX) - if (macNeedsActivate) { - CFStringRef reasonForActivity= CFSTR("No Display Sleep"); - IOReturn ok = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, reasonForActivity, &powerID); - - if (ok != kIOReturnSuccess) - macNeedsActivate = false; // no need to release the assertion on exit. + if (qApp && (qstrcmp(qApp->metaObject()->className(), "QApplication") == 0)) { + IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, + kIOPMAssertionLevelOn, CFSTR("QtTest running tests"), + &macPowerSavingDisabled); } #endif @@ -1966,11 +1959,10 @@ int QTest::qRun() QTestLog::stopLogging(); -#if defined(Q_OS_MACX) - if (macNeedsActivate) { - IOPMAssertionRelease(powerID); - } +#if defined(Q_OS_MACOS) + IOPMAssertionRelease(macPowerSavingDisabled); #endif + currentTestObject = nullptr; // Re-throw exception to make debugging easier @@ -2003,8 +1995,7 @@ void QTest::qCleanup() QSignalDumper::endDump(); #if defined(Q_OS_MACOS) - if (macNeedsActivate) - IOPMAssertionRelease(powerID); + IOPMAssertionRelease(macPowerSavingDisabled); #endif }