~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/webkit/WebKit/qt/tests/qwebhistory/tst_qwebhistory.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2008 Holger Hans Peter Freyther
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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.
 
18
*/
 
19
 
 
20
#include <QtTest/QtTest>
 
21
#include <QAction>
 
22
 
 
23
#include "qwebpage.h"
 
24
#include "qwebview.h"
 
25
#include "qwebframe.h"
 
26
#include "qwebhistory.h"
 
27
#include "qdebug.h"
 
28
 
 
29
class tst_QWebHistory : public QObject
 
30
{
 
31
    Q_OBJECT
 
32
 
 
33
public:
 
34
    tst_QWebHistory();
 
35
    virtual ~tst_QWebHistory();
 
36
 
 
37
protected :
 
38
    void loadPage(int nr)
 
39
    {
 
40
        frame->load(QUrl("qrc:/data/page" + QString::number(nr) + ".html"));
 
41
        waitForLoadFinished.exec();
 
42
    }
 
43
 
 
44
public slots:
 
45
    void init();
 
46
    void cleanup();
 
47
 
 
48
private slots:
 
49
    void title();
 
50
    void count();
 
51
    void back();
 
52
    void forward();
 
53
    void itemAt();
 
54
    void goToItem();
 
55
    void items();
 
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();
 
65
    void clear();
 
66
 
 
67
 
 
68
private:
 
69
    QWebPage* page;
 
70
    QWebFrame* frame;
 
71
    QWebHistory* hist;
 
72
    QEventLoop waitForLoadFinished;  //operation on history are asynchronous!
 
73
    int histsize;
 
74
};
 
75
 
 
76
tst_QWebHistory::tst_QWebHistory()
 
77
{
 
78
}
 
79
 
 
80
tst_QWebHistory::~tst_QWebHistory()
 
81
{
 
82
}
 
83
 
 
84
void tst_QWebHistory::init()
 
85
{
 
86
    page = new QWebPage(this);
 
87
    frame = page->mainFrame();
 
88
    connect(page, SIGNAL(loadFinished(bool)), &waitForLoadFinished, SLOT(quit()));
 
89
 
 
90
    for (int i = 1;i < 6;i++) {
 
91
        loadPage(i);
 
92
    }
 
93
    hist = page->history();
 
94
    histsize = 5;
 
95
}
 
96
 
 
97
void tst_QWebHistory::cleanup()
 
98
{
 
99
    delete page;
 
100
}
 
101
 
 
102
/**
 
103
  * Check QWebHistoryItem::title() method
 
104
  */
 
105
void tst_QWebHistory::title()
 
106
{
 
107
    QCOMPARE(hist->currentItem().title(), QString("page5"));
 
108
}
 
109
 
 
110
/**
 
111
  * Check QWebHistory::count() method
 
112
  */
 
113
void tst_QWebHistory::count()
 
114
{
 
115
    QCOMPARE(hist->count(), histsize);
 
116
}
 
117
 
 
118
/**
 
119
  * Check QWebHistory::back() method
 
120
  */
 
121
void tst_QWebHistory::back()
 
122
{
 
123
    for (int i = histsize;i > 1;i--) {
 
124
        QCOMPARE(page->mainFrame()->toPlainText(), QString("page") + QString::number(i));
 
125
        hist->back();
 
126
        waitForLoadFinished.exec();
 
127
    }
 
128
    //try one more time (too many). crash test
 
129
    hist->back();
 
130
}
 
131
 
 
132
/**
 
133
  * Check QWebHistory::forward() method
 
134
  */
 
135
void tst_QWebHistory::forward()
 
136
{
 
137
    //rewind history :-)
 
138
    while (hist->canGoBack()) {
 
139
        hist->back();
 
140
        waitForLoadFinished.exec();
 
141
    }
 
142
 
 
143
    for (int i = 1;i < histsize;i++) {
 
144
        QCOMPARE(page->mainFrame()->toPlainText(), QString("page") + QString::number(i));
 
145
        hist->forward();
 
146
        waitForLoadFinished.exec();
 
147
    }
 
148
    //try one more time (too many). crash test
 
149
    hist->forward();
 
150
}
 
151
 
 
152
/**
 
153
  * Check QWebHistory::itemAt() method
 
154
  */
 
155
void tst_QWebHistory::itemAt()
 
156
{
 
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());
 
160
    }
 
161
    //check out of range values
 
162
    QVERIFY(!hist->itemAt(-1).isValid());
 
163
    QVERIFY(!hist->itemAt(histsize).isValid());
 
164
}
 
165
 
 
166
/**
 
167
  * Check QWebHistory::goToItem() method
 
168
  */
 
169
void tst_QWebHistory::goToItem()
 
170
{
 
171
    QWebHistoryItem current = hist->currentItem();
 
172
    hist->back();
 
173
    waitForLoadFinished.exec();
 
174
    hist->back();
 
175
    waitForLoadFinished.exec();
 
176
    QVERIFY(hist->currentItem().title() != current.title());
 
177
    hist->goToItem(current);
 
178
    waitForLoadFinished.exec();
 
179
    QCOMPARE(hist->currentItem().title(), current.title());
 
180
}
 
181
 
 
182
/**
 
183
  * Check QWebHistory::items() method
 
184
  */
 
185
void tst_QWebHistory::items() 
 
186
{
 
187
    QList<QWebHistoryItem> items = hist->items();
 
188
    //check count
 
189
    QCOMPARE(histsize, items.count());
 
190
 
 
191
    //check order
 
192
    for (int i = 1;i <= histsize;i++) {
 
193
        QCOMPARE(items.at(i - 1).title(), QString("page") + QString::number(i));
 
194
    }
 
195
}
 
196
 
 
197
/**
 
198
  * Check history state after serialization (pickle, persistent..) method
 
199
  * Checks history size, history order
 
200
  */
 
201
void tst_QWebHistory::serialize_1() 
 
202
{
 
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
 
206
 
 
207
    save << *hist;
 
208
    QVERIFY(save.status() == QDataStream::Ok);
 
209
    QCOMPARE(hist->count(), histsize);
 
210
 
 
211
    //check size of history
 
212
    //load next page to find differences
 
213
    loadPage(6);
 
214
    QCOMPARE(hist->count(), histsize + 1);
 
215
    load >> *hist;
 
216
    QVERIFY(load.status() == QDataStream::Ok);
 
217
    QCOMPARE(hist->count(), histsize);
 
218
 
 
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));
 
223
    }
 
224
}
 
225
 
 
226
/**
 
227
  * Check history state after serialization (pickle, persistent..) method
 
228
  * Checks history currentIndex value
 
229
  */
 
230
void tst_QWebHistory::serialize_2() 
 
231
{
 
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
 
235
 
 
236
    int oldCurrentIndex = hist->currentItemIndex();
 
237
 
 
238
    hist->back();
 
239
    waitForLoadFinished.exec();
 
240
    hist->back();
 
241
    waitForLoadFinished.exec();
 
242
    //check if current index was changed (make sure that it is not last item)
 
243
    QVERIFY(hist->currentItemIndex() != oldCurrentIndex);
 
244
    //save current index
 
245
    oldCurrentIndex = hist->currentItemIndex();
 
246
 
 
247
    save << *hist;
 
248
    QVERIFY(save.status() == QDataStream::Ok);
 
249
    load >> *hist;
 
250
    QVERIFY(load.status() == QDataStream::Ok);
 
251
 
 
252
    //check current index
 
253
    QCOMPARE(hist->currentItemIndex(), oldCurrentIndex);
 
254
}
 
255
 
 
256
/**
 
257
  * Check history state after serialization (pickle, persistent..) method
 
258
  * Checks QWebHistoryItem public property after serialization
 
259
  */
 
260
void tst_QWebHistory::serialize_3() 
 
261
{
 
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
 
265
 
 
266
    //prepare two different history items
 
267
    QWebHistoryItem a = hist->currentItem();
 
268
    a.setUserData("A - user data");
 
269
 
 
270
    //check properties BEFORE serialization
 
271
    QString title(a.title());
 
272
    QDateTime lastVisited(a.lastVisited());
 
273
    QUrl originalUrl(a.originalUrl());
 
274
    QUrl url(a.url());
 
275
    QVariant userData(a.userData());
 
276
 
 
277
    save << *hist;
 
278
    QVERIFY(save.status() == QDataStream::Ok);
 
279
    QVERIFY(!load.atEnd());
 
280
    hist->clear();
 
281
    QVERIFY(hist->count() == 1);
 
282
    load >> *hist;
 
283
    QVERIFY(load.status() == QDataStream::Ok);
 
284
    QWebHistoryItem b = hist->currentItem();
 
285
 
 
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);
 
292
 
 
293
    //Check if all data was read
 
294
    QVERIFY(load.atEnd());
 
295
}
 
296
 
 
297
/** Simple checks should be a bit redundant to streaming operators */
 
298
void tst_QWebHistory::saveAndRestore_1() 
 
299
{
 
300
    QAction* actionBack = page->action(QWebPage::Back);
 
301
    hist->back();
 
302
    waitForLoadFinished.exec();
 
303
    QVERIFY(actionBack->isEnabled());
 
304
    QByteArray buffer(hist->saveState());
 
305
    hist->clear();
 
306
    QVERIFY(!actionBack->isEnabled());
 
307
    QVERIFY(hist->count() == 1);
 
308
    hist->restoreState(buffer);
 
309
 
 
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());
 
318
}
 
319
 
 
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() 
 
323
{
 
324
    QByteArray buffer;
 
325
    hist->restoreState(buffer);
 
326
    QVERIFY(hist->count() == 1);
 
327
    QVERIFY(hist->itemAt(0).isValid());
 
328
}
 
329
 
 
330
/** Try to use bad version value */
 
331
void tst_QWebHistory::saveAndRestore_3() 
 
332
{
 
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());
 
337
}
 
338
 
 
339
/** The test shouldn't crash */
 
340
void tst_QWebHistory::saveAndRestore_crash_1()
 
341
{
 
342
    QByteArray tmp = hist->saveState();
 
343
    for (unsigned i = 0; i < 5; i++){
 
344
        hist->restoreState(tmp);
 
345
        hist->saveState();
 
346
    }
 
347
}
 
348
 
 
349
/** The test shouldn't crash */
 
350
void tst_QWebHistory::saveAndRestore_crash_2()
 
351
{
 
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);
 
357
        hist2->saveState();
 
358
    }
 
359
    delete page2;
 
360
}
 
361
 
 
362
/** The test shouldn't crash */
 
363
void tst_QWebHistory::saveAndRestore_crash_3()
 
364
{
 
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);
 
374
        hist2->back();
 
375
        tmp = hist2->saveState();
 
376
        hist2->clear();
 
377
    }
 
378
    delete page2;
 
379
}
 
380
 
 
381
/** ::clear */
 
382
void tst_QWebHistory::clear()
 
383
{
 
384
    QAction* actionBack = page->action(QWebPage::Back);
 
385
    QVERIFY(actionBack->isEnabled());
 
386
    hist->saveState();
 
387
    QVERIFY(hist->count() > 1);
 
388
    hist->clear();
 
389
    QVERIFY(hist->count() == 1);  // Leave current item.
 
390
    QVERIFY(!actionBack->isEnabled());
 
391
 
 
392
 
 
393
    QWebPage* page2 = new QWebPage(this);
 
394
    QWebHistory* hist2 = page2->history();
 
395
    QVERIFY(hist2->count() == 0);
 
396
    hist2->clear();
 
397
    QVERIFY(hist2->count() == 0); // Do not change anything.
 
398
    delete page2;
 
399
}
 
400
 
 
401
QTEST_MAIN(tst_QWebHistory)
 
402
#include "tst_qwebhistory.moc"