1/****************************************************************************
2**
3** Copyright (C) 2016 Intel Corporation.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <QtGui/QGuiApplication>
30#include <QtTest/QtTest>
31#include <QtCore/QProcess>
32
33class tst_QProcess_and_GuiEventLoop : public QObject
34{
35 Q_OBJECT
36private slots:
37 void waitForAndEventLoop();
38};
39
40
41void tst_QProcess_and_GuiEventLoop::waitForAndEventLoop()
42{
43#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED)
44 QSKIP("Not supported on Android");
45#else
46
47 // based on testcase provided in QTBUG-39488
48 QByteArray msg = "Hello World";
49
50 QProcess process;
51 process.start(program: "write-read-write/write-read-write", arguments: QStringList() << msg);
52 QVERIFY(process.waitForStarted(5000));
53 QVERIFY(process.waitForReadyRead(5000));
54 QCOMPARE(process.readAll().trimmed(), msg);
55
56 // run the GUI event dispatcher once
57 QSignalSpy spy(&process, SIGNAL(readyRead()));
58 qApp->processEvents(flags: QEventLoop::AllEvents, maxtime: 100);
59
60 // we mustn't have read anything in the event loop
61 QCOMPARE(spy.count(), 0);
62
63 // ensure the process hasn't died
64 QVERIFY(!process.waitForFinished(250));
65
66 // we mustn't have read anything during waitForFinished either
67 QCOMPARE(spy.count(), 0);
68
69 // release the child for the second write
70 process.write(data: "\n");
71 QVERIFY(process.waitForFinished(5000));
72 QCOMPARE(int(process.exitStatus()), int(QProcess::NormalExit));
73 QCOMPARE(process.exitCode(), 0);
74 QCOMPARE(spy.count(), 1);
75 QCOMPARE(process.readAll().trimmed(), msg);
76#endif
77}
78
79QTEST_MAIN(tst_QProcess_and_GuiEventLoop)
80
81#include "tst_qprocess_and_guieventloop.moc"
82

source code of qtbase/tests/auto/other/qprocess_and_guieventloop/tst_qprocess_and_guieventloop.cpp