servicerunnertest.cpp 5.66 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
 *   Copyright (C) 2016 Harald Sitter <sitter@kde.org>
 *
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) version 3, or any
 *   later version accepted by the membership of KDE e.V. (or its
 *   successor approved by the membership of KDE e.V.), which shall
 *   act as a proxy defined in Section 6 of version 3 of the license.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <QDebug>
#include <QDir>
#include <QFile>
#include <QObject>
#include <QStandardPaths>
#include <QTest>

#include <KSycoca>

#include "../servicerunner.h"

32
#include <clocale>
33

34
35
36
37
38
39
40
41
42
class ServiceRunnerTest : public QObject
{
    Q_OBJECT
private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();

    void testChromeAppsRelevance();
    void testKonsoleVsYakuakeComment();
43
    void testSystemSettings();
44
45
46
47
48
49
50
51
52
53
};

void ServiceRunnerTest::initTestCase()
{
    QStandardPaths::setTestModeEnabled(true);

    auto appsPath = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
    QDir(appsPath).removeRecursively();
    QVERIFY(QDir().mkpath(appsPath));
    auto fixtureDir = QDir(QFINDTESTDATA("fixtures"));
Alexander Lohnau's avatar
Alexander Lohnau committed
54
55
    const auto infoList = fixtureDir.entryInfoList(QDir::Files);
    for (const auto &fileInfo : infoList) {
56
57
58
        auto source = fileInfo.absoluteFilePath();
        auto target = appsPath + QDir::separator() + fileInfo.fileName();
        QVERIFY2(QFile::copy(fileInfo.absoluteFilePath(), target),
59
                 qPrintable(QStringLiteral("can't copy %1 => %2").arg(source, target)));
60
61
62
63
64
    }

    setlocale(LC_ALL, "C.utf8");

    KSycoca::self()->ensureCacheValid();
65
66
67
68
69

    // Make sure noDisplay behaves consistently WRT OnlyShowIn etc.
    QVERIFY(setenv("XDG_CURRENT_DESKTOP", "KDE", 1) == 0);
    // NOTE: noDisplay also includes X-KDE-OnlyShowOnQtPlatforms which is a bit harder to fake
    //       and not currently under testing anyway.
70
71
72
73
74
75
76
77
78
79
}

void ServiceRunnerTest::cleanupTestCase()
{
}

void ServiceRunnerTest::testChromeAppsRelevance()
{
    ServiceRunner runner(this, QVariantList());
    Plasma::RunnerContext context;
80
    context.setQuery(QStringLiteral("chrome"));
81
82
83
84
85

    runner.match(context);

    bool chromeFound = false;
    bool signalFound = false;
Alexander Lohnau's avatar
Alexander Lohnau committed
86
87
    const auto matches = context.matches();
    for (const auto &match : matches) {
88
        qDebug() << "matched" << match.text();
Laurent Montel's avatar
Laurent Montel committed
89
        if (!match.text().contains(QLatin1String("ServiceRunnerTest"))) {
90
91
92
            continue;
        }

Laurent Montel's avatar
Laurent Montel committed
93
        if (match.text() == QLatin1String("Google Chrome ServiceRunnerTest")) {
94
95
            QCOMPARE(match.relevance(), 0.8);
            chromeFound = true;
Laurent Montel's avatar
Laurent Montel committed
96
        } else if (match.text() == QLatin1String("Signal ServiceRunnerTest")) {
97
98
99
100
101
102
103
104
105
106
107
108
109
110
            // Rates lower because it doesn't have it in the name.
            QCOMPARE(match.relevance(), 0.7);
            signalFound = true;
        }
    }
    QVERIFY(chromeFound);
    QVERIFY(signalFound);
}

void ServiceRunnerTest::testKonsoleVsYakuakeComment()
{
    // Yakuake has konsole mentioned in comment, should be rated lower.
    ServiceRunner runner(this, QVariantList());
    Plasma::RunnerContext context;
111
    context.setQuery(QStringLiteral("kons"));
112
113
114
115
116

    runner.match(context);

    bool konsoleFound = false;
    bool yakuakeFound = false;
Alexander Lohnau's avatar
Alexander Lohnau committed
117
118
    const auto matches = context.matches();
    for (const auto &match : matches) {
119
        qDebug() << "matched" << match.text();
Laurent Montel's avatar
Laurent Montel committed
120
        if (!match.text().contains(QLatin1String("ServiceRunnerTest"))) {
121
122
123
            continue;
        }

Laurent Montel's avatar
Laurent Montel committed
124
        if (match.text() == QLatin1String("Konsole ServiceRunnerTest")) {
125
126
            QCOMPARE(match.relevance(), 0.99);
            konsoleFound = true;
Laurent Montel's avatar
Laurent Montel committed
127
        } else if (match.text() == QLatin1String("Yakuake ServiceRunnerTest")) {
128
129
130
131
132
133
134
135
136
            // Rates lower because it doesn't have it in the name.
            QCOMPARE(match.relevance(), 0.59);
            yakuakeFound = true;
        }
    }
    QVERIFY(konsoleFound);
    QVERIFY(yakuakeFound);
}

137
138
139
void ServiceRunnerTest::testSystemSettings()
{
    // In 5.9.0 'System Settings' suddenly didn't come back as a match for 'settings' anymore.
Yuri Chornoivan's avatar
Yuri Chornoivan committed
140
    // System Settings has a noKDE version and a KDE version, if the noKDE version is encountered
141
142
143
144
145
    // first it will be added to the seen cache, however disqualification of already seen items
    // may then also disqualify the KDE version of system settings on account of having already
    // seen it. This test makes sure we find the right version.
    ServiceRunner runner(this, QVariantList());
    Plasma::RunnerContext context;
146
    context.setQuery(QStringLiteral("settings"));
147
148
149
150
151

    runner.match(context);

    bool systemSettingsFound = false;
    bool foreignSystemSettingsFound = false;
Alexander Lohnau's avatar
Alexander Lohnau committed
152
153
    const auto matches = context.matches();
    for (const auto &match : matches) {
154
        qDebug() << "matched" << match.text();
Laurent Montel's avatar
Laurent Montel committed
155
        if (match.text() == QLatin1String("System Settings ServiceRunnerTest")) {
156
157
            systemSettingsFound = true;
        }
Laurent Montel's avatar
Laurent Montel committed
158
        if (match.text() == QLatin1String("KDE System Settings ServiceRunnerTest")) {
159
160
161
162
163
164
165
            foreignSystemSettingsFound = true;
        }
    }
    QVERIFY(systemSettingsFound);
    QVERIFY(!foreignSystemSettingsFound);
}

166
167
168
QTEST_MAIN(ServiceRunnerTest)

#include "servicerunnertest.moc"