providertest.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
/*
    Copyright (C) 2016 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program 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 Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "servercontroller.h"

#include <rest/restapi.h>
#include <rest/restclient.h>

#include <provider/core/provider.h>
Volker Krause's avatar
Volker Krause committed
24
#include <provider/core/platforminfosource.h>
25
26
27
28
29
30
31
#include <provider/core/screeninfosource.h>

#include <core/product.h>
#include <core/schemaentryelement.h>
#include <core/schemaentrytemplates.h>

#include <QDebug>
32
33
34
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
35
36
37
38
39
40
41
#include <QtTest/qtest.h>
#include <QNetworkReply>
#include <QObject>
#include <QSignalSpy>
#include <QStandardPaths>

using namespace UserFeedback;
Volker Krause's avatar
Volker Krause committed
42
using namespace UserFeedback::Console;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

class ProviderTest : public QObject
{
    Q_OBJECT
private:
    ServerController m_server;

    ServerInfo testServer() const
    {
        ServerInfo s;
        s.setUrl(m_server.url());
        return s;
    }

    bool waitForFinished(QNetworkReply *reply)
    {
        Q_ASSERT(reply);
        QSignalSpy spy(reply, &QNetworkReply::finished);
        Q_ASSERT(spy.isValid());
        return spy.wait();
    }

private slots:
    void initTestCase()
    {
68
        Q_INIT_RESOURCE(schematemplates);
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        QStandardPaths::setTestModeEnabled(true);
        QVERIFY(m_server.start());
    }

    void testProvider()
    {
        // delete previous leftovers
        RESTClient client;
        client.connectToServer(testServer());
        QVERIFY(client.isConnected());
        Product p;
        p.setName(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
        auto reply = RESTApi::deleteProduct(&client, p);
        waitForFinished(reply);

        // create test product
85
86
        for (const auto &tpl : SchemaEntryTemplates::availableTemplates())
            p.addTemplate(tpl);
87
88
89
90
91
92
93
        QVERIFY(p.isValid());
        reply = RESTApi::createProduct(&client, p);
        QVERIFY(waitForFinished(reply));
        QCOMPARE(reply->error(), QNetworkReply::NoError);

        // submit data
        Provider provider;
94
        provider.setStatisticsCollectionMode(Provider::DetailedUsageStatistics);
95
96
        provider.setProductIdentifier(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
        provider.setFeedbackServer(m_server.url());
97
98
        provider.addDataSource(new ScreenInfoSource, Provider::DetailedUsageStatistics);
        provider.addDataSource(new PlatformInfoSource, Provider::DetailedUsageStatistics);
99
        provider.submit();
100
        QTest::qWait(100); // HACK submit is async
101
102
103

        // retrieve data
        reply = RESTApi::listSamples(&client, p);
104
        QVERIFY(waitForFinished(reply));
105
        QVERIFY(reply->header(QNetworkRequest::ContentTypeHeader).toString().startsWith(QLatin1String("text/json")));
106
107
108
109
110
111
112
113
114
        auto doc = QJsonDocument::fromJson(reply->readAll());
        QVERIFY(doc.isArray());
        auto a = doc.array();
        QCOMPARE(a.size(), 1);

        auto obj = a.at(0).toObject();
        QVERIFY(!obj.isEmpty());
        QVERIFY(obj.contains(QLatin1String("id")));
        QVERIFY(obj.contains(QLatin1String("timestamp")));
Volker Krause's avatar
Volker Krause committed
115
116
117
118
119
120
121
122
123
124
125
126

        QVERIFY(obj.contains(QLatin1String("platform")));
        auto sub = obj.value(QLatin1String("platform")).toObject();
        QVERIFY(sub.contains(QLatin1String("os")));
        QVERIFY(sub.contains(QLatin1String("version")));

        QVERIFY(obj.contains(QLatin1String("screens")));
        a = obj.value(QLatin1String("screens")).toArray();
        QVERIFY(a.size() > 0);
        sub = a.at(0).toObject();
        QVERIFY(sub.contains(QLatin1String("height")));
        QVERIFY(sub.contains(QLatin1String("width")));
127
    }
128
129
130
131
132
133
134
135
136

    void testNoTelemetry()
    {
        Provider provider;
        provider.setProductIdentifier(QStringLiteral("org.kde.UserFeedback.UnitTestProduct"));
        provider.addDataSource(new ScreenInfoSource, Provider::BasicSystemInformation);
        provider.addDataSource(new PlatformInfoSource, Provider::BasicSystemInformation);
        provider.setStatisticsCollectionMode(Provider::NoStatistics);
        QByteArray b;
137
        QMetaObject::invokeMethod(&provider, "jsonData", Q_RETURN_ARG(QByteArray, b), Q_ARG(UserFeedback::Provider::StatisticsCollectionMode, provider.statisticsCollectionMode()));
138
139
140
        b.replace('\n', "");
        QCOMPARE(b.constData(), "{}");
    }
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

    void testLoadStore()
    {
        {
            Provider p;
            p.setStatisticsCollectionMode(Provider::NoStatistics);
            p.setSurveyInterval(-1);
        }

        {
            Provider p;
            QCOMPARE(p.statisticsCollectionMode(), Provider::NoStatistics);
            QCOMPARE(p.surveyInterval(), -1);
            p.setStatisticsCollectionMode(Provider::DetailedSystemInformation);
            p.setSurveyInterval(90);
        }

        {
            Provider p;
            QCOMPARE(p.statisticsCollectionMode(), Provider::DetailedSystemInformation);
            QCOMPARE(p.surveyInterval(), 90);
        }
    }
164
165
166
167
168
};

QTEST_MAIN(ProviderTest)

#include "providertest.moc"