File indexing completed on 2024-04-14 14:19:41

0001 /*
0002  *  Copyright (C) 2007 David Faure   <faure@kde.org>
0003  *
0004  *  This library is free software; you can redistribute it and/or
0005  *  modify it under the terms of the GNU Library General Public
0006  *  License version 2 as published by the Free Software Foundation;
0007  *
0008  *  This library is distributed in the hope that it will be useful,
0009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011  *  Library General Public License for more details.
0012  *
0013  *  You should have received a copy of the GNU Library General Public License
0014  *  along with this library; see the file COPYING.LIB.  If not, write to
0015  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016  *  Boston, MA 02110-1301, USA.
0017  */
0018 
0019 #include <klocale.h>
0020 #include <kglobal.h>
0021 
0022 #include <QFutureSynchronizer>
0023 #include <QObject>
0024 #include <QtConcurrentRun>
0025 #include <QTimer>
0026 
0027 #include <QTest>
0028 #include <qtest_kde.h> // kWaitForSignal
0029 
0030 static QString testMethod()
0031 {
0032     return QLatin1String("application/octet-stream");
0033 }
0034 
0035 class KGlobalTest : public QObject
0036 {
0037     Q_OBJECT
0038 private Q_SLOTS:
0039     void testStaticQString()
0040     {
0041         QCOMPARE(testMethod(), QString::fromLatin1("application/octet-stream"));
0042     }
0043 
0044     void testFindDirectChild()
0045     {
0046         QTimer child(this);
0047         QCOMPARE(KGlobal::findDirectChild<QTimer *>(this), &child);
0048         QCOMPARE(KGlobal::findDirectChild<QTimer *>(&child), (QTimer *)nullptr);
0049         QCOMPARE(KGlobal::findDirectChild<QEventLoop *>(this), (QEventLoop *)nullptr);
0050     }
0051 
0052     // The former implementation of QTest::kWaitForSignal would return
0053     // false even if the signal was emitted, when the timeout fired too
0054     // (e.g. due to a breakpoint in gdb).
0055     void testWaitForSignal()
0056     {
0057         QTimer::singleShot(5, this, SLOT(emitSigFoo()));
0058         QVERIFY(QTest::kWaitForSignal(this, SIGNAL(sigFoo()), 20));
0059     }
0060 
0061     void testWaitForSignalTimeout()
0062     {
0063         QVERIFY(!QTest::kWaitForSignal(this, SIGNAL(sigFoo()), 1));
0064     }
0065 
0066     // For testing from multiple threads in testThreads
0067     void testLocale()
0068     {
0069         KLocale::global();
0070         KLocale::global()->setDecimalPlaces(2);
0071         QCOMPARE(KLocale::global()->formatNumber(70), QString("70.00"));
0072     }
0073 
0074     // Calling this directly aborts in KLocale::global(), this is intended.
0075     // We have to install the qtranslator in the main thread.
0076     void testThreads()
0077     {
0078         QThreadPool::globalInstance()->setMaxThreadCount(10);
0079         QFutureSynchronizer<void> sync;
0080         sync.addFuture(QtConcurrent::run(this, &KGlobalTest::testLocale));
0081         sync.addFuture(QtConcurrent::run(this, &KGlobalTest::testLocale));
0082         // sync dtor blocks waiting for finished
0083     }
0084 
0085 protected Q_SLOTS:
0086     void emitSigFoo()
0087     {
0088         emit sigFoo();
0089 #ifndef Q_OS_MAC
0090         // Mac currently uses the unix event loop (not glib) which
0091         // has issues and blocks in nested event loops such as the
0092         // one triggered by the below. Since this is a limitation
0093         // of Qt and working around it here doesn't invalidate the
0094         // actual test case, let's do that.
0095         QTest::qWait(10);
0096 #endif
0097     }
0098 
0099 Q_SIGNALS:
0100     void sigFoo();
0101 };
0102 
0103 QTEST_MAIN(KGlobalTest)
0104 
0105 #include "kglobaltest.moc"