f579ce46c8700a504f597f60a764c442453037db
[situare] / tests / map / friendgroupitem / testfriendgroupitem.cpp
1 /*
2     Situare - A location system for Facebook
3     Copyright (C) 2010  Ixonos Plc. Authors:
4
5         Sami Rämö - sami.ramo@ixonos.com
6
7     Situare is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     version 2 as published by the Free Software Foundation.
10
11     Situare is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with Situare; if not, write to the Free Software
18     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19     USA.
20 */
21
22 #include <QtTest/QtTest>
23
24 #include "map/mapcommon.h"
25 #include "map/friendgroupitem.h"
26 #include "map/friendlocationitem.h"
27 #include "map/mapscene.h"
28
29 class TestFriendGroupItem: public QObject
30 {
31     Q_OBJECT
32
33 private slots:
34     void initTestCase(); // before first test
35     void cleanupTestCase(); // after last test have been run
36     void init(); // before each test
37     void cleanup(); // after each test
38
39     void addFriends();
40     void constructor();
41     void dropFriends();
42     void mergeWithGroup();
43
44 private:
45     FriendLocationItem *friend1;
46     FriendLocationItem *friend2;
47     FriendLocationItem *friend3;
48     FriendLocationItem *friend4;
49     FriendGroupItem *group;
50     QPixmap *pixmap;
51     QUrl url;
52     MapScene scene;
53 };
54
55 void TestFriendGroupItem::initTestCase()
56 {
57     pixmap = new QPixmap("situare_user.gif");
58     url = QUrl("http://www.something.com/pict.png");
59 }
60
61 void TestFriendGroupItem::cleanupTestCase()
62 {
63     delete pixmap;
64 }
65
66 void TestFriendGroupItem::init()
67 {
68     QString userID("ABC123");
69
70     // create test friends
71     friend1 = new FriendLocationItem(userID, this);
72     friend2 = new FriendLocationItem(userID, this);
73     friend3 = new FriendLocationItem(userID, this);
74     friend4 = new FriendLocationItem(userID, this);
75     QVERIFY(friend1 != 0);
76     QVERIFY(friend2 != 0);
77     QVERIFY(friend3 != 0);
78     QVERIFY(friend4 != 0);
79
80     // set pixmaps
81     friend1->setProfileImage(*pixmap, url);
82     friend2->setProfileImage(*pixmap, url);
83     friend3->setProfileImage(*pixmap, url);
84     friend4->setProfileImage(*pixmap, url);
85
86     // add friends to scene
87     scene.addItem(friend1);
88     scene.addItem(friend2);
89     scene.addItem(friend3);
90     scene.addItem(friend4);
91
92     // override coordinate positions with scene pixel positions so testing is more
93     // convenient
94     friend1->setPos(100, 100);
95     friend2->setPos(100, 150);
96     friend3->setPos(150, 100);
97     friend4->setPos(150, 150);
98
99     // create test group
100     group = new FriendGroupItem(friend1);
101     QVERIFY(group != 0);
102     QVERIFY(friend1->isPartOfGroup());
103 }
104
105 void TestFriendGroupItem::cleanup()
106 {
107     delete group;
108
109     // remove and delete all items from the scene
110     scene.clear();
111 }
112
113 void TestFriendGroupItem::addFriends()
114 {
115     group->joinFriend(friend2);
116     group->joinFriend(friend3);
117     QCOMPARE(friend2->isPartOfGroup(), true);
118     QCOMPARE(friend3->isPartOfGroup(), true);
119     QCOMPARE(friend4->isPartOfGroup(), false); // not yet part of group
120
121     // group position should still be same as first group members
122     QCOMPARE(group->pos(), QPointF(100, 100));
123 }
124
125 void TestFriendGroupItem::constructor()
126 {
127     // group picture should be set
128     QVERIFY(!group->pixmap().isNull());
129
130     // group position should be same as first group members
131     QCOMPARE(group->pos(), QPointF(100, 100));
132
133     // zValue should be set
134     QCOMPARE(group->zValue(), static_cast<qreal>(FRIEND_LOCATION_ICON_Z_LEVEL));
135
136     // icon offset should be set
137     QCOMPARE(group->offset(), QPointF(-pixmap->width() / 2, -pixmap->height() / 2));
138 }
139
140 void TestFriendGroupItem::dropFriends()
141 {
142     group->joinFriend(friend2);
143     group->joinFriend(friend3);
144     group->joinFriend(friend4);
145
146     QCOMPARE(friend1->isPartOfGroup(), true);
147     QCOMPARE(friend2->isPartOfGroup(), true);
148     QCOMPARE(friend3->isPartOfGroup(), true);
149     QCOMPARE(friend4->isPartOfGroup(), true);
150
151     // move friend4 to overlap one pixel with friend1
152     friend4->setPos(friend4->pos() - QPointF(1, 1));
153
154     // dropping should return false because there should be two friends remaining
155     QVERIFY(group->dropFriends(MAX_MAP_ZOOM_LEVEL) == false);
156
157     // friends 2 & 3 should be dropped
158     QCOMPARE(friend1->isPartOfGroup(), true);
159     QCOMPARE(friend2->isPartOfGroup(), false);
160     QCOMPARE(friend3->isPartOfGroup(), false);
161     QCOMPARE(friend4->isPartOfGroup(), true);
162
163     // move friend4 back to original position
164     friend4->setPos(friend4->pos() + QPointF(1, 1));
165
166     // dropping should return true because there should be no overlapping friends anymore
167     QVERIFY(group->dropFriends(MAX_MAP_ZOOM_LEVEL) == true);
168
169     // no-one should be part of group anymore
170     QCOMPARE(friend1->isPartOfGroup(), false);
171     QCOMPARE(friend2->isPartOfGroup(), false);
172     QCOMPARE(friend3->isPartOfGroup(), false);
173     QCOMPARE(friend4->isPartOfGroup(), false);
174 }
175
176 void TestFriendGroupItem::mergeWithGroup()
177 {
178     group->joinFriend(friend2);
179
180     // create another group
181     FriendGroupItem anotherGroup(friend3);
182     anotherGroup.joinFriend(friend4);
183
184     // move friends to first group
185     anotherGroup.mergeWithGroup(group);
186
187     // no-one should actually be dropped because groups should be empty already
188     // and shoud return true because group shoud be empty
189     QVERIFY(anotherGroup.dropFriends(MAX_MAP_ZOOM_LEVEL) == true);
190
191     // everyone shoul still be part of first group
192     QCOMPARE(friend1->isPartOfGroup(), true);
193     QCOMPARE(friend2->isPartOfGroup(), true);
194     QCOMPARE(friend3->isPartOfGroup(), true);
195     QCOMPARE(friend4->isPartOfGroup(), true);
196 }
197
198 QTEST_MAIN(TestFriendGroupItem)
199
200 #include "testfriendgroupitem.moc"