File indexing completed on 2024-04-21 05:46:12
0001 /* 0002 SPDX-FileCopyrightText: 2007 Nicolas Ternisien <nicolas.ternisien@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "testUtil.h" 0008 #include <QFile> 0009 #include <QTemporaryFile> 0010 #include <QTest> 0011 0012 //#include <qtest_kde.h> 0013 0014 #include "kernelFactory.h" 0015 #include "systemFactory.h" 0016 0017 #include "ksystemlog_debug.h" 0018 0019 #include "logViewModel.h" 0020 #include "logViewWidget.h" 0021 0022 #include "analyzer.h" 0023 static void enableLoggign() 0024 { 0025 KSYSTEMLOG().setFilterRules(QStringLiteral("ksystemlog=true")); 0026 Q_INIT_RESOURCE(testResources); 0027 } 0028 0029 Q_COREAPP_STARTUP_FUNCTION(enableLoggign) 0030 0031 TestUtil::TestUtil() 0032 { 0033 } 0034 0035 TestUtil::~TestUtil() 0036 { 0037 } 0038 0039 void TestUtil::registerLogModeFactories() const 0040 { 0041 qCDebug(KSYSTEMLOG) << "Registering existing log mode factories"; 0042 Globals::instance().registerLogModeFactory(new SystemLogModeFactory()); 0043 Globals::instance().registerLogModeFactory(new KernelLogModeFactory()); 0044 } 0045 0046 LogViewModel *TestUtil::defineLogViewModel(Analyzer *analyzer) const 0047 { 0048 auto logViewWidget = new LogViewWidget(); 0049 auto model = new LogViewModel(logViewWidget); 0050 0051 analyzer->setLogViewModel(model); 0052 0053 return model; 0054 } 0055 0056 Analyzer *TestUtil::createAnalyzer(const QString &logModeName, LogViewModel **model) const 0057 { 0058 Analyzer *analyzer = Globals::instance().findLogMode(logModeName)->createAnalyzer(); 0059 0060 *model = defineLogViewModel(analyzer); 0061 0062 return analyzer; 0063 } 0064 0065 void TestUtil::destroyReader(Analyzer *analyzer) const 0066 { 0067 // TODO Unable to delete the created LogViewWidget, because the LogViewModel is not accessible from Reader 0068 delete analyzer; 0069 } 0070 0071 QVector<LogFile> TestUtil::createLogFiles(const QString &resourceFileName) const 0072 { 0073 QTemporaryFile *tempFile = QTemporaryFile::createNativeFile(resourceFileName); 0074 tempFile->setAutoRemove(false); 0075 qCDebug(KSYSTEMLOG) << "Using log file name " << tempFile; 0076 tempFile->setPermissions(QFile::WriteUser | QFile::ReadUser | QFile::ReadOwner | QFile::WriteOwner); 0077 0078 LogLevel *informationLogLevel = Globals::instance().informationLogLevel(); 0079 0080 QVector<LogFile> logFiles; 0081 LogFile const logFile(QUrl::fromLocalFile(tempFile->fileName()), informationLogLevel); 0082 delete tempFile; 0083 logFiles.append(logFile); 0084 0085 return logFiles; 0086 } 0087 0088 void TestUtil::testLine(LogLine *line, const QString &originalFileName, LogLevel *logLevel, const QDateTime &time, const QStringList &items) const 0089 { 0090 QCOMPARE(line->time(), time); 0091 QCOMPARE(line->sourceFileName(), originalFileName); 0092 QCOMPARE(line->logLevel()->id(), logLevel->id()); 0093 0094 // Test log line items 0095 QStringList const logItems = line->logItems(); 0096 QCOMPARE(logItems.count(), items.count()); 0097 QCOMPARE(logItems, items); 0098 } 0099 0100 void TestUtil::addLogLines(const QString &fileName, const QStringList &addedLines) const 0101 { 0102 // Wait 1 sec to be sure the dirty signal will be emitted 0103 QTest::qWait(1000); 0104 0105 QFile data(fileName); 0106 if (data.open(QFile::Append | QIODevice::Text)) { 0107 qCDebug(KSYSTEMLOG) << "Opening " << fileName << " for writing " << addedLines.count() << " line(s)."; 0108 0109 QTextStream out(&data); 0110 for (const QString &line : addedLines) { 0111 out << line << '\n'; 0112 } 0113 0114 out.flush(); 0115 data.close(); 0116 } else { 0117 QFAIL(QString::fromLatin1("Unable to open the test log file %1").arg(fileName).toUtf8().constData()); 0118 } 0119 0120 // Wait 3 secs to be sure the log file changed have been processed 0121 QTest::qWait(1000); 0122 } 0123 0124 #include "moc_testUtil.cpp"