| 1 |
/* |
| 2 |
* Copyright (C) 2014-2015 Jolla Ltd. <[email protected]> |
| 3 |
* |
| 4 |
* You may use this file under the terms of the BSD license as follows: |
| 5 |
* |
| 6 |
* "Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions are |
| 8 |
* met: |
| 9 |
* * Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* * Redistributions in binary form must reproduce the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer in |
| 13 |
* the documentation and/or other materials provided with the |
| 14 |
* distribution. |
| 15 |
* * Neither the name of Nemo Mobile nor the names of its contributors |
| 16 |
* may be used to endorse or promote products derived from this |
| 17 |
* software without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <QtTest/QTest> |
| 33 |
#include "facebooknotificationsdatabase.h" |
| 34 |
#include "socialsyncinterface.h" |
| 35 |
#include <QtCore/QDebug> |
| 36 |
#include <QtCore/QDir> |
| 37 |
#include <QtCore/QStandardPaths> |
| 38 |
#include <QtSql/QSqlDatabase> |
| 39 |
#include <QtSql/QSqlQuery> |
| 40 |
|
| 41 |
class FacebookNotificationsTest: public QObject |
| 42 |
{ |
| 43 |
Q_OBJECT |
| 44 |
private: |
| 45 |
|
| 46 |
private slots: |
| 47 |
// Perform some cleanups |
| 48 |
// we basically remove the whole ~/.local/share/system/privileged. While it is |
| 49 |
// damaging on a device, on a desktop system it should not be much |
| 50 |
// damaging. |
| 51 |
void initTestCase() |
| 52 |
{ |
| 53 |
QStandardPaths::setTestModeEnabled(true); |
| 54 |
|
| 55 |
QDir dir (PRIVILEGED_DATA_DIR); |
| 56 |
dir.removeRecursively(); |
| 57 |
} |
| 58 |
|
| 59 |
void notifications() |
| 60 |
{ |
| 61 |
QDateTime time1(QDate(2013, 1, 2), QTime(12, 34, 56)); |
| 62 |
QDateTime time2(QDate(2012, 3, 4), QTime(10, 11, 12)); |
| 63 |
QDateTime time3(QDate(2014, 1, 2), QTime(12, 34, 56)); |
| 64 |
|
| 65 |
const QString id1 = QLatin1String("id1"); |
| 66 |
const QString id2 = QLatin1String("id2"); |
| 67 |
const QString id3 = QLatin1String("id3"); |
| 68 |
|
| 69 |
const QString from1 = QLatin1String("from1"); |
| 70 |
const QString from2 = QLatin1String("from2"); |
| 71 |
const QString from3 = QLatin1String("from3"); |
| 72 |
|
| 73 |
const QString to1 = QLatin1String("to1"); |
| 74 |
const QString to2 = QLatin1String("to2"); |
| 75 |
const QString to3 = QLatin1String("to3"); |
| 76 |
|
| 77 |
const QString title1 = QLatin1String("title1"); |
| 78 |
const QString title2 = QLatin1String("title2"); |
| 79 |
const QString title3 = QLatin1String("title3"); |
| 80 |
|
| 81 |
const QString link1 = QLatin1String("link1"); |
| 82 |
const QString link2 = QLatin1String("link2"); |
| 83 |
const QString link3 = QLatin1String("link3"); |
| 84 |
|
| 85 |
const QString app1 = QLatin1String("app1"); |
| 86 |
const QString app2 = QLatin1String("app2"); |
| 87 |
const QString app3 = QLatin1String("app3"); |
| 88 |
|
| 89 |
const QString object1 = QLatin1String("object1"); |
| 90 |
const QString object2 = QLatin1String("object2"); |
| 91 |
const QString object3 = QLatin1String("object3"); |
| 92 |
|
| 93 |
const bool unread1 = true; |
| 94 |
const bool unread2 = false; |
| 95 |
const bool unread3 = false; |
| 96 |
|
| 97 |
int account1 = 1; |
| 98 |
int account2 = 2; |
| 99 |
|
| 100 |
QString clientId = QLatin1String("clientId"); |
| 101 |
|
| 102 |
FacebookNotificationsDatabase database; |
| 103 |
|
| 104 |
database.addFacebookNotification(id1, from1, to1, time1, time1, |
| 105 |
title1, link1, app1, object1, unread1, account1, clientId); |
| 106 |
|
| 107 |
database.addFacebookNotification(id2, from2, to2, time2, time2, |
| 108 |
title2, link2, app2, object2, unread2, account1, clientId); |
| 109 |
|
| 110 |
database.addFacebookNotification(id3, from3, to3, time3, time3, |
| 111 |
title3, link3, app3, object3, unread3, account2, clientId); |
| 112 |
database.sync(); |
| 113 |
database.wait(); |
| 114 |
QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished); |
| 115 |
|
| 116 |
QList<FacebookNotification::ConstPtr> notifications; |
| 117 |
|
| 118 |
notifications = database.notifications(); |
| 119 |
QCOMPARE(notifications.count(), 3); |
| 120 |
|
| 121 |
FacebookNotification::ConstPtr notification; |
| 122 |
do { |
| 123 |
notification = notifications.takeFirst(); |
| 124 |
} while (notification->facebookId() != id1 && notifications.count() > 0); |
| 125 |
|
| 126 |
QCOMPARE(notification->facebookId(), id1); |
| 127 |
QCOMPARE(notification->from(), from1); |
| 128 |
QCOMPARE(notification->to(), to1); |
| 129 |
QCOMPARE(notification->createdTime(), time1); |
| 130 |
QCOMPARE(notification->updatedTime(), time1); |
| 131 |
QCOMPARE(notification->title(), title1); |
| 132 |
QCOMPARE(notification->link(), link1); |
| 133 |
QCOMPARE(notification->application(), app1); |
| 134 |
QCOMPARE(notification->object(), object1); |
| 135 |
QCOMPARE(notification->unread(), unread1); |
| 136 |
QCOMPARE(notification->accountId(), account1); |
| 137 |
QCOMPARE(notification->clientId(), clientId); |
| 138 |
|
| 139 |
QStringList toBeDeleted; |
| 140 |
toBeDeleted.append(id1); |
| 141 |
toBeDeleted.append(id2); |
| 142 |
database.removeNotifications(toBeDeleted); |
| 143 |
database.sync(); |
| 144 |
database.wait(); |
| 145 |
QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished); |
| 146 |
|
| 147 |
notifications = database.notifications(); |
| 148 |
QCOMPARE(notifications.count(), 1); |
| 149 |
|
| 150 |
notification = notifications.takeFirst(); |
| 151 |
QCOMPARE(notification->facebookId(), id3); |
| 152 |
QCOMPARE(notification->from(), from3); |
| 153 |
QCOMPARE(notification->to(), to3); |
| 154 |
QCOMPARE(notification->createdTime(), time3); |
| 155 |
QCOMPARE(notification->updatedTime(), time3); |
| 156 |
QCOMPARE(notification->title(), title3); |
| 157 |
QCOMPARE(notification->link(), link3); |
| 158 |
QCOMPARE(notification->application(), app3); |
| 159 |
QCOMPARE(notification->object(), object3); |
| 160 |
QCOMPARE(notification->unread(), unread3); |
| 161 |
QCOMPARE(notification->accountId(), account2); |
| 162 |
QCOMPARE(notification->clientId(), clientId); |
| 163 |
|
| 164 |
database.removeNotification(id3); |
| 165 |
database.sync(); |
| 166 |
database.wait(); |
| 167 |
QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished); |
| 168 |
|
| 169 |
notifications = database.notifications(); |
| 170 |
QCOMPARE(notifications.count(), 0); |
| 171 |
|
| 172 |
database.addFacebookNotification(id1, from1, to1, time1, time1, |
| 173 |
title1, link1, app1, object1, unread1, account1, clientId); |
| 174 |
|
| 175 |
database.addFacebookNotification(id2, from2, to2, time2, time2, |
| 176 |
title2, link2, app2, object2, unread2, account1, clientId); |
| 177 |
|
| 178 |
database.addFacebookNotification(id3, from3, to3, time3, time3, |
| 179 |
title3, link3, app3, object3, unread3, account2, clientId); |
| 180 |
database.sync(); |
| 181 |
database.wait(); |
| 182 |
QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished); |
| 183 |
|
| 184 |
notifications = database.notifications(); |
| 185 |
QCOMPARE(notifications.count(), 3); |
| 186 |
|
| 187 |
database.removeNotifications(account1); |
| 188 |
database.sync(); |
| 189 |
database.wait(); |
| 190 |
QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished); |
| 191 |
|
| 192 |
notifications = database.notifications(); |
| 193 |
QCOMPARE(notifications.count(), 1); |
| 194 |
|
| 195 |
database.removeNotifications(account2); |
| 196 |
database.sync(); |
| 197 |
database.wait(); |
| 198 |
QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished); |
| 199 |
|
| 200 |
notifications = database.notifications(); |
| 201 |
QCOMPARE(notifications.count(), 0); |
| 202 |
|
| 203 |
database.addFacebookNotification(id1, from1, to1, time1, time1, |
| 204 |
title1, link1, app1, object1, unread1, account1, clientId); |
| 205 |
database.removeNotification(id1); |
| 206 |
database.sync(); |
| 207 |
database.wait(); |
| 208 |
QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished); |
| 209 |
|
| 210 |
notifications = database.notifications(); |
| 211 |
QCOMPARE(notifications.count(), 0); |
| 212 |
|
| 213 |
database.addFacebookNotification(id1, from1, to1, time1, time1, |
| 214 |
title1, link1, app1, object1, unread1, account1, clientId); |
| 215 |
|
| 216 |
database.addFacebookNotification(id2, from2, to2, time2, time2, |
| 217 |
title2, link2, app2, object2, unread2, account1, clientId); |
| 218 |
|
| 219 |
database.addFacebookNotification(id3, from3, to3, time3, time3, |
| 220 |
title3, link3, app3, object3, unread3, account2, clientId); |
| 221 |
database.sync(); |
| 222 |
database.wait(); |
| 223 |
QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished); |
| 224 |
|
| 225 |
notifications = database.notifications(); |
| 226 |
QCOMPARE(notifications.count(), 3); |
| 227 |
|
| 228 |
database.removeAllNotifications(); |
| 229 |
database.wait(); |
| 230 |
|
| 231 |
notifications = database.notifications(); |
| 232 |
QCOMPARE(notifications.count(), 0); |
| 233 |
} |
| 234 |
|
| 235 |
void purgeOldNotifications() |
| 236 |
{ |
| 237 |
QDateTime time1 = QDateTime::currentDateTime(); |
| 238 |
QDateTime time2; |
| 239 |
#if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) |
| 240 |
time2.setSecsSinceEpoch(time1.toSecsSinceEpoch() - 10 * 24 * 60 * 60); |
| 241 |
#else |
| 242 |
time2.setTime_t(time1.toTime_t() - 10 * 24 * 60 * 60); |
| 243 |
#endif |
| 244 |
|
| 245 |
const QString id1 = QLatin1String("id1"); |
| 246 |
const QString id2 = QLatin1String("id2"); |
| 247 |
|
| 248 |
const QString from1 = QLatin1String("from1"); |
| 249 |
const QString from2 = QLatin1String("from2"); |
| 250 |
|
| 251 |
const QString to1 = QLatin1String("to1"); |
| 252 |
const QString to2 = QLatin1String("to2"); |
| 253 |
|
| 254 |
const QString title1 = QLatin1String("title1"); |
| 255 |
const QString title2 = QLatin1String("title2"); |
| 256 |
|
| 257 |
const QString link1 = QLatin1String("link1"); |
| 258 |
const QString link2 = QLatin1String("link2"); |
| 259 |
|
| 260 |
const QString app1 = QLatin1String("app1"); |
| 261 |
const QString app2 = QLatin1String("app2"); |
| 262 |
|
| 263 |
const QString object1 = QLatin1String("object1"); |
| 264 |
const QString object2 = QLatin1String("object2"); |
| 265 |
|
| 266 |
const bool unread1 = true; |
| 267 |
const bool unread2 = false; |
| 268 |
|
| 269 |
int account1 = 1; |
| 270 |
|
| 271 |
QString clientId = QLatin1String("clientId"); |
| 272 |
|
| 273 |
FacebookNotificationsDatabase database; |
| 274 |
|
| 275 |
database.addFacebookNotification(id1, from1, to1, time1, time1, |
| 276 |
title1, link1, app1, object1, unread1, account1, clientId); |
| 277 |
|
| 278 |
database.addFacebookNotification(id2, from2, to2, time2, time2, |
| 279 |
title2, link2, app2, object2, unread2, account1, clientId); |
| 280 |
|
| 281 |
database.sync(); |
| 282 |
database.wait(); |
| 283 |
QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished); |
| 284 |
|
| 285 |
QList<FacebookNotification::ConstPtr> notifications; |
| 286 |
|
| 287 |
notifications = database.notifications(); |
| 288 |
QCOMPARE(notifications.count(), 2); |
| 289 |
|
| 290 |
database.purgeOldNotifications(9); |
| 291 |
database.sync(); |
| 292 |
database.wait(); |
| 293 |
QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished); |
| 294 |
|
| 295 |
notifications = database.notifications(); |
| 296 |
QCOMPARE(notifications.count(), 1); |
| 297 |
|
| 298 |
FacebookNotification::ConstPtr notification = notifications.takeFirst(); |
| 299 |
QCOMPARE(notification->facebookId(), id1); |
| 300 |
} |
| 301 |
|
| 302 |
void cleanupTestCase() |
| 303 |
{ |
| 304 |
// Do the same cleanups |
| 305 |
QDir dir (PRIVILEGED_DATA_DIR); |
| 306 |
dir.removeRecursively(); |
| 307 |
} |
| 308 |
}; |
| 309 |
|
| 310 |
QTEST_MAIN(FacebookNotificationsTest) |
| 311 |
|
| 312 |
#include "main.moc" |