File indexing completed on 2024-04-28 09:26:03
0001 /* 0002 * SPDX-FileCopyrightText: 2016 Sebastian Kügler <sebas@kde.org> 0003 * 0004 * SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include <QLoggingCategory> 0008 #include <QObject> 0009 #include <QStandardPaths> 0010 #include <QTest> 0011 0012 #include "../src/log.h" 0013 0014 Q_DECLARE_LOGGING_CATEGORY(KSCREEN_TESTLOG) 0015 0016 Q_LOGGING_CATEGORY(KSCREEN_TESTLOG, "kscreen.testlog") 0017 0018 using namespace KScreen; 0019 0020 auto KSCREEN_LOGGING = "KSCREEN_LOGGING"; 0021 0022 class TestLog : public QObject 0023 { 0024 Q_OBJECT 0025 0026 private Q_SLOTS: 0027 void init(); 0028 void initTestCase(); 0029 void cleanupTestCase(); 0030 void testContext(); 0031 void testEnabled(); 0032 void testLog(); 0033 0034 private: 0035 QString m_defaultLogFile; 0036 }; 0037 0038 void TestLog::init() 0039 { 0040 QStandardPaths::setTestModeEnabled(true); 0041 m_defaultLogFile = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kscreen/kscreen.log"); 0042 } 0043 0044 void TestLog::initTestCase() 0045 { 0046 qputenv(KSCREEN_LOGGING, QByteArray("true")); 0047 } 0048 0049 void TestLog::cleanupTestCase() 0050 { 0051 qunsetenv(KSCREEN_LOGGING); 0052 } 0053 0054 void TestLog::testContext() 0055 { 0056 auto log = Log::instance(); 0057 QString ctx = QStringLiteral("context text"); 0058 QVERIFY(log != nullptr); 0059 log->setContext(ctx); 0060 QCOMPARE(log->context(), ctx); 0061 0062 delete log; 0063 } 0064 0065 void TestLog::testEnabled() 0066 { 0067 qputenv(KSCREEN_LOGGING, QByteArray("faLSe")); 0068 0069 auto log = Log::instance(); 0070 QCOMPARE(log->enabled(), false); 0071 QCOMPARE(log->logFile(), QString()); 0072 0073 delete log; 0074 qunsetenv(KSCREEN_LOGGING); 0075 0076 log = Log::instance(); 0077 QCOMPARE(log->enabled(), false); 0078 QCOMPARE(log->logFile(), QString()); 0079 0080 delete log; 0081 qputenv(KSCREEN_LOGGING, QByteArray("truE")); 0082 0083 log = Log::instance(); 0084 QCOMPARE(log->enabled(), true); 0085 QCOMPARE(log->logFile(), m_defaultLogFile); 0086 0087 delete log; 0088 } 0089 0090 void TestLog::testLog() 0091 { 0092 auto log = Log::instance(); 0093 Q_UNUSED(log); 0094 0095 QFile lf(m_defaultLogFile); 0096 lf.remove(); 0097 QVERIFY(!lf.exists()); 0098 0099 QString logmsg = QStringLiteral("This is a log message. ♥"); 0100 Log::log(logmsg); 0101 0102 QVERIFY(lf.exists()); 0103 QVERIFY(lf.remove()); 0104 0105 qCDebug(KSCREEN_TESTLOG) << "qCDebug message from testlog"; 0106 QVERIFY(lf.exists()); 0107 QVERIFY(lf.remove()); 0108 0109 delete Log::instance(); 0110 0111 // Make sure on log file gets written when disabled 0112 qputenv(KSCREEN_LOGGING, "false"); 0113 0114 qCDebug(KSCREEN_TESTLOG) << logmsg; 0115 QCOMPARE(Log::instance()->enabled(), false); 0116 QVERIFY(!lf.exists()); 0117 0118 Log::log(logmsg); 0119 QVERIFY(!lf.exists()); 0120 0121 // Make sure we don't crash on cleanup 0122 delete Log::instance(); 0123 delete Log::instance(); 0124 } 0125 0126 QTEST_MAIN(TestLog) 0127 0128 #include "testlog.moc"