File indexing completed on 2024-10-06 03:38:15

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 2019 David Hallas <david@davidhallas.dk>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "kprocesslisttest.h"
0009 #include "kprocesslist.h"
0010 #include "kuser.h"
0011 #include <QCoreApplication>
0012 #include <QTest>
0013 #include <algorithm>
0014 
0015 namespace
0016 {
0017 QString getTestExeName()
0018 {
0019     static QString testExeName = QCoreApplication::instance()->applicationFilePath().section(QLatin1Char('/'), -1);
0020     return testExeName;
0021 }
0022 
0023 }
0024 
0025 QTEST_MAIN(KProcessListTest)
0026 
0027 void KProcessListTest::testKProcessInfoConstructionAssignment()
0028 {
0029     KProcessList::KProcessInfo processInfoDefaultConstructed;
0030     QVERIFY(processInfoDefaultConstructed.isValid() == false);
0031     const qint64 pid(42);
0032     const QString name(QStringLiteral("/bin/some_exe"));
0033     const QString user(QStringLiteral("some_user"));
0034     KProcessList::KProcessInfo processInfo(pid, name, user);
0035     QVERIFY(processInfo.isValid() == true);
0036     QCOMPARE(processInfo.pid(), pid);
0037     QCOMPARE(processInfo.name(), name);
0038     QCOMPARE(processInfo.user(), user);
0039     KProcessList::KProcessInfo processInfoCopy(processInfo);
0040     QVERIFY(processInfoCopy.isValid() == true);
0041     QCOMPARE(processInfoCopy.pid(), pid);
0042     QCOMPARE(processInfoCopy.name(), name);
0043     QCOMPARE(processInfoCopy.user(), user);
0044     KProcessList::KProcessInfo processInfoAssignment;
0045     processInfoAssignment = processInfo;
0046     QVERIFY(processInfoAssignment.isValid() == true);
0047     QCOMPARE(processInfoAssignment.pid(), pid);
0048     QCOMPARE(processInfoAssignment.name(), name);
0049     QCOMPARE(processInfoAssignment.user(), user);
0050 }
0051 
0052 void KProcessListTest::testProcessInfoList()
0053 {
0054     KProcessList::KProcessInfoList processInfoList = KProcessList::processInfoList();
0055     QVERIFY(processInfoList.empty() == false);
0056     auto testProcessIterator = std::find_if(processInfoList.begin(), processInfoList.end(), [](const KProcessList::KProcessInfo &info) {
0057         return QDir::fromNativeSeparators(info.command()).endsWith(QLatin1String("/") + getTestExeName());
0058     });
0059     QVERIFY(testProcessIterator != processInfoList.end());
0060     const auto &processInfo = *testProcessIterator;
0061     QVERIFY(processInfo.isValid() == true);
0062     QVERIFY(QDir::fromNativeSeparators(processInfo.command()).endsWith(QLatin1String("/") + getTestExeName()));
0063     QCOMPARE(processInfo.name(), getTestExeName());
0064     QCOMPARE(processInfo.pid(), QCoreApplication::applicationPid());
0065     QCOMPARE(processInfo.user(), KUser().loginName());
0066 }
0067 
0068 void KProcessListTest::testProcessInfo()
0069 {
0070     const qint64 testExePid = QCoreApplication::applicationPid();
0071     KProcessList::KProcessInfo processInfo = KProcessList::processInfo(testExePid);
0072     QVERIFY(processInfo.isValid() == true);
0073     QVERIFY(QDir::fromNativeSeparators(processInfo.command()).endsWith(QLatin1String("/") + getTestExeName()));
0074     QCOMPARE(processInfo.pid(), testExePid);
0075     QCOMPARE(processInfo.user(), KUser().loginName());
0076 }
0077 
0078 void KProcessListTest::testProcessInfoNotFound()
0079 {
0080     KProcessList::KProcessInfo processInfo = KProcessList::processInfo(-1);
0081     QVERIFY(processInfo.isValid() == false);
0082 }
0083 
0084 #include "moc_kprocesslisttest.cpp"