2
Copyright (C) 2008 Holger Hans Peter Freyther
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17
Boston, MA 02110-1301, USA.
20
#include <QtTest/QtTest>
25
#include "qwebframe.h"
26
#include "qwebhistory.h"
29
class tst_QWebHistory : public QObject
35
virtual ~tst_QWebHistory();
40
frame->load(QUrl("qrc:/data/page" + QString::number(nr) + ".html"));
41
waitForLoadFinished.exec();
56
void serialize_1(); //QWebHistory countity
57
void serialize_2(); //QWebHistory index
58
void serialize_3(); //QWebHistoryItem
59
void saveAndRestore_1(); //simple checks saveState and restoreState
60
void saveAndRestore_2(); //bad parameters saveState and restoreState
61
void saveAndRestore_3(); //try use different version
62
void saveAndRestore_crash_1();
63
void saveAndRestore_crash_2();
64
void saveAndRestore_crash_3();
72
QEventLoop waitForLoadFinished; //operation on history are asynchronous!
76
tst_QWebHistory::tst_QWebHistory()
80
tst_QWebHistory::~tst_QWebHistory()
84
void tst_QWebHistory::init()
86
page = new QWebPage(this);
87
frame = page->mainFrame();
88
connect(page, SIGNAL(loadFinished(bool)), &waitForLoadFinished, SLOT(quit()));
90
for (int i = 1;i < 6;i++) {
93
hist = page->history();
97
void tst_QWebHistory::cleanup()
103
* Check QWebHistoryItem::title() method
105
void tst_QWebHistory::title()
107
QCOMPARE(hist->currentItem().title(), QString("page5"));
111
* Check QWebHistory::count() method
113
void tst_QWebHistory::count()
115
QCOMPARE(hist->count(), histsize);
119
* Check QWebHistory::back() method
121
void tst_QWebHistory::back()
123
for (int i = histsize;i > 1;i--) {
124
QCOMPARE(page->mainFrame()->toPlainText(), QString("page") + QString::number(i));
126
waitForLoadFinished.exec();
128
//try one more time (too many). crash test
133
* Check QWebHistory::forward() method
135
void tst_QWebHistory::forward()
138
while (hist->canGoBack()) {
140
waitForLoadFinished.exec();
143
for (int i = 1;i < histsize;i++) {
144
QCOMPARE(page->mainFrame()->toPlainText(), QString("page") + QString::number(i));
146
waitForLoadFinished.exec();
148
//try one more time (too many). crash test
153
* Check QWebHistory::itemAt() method
155
void tst_QWebHistory::itemAt()
157
for (int i = 1;i < histsize;i++) {
158
QCOMPARE(hist->itemAt(i - 1).title(), QString("page") + QString::number(i));
159
QVERIFY(hist->itemAt(i - 1).isValid());
161
//check out of range values
162
QVERIFY(!hist->itemAt(-1).isValid());
163
QVERIFY(!hist->itemAt(histsize).isValid());
167
* Check QWebHistory::goToItem() method
169
void tst_QWebHistory::goToItem()
171
QWebHistoryItem current = hist->currentItem();
173
waitForLoadFinished.exec();
175
waitForLoadFinished.exec();
176
QVERIFY(hist->currentItem().title() != current.title());
177
hist->goToItem(current);
178
waitForLoadFinished.exec();
179
QCOMPARE(hist->currentItem().title(), current.title());
183
* Check QWebHistory::items() method
185
void tst_QWebHistory::items()
187
QList<QWebHistoryItem> items = hist->items();
189
QCOMPARE(histsize, items.count());
192
for (int i = 1;i <= histsize;i++) {
193
QCOMPARE(items.at(i - 1).title(), QString("page") + QString::number(i));
198
* Check history state after serialization (pickle, persistent..) method
199
* Checks history size, history order
201
void tst_QWebHistory::serialize_1()
203
QByteArray tmp; //buffer
204
QDataStream save(&tmp, QIODevice::WriteOnly); //here data will be saved
205
QDataStream load(&tmp, QIODevice::ReadOnly); //from here data will be loaded
208
QVERIFY(save.status() == QDataStream::Ok);
209
QCOMPARE(hist->count(), histsize);
211
//check size of history
212
//load next page to find differences
214
QCOMPARE(hist->count(), histsize + 1);
216
QVERIFY(load.status() == QDataStream::Ok);
217
QCOMPARE(hist->count(), histsize);
219
//check order of historyItems
220
QList<QWebHistoryItem> items = hist->items();
221
for (int i = 1;i <= histsize;i++) {
222
QCOMPARE(items.at(i - 1).title(), QString("page") + QString::number(i));
227
* Check history state after serialization (pickle, persistent..) method
228
* Checks history currentIndex value
230
void tst_QWebHistory::serialize_2()
232
QByteArray tmp; //buffer
233
QDataStream save(&tmp, QIODevice::WriteOnly); //here data will be saved
234
QDataStream load(&tmp, QIODevice::ReadOnly); //from here data will be loaded
236
int oldCurrentIndex = hist->currentItemIndex();
239
waitForLoadFinished.exec();
241
waitForLoadFinished.exec();
242
//check if current index was changed (make sure that it is not last item)
243
QVERIFY(hist->currentItemIndex() != oldCurrentIndex);
245
oldCurrentIndex = hist->currentItemIndex();
248
QVERIFY(save.status() == QDataStream::Ok);
250
QVERIFY(load.status() == QDataStream::Ok);
252
//check current index
253
QCOMPARE(hist->currentItemIndex(), oldCurrentIndex);
257
* Check history state after serialization (pickle, persistent..) method
258
* Checks QWebHistoryItem public property after serialization
260
void tst_QWebHistory::serialize_3()
262
QByteArray tmp; //buffer
263
QDataStream save(&tmp, QIODevice::WriteOnly); //here data will be saved
264
QDataStream load(&tmp, QIODevice::ReadOnly); //from here data will be loaded
266
//prepare two different history items
267
QWebHistoryItem a = hist->currentItem();
268
a.setUserData("A - user data");
270
//check properties BEFORE serialization
271
QString title(a.title());
272
QDateTime lastVisited(a.lastVisited());
273
QUrl originalUrl(a.originalUrl());
275
QVariant userData(a.userData());
278
QVERIFY(save.status() == QDataStream::Ok);
279
QVERIFY(!load.atEnd());
281
QVERIFY(hist->count() == 1);
283
QVERIFY(load.status() == QDataStream::Ok);
284
QWebHistoryItem b = hist->currentItem();
286
//check properties AFTER serialization
287
QCOMPARE(b.title(), title);
288
QCOMPARE(b.lastVisited(), lastVisited);
289
QCOMPARE(b.originalUrl(), originalUrl);
290
QCOMPARE(b.url(), url);
291
QCOMPARE(b.userData(), userData);
293
//Check if all data was read
294
QVERIFY(load.atEnd());
297
/** Simple checks should be a bit redundant to streaming operators */
298
void tst_QWebHistory::saveAndRestore_1()
300
QAction* actionBack = page->action(QWebPage::Back);
302
waitForLoadFinished.exec();
303
QVERIFY(actionBack->isEnabled());
304
QByteArray buffer(hist->saveState());
306
QVERIFY(!actionBack->isEnabled());
307
QVERIFY(hist->count() == 1);
308
hist->restoreState(buffer);
310
//check only few values, do not make full test
311
//because most of the code is shared with streaming operators
312
//and these are checked before
313
QCOMPARE(hist->count(), histsize);
314
QCOMPARE(hist->currentItemIndex(), histsize - 2);
315
QCOMPARE(hist->itemAt(0).title(), QString("page1"));
316
QCOMPARE(hist->itemAt(histsize - 1).title(), QString("page") + QString::number(histsize));
317
QVERIFY(actionBack->isEnabled());
320
/** Check returns value if there are bad parameters. Actually, result
321
* is no so importent. The test shouldn't crash :-) */
322
void tst_QWebHistory::saveAndRestore_2()
325
hist->restoreState(buffer);
326
QVERIFY(hist->count() == 1);
327
QVERIFY(hist->itemAt(0).isValid());
330
/** Try to use bad version value */
331
void tst_QWebHistory::saveAndRestore_3()
333
QByteArray tmp = hist->saveState((QWebHistory::HistoryStateVersion)29999);
334
QVERIFY(hist->saveState((QWebHistory::HistoryStateVersion)29999).isEmpty());
335
QVERIFY(hist->count() == histsize);
336
QVERIFY(hist->itemAt(3).isValid());
339
/** The test shouldn't crash */
340
void tst_QWebHistory::saveAndRestore_crash_1()
342
QByteArray tmp = hist->saveState();
343
for (unsigned i = 0; i < 5; i++){
344
hist->restoreState(tmp);
349
/** The test shouldn't crash */
350
void tst_QWebHistory::saveAndRestore_crash_2()
352
QByteArray tmp = hist->saveState();
353
QWebPage* page2 = new QWebPage(this);
354
QWebHistory* hist2 = page2->history();
355
for (unsigned i = 0; i < 5; i++){
356
hist2->restoreState(tmp);
362
/** The test shouldn't crash */
363
void tst_QWebHistory::saveAndRestore_crash_3()
365
QByteArray tmp = hist->saveState();
366
QWebPage* page2 = new QWebPage(this);
367
QWebHistory* hist1 = hist;
368
QWebHistory* hist2 = page2->history();
369
for (unsigned i = 0; i < 5; i++){
370
hist1->restoreState(tmp);
371
hist2->restoreState(tmp);
372
QVERIFY(hist1->count() == hist2->count());
373
QVERIFY(hist1->count() == histsize);
375
tmp = hist2->saveState();
382
void tst_QWebHistory::clear()
384
QAction* actionBack = page->action(QWebPage::Back);
385
QVERIFY(actionBack->isEnabled());
387
QVERIFY(hist->count() > 1);
389
QVERIFY(hist->count() == 1); // Leave current item.
390
QVERIFY(!actionBack->isEnabled());
393
QWebPage* page2 = new QWebPage(this);
394
QWebHistory* hist2 = page2->history();
395
QVERIFY(hist2->count() == 0);
397
QVERIFY(hist2->count() == 0); // Do not change anything.
401
QTEST_MAIN(tst_QWebHistory)
402
#include "tst_qwebhistory.moc"