QTestLib: Disable App Nap on macOS

App Nap may cause stalls or timer delays during test runs.

Change-Id: I828282d12127918439a9a2a4f7d7be6cac457b42
Task-number: QTBUG-61499
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Morten Johan Sørvig 2017-08-14 21:28:57 +02:00 committed by Simon Hausmann
parent 8b8578a155
commit 1497e5853b
3 changed files with 41 additions and 0 deletions

View File

@ -1728,6 +1728,9 @@ void QTest::qInit(QObject *testObject, int argc, char **argv)
// Don't restore saved window state for auto tests.
QTestPrivate::disableWindowRestore();
// Disable App Nap which may cause tests to stall.
QTestPrivate::AppNapDisabler appNapDisabler;
#endif
#if defined(Q_OS_MACX)

View File

@ -54,6 +54,33 @@ namespace QTestPrivate {
void disableWindowRestore() {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"ApplePersistenceIgnoreState"];
}
/*! \internal
\class AppNapDisabler
\brief Disables App Nap by registereing a bacground activity.
App Nap remains disabled as long as the AppNapDisabler instance
exists.
*/
/*! \internal
Creates an AppNapDisabler instance and starts a NSActivityBackground activity.
*/
AppNapDisabler::AppNapDisabler()
{
m_activity = [[NSProcessInfo processInfo] beginActivityWithOptions:NSActivityBackground
reason:@"Qt Auto Test"];
[m_activity retain];
}
/*! \internal
Destroys the AppNapDisabler instance and ends the NSActivityBackground activity.
*/
AppNapDisabler::~AppNapDisabler()
{
[[NSProcessInfo processInfo] endActivity:m_activity];
[m_activity release];
}
}
QT_END_NAMESPACE

View File

@ -58,6 +58,17 @@ QT_BEGIN_NAMESPACE
namespace QTestPrivate {
void disableWindowRestore();
class AppNapDisabler
{
public:
AppNapDisabler();
~AppNapDisabler();
AppNapDisabler(const AppNapDisabler&) = delete;
AppNapDisabler& operator=(const AppNapDisabler&) = delete;
private:
id m_activity;
};
}
QT_END_NAMESPACE