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
|
/*
* Copyright 2014 Canonical Ltd.
*
* This file is part of webbrowser-app.
*
* webbrowser-app 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.
*
* webbrowser-app 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/>.
*/
// Qt
#include <QtCore/QTemporaryFile>
#include <QtTest/QSignalSpy>
#include <QtTest/QtTest>
// local
#include "session-storage.h"
class SessionStorageTests : public QObject
{
Q_OBJECT
private:
SessionStorage* session;
private Q_SLOTS:
void init()
{
session = new SessionStorage;
}
void cleanup()
{
delete session;
}
void shouldNotDoAnythingWithEmptyDataFile()
{
QVERIFY(session->dataFile().isEmpty());
QVERIFY(!session->isLocked());
session->store(QString("foobar")); // should be a no-op
QVERIFY(session->retrieve().isEmpty());
}
void shouldSetDataFile()
{
QSignalSpy dataFileSpy(session, SIGNAL(dataFileChanged()));
QSignalSpy lockedSpy(session, SIGNAL(lockedChanged()));
// Set an invalid session file
QString invalidDataFile("/f00/bAr/session.json");
session->setDataFile(invalidDataFile);
QCOMPARE(session->dataFile(), invalidDataFile);
QCOMPARE(dataFileSpy.count(), 1);
QCOMPARE(lockedSpy.count(), 0);
QVERIFY(!session->isLocked());
dataFileSpy.clear();
// Set a valid session file
QTemporaryFile file;
QVERIFY(file.open());
file.close();
session->setDataFile(file.fileName());
QCOMPARE(session->dataFile(), file.fileName());
QCOMPARE(dataFileSpy.count(), 1);
QCOMPARE(lockedSpy.count(), 1);
QVERIFY(session->isLocked());
dataFileSpy.clear();
lockedSpy.clear();
// Reset the session file
session->setDataFile("");
QCOMPARE(session->dataFile(), QString(""));
QCOMPARE(dataFileSpy.count(), 1);
QCOMPARE(lockedSpy.count(), 1);
QVERIFY(!session->isLocked());
dataFileSpy.clear();
lockedSpy.clear();
}
void shouldStoreAndRetrieveCorrectlyAfterwards()
{
QTemporaryFile file;
QVERIFY(file.open());
file.close();
session->setDataFile(file.fileName());
QVERIFY(session->isLocked());
QString data("f00bAr");
session->store(data);
QCOMPARE(session->retrieve(), data);
delete session;
session = new SessionStorage;
session->setDataFile(file.fileName());
QVERIFY(session->isLocked());
QCOMPARE(session->retrieve(), data);
}
void shouldLockOutSecondInstance()
{
QTemporaryFile file;
QVERIFY(file.open());
file.close();
session->setDataFile(file.fileName());
QVERIFY(session->isLocked());
SessionStorage session2;
session2.setDataFile(session->dataFile());
QVERIFY(!session2.isLocked());
// Verify that the session that held the lock going away
// doesn’t automagically make the next one acquire it
// (this would be undesirable as it would allow the second
// instance to overwrite the first session’s data).
delete session;
session = NULL;
QVERIFY(!session2.isLocked());
}
};
QTEST_MAIN(SessionStorageTests)
#include "tst_SessionStorageTests.moc"
|