File indexing completed on 2024-05-19 05:42:29

0001 // ct_lvttst_fixture_qt.cpp                                                -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <ct_lvttst_fixture_qt.h>
0021 #include <ct_lvttst_testcfgoptions.h>
0022 
0023 void testMessageHandler(QtMsgType msgType, const QMessageLogContext& context, const QString& message)
0024 {
0025     // clang-tidy cert-err33-c requires us to check the return value of printf
0026     auto checkRet = [](int ret) {
0027         assert(ret > 0);
0028         (void) ret;
0029     };
0030 
0031     QByteArray localMsg = message.toLocal8Bit();
0032     switch (msgType) {
0033     case QtDebugMsg:
0034         checkRet(fprintf(stderr, "Debug: %s\n", localMsg.constData()));
0035         break;
0036     case QtInfoMsg:
0037         checkRet(fprintf(stderr, "Info: %s\n", localMsg.constData()));
0038         break;
0039     case QtWarningMsg:
0040         checkRet(fprintf(stderr, "Warning: %s\n", localMsg.constData()));
0041         break;
0042     case QtCriticalMsg:
0043         checkRet(fprintf(stderr, "Critical: %s\n", localMsg.constData()));
0044         break;
0045     case QtFatalMsg:
0046         checkRet(fprintf(stderr, "Fatal: %s\n", localMsg.constData()));
0047         abort();
0048     }
0049 }
0050 
0051 QTApplicationFixture::QTApplicationFixture(): qapp(TestCfgOptions::instance().argc, TestCfgOptions::instance().argv)
0052 {
0053     qInstallMessageHandler(testMessageHandler);
0054 }
0055 
0056 void QTApplicationFixture::processEvents(int n)
0057 {
0058     while (n--) {
0059         QApplication::processEvents(QEventLoop::AllEvents);
0060     }
0061 }
0062 
0063 void QTApplicationFixture::interact()
0064 {
0065     // Only meant to be used for debug. Creates a point of interaction with GUI
0066     QApplication::exec();
0067 }