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
|
/*
* Copyright (C) 2015 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 <QtTest/QtTest>
#include <QString>
#include <QSignalSpy>
#include "storagemonitor.h"
#include "storageinfocontrol.h"
class tst_StorageMonitor : public QObject
{
Q_OBJECT
private slots:
void init();
void cleanup();
void testThresholds();
void testValid();
void testReady();
private:
StorageMonitor *m_storage;
StorageInfoControl *m_control;
};
void tst_StorageMonitor::init()
{
m_storage = new StorageMonitor();
m_control = StorageInfoControl::instance();
}
void tst_StorageMonitor::cleanup()
{
delete m_storage;
}
void tst_StorageMonitor::testThresholds()
{
QSignalSpy spyLow(m_storage, SIGNAL(diskSpaceLowChanged()));
QSignalSpy spyCritical(m_storage, SIGNAL(diskSpaceCriticallyLowChanged()));
QString path = "/tmp/ok";
// before setting a path, we should get no disk space warnings
QCOMPARE(m_storage->diskSpaceLow(), false);
QCOMPARE(m_storage->diskSpaceCriticallyLow(), false);
m_control->freeSpaceMap.insert(path, LOW_SPACE_THRESHOLD + 1);
m_storage->setLocation(path);
QCOMPARE(m_storage->diskSpaceLow(), false);
QCOMPARE(m_storage->diskSpaceCriticallyLow(), false);
// drop below first threshold
m_control->freeSpaceMap.insert(path, LOW_SPACE_THRESHOLD - 1);
spyLow.wait(POLL_INTERVAL * 1.5);
spyCritical.wait(POLL_INTERVAL * 1.5);
QCOMPARE(spyLow.count(), 1);
QCOMPARE(spyCritical.count(), 0);
QCOMPARE(m_storage->diskSpaceLow(), true);
QCOMPARE(m_storage->diskSpaceCriticallyLow(), false);
// drop below first threshold
m_control->freeSpaceMap.insert(path, CRITICALLY_LOW_SPACE_THRESHOLD - 1);
spyLow.wait(POLL_INTERVAL);
spyCritical.wait(POLL_INTERVAL);
QCOMPARE(spyLow.count(), 1);
QCOMPARE(spyCritical.count(), 1);
QCOMPARE(m_storage->diskSpaceLow(), true);
QCOMPARE(m_storage->diskSpaceCriticallyLow(), true);
// pull up to full free space again
m_control->freeSpaceMap.insert(path, LOW_SPACE_THRESHOLD + MEGABYTE);
spyLow.wait(POLL_INTERVAL);
spyCritical.wait(POLL_INTERVAL);
QCOMPARE(spyLow.count(), 2);
QCOMPARE(spyCritical.count(), 2);
QCOMPARE(m_storage->diskSpaceLow(), false);
QCOMPARE(m_storage->diskSpaceCriticallyLow(), false);
}
void tst_StorageMonitor::testValid()
{
QSignalSpy spyLow(m_storage, SIGNAL(diskSpaceLowChanged()));
QSignalSpy spyCritical(m_storage, SIGNAL(diskSpaceCriticallyLowChanged()));
QString path = "/tmp/invalid";
// before setting a path, we should get no disk space warnings
QCOMPARE(m_storage->diskSpaceLow(), false);
QCOMPARE(m_storage->diskSpaceCriticallyLow(), false);
m_control->freeSpaceMap.insert(path, LOW_SPACE_THRESHOLD + 1);
m_storage->setLocation(path);
QCOMPARE(m_storage->diskSpaceLow(), false);
QCOMPARE(m_storage->diskSpaceCriticallyLow(), false);
// make the disk ready and change the space, and verify that no changes are
// emitted (the disk is still invalid)
m_control->readyMap.insert(path, true);
m_control->freeSpaceMap.insert(path, LOW_SPACE_THRESHOLD - 1);
spyLow.wait(POLL_INTERVAL);
spyCritical.wait(POLL_INTERVAL);
QCOMPARE(spyLow.count(), 0);
QCOMPARE(spyCritical.count(), 0);
}
void tst_StorageMonitor::testReady()
{
QSignalSpy spyLow(m_storage, SIGNAL(diskSpaceLowChanged()));
QSignalSpy spyCritical(m_storage, SIGNAL(diskSpaceCriticallyLowChanged()));
QString path = "/tmp/unmounted";
// before setting a path, we should get no disk space warnings
QCOMPARE(m_storage->diskSpaceLow(), false);
QCOMPARE(m_storage->diskSpaceCriticallyLow(), false);
m_control->freeSpaceMap.insert(path, LOW_SPACE_THRESHOLD + 1);
m_storage->setLocation(path);
QCOMPARE(m_storage->diskSpaceLow(), false);
QCOMPARE(m_storage->diskSpaceCriticallyLow(), false);
// change the free space, and verify that no changes are
// emitted (the disk is still not ready)
m_control->freeSpaceMap.insert(path, LOW_SPACE_THRESHOLD - 1);
spyLow.wait(POLL_INTERVAL);
spyCritical.wait(POLL_INTERVAL);
QCOMPARE(spyLow.count(), 0);
QCOMPARE(spyCritical.count(), 0);
// now make the disk ready and verify that we actually get a warning
m_control->readyMap.insert(path, true);
spyLow.wait(POLL_INTERVAL);
QCOMPARE(spyLow.count(), 1);
}
QTEST_MAIN(tst_StorageMonitor);
#include "tst_storagemonitor.moc"
|