Modified ListView tests.
[situare] / tests / ui / listview / testlistview.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@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>
23 #include <QtGui>
24
25 #include "../../../src/ui/friendlistitem.h"
26 #include "../../../src/ui/listview.h"
27
28 class TestListView: public QObject
29 {
30     Q_OBJECT
31
32 private slots:
33     void cleanupTestCase();
34     void initTestCase();
35     void addListItem();
36     void addItemToView();
37     void clearUnused();
38     void filterList();
39     void takeListItemFromView();
40
41 private:
42     ListView *listView;
43 };
44
45 void TestListView::cleanupTestCase()
46 {
47     delete listView;
48 }
49
50 void TestListView::initTestCase()
51 {
52     listView = new ListView();
53
54     QVERIFY(listView != 0);
55 }
56
57 void TestListView::addListItem()
58 {
59     listView->addListItem("key1", new FriendListItem());
60     QCOMPARE(listView->count(), 1);
61
62     listView->addListItem("key1", new FriendListItem());
63     QCOMPARE(listView->count(), 1);
64
65     listView->addListItem("key2", new FriendListItem());
66     QCOMPARE(listView->count(), 2);
67 }
68
69 void TestListView::addItemToView()
70 {
71     listView->addListItemToView(new FriendListItem());
72     QCOMPARE(listView->count(), 3);
73
74     listView->addListItemToView(new FriendListItem());
75     QCOMPARE(listView->count(), 4);
76
77     listView->clearList();
78     QCOMPARE(listView->count(), 0);
79 }
80
81 void TestListView::clearUnused()
82 {
83 //    listView->addListItem("key1", new FriendListItem());
84 //    listView->addListItem("key2", new FriendListItem());
85 //    listView->addListItem("key3", new FriendListItem());
86 //    listView->addListItem("key4", new FriendListItem());
87 //    QCOMPARE(listView->count(), 4);
88
89 //    QStringList newUserIDs;
90 //    newUserIDs.append("key2");
91 //    newUserIDs.append("key3");
92 //    listView->clearUnused(newUserIDs);
93 //    QCOMPARE(listView->count(), 2);
94 //    QVERIFY(listView->listItem("key1") == 0);
95 //    QVERIFY(listView->listItem("key2") != 0);
96 //    QVERIFY(listView->listItem("key3") != 0);
97 //    QVERIFY(listView->listItem("key4") == 0);
98 }
99
100 void TestListView::filterList()
101 {
102     listView->clear();
103     QCOMPARE(listView->count(), 0);
104
105     FriendListItem *item1 = new FriendListItem();
106     item1->setId("key1");
107     FriendListItem *item2 = new FriendListItem();
108     item2->setId("key2");
109     FriendListItem *item3 = new FriendListItem();
110     item3->setId("key3");
111     FriendListItem *item4 = new FriendListItem();
112     item4->setId("key4");
113     listView->addListItem("key1", item1);
114     listView->addListItem("key2", item2);
115     listView->addListItem("key3", item3);
116     listView->addListItem("key4", item4);
117     QCOMPARE(listView->count(), 4);
118
119     QList<QString> userIDs;
120     userIDs.append("key1");
121     listView->filter(userIDs);
122     QCOMPARE(listView->count(), 4);
123
124     int hiddenCounter = 0;
125
126     for (int i = 0; i < listView->count(); ++i) {
127         if (listView->item(i)->isHidden())
128             hiddenCounter++;
129     }
130
131     QCOMPARE(hiddenCounter, 3);
132
133     userIDs.append("key2");
134     listView->filter(userIDs);
135     QCOMPARE(listView->count(), 4);
136     hiddenCounter = 0;
137
138     for (int i = 0; i < listView->count(); ++i) {
139         if (listView->item(i)->isHidden())
140             hiddenCounter++;
141     }
142
143     QCOMPARE(hiddenCounter, 2);
144
145     listView->clearFilter();
146
147     hiddenCounter = 0;
148
149     for (int i = 0; i < listView->count(); ++i) {
150         if (listView->item(i)->isHidden())
151             hiddenCounter++;
152     }
153
154     QCOMPARE(hiddenCounter, 0);
155 }
156
157 void TestListView::takeListItemFromView()
158 {
159     QCOMPARE(listView->count(), 4);
160
161     ListItem *item = listView->takeListItemFromView("key3");
162     QVERIFY(item != 0);
163     QCOMPARE(listView->count(), 3);
164     QCOMPARE(item->id(), QString("key3"));
165
166     listView->addListItemToView(item);
167     QCOMPARE(listView->count(), 4);
168 }
169
170 QTEST_MAIN(TestListView)
171 #include "testlistview.moc"