/
mem_eventmodel.cpp
242 lines (183 loc) · 7.08 KB
/
mem_eventmodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/******************************************************************************
**
** This file is part of libcommhistory.
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Reto Zingg <reto.zingg@nokia.com>
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License version 2.1 as
** published by the Free Software Foundation.
**
** This library is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
** or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
** License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this library; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**
******************************************************************************/
#include <QtTest/QtTest>
#include <QTime>
#include <time.h>
#include <malloc.h>
#include "eventmodel.h"
#include "callmodel.h"
#include "common.h"
#include "mem_eventmodel.h"
using namespace CommHistory;
Group group;
#define WAIT_TIMEOUT 30000
#define CALM_TIMEOUT 500
#define MALLINFO_DUMP(s) {struct mallinfo m = mallinfo();qDebug() << "MALLINFO" << (s) << m.arena << m.uordblks << m.fordblks;}
static void waitWithDeletes(int msec)
{
QElapsedTimer timer;
timer.start();
while (timer.elapsed() < msec) {
QCoreApplication::sendPostedEvents();
QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete);
QCoreApplication::processEvents();
}
}
void MemEventModelTest::initTestCase()
{
initTestDatabase();
MALLINFO_DUMP("INIT");
addTestGroup(group, "/org/freedesktop/Telepathy/Account/gabble/jabber/dut_40localhost0", "td@localhost");
QTest::qWait(CALM_TIMEOUT);
MALLINFO_DUMP("GROUP ADDED");
}
void MemEventModelTest::addEvent()
{
MALLINFO_DUMP("start");
EventModel *model = new EventModel();
MALLINFO_DUMP("model ready");
Event e1;
e1.setGroupId(group.id());
e1.setType(Event::IMEvent);
e1.setDirection(Event::Outbound);
e1.setStartTime(QDateTime::fromString("2010-01-08T13:37:00Z", Qt::ISODate));
e1.setEndTime(QDateTime::fromString("2010-01-08T13:37:00Z", Qt::ISODate));
e1.setLocalUid("/org/freedesktop/Telepathy/Account/gabble/jabber/dut_40localhost0");
e1.setRecipients(Recipient(e1.localUid(), "td@localhost"));
e1.setFreeText("addEvents 1");
QSignalSpy eventsCommitted(model, SIGNAL(eventsCommitted(const QList<CommHistory::Event>&, bool)));
QVERIFY(model->addEvent(e1));
QVERIFY(waitSignal(eventsCommitted, WAIT_TIMEOUT));
QTest::qWait(CALM_TIMEOUT);
MALLINFO_DUMP("query done");
delete model;
MALLINFO_DUMP("done");
}
void MemEventModelTest::addEvents()
{
MALLINFO_DUMP("start");
MALLINFO_DUMP("model ready");
int mem=0;
int lastMem=0;
for (int i = 0; i < 100; i++) {
EventModel *model = new EventModel();
Event e1;
e1.setGroupId(group.id());
e1.setType(Event::IMEvent);
e1.setDirection(Event::Outbound);
e1.setStartTime(QDateTime::fromString("2010-01-08T13:37:00Z", Qt::ISODate));
e1.setEndTime(QDateTime::fromString("2010-01-08T13:37:00Z", Qt::ISODate));
e1.setLocalUid("/org/freedesktop/Telepathy/Account/gabble/jabber/dut_40localhost0");
e1.setRecipients(Recipient(e1.localUid(), "td@localhost"));
e1.setFreeText(QString("addEvents %1").arg(i));
QSignalSpy eventsCommitted(model, SIGNAL(eventsCommitted(const QList<CommHistory::Event>&, bool)));
QVERIFY(model->addEvent(e1));
QVERIFY(waitSignal(eventsCommitted, WAIT_TIMEOUT));
waitWithDeletes(100);
delete model;
waitWithDeletes(100);
MALLINFO_DUMP("query done");
struct mallinfo m = mallinfo();
if (i >= 50)
mem += m.uordblks - lastMem;
lastMem = m.uordblks;
}
QTest::qWait(CALM_TIMEOUT);
MALLINFO_DUMP("ALL DONE");
qDebug() << "AVG LEAK" << mem/50;
MALLINFO_DUMP("done");
}
void MemEventModelTest::modifyEvent()
{
EventModel *model = new EventModel();
Event im;
im.setType(Event::IMEvent);
im.setDirection(Event::Outbound);
im.setGroupId(group.id());
im.setStartTime(QDateTime::fromString("2009-08-26T09:37:47Z", Qt::ISODate));
im.setEndTime(QDateTime::fromString("2009-08-26T09:37:47Z", Qt::ISODate));
im.setLocalUid("/org/freedesktop/Telepathy/Account/gabble/jabber/dut_40localhost0");
im.setRecipients(Recipient(im.localUid(), "td@localhost"));
im.setFreeText("imtest");
QSignalSpy eventsCommitted(model, SIGNAL(eventsCommitted(const QList<CommHistory::Event>&, bool)));
QVERIFY(model->addEvent(im));
QVERIFY(waitSignal(eventsCommitted, WAIT_TIMEOUT));
im.resetModifiedProperties();
im.setFreeText("imtest \"q\" modified\t tabs");
im.setStartTime(QDateTime::currentDateTime());
im.setEndTime(QDateTime::currentDateTime());
im.setIsRead(false);
// should we actually test more properties?
QVERIFY(model->modifyEvent(im));
QVERIFY(waitSignal(eventsCommitted, WAIT_TIMEOUT));
QTest::qWait(CALM_TIMEOUT);
delete model;
}
void MemEventModelTest::deleteEvent()
{
EventModel *model = new EventModel();
Event event;
event.setType(Event::IMEvent);
event.setDirection(Event::Inbound);
event.setGroupId(group.id());
event.setStartTime(QDateTime::fromString("2009-08-26T09:37:47Z", Qt::ISODate));
event.setEndTime(QDateTime::fromString("2009-08-26T09:37:47Z", Qt::ISODate));
event.setLocalUid("/org/freedesktop/Telepathy/Account/gabble/jabber/dut_40localhost0");
event.setRecipients(Recipient(event.localUid(), "td@localhost"));
event.setFreeText("deletetest");
QSignalSpy eventsCommitted(model, SIGNAL(eventsCommitted(const QList<CommHistory::Event>&, bool)));
QVERIFY(model->addEvent(event));
QVERIFY(waitSignal(eventsCommitted, WAIT_TIMEOUT));
QVERIFY(model->deleteEvent(event));
QVERIFY(waitSignal(eventsCommitted, WAIT_TIMEOUT));
QTest::qWait(CALM_TIMEOUT);
delete model;
}
void MemEventModelTest::callSetFilter()
{
MALLINFO_DUMP("start");
CallModel *model = new CallModel();
model->setResolveContacts(EventModel::DoNotResolve);
QSignalSpy ready(model, SIGNAL(modelReady(bool)));
model->getEvents();
int expectedReadyCount = 1;
QTRY_COMPARE(ready.count(), expectedReadyCount);
expectedReadyCount++;
for(int i = 0; i < 5; i++) {
if (i&1)
model->setFilter(CallModel::SortByTime, CommHistory::CallEvent::MissedCallType);
else
model->setFilter(CallModel::SortByContact, CommHistory::CallEvent::UnknownCallType);
QTRY_COMPARE(ready.count(), expectedReadyCount);
expectedReadyCount++;
MALLINFO_DUMP("get");
}
delete model;
MALLINFO_DUMP("del");
QTest::qWait(CALM_TIMEOUT);
MALLINFO_DUMP("don");
}
void MemEventModelTest::cleanupTestCase()
{
MALLINFO_DUMP("CLEANUP");
}
QTEST_MAIN(MemEventModelTest)