Debian Codegraph @

tests/tst_facebookpost/main.cpp

raw
1 /*
2 * Copyright (C) 2013 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 "facebookpostsdatabase.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 FacebookPostsTest: 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 posts()
60 {
61 QDateTime time1(QDate(2013, 1, 2), QTime(12, 34, 56));
62 QDateTime time2(QDate(2012, 3, 4), QTime(10, 11, 12));
63
64 const QString id1 = QLatin1String("id1");
65 const QString id2 = QLatin1String("id2");
66 const QString id3 = QLatin1String("id3");
67
68 const QString name1 = QLatin1String("name1");
69 const QString name2 = QLatin1String("name2");
70 const QString name3 = QLatin1String("name3");
71
72 const QString body1 = QLatin1String("body1");
73 const QString body2 = QLatin1String("body2");
74 const QString body3 = QLatin1String("body3");
75
76 const QString icon1 = QLatin1String("/icon.jpg");
77 const QString image1 = QLatin1String("http://example.com/image1.jpg");
78 const QString image2 = QLatin1String("http://example.com/image2.jpg");
79 const QString image3 = QLatin1String("http://example.com/image3.jpg");
80 const QString attachment1 = QLatin1String("attachment1");
81 const QString caption1 = QLatin1String("caption1");
82 const QString description1 = QLatin1String("description1");
83 const QString url1 = QLatin1String("http://example.com/attachment.png");
84 const QString client1 = QLatin1String("client1");
85
86 FacebookPostsDatabase database;
87
88 database.addFacebookPost(
89 id1, name1, body1, time1, icon1,
90 QList<QPair<QString, SocialPostImage::ImageType> >()
91 << qMakePair(image1, SocialPostImage::Photo)
92 << qMakePair(image2, SocialPostImage::Video),
93 attachment1, caption1, description1, url1,
94 true, true, client1, 1);
95 database.addFacebookPost(
96 id1, name1, body1, time1, icon1,
97 QList<QPair<QString, SocialPostImage::ImageType> >()
98 << qMakePair(image1, SocialPostImage::Photo)
99 << qMakePair(image2, SocialPostImage::Video),
100 attachment1, caption1, description1, url1,
101 true, true, client1, 2);
102 database.addFacebookPost(
103 id2, name2, body2, time2, icon1,
104 QList<QPair<QString, SocialPostImage::ImageType> >()
105 << qMakePair(image3, SocialPostImage::Photo),
106 QString(), QString(), QString(), QString(),
107 false, false, client1, 1);
108 database.addFacebookPost(
109 id3, name3, body3, time2, icon1,
110 QList<QPair<QString, SocialPostImage::ImageType> >(),
111 QString(), QString(), QString(), QString(),
112 true, true, client1, 2);
113
114 database.commit();
115 database.wait();
116 QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished);
117
118 database.refresh();
119 database.wait();
120 QCOMPARE(database.readStatus(), AbstractSocialCacheDatabase::Finished);
121
122 QList<SocialPost::ConstPtr> posts;
123
124 posts = database.posts();
125 QCOMPARE(posts.count(), 3);
126
127 SocialPost::ConstPtr post;
128 do {
129 post = posts.takeFirst();
130 } while (post->identifier() != id1 && posts.count() > 0);
131
132 QCOMPARE(post->identifier(), id1);
133 QCOMPARE(post->name(), name1);
134 QCOMPARE(post->body(), body1);
135 QCOMPARE(post->timestamp(), time1);
136 QCOMPARE(post->icon(), icon1);
137 QCOMPARE(post->images().count(), 2);
138 QCOMPARE(FacebookPostsDatabase::attachmentName(post), attachment1);
139 QCOMPARE(FacebookPostsDatabase::attachmentCaption(post), caption1);
140 QCOMPARE(FacebookPostsDatabase::attachmentDescription(post), description1);
141 QCOMPARE(FacebookPostsDatabase::attachmentUrl(post), url1);
142 QCOMPARE(FacebookPostsDatabase::allowLike(post), true);
143 QCOMPARE(FacebookPostsDatabase::allowComment(post), true);
144 QCOMPARE(FacebookPostsDatabase::clientId(post), client1);
145 QCOMPARE(post->accounts().count(), 2);
146
147 database.removePosts(2);
148 database.commit();
149 database.refresh();
150 database.wait();
151 QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished);
152 QCOMPARE(database.readStatus(), AbstractSocialCacheDatabase::Finished);
153
154 posts = database.posts();
155 QCOMPARE(posts.count(), 2);
156
157 database.removePosts(1);
158 database.commit();
159 database.refresh();
160 database.wait();
161 QCOMPARE(database.writeStatus(), AbstractSocialCacheDatabase::Finished);
162 QCOMPARE(database.readStatus(), AbstractSocialCacheDatabase::Finished);
163
164 posts = database.posts();
165 QCOMPARE(posts.count(), 0);
166 }
167
168 void socialPostExtraAccessors()
169 {
170 QVariantMap extra;
171 extra.insert(QLatin1String("string"), QLatin1String("value"));
172 extra.insert(QLatin1String("int"), 42);
173 extra.insert(QLatin1String("bool"), true);
174 extra.insert(QLatin1String("empty"), QString());
175 extra.insert(QLatin1String("null"), QVariant());
176
177 SocialPost::ConstPtr post = SocialPost::create(QLatin1String("id"),
178 QLatin1String("name"),
179 QLatin1String("body"),
180 QDateTime(),
181 QMap<int, SocialPostImage::ConstPtr>(),
182 extra,
183 QList<int>());
184
185 QCOMPARE(post->extraString(QLatin1String("string")), QLatin1String("value"));
186 QCOMPARE(post->extraString(QLatin1String("missing"), QLatin1String("fallback")), QLatin1String("fallback"));
187 QCOMPARE(post->extraString(QStringList() << QLatin1String("missing") << QLatin1String("string")),
188 QLatin1String("value"));
189 QCOMPARE(post->extraString(QStringList() << QLatin1String("empty") << QLatin1String("missing"),
190 QLatin1String("fallback")),
191 QLatin1String("fallback"));
192
193 QCOMPARE(post->extraInt(QLatin1String("int")), 42);
194 QCOMPARE(post->extraInt(QLatin1String("missing"), 7), 7);
195 QCOMPARE(post->extraInt(QStringList() << QLatin1String("missing") << QLatin1String("int")), 42);
196
197 QCOMPARE(post->extraBool(QLatin1String("bool")), true);
198 QCOMPARE(post->extraBool(QLatin1String("missing"), false), false);
199 QCOMPARE(post->extraBool(QStringList() << QLatin1String("missing") << QLatin1String("bool")), true);
200 }
201
202 void cleanupTestCase()
203 {
204 // Do the same cleanups
205 QDir dir (PRIVILEGED_DATA_DIR);
206 dir.removeRecursively();
207
208 }
209 };
210
211 QTEST_MAIN(FacebookPostsTest)
212
213 #include "main.moc"