File indexing completed on 2022-11-29 18:25:14

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 <QObject>
0023 #include <QtConcurrent>
0024 
0025 #include <QTest>
0026 #include <qtest_kde.h> // kWaitForSignal
0027 
0028 static QString testMethod()
0029 {
0030     return QLatin1String("application/octet-stream");
0031 }
0032 
0033 class KGlobalTest : public QObject
0034 {
0035     Q_OBJECT
0036 private Q_SLOTS:
0037     void testStaticQString()
0038     {
0039         QCOMPARE(testMethod(), QString::fromLatin1("application/octet-stream"));
0040     }
0041 
0042     void testFindDirectChild()
0043     {
0044         QTimer child(this);
0045         QCOMPARE(KGlobal::findDirectChild<QTimer *>(this), &child);
0046         QCOMPARE(KGlobal::findDirectChild<QTimer *>(&child), (QTimer *)nullptr);
0047         QCOMPARE(KGlobal::findDirectChild<QEventLoop *>(this), (QEventLoop *)nullptr);
0048     }
0049 
0050     // The former implementation of QTest::kWaitForSignal would return
0051     // false even if the signal was emitted, when the timeout fired too
0052     // (e.g. due to a breakpoint in gdb).
0053     void testWaitForSignal()
0054     {
0055         QTimer::singleShot(5, this, SLOT(emitSigFoo()));
0056         QVERIFY(QTest::kWaitForSignal(this, SIGNAL(sigFoo()), 20));
0057     }
0058 
0059     void testWaitForSignalTimeout()
0060     {
0061         QVERIFY(!QTest::kWaitForSignal(this, SIGNAL(sigFoo()), 1));
0062     }
0063 
0064     // For testing from multiple threads in testThreads
0065     void testLocale()
0066     {
0067         KLocale::global();
0068         KLocale::global()->setDecimalPlaces(2);
0069         QCOMPARE(KLocale::global()->formatNumber(70), QString("70.00"));
0070     }
0071 
0072     // Calling this directly aborts in KLocale::global(), this is intended.
0073     // We have to install the qtranslator in the main thread.
0074     void testThreads()
0075     {
0076         QThreadPool::globalInstance()->setMaxThreadCount(10);
0077         QFutureSynchronizer<void> sync;
0078         sync.addFuture(QtConcurrent::run(this, &KGlobalTest::testLocale));
0079         sync.addFuture(QtConcurrent::run(this, &KGlobalTest::testLocale));
0080         // sync dtor blocks waiting for finished
0081     }
0082 
0083 protected Q_SLOTS:
0084     void emitSigFoo()
0085     {
0086         emit sigFoo();
0087 #ifndef Q_OS_MAC
0088         // Mac currently uses the unix event loop (not glib) which
0089         // has issues and blocks in nested event loops such as the
0090         // one triggered by the below. Since this is a limitation
0091         // of Qt and working around it here doesn't invalidate the
0092         // actual test case, let's do that.
0093         QTest::qWait(10);
0094 #endif
0095     }
0096 
0097 Q_SIGNALS:
0098     void sigFoo();
0099 };
0100 
0101 QTEST_MAIN(KGlobalTest)
0102 
0103 #include "kglobaltest.moc"