File: WindowStateStorageTest.cpp

package info (click to toggle)
lomiri 0.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (121 lines) | stat: -rw-r--r-- 5,173 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (C) 2017 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * 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 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 "windowstatestorage.h"

#include <QTest>
#include <QStandardPaths>

class WindowStateStorageTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase() {
        QStandardPaths::setTestModeEnabled(true);
    }

    void init()
    {
        storage = new WindowStateStorage(":memory:", this);
    }

    void cleanup()
    {
        delete storage;
    }

    void testDbNameMemory() {
        QCOMPARE(storage->getDbName(), QStringLiteral(":memory:"));
    }

    // This test serves as a reminder: if you're changing the databse location,
    // copy the old database or risk the wrath of long-time users.
    void testDbNameDefault() {
        delete storage;
        storage = new WindowStateStorage(QString(), this);
        QCOMPARE(storage->getDbName(), QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QStringLiteral("/lomiri/windowstatestorage.sqlite"));
    }

    // Ensure that the WindowStateStorage object can be used even if opening
    // the database fails, just returning default values.
    void testErrorBehavior() {
        QString id{QTest::currentTestFunction()};
        QRect defaultValue{1, 1, 1, 1};
        WindowStateStorage::WindowState state{WindowStateStorage::WindowState::WindowStateMaximizedTopLeft};
        WindowStateStorage::WindowState defaultState{WindowStateStorage::WindowState::WindowStateMaximizedBottomRight};

        delete storage;
        QTest::ignoreMessage(QtWarningMsg, "AsyncQuery::initdb: Error opening state database. Window positions will not be saved or restored. \"/nonexistent/there/is/no/way/this/exists/\" \"Error opening database\" \"unable to open database file\"");
        storage = new WindowStateStorage(QStringLiteral("/nonexistent/there/is/no/way/this/exists/"), this);
        QCOMPARE(storage->getDbName(), QStringLiteral("ERROR"));

        storage->saveState(id, state);
        storage->saveGeometry(id, QRect(2, 2, 2, 2));
        storage->saveStage(id, 2);
        QCOMPARE(storage->getState(id, defaultState), defaultState);
        QCOMPARE(storage->getGeometry(id, defaultValue), defaultValue);
        QCOMPARE(storage->getStage(id, 4), 4);
    }

    void testSaveRestoreState() {
        const WindowStateStorage::WindowState state{WindowStateStorage::WindowState::WindowStateMaximizedTopLeft};
        storage->saveState(QTest::currentTestFunction(), state);
        QTRY_COMPARE(storage->getState(QTest::currentTestFunction(), WindowStateStorage::WindowState::WindowStateNormal), state);
    }

    void testSaveRestoreGeometry() {
        const QRect geometry{10, 20, 30, 40};
        storage->saveGeometry(QTest::currentTestFunction(), geometry);
        QTRY_COMPARE(storage->getGeometry(QTest::currentTestFunction(), QRect()), geometry);
    }

    void testSaveRestoreStage() {
        const int stage{1};
        storage->saveStage(QTest::currentTestFunction(), stage);
        QTRY_COMPARE(storage->getStage(QTest::currentTestFunction(), 0), stage);
    }

    void testProtectAgainstInvalidGeometry() {
        const QRect geometry{10, 20, 0, 10}; // zero-width (invalid) rectangle
        storage->saveGeometry(QTest::currentTestFunction(), geometry);
        const QRect defaultGeometry{10, 20, 30, 40};
        const QRect loadedGeometry = storage->getGeometry(QTest::currentTestFunction(), defaultGeometry);
        // ensure we don't load a broken geometry, instead we fall back to the default one
        QCOMPARE(loadedGeometry, defaultGeometry);
    }

    void testProtectAgainstSqlInjection() {
        QString naughtyAppId = QStringLiteral("stageQuery;DROP TABLE stage");
        QString naughtyAppIdSingleQuotes = QStringLiteral("stageQuery';DROP TABLE stage");
        QString naughtyAppIdDoubleQuotes = QStringLiteral("stageQuery\";DROP TABLE stage");
        QString naughtyAppIdBackslashes = QStringLiteral("stageQuery\\\";DROP TABLE stage");
        storage->saveStage(naughtyAppId, 1);
        QTRY_COMPARE(storage->getStage(naughtyAppId, 0), 1);
        storage->saveStage(naughtyAppIdSingleQuotes, 2);
        QTRY_COMPARE(storage->getStage(naughtyAppIdSingleQuotes, 0), 2);
        storage->saveStage(naughtyAppIdDoubleQuotes, 3);
        QTRY_COMPARE(storage->getStage(naughtyAppIdDoubleQuotes, 0), 3);
        storage->saveStage(naughtyAppIdBackslashes, 4);
        QTRY_COMPARE(storage->getStage(naughtyAppIdBackslashes, 0), 4);
    }

private:
    WindowStateStorage * storage{nullptr};
};

QTEST_GUILESS_MAIN(WindowStateStorageTest)
#include "WindowStateStorageTest.moc"