File: surveyprovidertest.cpp

package info (click to toggle)
kuserfeedback 1.3.0-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,496 kB
  • sloc: cpp: 13,251; php: 2,192; xml: 224; yacc: 90; lex: 82; sh: 17; makefile: 8
file content (113 lines) | stat: -rw-r--r-- 3,892 bytes parent folder | download | duplicates (2)
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
32
33
34
35
36
37
38
39
40
41
42
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
    SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

#include <provider.h>
#include <surveyinfo.h>

#include <QDebug>
#include <QtTest/qtest.h>
#include <QObject>
#include <QSettings>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QUuid>

using namespace KUserFeedback;

class SurveyProviderTest : public QObject
{
    Q_OBJECT
private slots:
    void initTestCase()
    {
        qRegisterMetaType<KUserFeedback::SurveyInfo>();

        QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
        QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
        QCoreApplication::setApplicationName(QStringLiteral("surveyprovidertest"));
        QStandardPaths::setTestModeEnabled(true);
    }

    void testSurveySelect()
    {
        {
            QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback.org.kde.surveyprovidertest"));;
            s.beginGroup(QLatin1String("UserFeedback"));
            s.remove(QLatin1String("LastSurvey"));
            s.remove(QLatin1String("CompletedSurveys"));
        }
        {
            QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback"));;
            s.beginGroup(QLatin1String("UserFeedback"));
            s.remove(QLatin1String("LastSurvey"));
        }

        SurveyInfo s1;
        s1.setUuid(QUuid::createUuid());
        s1.setUrl(QUrl(QStringLiteral("https://www.kde.org")));
        QVERIFY(s1.isValid());

        Provider p;
        p.setSurveyInterval(90);
        QSignalSpy spy(&p, SIGNAL(surveyAvailable(KUserFeedback::SurveyInfo)));
        QVERIFY(spy.isValid());

        // global kill switch prevents survey
        bool rv = false;
        p.setEnabled(false);
        QMetaObject::invokeMethod(&p, "selectSurvey", Q_RETURN_ARG(bool, rv), Q_ARG(KUserFeedback::SurveyInfo, s1));
        QVERIFY(!rv);
        QVERIFY(spy.isEmpty());

        p.setEnabled(true);
        QMetaObject::invokeMethod(&p, "selectSurvey", Q_RETURN_ARG(bool, rv), Q_ARG(KUserFeedback::SurveyInfo, s1));
        QVERIFY(rv);
        QCOMPARE(spy.size(), 1);

        // again, not completed yet
        QMetaObject::invokeMethod(&p, "selectSurvey", Q_RETURN_ARG(bool, rv), Q_ARG(KUserFeedback::SurveyInfo, s1));
        QVERIFY(rv);
        QCOMPARE(spy.size(), 2);
        spy.clear();

        // survey completed, should no longer be accepted
        p.surveyCompleted(s1);
        QMetaObject::invokeMethod(&p, "selectSurvey", Q_RETURN_ARG(bool, rv), Q_ARG(KUserFeedback::SurveyInfo, s1));
        QVERIFY(!rv);
        QVERIFY(spy.isEmpty());

        SurveyInfo s2;
        s2.setUuid(QUuid::createUuid());
        s2.setUrl(QUrl(QStringLiteral("https://www.kde.org/survey2")));
        QVERIFY(s2.isValid());

        // next survey, but interval hasn't passed yet
        p.setSurveyInterval(90);
        QMetaObject::invokeMethod(&p, "selectSurvey", Q_RETURN_ARG(bool, rv), Q_ARG(KUserFeedback::SurveyInfo, s2));
        QVERIFY(!rv);
        QVERIFY(spy.isEmpty());

        // survey interval passed
        p.setSurveyInterval(0);
        QMetaObject::invokeMethod(&p, "selectSurvey", Q_RETURN_ARG(bool, rv), Q_ARG(KUserFeedback::SurveyInfo, s2));
        QVERIFY(rv);

        // global survey coordination prevents next survey
        p.setSurveyInterval(90);
        {
            QSettings s(QCoreApplication::organizationName(), QStringLiteral("UserFeedback.org.kde.surveyprovidertest"));;
            s.beginGroup(QLatin1String("UserFeedback"));
            s.remove(QLatin1String("LastSurvey"));
            s.remove(QLatin1String("CompletedSurveys"));
        }
        QMetaObject::invokeMethod(&p, "selectSurvey", Q_RETURN_ARG(bool, rv), Q_ARG(KUserFeedback::SurveyInfo, s1));
        QVERIFY(!rv);
    }
};

QTEST_GUILESS_MAIN(SurveyProviderTest)

#include "surveyprovidertest.moc"