aa7847f33b27c64eb0eea39fb6cdf4a56a2f89dc
[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     MapScene scene;
52 };
53
54 void TestFriendGroupItem::initTestCase()
55 {
56     pixmap = new QPixmap("situare_user.gif");
57 }
58
59 void TestFriendGroupItem::cleanupTestCase()
60 {
61     delete pixmap;
62 }
63
64 void TestFriendGroupItem::init()
65 {
66     // create test friends
67     friend1 = new FriendLocationItem(*pixmap, QPoint(0, 0), this);
68     friend2 = new FriendLocationItem(*pixmap, QPoint(0, 0), this);
69     friend3 = new FriendLocationItem(*pixmap, QPoint(0, 0), this);
70     friend4 = new FriendLocationItem(*pixmap, QPoint(0, 0), this);
71     QVERIFY(friend1 != 0);
72     QVERIFY(friend2 != 0);
73     QVERIFY(friend3 != 0);
74     QVERIFY(friend4 != 0);
75
76     // add friends to scene
77     scene.addItem(friend1);
78     scene.addItem(friend2);
79     scene.addItem(friend3);
80     scene.addItem(friend4);
81
82     // override coordinate positions with scene pixel positions so testing is more
83     // convenient
84     friend1->setPos(100, 100);
85     friend2->setPos(100, 150);
86     friend3->setPos(150, 100);
87     friend4->setPos(150, 150);
88
89     // create test group
90     group = new FriendGroupItem(friend1);
91     QVERIFY(group != 0);
92     QVERIFY(friend1->isPartOfGroup());
93 }
94
95 void TestFriendGroupItem::cleanup()
96 {
97     delete group;
98
99     // remove and delete all items from the scene
100     scene.clear();
101 }
102
103 void TestFriendGroupItem::addFriends()
104 {
105     group->joinFriend(friend2);
106     group->joinFriend(friend3);
107     QCOMPARE(friend2->isPartOfGroup(), true);
108     QCOMPARE(friend3->isPartOfGroup(), true);
109     QCOMPARE(friend4->isPartOfGroup(), false); // not yet part of group
110
111     // group position should still be same as first group members
112     QCOMPARE(group->pos(), QPointF(100, 100));
113 }
114
115 void TestFriendGroupItem::constructor()
116 {
117     // group picture should be set
118     QVERIFY(!group->pixmap().isNull());
119
120     // group position should be same as first group members
121     QCOMPARE(group->pos(), QPointF(100, 100));
122
123     // zValue should be set
124     QCOMPARE(group->zValue(), static_cast<qreal>(FRIEND_LOCATION_ICON_Z_LEVEL));
125
126     // icon offset should be set
127     QCOMPARE(group->offset(), QPointF(-pixmap->width() / 2, -pixmap->height() / 2));
128 }
129
130 void TestFriendGroupItem::dropFriends()
131 {
132     group->joinFriend(friend2);
133     group->joinFriend(friend3);
134     group->joinFriend(friend4);
135
136     QCOMPARE(friend1->isPartOfGroup(), true);
137     QCOMPARE(friend2->isPartOfGroup(), true);
138     QCOMPARE(friend3->isPartOfGroup(), true);
139     QCOMPARE(friend4->isPartOfGroup(), true);
140
141     // move friend4 to overlap one pixel with friend1
142     friend4->setPos(friend4->pos() - QPointF(1, 1));
143
144     // dropping should return false because there should be two friends remaining
145     QVERIFY(group->dropFriends(MAX_MAP_ZOOM_LEVEL) == false);
146
147     // friends 2 & 3 should be dropped
148     QCOMPARE(friend1->isPartOfGroup(), true);
149     QCOMPARE(friend2->isPartOfGroup(), false);
150     QCOMPARE(friend3->isPartOfGroup(), false);
151     QCOMPARE(friend4->isPartOfGroup(), true);
152
153     // move friend4 back to original position
154     friend4->setPos(friend4->pos() + QPointF(1, 1));
155
156     // dropping should return true because there should be no overlapping friends anymore
157     QVERIFY(group->dropFriends(MAX_MAP_ZOOM_LEVEL) == true);
158
159     // no-one should be part of group anymore
160     QCOMPARE(friend1->isPartOfGroup(), false);
161     QCOMPARE(friend2->isPartOfGroup(), false);
162     QCOMPARE(friend3->isPartOfGroup(), false);
163     QCOMPARE(friend4->isPartOfGroup(), false);
164 }
165
166 void TestFriendGroupItem::mergeWithGroup()
167 {
168     group->joinFriend(friend2);
169
170     // create another group
171     FriendGroupItem anotherGroup(friend3);
172     anotherGroup.joinFriend(friend4);
173
174     // move friends to first group
175     anotherGroup.mergeWithGroup(group);
176
177     // no-one should actually be dropped because groups should be empty already
178     // and shoud return true because group shoud be empty
179     QVERIFY(anotherGroup.dropFriends(MAX_MAP_ZOOM_LEVEL) == true);
180
181     // everyone shoul still be part of first group
182     QCOMPARE(friend1->isPartOfGroup(), true);
183     QCOMPARE(friend2->isPartOfGroup(), true);
184     QCOMPARE(friend3->isPartOfGroup(), true);
185     QCOMPARE(friend4->isPartOfGroup(), true);
186 }
187
188 QTEST_MAIN(TestFriendGroupItem)
189
190 #include "testfriendgroupitem.moc"