File: coronatest.cpp

package info (click to toggle)
plasma-framework 5.116.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 19,088 kB
  • sloc: cpp: 29,562; javascript: 637; sh: 517; python: 145; xml: 110; php: 27; makefile: 7
file content (207 lines) | stat: -rw-r--r-- 6,744 bytes parent folder | download
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
    SPDX-FileCopyrightText: 2014 Marco Martin <mart@kde.org>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include "coronatest.h"
#include <KActionCollection>

#include <QAction>
#include <QApplication>
#include <QProcess>
#include <QRandomGenerator>
#include <QSignalSpy>
#include <QStandardPaths>

SimpleCorona::SimpleCorona(QObject *parent)
    : Plasma::Corona(parent)
{
}

SimpleCorona::~SimpleCorona()
{
}

QRect SimpleCorona::screenGeometry(int screen) const
{
    // completely arbitrary, still not tested
    return QRect(100 * screen, 100, 100, 100);
}

int SimpleCorona::screenForContainment(const Plasma::Containment *c) const
{
    if (qobject_cast<const SimpleNoScreenContainment *>(c)) {
        return -1;
    }
    return 0;
}

SimpleApplet::SimpleApplet(QObject *parentObject, const KPluginMetaData &data, const QVariantList &args)
    : Plasma::Applet(parentObject, data, args)
{
    // updateConstraints(Plasma::Types::UiReadyConstraint);
    m_timer.setSingleShot(true);
    m_timer.setInterval(QRandomGenerator::global()->bounded((500 + 1) - 100) + 100);
    m_timer.start();
    connect(&m_timer, &QTimer::timeout, [=]() {
        updateConstraints(Plasma::Types::UiReadyConstraint);
    });
}

SimpleContainment::SimpleContainment(QObject *parentObject, const KPluginMetaData &data, const QVariantList &args)
    : Plasma::Containment(parentObject, data, args)
{
    // updateConstraints(Plasma::Types::UiReadyConstraint);
    m_timer.setSingleShot(true);
    m_timer.setInterval(QRandomGenerator::global()->bounded((500 + 1) - 100) + 100);
    m_timer.start();
    connect(&m_timer, &QTimer::timeout, [=]() {
        updateConstraints(Plasma::Types::UiReadyConstraint);
    });
}

SimpleNoScreenContainment::SimpleNoScreenContainment(QObject *parentObject, const KPluginMetaData &data, const QVariantList &args)
    : Plasma::Containment(parentObject, data, args)
{
    // This containment will *never* be isUiReady()
}

void CoronaTest::initTestCase()
{
    QStandardPaths::setTestModeEnabled(true);
    m_corona = new SimpleCorona;

    m_configDir = QDir(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation));
    m_configDir.removeRecursively();

    QVERIFY(m_configDir.mkpath(QStringLiteral(".")));

    QVERIFY(QFile::copy(QStringLiteral(":/plasma-test-appletsrc"), m_configDir.filePath(QStringLiteral("plasma-test-appletsrc"))));
}

void CoronaTest::cleanupTestCase()
{
    m_configDir.removeRecursively();
    delete m_corona;
}

void CoronaTest::restore()
{
    m_corona->loadLayout(QStringLiteral("plasma-test-appletsrc"));
    QCOMPARE(m_corona->containments().count(), 3);

    const auto containments = m_corona->containments();
    for (auto cont : containments) {
        switch (cont->id()) {
        case 1:
            QCOMPARE(cont->applets().count(), 2);
            break;
        default:
            QCOMPARE(cont->applets().count(), 0);
            break;
        }
    }
}

void CoronaTest::checkOrder()
{
    QCOMPARE(m_corona->containments().count(), 3);

    // check containments order
    QCOMPARE(m_corona->containments().at(0)->id(), (uint)1);
    QCOMPARE(m_corona->containments().at(1)->id(), (uint)4);
    QCOMPARE(m_corona->containments().at(2)->id(), (uint)5);

    // check applets order
    QCOMPARE(m_corona->containments().at(0)->applets().count(), 2);
    QCOMPARE(m_corona->containments().at(0)->applets().at(0)->id(), (uint)2);
    QCOMPARE(m_corona->containments().at(0)->applets().at(1)->id(), (uint)3);
}

void CoronaTest::startupCompletion()
{
    QVERIFY(!m_corona->isStartupCompleted());
    QVERIFY(!m_corona->containments().at(0)->isUiReady());

    QSignalSpy spy(m_corona, SIGNAL(startupCompleted()));
    QVERIFY(spy.wait(1000));

    QVERIFY(m_corona->isStartupCompleted());
    QVERIFY(m_corona->containments().at(0)->isUiReady());
}

void CoronaTest::addRemoveApplets()
{
    m_corona->containments().at(0)->createApplet(QStringLiteral("invalid"));
    QCOMPARE(m_corona->containments().at(0)->applets().count(), 3);

    // remove action present
    QVERIFY(m_corona->containments().at(0)->applets().at(0)->actions()->action(QStringLiteral("remove")));
    // kill an applet
    m_corona->containments().at(0)->applets().at(0)->destroy();

    QSignalSpy spy(m_corona->containments().at(0)->applets().at(0), SIGNAL(destroyed()));
    QVERIFY(spy.wait(1000));
    QCOMPARE(m_corona->containments().at(0)->applets().count(), 2);
}

// this test has to be the last, since systemimmutability
// can't be programmatically unlocked
void CoronaTest::immutability()
{
    // immutability
    QCOMPARE(m_corona->immutability(), Plasma::Types::Mutable);
    m_corona->setImmutability(Plasma::Types::UserImmutable);
    QCOMPARE(m_corona->immutability(), Plasma::Types::UserImmutable);

    auto containments = m_corona->containments();
    for (Plasma::Containment *cont : std::as_const(containments)) {
        QCOMPARE(cont->immutability(), Plasma::Types::UserImmutable);
        const auto lstApplets = cont->applets();
        for (Plasma::Applet *app : lstApplets) {
            QCOMPARE(app->immutability(), Plasma::Types::UserImmutable);
        }
    }

    m_corona->setImmutability(Plasma::Types::Mutable);
    QCOMPARE(m_corona->immutability(), Plasma::Types::Mutable);

    containments = m_corona->containments();
    for (Plasma::Containment *cont : std::as_const(containments)) {
        QCOMPARE(cont->immutability(), Plasma::Types::Mutable);
        const auto lstApplets = cont->applets();
        for (Plasma::Applet *app : lstApplets) {
            QCOMPARE(app->immutability(), Plasma::Types::Mutable);
        }
    }

    m_corona->setImmutability(Plasma::Types::SystemImmutable);
    QCOMPARE(m_corona->immutability(), Plasma::Types::SystemImmutable);

    containments = m_corona->containments();
    for (Plasma::Containment *cont : std::as_const(containments)) {
        QCOMPARE(cont->immutability(), Plasma::Types::SystemImmutable);
        const auto lstApplets = cont->applets();
        for (Plasma::Applet *app : lstApplets) {
            QCOMPARE(app->immutability(), Plasma::Types::SystemImmutable);
        }
    }

    // can't unlock systemimmutable
    m_corona->setImmutability(Plasma::Types::Mutable);
    QCOMPARE(m_corona->immutability(), Plasma::Types::SystemImmutable);

    containments = m_corona->containments();
    for (Plasma::Containment *cont : std::as_const(containments)) {
        QCOMPARE(cont->immutability(), Plasma::Types::SystemImmutable);
        const auto lstApplets = cont->applets();
        for (Plasma::Applet *app : lstApplets) {
            QCOMPARE(app->immutability(), Plasma::Types::SystemImmutable);
        }
    }
}

QTEST_MAIN(CoronaTest)

#include "moc_coronatest.cpp"