| 1 | /* |
| 2 | * Copyright (C) 2013 Canonical Ltd |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 3 as |
| 6 | * published by the Free Software Foundation. |
| 7 | * |
| 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. |
| 12 | * |
| 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/>. |
| 15 | * |
| 16 | * Author : David Planella <[email protected]> |
| 17 | */ |
| 18 | |
| 19 | #include "placesmodel.h" |
| 20 | |
| 21 | #include <QString> |
| 22 | #include <QtTest> |
| 23 | #include <QSettings> |
| 24 | #include <QFile> |
| 25 | #include <QFileInfo> |
| 26 | #include <QTemporaryFile> |
| 27 | #include <QTemporaryDir> |
| 28 | |
| 29 | |
| 30 | class SaveSettings: public QTemporaryFile |
| 31 | { |
| 32 | public: |
| 33 | SaveSettings(QObject *parent = 0) : QTemporaryFile(parent) {} |
| 34 | bool openReadOnly() |
| 35 | { |
| 36 | return open(QFile::ReadOnly); |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | class PlacesmodelTest : public QObject |
| 41 | { |
| 42 | Q_OBJECT |
| 43 | |
| 44 | public: |
| 45 | PlacesmodelTest(); |
| 46 | |
| 47 | private Q_SLOTS: |
| 48 | void init(); |
| 49 | void cleanup(); |
| 50 | void cleanupTestCase(); |
| 51 | void addUserPlace(); |
| 52 | void removeUserPlace(); |
| 53 | void addExistentDefaultPlace(); // should fail |
| 54 | void removeDefaultPlace(); |
| 55 | void addRemovedDefaultPlace(); |
| 56 | private: |
| 57 | SaveSettings *m_saved_settings; |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | PlacesmodelTest::PlacesmodelTest() : m_saved_settings(0) |
| 62 | { |
| 63 | |
| 64 | } |
| 65 | |
| 66 | void PlacesmodelTest::init() |
| 67 | { |
| 68 | PlacesModel places; |
| 69 | QFileInfo saved(places.m_settings->fileName()); |
| 70 | if (saved.exists()) |
| 71 | { |
| 72 | QFile settings(saved.absoluteFilePath()); |
| 73 | QCOMPARE(settings.open(QFile::ReadOnly), true); |
| 74 | m_saved_settings = new SaveSettings(this); |
| 75 | QCOMPARE(m_saved_settings->open(), true); |
| 76 | QByteArray settings_data = settings.readAll(); |
| 77 | QCOMPARE(m_saved_settings->write(settings_data), (qint64)settings_data.size()); |
| 78 | m_saved_settings->close(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void PlacesmodelTest::cleanupTestCase() |
| 83 | { |
| 84 | if (m_saved_settings) |
| 85 | { |
| 86 | QCOMPARE(m_saved_settings->openReadOnly(), true); |
| 87 | PlacesModel places; |
| 88 | QFile saved(places.m_settings->fileName()); |
| 89 | QCOMPARE(saved.open(QFile::WriteOnly | QFile::Truncate), true); |
| 90 | QByteArray saved_data = m_saved_settings->readAll(); |
| 91 | QCOMPARE(saved.write(saved_data), (qint64)saved_data.size()); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void PlacesmodelTest::cleanup() |
| 96 | { |
| 97 | cleanupTestCase(); |
| 98 | } |
| 99 | |
| 100 | void PlacesmodelTest::addUserPlace() |
| 101 | { |
| 102 | QTemporaryDir tempDir; |
| 103 | PlacesModel places; |
| 104 | int locations_counter = places.rowCount(); |
| 105 | QCOMPARE(places.indexOfLocation(tempDir.path()), -1); |
| 106 | places.addLocation(tempDir.path()); |
| 107 | QTest::qWait(50); |
| 108 | QCOMPARE(places.rowCount(), locations_counter + 1); |
| 109 | QVERIFY(places.indexOfLocation(tempDir.path()) != -1); |
| 110 | |
| 111 | //now try to add it again which must fail |
| 112 | places.addLocation(tempDir.path()); |
| 113 | QTest::qWait(50); |
| 114 | QCOMPARE(places.rowCount(), locations_counter + 1); |
| 115 | |
| 116 | //another model instance |
| 117 | PlacesModel places2; |
| 118 | QVERIFY(places2.indexOfLocation(tempDir.path()) != -1); |
| 119 | //added item must be in m_userSavedLocations |
| 120 | QVERIFY(places2.m_userSavedLocations.indexOf(tempDir.path()) != -1); |
| 121 | QCOMPARE(places2.rowCount(), locations_counter + 1); |
| 122 | } |
| 123 | |
| 124 | void PlacesmodelTest::removeUserPlace() |
| 125 | { |
| 126 | // first add a temporary place |
| 127 | QTemporaryDir tempDir; |
| 128 | PlacesModel places; |
| 129 | int locations_counter = places.rowCount(); |
| 130 | QCOMPARE(places.indexOfLocation(tempDir.path()), -1); |
| 131 | places.addLocation(tempDir.path()); |
| 132 | QTest::qWait(50); |
| 133 | QCOMPARE(places.rowCount(), locations_counter + 1); |
| 134 | QVERIFY(places.indexOfLocation(tempDir.path()) != -1); |
| 135 | //then remove it |
| 136 | places.removeItem(tempDir.path()); |
| 137 | QTest::qWait(50); |
| 138 | QCOMPARE(places.rowCount(), locations_counter); |
| 139 | QCOMPARE(places.indexOfLocation(tempDir.path()), -1); |
| 140 | |
| 141 | //another PlacesModel instance |
| 142 | PlacesModel places2; |
| 143 | QCOMPARE(places2.rowCount(), locations_counter); |
| 144 | //item removed is not in the m_locations |
| 145 | QCOMPARE(places2.indexOfLocation(tempDir.path()), -1); |
| 146 | //item removed is in m_userRemovedLocations |
| 147 | QVERIFY(places2.m_userRemovedLocations.indexOf(tempDir.path()) != -1); |
| 148 | } |
| 149 | |
| 150 | void PlacesmodelTest::addExistentDefaultPlace() |
| 151 | { |
| 152 | PlacesModel places; |
| 153 | int home_index = places.indexOfLocation(QDir::homePath()); |
| 154 | QVERIFY(home_index != -1); |
| 155 | int places_counter = places.rowCount(); |
| 156 | places.addLocation(QDir::homePath()); |
| 157 | QTest::qWait(50); |
| 158 | //counter must be the same which indicates nothing was added |
| 159 | QCOMPARE(places.rowCount(), places_counter); |
| 160 | } |
| 161 | |
| 162 | void PlacesmodelTest::removeDefaultPlace() |
| 163 | { |
| 164 | PlacesModel places; |
| 165 | int home_index = places.indexOfLocation(QDir::homePath()); |
| 166 | QVERIFY(home_index != -1); |
| 167 | int places_counter = places.rowCount(); |
| 168 | places.removeItem(home_index); |
| 169 | QTest::qWait(50); |
| 170 | QCOMPARE(places.rowCount(), places_counter - 1); |
| 171 | QCOMPARE(places.indexOfLocation(QDir::homePath()), -1); |
| 172 | |
| 173 | //use another instance to check |
| 174 | PlacesModel places2; |
| 175 | QCOMPARE(places2.rowCount(), places_counter - 1); |
| 176 | QCOMPARE(places2.indexOfLocation(QDir::homePath()), -1); |
| 177 | //default place is in m_userRemovedLocations |
| 178 | QVERIFY(places2.m_userRemovedLocations.indexOf(QDir::homePath()) != -1); |
| 179 | } |
| 180 | |
| 181 | void PlacesmodelTest::addRemovedDefaultPlace() |
| 182 | { |
| 183 | //first remove a default place |
| 184 | PlacesModel places; |
| 185 | int home_index = places.indexOfLocation(QDir::homePath()); |
| 186 | QVERIFY(home_index != -1); |
| 187 | int places_counter = places.rowCount(); |
| 188 | places.removeItem(home_index); |
| 189 | QTest::qWait(50); |
| 190 | QCOMPARE(places.rowCount(), places_counter - 1); |
| 191 | QCOMPARE(places.indexOfLocation(QDir::homePath()), -1); |
| 192 | |
| 193 | //now add the default location back |
| 194 | places.addLocation(QDir::homePath()); |
| 195 | QTest::qWait(50); |
| 196 | QCOMPARE(places.rowCount(), places_counter); |
| 197 | QVERIFY(places.indexOfLocation(QDir::homePath()) != -1); |
| 198 | //it must not exist neither on m_userSavedLocations nor m_userRemovedLocations |
| 199 | QCOMPARE(places.m_userSavedLocations.indexOf(QDir::homePath()), -1); |
| 200 | QCOMPARE(places.m_userRemovedLocations.indexOf(QDir::homePath()), -1); |
| 201 | |
| 202 | //check on a second instance |
| 203 | PlacesModel places2; |
| 204 | QVERIFY(places2.indexOfLocation(QDir::homePath()) != -1); |
| 205 | QCOMPARE(places2.m_userSavedLocations.indexOf(QDir::homePath()), -1); |
| 206 | QCOMPARE(places2.m_userRemovedLocations.indexOf(QDir::homePath()), -1); |
| 207 | } |
| 208 | |
| 209 | QTEST_MAIN(PlacesmodelTest) |
| 210 | |
| 211 | #include "placesmodeltest.moc" |