File: TestPageManager.cpp

package info (click to toggle)
plasma-systemmonitor 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,016 kB
  • sloc: cpp: 2,851; xml: 236; python: 29; makefile: 7; sh: 4
file content (282 lines) | stat: -rw-r--r-- 9,948 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 *    SPDX-FileCopyrightText: 2025 Arjen Hiemstra <ahiemstra@heimr.nl>
 *
 *    SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */

#include <QTest>

#include <KConfigGroup>

#include "PageManager.h"
#include "PageManager_p.h"

using namespace Qt::StringLiterals;
namespace fs = std::filesystem;

class PageManagerTest : public QObject
{
    Q_OBJECT

    std::filesystem::path m_appDataWriteLocation;
    std::filesystem::path m_appDataReadLocation;

    // Helper function to create and load a new page manager instance.
    // This avoids us needing to use the singleton instance, so we don't store
    // state between different tests.
    std::unique_ptr<PageManager> makePageManager()
    {
        auto pm = std::make_unique<PageManager>(std::make_unique<PageManagerPrivate>());
        pm->load();
        return pm;
    }

    // Copy a file from test data local to QStandardPaths local.
    void copyToLocal(const char *fileName)
    {
        auto path = QFINDTESTDATA("data/local/"_L1 + fileName);
        QVERIFY(QFile::copy(fs::path(path.toStdString()), m_appDataWriteLocation / fileName));
    }

private Q_SLOTS:

    void initTestCase()
    {
        // PageManager uses QStandardPaths::AppDataLocation to look for pages.
        // Ensure we have these available and in a known state, that includes
        // ensuring we do not read any actual System Monitor installed pages.
        QCoreApplication::setApplicationName(u"plasma-systemmonitor"_s);

        auto path = QFINDTESTDATA("TestPageManager_data/plasma-systemmonitor/newtestpage.page");
        auto systemDir = path.left(path.indexOf("plasma-systemmonitor/newtestpage.page"));
        QStringList dataDirs = {
            systemDir,
            // To test that we handle non-existent paths correctly.
            u"/some/nonexistent/path/"_s,
        };
        qputenv("XDG_DATA_DIRS", dataDirs.join(':').toUtf8());
        QStandardPaths::setTestModeEnabled(true);

        m_appDataReadLocation = fs::path(systemDir.toStdString()) / "plasma-systemmonitor";

        m_appDataWriteLocation = fs::path(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString());
        fs::create_directories(m_appDataWriteLocation);

        // Clear replacePages so the state is the same between all tests.
        PageManagerPrivate::s_replacePages = {};
    }

    void cleanup()
    {
        // Cleanup any local data that was written by the test.
        fs::remove_all(m_appDataWriteLocation);
        fs::create_directories(m_appDataWriteLocation);

        // Ensure we have a clean set of replacePages, even if the test code
        // modified things.
        PageManagerPrivate::s_replacePages = {};
    }

    void testLoad()
    {
        copyToLocal("basepage.page");
        copyToLocal("localpage.page");
        copyToLocal("replacepage.page");

        auto pm = makePageManager();

        QCOMPARE(pm->pages().count(), 6);

        auto newTestPage = pm->page(u"newtestpage.page"_s);
        QVERIFY(newTestPage);
        QCOMPARE(newTestPage->data()->property("title"), u"New Test Page"_s);
        QCOMPARE(newTestPage->isOutdated(), false);
        QCOMPARE(newTestPage->hasReplacedOutdated(), false);
        QCOMPARE(newTestPage->writeableState(), PageController::WriteableState::NotWriteable);

        auto localPage = pm->page(u"localpage.page"_s);
        QVERIFY(localPage);
        QCOMPARE(localPage->writeableState(), PageController::WriteableState::Writeable);

        auto replacePage = pm->page(u"replacepage.page"_s);
        QVERIFY(replacePage);
        QCOMPARE(replacePage->data()->property("title"), u"Local Replaced Page"_s);
        QCOMPARE(replacePage->isOutdated(), false);
        QCOMPARE(replacePage->writeableState(), PageController::WriteableState::LocalChanges);

        auto oldTestPage = pm->page(u"oldtestpage.page"_s);
        QVERIFY(oldTestPage);
        QCOMPARE(oldTestPage->isOutdated(), false);
        QCOMPARE(oldTestPage->writeableState(), PageController::WriteableState::NotWriteable);

        auto basePage = pm->page(u"basepage.page"_s);
        QVERIFY(basePage);
        QCOMPARE(basePage->isOutdated(), true);
        QCOMPARE(basePage->writeableState(), PageController::WriteableState::LocalChanges);

        // Ensure we're not inadvertently sharing data between pages.
        QCOMPARE(basePage->config()->groupList().contains("Face-79169072682643"), false);
    }

    void testReplaceOutdated()
    {
        copyToLocal("basepage.page");

        PageManagerPrivate::s_replacePages = {
            ReplaceInfo{u"basepage.page"_s, u"old-basepage.page"_s, 0},
        };

        auto pm = makePageManager();

        auto basePage = pm->page(u"basepage.page"_s);
        QVERIFY(basePage);
        QCOMPARE(basePage->hasReplacedOutdated(), true);

        auto oldBasePage = pm->page(u"old-basepage.page"_s);
        QVERIFY(oldBasePage);
        // Verify that it's actually using the data from our old page.
        QCOMPARE(oldBasePage->data()->property("title"), u"Base Page"_s);
    }

    void testIgnoreReplacePages()
    {
        PageManagerPrivate::s_replacePages = {
            ReplaceInfo{u"basepage.page"_s, u"old-basepage.page"_s, 0},
        };

        auto pm = makePageManager();

        auto basePage = pm->page(u"basepage.page"_s);
        QVERIFY(basePage);

        // When we do not have any local changes for a replaced page, the
        // replaced page should be ignored and never added to the set of pages.
        auto oldBasePage = pm->page(u"old-basepage.page"_s);
        QVERIFY(!oldBasePage);
    }

    void testAddPage()
    {
        auto pm = makePageManager();

        auto page = pm->addPage(u"addedtestpage"_s, {{u"title"_s, u"Added Test Page"_s}});
        QVERIFY(page);
        QCOMPARE(page->path(), m_appDataWriteLocation / "addedtestpage.page");
        QCOMPARE(page->data()->property("title"), u"Added Test Page"_s);

        auto page1 = pm->addPage(u"addedtestpage"_s, {{u"title"_s, u"Added Test Page"_s}});
        QVERIFY(page1);
        QCOMPARE(page1->path(), m_appDataWriteLocation / "addedtestpage1.page");
        QCOMPARE(page1->data()->property("title"), u"Added Test Page"_s);
    }

    void testSave()
    {
        copyToLocal("basepage.page");
        copyToLocal("localpage.page");
        copyToLocal("replacepage.page");

        auto pm = makePageManager();

        auto page = pm->addPage(u"addedtestpage"_s, {{u"title"_s, u"Added Test Page"_s}});
        QVERIFY(page);
        QVERIFY(page->save());
        QVERIFY(fs::exists(page->path()));

        auto localPage = pm->page(u"localpage.page"_s);
        QVERIFY(localPage);
        QVERIFY(localPage->save());

        auto replacePage = pm->page(u"replacepage.page"_s);
        QVERIFY(replacePage);
        QVERIFY(replacePage->save());

        auto basePage = pm->page(u"basepage.page"_s);
        QVERIFY(basePage);
        QVERIFY(basePage->save());

        // Saving an old config that used to use cascading should now write out
        // a full page.
        KConfig config(QString::fromStdString(basePage->path()), KConfig::SimpleConfig);
        QVERIFY(config.hasGroup(u"page"_s));
        QCOMPARE(config.group("page").readEntry("version", 0), PageManager::CurrentPageVersion);
        QVERIFY(config.group("page").hasGroup("row-0"));
    }

    void testUpgradeDiscard()
    {
        copyToLocal("basepage.page");

        auto pm = makePageManager();

        auto basePage = pm->page(u"basepage.page"_s);
        QVERIFY(basePage);
        QCOMPARE(basePage->writeableState(), PageController::WriteableState::LocalChanges);

        auto oldPath = basePage->path();

        QVERIFY(basePage->upgrade(PageController::PageChanges::DiscardChanges));
        QVERIFY(!fs::exists(oldPath));
        QCOMPARE(basePage->data()->property("title"), u"New Base Page"_s);
    }

    void testUpgradeKeep()
    {
        copyToLocal("basepage.page");

        auto pm = makePageManager();

        auto basePage = pm->page(u"basepage.page"_s);
        QVERIFY(basePage);
        QCOMPARE(basePage->writeableState(), PageController::WriteableState::LocalChanges);

        auto oldPath = basePage->path();

        QVERIFY(basePage->upgrade(PageController::PageChanges::KeepChanges));
        QVERIFY(fs::exists(oldPath));
        QCOMPARE(basePage->data()->property("title"), u"Base Page"_s);
    }

    void testEdit()
    {
        auto pm = makePageManager();

        auto newTestPage = pm->page(u"newtestpage.page"_s);
        QVERIFY(newTestPage);

        newTestPage->data()->setProperty("title", "Edited Title");
        newTestPage->save();

        QCOMPARE(newTestPage->path(), m_appDataWriteLocation / "newtestpage.page");
        QCOMPARE(newTestPage->writeableState(), PageController::WriteableState::LocalChanges);

        KConfig config(QString::fromStdString(newTestPage->path()), KConfig::SimpleConfig);
        QVERIFY(config.hasGroup(u"page"_s));
        QCOMPARE(config.group("page").readEntry("Title", QString{}), u"Edited Title");
    }

    void testReset()
    {
        auto pm = makePageManager();

        auto newTestPage = pm->page(u"newtestpage.page"_s);
        QVERIFY(newTestPage);

        newTestPage->data()->setProperty("title", u"Edited Title"_s);
        newTestPage->save();

        QCOMPARE(newTestPage->path(), m_appDataWriteLocation / "newtestpage.page");
        QCOMPARE(newTestPage->writeableState(), PageController::WriteableState::LocalChanges);

        pm->removeLocalPageFiles(newTestPage->fileName());

        QCOMPARE(newTestPage->path(), m_appDataReadLocation / "newtestpage.page");
        QCOMPARE(newTestPage->writeableState(), PageController::WriteableState::NotWriteable);

        QCOMPARE(newTestPage->data()->property("title").toString(), u"New Test Page"_s);
    }
};

QTEST_MAIN(PageManagerTest);

#include "TestPageManager.moc"