Unit tests re-factoring for new coordinates on-going...
[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 #include <QGraphicsScene>
24
25 #include "map/mapcommon.h"
26 #include "map/friendgroupitem.h"
27 #include "map/friendlocationitem.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
43 private:
44     FriendLocationItem *friend1;
45     FriendLocationItem *friend2;
46     FriendLocationItem *friend3;
47     FriendLocationItem *friend4;
48     FriendGroupItem *group;
49     QPixmap *pixmap;
50     QUrl url;
51     QGraphicsScene scene;
52 };
53
54 void TestFriendGroupItem::initTestCase()
55 {
56     pixmap = new QPixmap("friend_group.png");
57     url = QUrl("http://www.something.com/pict.png");
58 }
59
60 void TestFriendGroupItem::cleanupTestCase()
61 {
62     delete pixmap;
63 }
64
65 void TestFriendGroupItem::init()
66 {
67     QString userID("ABC123");
68
69     // create test friends
70     friend1 = new FriendLocationItem(userID, this);
71     friend2 = new FriendLocationItem(userID, this);
72     friend3 = new FriendLocationItem(userID, this);
73     friend4 = new FriendLocationItem(userID, this);
74     QVERIFY(friend1 != 0);
75     QVERIFY(friend2 != 0);
76     QVERIFY(friend3 != 0);
77     QVERIFY(friend4 != 0);
78
79     // set pixmaps
80     friend1->setProfileImage(*pixmap, url);
81     friend2->setProfileImage(*pixmap, url);
82     friend3->setProfileImage(*pixmap, url);
83     friend4->setProfileImage(*pixmap, url);
84
85     // add friends to scene
86     scene.addItem(friend1);
87     scene.addItem(friend2);
88     scene.addItem(friend3);
89     scene.addItem(friend4);
90
91     // override coordinate positions with scene pixel positions so testing is more
92     // convenient
93     const int ITEM_SIDE = 58; // defined in baselocationitem.cpp
94     friend1->setPos(100, 100);
95     friend2->setPos(friend1->pos() + QPoint(0, ITEM_SIDE)); // east from friend1
96     friend3->setPos(friend1->pos() + QPoint(ITEM_SIDE, 0)); // south from friend1
97     friend4->setPos(friend1->pos() + QPoint(ITEM_SIDE, ITEM_SIDE)); // south-east from friend1
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     group->dropFriends();
152
153     // no-one should be part of group anymore
154     QCOMPARE(friend1->isPartOfGroup(), false);
155     QCOMPARE(friend2->isPartOfGroup(), false);
156     QCOMPARE(friend3->isPartOfGroup(), false);
157     QCOMPARE(friend4->isPartOfGroup(), false);
158 }
159
160 QTEST_MAIN(TestFriendGroupItem)
161
162 #include "testfriendgroupitem.moc"