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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010 Peter Penz <peter.penz19@gmail.com>
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 as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "kfilemetainfotest.h"
0021 #include <kiotesthelper.h>
0022 
0023 #include <kdirlister.h>
0024 #include <kfilemetainfo.h>
0025 #include <qtest.h>
0026 
0027 #include <config-kdelibs4support.h>
0028 
0029 #include <QThread>
0030 
0031 /**
0032  * Reads the meta-data of a given file inside a thread.
0033  * Used to test KFileMetaInfo for reentrancy issues.
0034  */
0035 class KFileMetaInfoThread : public QThread
0036 {
0037 public:
0038     KFileMetaInfoThread(const QString &fileName);
0039 
0040 protected:
0041     void run() override;
0042 
0043 private:
0044     QString m_fileName;
0045 };
0046 
0047 KFileMetaInfoThread::KFileMetaInfoThread(const QString &fileName) :
0048     m_fileName(fileName)
0049 {
0050 }
0051 
0052 void KFileMetaInfoThread::run()
0053 {
0054     KFileMetaInfo fileMetaInfo(m_fileName);
0055 }
0056 
0057 QTEST_MAIN(KFileMetaInfoTest)
0058 
0059 void KFileMetaInfoTest::initTestCase()
0060 {
0061     m_exitCount = 0;
0062 }
0063 
0064 void KFileMetaInfoTest::testMetaInfo()
0065 {
0066     const QString file = m_tempDir.path() + "/testfilename";
0067     createTestFile(file);
0068 
0069     KFileMetaInfo fileMetaInfo(file);
0070 #if ! KIO_NO_STRIGI
0071     QVERIFY(fileMetaInfo.isValid());
0072 
0073     const KFileMetaInfoItem &item = fileMetaInfo.item("http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#fileName");
0074     QCOMPARE(item.value().toString(), QString("testfilename"));
0075 #endif
0076 }
0077 
0078 void KFileMetaInfoTest::testReentrancy()
0079 {
0080     const QString file = m_tempDir.path() + "/testfilename";
0081     createTestFile(file);
0082 
0083     m_exitCount = 20;
0084     for (int i = 0; i < m_exitCount; ++i) {
0085         QThread *thread = new KFileMetaInfoThread(file);
0086         thread->setParent(this);
0087         connect(thread, SIGNAL(finished()), this, SLOT(exitLoop()));
0088         thread->start();
0089     }
0090 
0091     m_eventLoop.exec();
0092 }
0093 
0094 void KFileMetaInfoTest::exitLoop()
0095 {
0096     --m_exitCount;
0097     if (m_exitCount <= 0) {
0098         m_eventLoop.quit();
0099     }
0100 }
0101