summaryrefslogtreecommitdiffstats
path: root/tests/auto/integration/qwindowcapturebackend/fixture.h
blob: 2f72c3468c93cd6bcb27425d136494925aab6be2 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#ifndef WINDOW_CAPTURE_FIXTURE_H
#define WINDOW_CAPTURE_FIXTURE_H

#include "grabber.h"
#include "widget.h"

#include <chrono>
#include <qmediacapturesession.h>
#include <qmediarecorder.h>
#include <qobject.h>
#include <qsignalspy.h>
#include <qtest.h>
#include <qvideoframe.h>
#include <qwindowcapture.h>
#include <qprocess.h>

QT_USE_NAMESPACE

constexpr inline std::chrono::milliseconds s_testTimeout = std::chrono::seconds(60);

/*!
    Utility used to hide application cursor for image comparison tests.
    On Windows, the mouse cursor is captured as part of the window capture.
    This and can cause differences when comparing captured images with images
    from QWindow::grab() which is used as a reference.
*/
struct DisableCursor final
{
    DisableCursor();
    ~DisableCursor();

    DisableCursor(const DisableCursor &) = delete;
    DisableCursor &operator=(const DisableCursor &) = delete;
};

/*!
    Fixture class that orchestrates setup/teardown of window capturing
*/
class WindowCaptureFixture : public QObject
{
    Q_OBJECT

public:
    WindowCaptureFixture();

    /*!
        Compare two images, ignoring format.
        If images differ, diagnostic output is logged and images are saved to file.
    */
    static bool compareImages(QImage actual, const QImage &expected,
                              const QString &fileSuffix = "");

    QMediaCaptureSession m_session;
    QWindowCapture m_capture;
    FrameGrabber m_grabber;

    QSignalSpy m_errors{ &m_capture, &QWindowCapture::errorOccurred };
    QSignalSpy m_activations{ &m_capture, &QWindowCapture::activeChanged };

private:
    /*!
        Calculate a result path based upon a single filename.
        On CI, the file will be located in COIN_CTEST_RESULTSDIR, and on developer
        computers, the file will be located in TEMP.

        The file name is on the form "testCase_testFunction_[dataTag_]fileName"
    */
    static QString getResultsPath(const QString &fileName);
};

/*!
    Fixture class that extends window capture fixture with a capturable widget
*/
class WindowCaptureWithWidgetFixture : public WindowCaptureFixture
{
    Q_OBJECT

public:
    /*!
        Starts capturing and returns true if successful.

        Two phase initialization is used to be able to detect
        failure to find widget window as a capturable window.
    */
    bool start(QSize size = { 60, 40 });

    /*!
        Waits until the a captured frame is received and returns it
    */
    QVideoFrame waitForFrame(qint64 noOlderThanTime = 0);

    DisableCursor m_cursorDisabled; // Avoid mouse cursor causing image differences
    TestWidget m_widget;
    QCapturableWindow m_captureWindow;

protected:
    static QCapturableWindow findCaptureWindow(const QString &windowTitle);
};

class WindowCaptureWithWidgetInOtherProcessFixture : public WindowCaptureWithWidgetFixture
{
    Q_OBJECT

public:
    ~WindowCaptureWithWidgetInOtherProcessFixture() { m_windowProcess.close(); }

    /*!
        Create widget in separate process and start capturing its content
    */
    bool start();

    QProcess m_windowProcess;
};

class WindowCaptureWithWidgetAndRecorderFixture : public WindowCaptureWithWidgetFixture
{
    Q_OBJECT

public:
    void start(QSize size = { 60, 40 }, bool togglePattern = true);

    /*!
        Stop recording.

        Since recorder finalizes the file asynchronously, even after destructors are called,
        we need to explicitly wait for the stopped state before ending the test. If we don't
        do this, the media file can not be deleted by the QTemporaryDir at destruction.
    */
    bool stop();

    bool testVideoFilePlayback(const QString& fileName);

public slots:
    void recorderStateChanged(QMediaRecorder::RecorderState state);

public:
    QTemporaryDir m_tempDir;
    const QString m_mediaFile = m_tempDir.filePath("test.mp4");
    QMediaRecorder m_recorder;
    QMediaRecorder::RecorderState m_recorderState = QMediaRecorder::StoppedState;
    QSignalSpy m_recorderErrors{ &m_recorder, &QMediaRecorder::errorOccurred };
};

#endif