aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/unittest/eventspy.cpp
blob: 109331d7b155447fb51eac2111f6a4aef60da4f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "eventspy.h"

#include <QCoreApplication>
#include <QEvent>

using namespace std::literals::chrono_literals;

EventSpy::EventSpy(uint eventType)
    : startTime(std::chrono::steady_clock::now()),
      eventType(eventType)
{
}

bool EventSpy::waitForEvent()
{
    while (shouldRun())
        QCoreApplication::processEvents();

    return eventHappened;
}

bool EventSpy::event(QEvent *event)
{
    if (event->type() == eventType) {
        eventHappened = true;

        return true;
    }

    return false;
}

bool EventSpy::shouldRun() const
{
    return !eventHappened
        && (std::chrono::steady_clock::now() - startTime) < 1s;
}