servicerunnertest.cpp 5.5 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
33
#include <locale.h>

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
54
55
56
57
};

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

    auto appsPath = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
    QDir(appsPath).removeRecursively();
    QVERIFY(QDir().mkpath(appsPath));
    auto fixtureDir = QDir(QFINDTESTDATA("fixtures"));
    for(auto fileInfo : fixtureDir.entryInfoList(QDir::Files)) {
        auto source = fileInfo.absoluteFilePath();
        auto target = appsPath + QDir::separator() + fileInfo.fileName();
        QVERIFY2(QFile::copy(fileInfo.absoluteFilePath(), target),
58
                 qPrintable(QStringLiteral("can't copy %1 => %2").arg(source, target)));
59
60
61
62
63
    }

    setlocale(LC_ALL, "C.utf8");

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

    // 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.
69
70
71
72
73
74
75
76
77
78
}

void ServiceRunnerTest::cleanupTestCase()
{
}

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

    runner.match(context);

    bool chromeFound = false;
    bool signalFound = false;
    for (auto match : context.matches()) {
        qDebug() << "matched" << match.text();
87
        if (!match.text().contains(QStringLiteral("ServiceRunnerTest"))) {
88
89
90
            continue;
        }

91
        if (match.text() == QStringLiteral("Google Chrome ServiceRunnerTest")) {
92
93
            QCOMPARE(match.relevance(), 0.8);
            chromeFound = true;
94
        } else if (match.text() == QStringLiteral("Signal ServiceRunnerTest")) {
95
96
97
98
99
100
101
102
103
104
105
106
107
108
            // 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;
109
    context.setQuery(QStringLiteral("kons"));
110
111
112
113
114
115
116

    runner.match(context);

    bool konsoleFound = false;
    bool yakuakeFound = false;
    for (auto match : context.matches()) {
        qDebug() << "matched" << match.text();
117
        if (!match.text().contains(QStringLiteral("ServiceRunnerTest"))) {
118
119
120
            continue;
        }

121
        if (match.text() == QStringLiteral("Konsole ServiceRunnerTest")) {
122
123
            QCOMPARE(match.relevance(), 0.99);
            konsoleFound = true;
124
        } else if (match.text() == QStringLiteral("Yakuake ServiceRunnerTest")) {
125
126
127
128
129
130
131
132
133
            // Rates lower because it doesn't have it in the name.
            QCOMPARE(match.relevance(), 0.59);
            yakuakeFound = true;
        }
    }
    QVERIFY(konsoleFound);
    QVERIFY(yakuakeFound);
}

134
135
136
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
137
    // System Settings has a noKDE version and a KDE version, if the noKDE version is encountered
138
139
140
141
142
    // 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;
143
    context.setQuery(QStringLiteral("settings"));
144
145
146
147
148
149
150

    runner.match(context);

    bool systemSettingsFound = false;
    bool foreignSystemSettingsFound = false;
    for (auto match : context.matches()) {
        qDebug() << "matched" << match.text();
151
        if (match.text() == QStringLiteral("System Settings ServiceRunnerTest")) {
152
153
            systemSettingsFound = true;
        }
154
        if (match.text() == QStringLiteral("KDE System Settings ServiceRunnerTest")) {
155
156
157
158
159
160
161
            foreignSystemSettingsFound = true;
        }
    }
    QVERIFY(systemSettingsFound);
    QVERIFY(!foreignSystemSettingsFound);
}

162
163
164
QTEST_MAIN(ServiceRunnerTest)

#include "servicerunnertest.moc"