2 * Copyright (C) 2017 Canonical, Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17#include "windowstatestorage.h"
20#include <QStandardPaths>
22class WindowStateStorageTest : public QObject
28 QStandardPaths::setTestModeEnabled(true);
33 storage = new WindowStateStorage(":memory:", this);
41 void testDbNameMemory() {
42 QCOMPARE(storage->getDbName(), QStringLiteral(":memory:"));
45 // This test serves as a reminder: if you're changing the databse location,
46 // copy the old database or risk the wrath of long-time users.
47 void testDbNameDefault() {
49 storage = new WindowStateStorage(QString(), this);
50 QCOMPARE(storage->getDbName(), QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QStringLiteral("/unity8/windowstatestorage.sqlite"));
53 // Ensure that the WindowStateStorage object can be used even if opening
54 // the database fails, just returning default values.
55 void testErrorBehavior() {
56 QString id{QTest::currentTestFunction()};
57 QRect defaultValue{1, 1, 1, 1};
58 WindowStateStorage::WindowState state{WindowStateStorage::WindowState::WindowStateMaximizedTopLeft};
59 WindowStateStorage::WindowState defaultState{WindowStateStorage::WindowState::WindowStateMaximizedBottomRight};
62 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\"");
63 storage = new WindowStateStorage(QStringLiteral("/nonexistent/there/is/no/way/this/exists/"), this);
64 QCOMPARE(storage->getDbName(), QStringLiteral("ERROR"));
66 storage->saveState(id, state);
67 storage->saveGeometry(id, QRect(2, 2, 2, 2));
68 storage->saveStage(id, 2);
69 QCOMPARE(storage->getState(id, defaultState), defaultState);
70 QCOMPARE(storage->getGeometry(id, defaultValue), defaultValue);
71 QCOMPARE(storage->getStage(id, 4), 4);
74 void testSaveRestoreState() {
75 const WindowStateStorage::WindowState state{WindowStateStorage::WindowState::WindowStateMaximizedTopLeft};
76 storage->saveState(QTest::currentTestFunction(), state);
77 QTRY_COMPARE(storage->getState(QTest::currentTestFunction(), WindowStateStorage::WindowState::WindowStateNormal), state);
80 void testSaveRestoreGeometry() {
81 const QRect geometry{10, 20, 30, 40};
82 storage->saveGeometry(QTest::currentTestFunction(), geometry);
83 QTRY_COMPARE(storage->getGeometry(QTest::currentTestFunction(), QRect()), geometry);
86 void testSaveRestoreStage() {
88 storage->saveStage(QTest::currentTestFunction(), stage);
89 QTRY_COMPARE(storage->getStage(QTest::currentTestFunction(), 0), stage);
92 void testProtectAgainstInvalidGeometry() {
93 const QRect geometry{10, 20, 0, 10}; // zero-width (invalid) rectangle
94 storage->saveGeometry(QTest::currentTestFunction(), geometry);
95 const QRect defaultGeometry{10, 20, 30, 40};
96 const QRect loadedGeometry = storage->getGeometry(QTest::currentTestFunction(), defaultGeometry);
97 // ensure we don't load a broken geometry, instead we fall back to the default one
98 QCOMPARE(loadedGeometry, defaultGeometry);
101 void testProtectAgainstSqlInjection() {
102 QString naughtyAppId = QStringLiteral("stageQuery;DROP TABLE stage");
103 QString naughtyAppIdSingleQuotes = QStringLiteral("stageQuery';DROP TABLE stage");
104 QString naughtyAppIdDoubleQuotes = QStringLiteral("stageQuery\";DROP TABLE stage");
105 QString naughtyAppIdBackslashes = QStringLiteral("stageQuery\\\";DROP TABLE stage");
106 storage->saveStage(naughtyAppId, 1);
107 QTRY_COMPARE(storage->getStage(naughtyAppId, 0), 1);
108 storage->saveStage(naughtyAppIdSingleQuotes, 2);
109 QTRY_COMPARE(storage->getStage(naughtyAppIdSingleQuotes, 0), 2);
110 storage->saveStage(naughtyAppIdDoubleQuotes, 3);
111 QTRY_COMPARE(storage->getStage(naughtyAppIdDoubleQuotes, 0), 3);
112 storage->saveStage(naughtyAppIdBackslashes, 4);
113 QTRY_COMPARE(storage->getStage(naughtyAppIdBackslashes, 0), 4);
117 WindowStateStorage * storage{nullptr};
120QTEST_GUILESS_MAIN(WindowStateStorageTest)
121#include "WindowStateStorageTest.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list