File indexing completed on 2024-04-21 03:52:23

0001 /*
0002     SPDX-FileCopyrightText: 2015 Alex Merry <alex.merry@kde.org>
0003 
0004     SPDX-License-Identifier: BSD-3-Clause
0005 */
0006 
0007 #include <QCoreApplication>
0008 #include <QLibrary>
0009 #include <QMetaObject>
0010 #include <QThread>
0011 
0012 class Thread : public QThread
0013 {
0014     Q_OBJECT
0015 
0016     QLibrary *m_lib;
0017 
0018 public:
0019     Thread()
0020         : m_lib(0)
0021     {}
0022     ~Thread()
0023     {
0024         delete m_lib;
0025     }
0026 
0027 Q_SIGNALS:
0028     void libraryLoaded();
0029 
0030 public Q_SLOTS:
0031     void printStrings()
0032     {
0033         Q_ASSERT(QThread::currentThread() == QCoreApplication::instance()->thread());
0034 
0035         QFunctionPointer print_strings = m_lib->resolve("print_strings");
0036         if (print_strings) {
0037             print_strings();
0038         } else {
0039             qFatal("Could not resolve print_strings: %s", qPrintable(m_lib->errorString()));
0040         }
0041 
0042         QCoreApplication::instance()->quit();
0043     }
0044 protected:
0045     void run()
0046     {
0047         m_lib = new QLibrary(MODULE_PATH);
0048 
0049         if (!m_lib->load()) {
0050             qFatal("Could not load module: %s", m_lib->errorString().toUtf8().data());
0051         }
0052 
0053         // Queue a call to printStrings() on the main event loop (giving the
0054         // translations a chance to be loaded).
0055         QMetaObject::invokeMethod(this, "printStrings", Qt::QueuedConnection);
0056     }
0057 };
0058 
0059 int main(int argc, char** argv)
0060 {
0061     QCoreApplication app(argc, argv);
0062 
0063     Thread thread;
0064 
0065     // Start the thread *after* QCoreApplication is started (otherwise the
0066     // plugin's startup function won't be run on the Thread, and we won't test
0067     // what we wanted to test).
0068     QMetaObject::invokeMethod(&thread, "start", Qt::QueuedConnection);
0069 
0070     app.exec();
0071 
0072     thread.wait();
0073 
0074     return 0;
0075 }
0076 
0077 #include "tr_thread_test.moc"