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
22 #include "qwebpage.h"
23 #include "qwebview.h"
24 #include "qwebframe.h"
25 #include "qwebhistory.h"
26 #include "qdebug.h"
27
28 class tst_QWebHistory : public QObject
29 {
30 Q_OBJECT
31
32 public:
33 tst_QWebHistory();
34 virtual ~tst_QWebHistory();
35
36 protected :
loadPage(int nr)37 void loadPage(int nr)
38 {
39 frame->load(QUrl("qrc:/data/page" + QString::number(nr) + ".html"));
40 waitForLoadFinished.exec();
41 }
42
43 public slots:
44 void init();
45 void cleanup();
46
47 private slots:
48 void title();
49 void count();
50 void back();
51 void forward();
52 void itemAt();
53 void goToItem();
54 void items();
55 void serialize_1(); //QWebHistory countity
56 void serialize_2(); //QWebHistory index
57 void serialize_3(); //QWebHistoryItem
58 void saveAndRestore_1(); //simple checks saveState and restoreState
59 void saveAndRestore_2(); //bad parameters saveState and restoreState
60 void saveAndRestore_3(); //try use different version
61
62 private:
63 QWebPage* page;
64 QWebFrame* frame;
65 QWebHistory* hist;
66 QEventLoop waitForLoadFinished; //operation on history are asynchronous!
67 int histsize;
68 };
69
tst_QWebHistory()70 tst_QWebHistory::tst_QWebHistory()
71 {
72 }
73
~tst_QWebHistory()74 tst_QWebHistory::~tst_QWebHistory()
75 {
76 }
77
init()78 void tst_QWebHistory::init()
79 {
80 page = new QWebPage(this);
81 frame = page->mainFrame();
82 connect(page, SIGNAL(loadFinished(bool)), &waitForLoadFinished, SLOT(quit()));
83
84 for (int i = 1;i < 6;i++) {
85 loadPage(i);
86 }
87 hist = page->history();
88 histsize = 5;
89 }
90
cleanup()91 void tst_QWebHistory::cleanup()
92 {
93 delete page;
94 }
95
96 /**
97 * Check QWebHistoryItem::title() method
98 */
title()99 void tst_QWebHistory::title()
100 {
101 QCOMPARE(hist->currentItem().title(), QString("page5"));
102 }
103
104 /**
105 * Check QWebHistory::count() method
106 */
count()107 void tst_QWebHistory::count()
108 {
109 QCOMPARE(hist->count(), histsize);
110 }
111
112 /**
113 * Check QWebHistory::back() method
114 */
back()115 void tst_QWebHistory::back()
116 {
117 for (int i = histsize;i > 1;i--) {
118 QCOMPARE(page->mainFrame()->toPlainText(), QString("page") + QString::number(i));
119 hist->back();
120 waitForLoadFinished.exec();
121 }
122 }
123
124 /**
125 * Check QWebHistory::forward() method
126 */
forward()127 void tst_QWebHistory::forward()
128 {
129 //rewind history :-)
130 while (hist->canGoBack()) {
131 hist->back();
132 waitForLoadFinished.exec();
133 }
134
135 for (int i = 1;i < histsize;i++) {
136 QCOMPARE(page->mainFrame()->toPlainText(), QString("page") + QString::number(i));
137 hist->forward();
138 waitForLoadFinished.exec();
139 }
140 }
141
142 /**
143 * Check QWebHistory::itemAt() method
144 */
itemAt()145 void tst_QWebHistory::itemAt()
146 {
147 for (int i = 1;i < histsize;i++) {
148 QCOMPARE(hist->itemAt(i - 1).title(), QString("page") + QString::number(i));
149 QVERIFY(hist->itemAt(i - 1).isValid());
150 }
151 //check out of range values
152 QVERIFY(!hist->itemAt(-1).isValid());
153 QVERIFY(!hist->itemAt(histsize).isValid());
154 }
155
156 /**
157 * Check QWebHistory::goToItem() method
158 */
goToItem()159 void tst_QWebHistory::goToItem()
160 {
161 QWebHistoryItem current = hist->currentItem();
162 hist->back();
163 waitForLoadFinished.exec();
164 hist->back();
165 waitForLoadFinished.exec();
166 QVERIFY(hist->currentItem().title() != current.title());
167 hist->goToItem(current);
168 waitForLoadFinished.exec();
169 QCOMPARE(hist->currentItem().title(), current.title());
170 }
171
172 /**
173 * Check QWebHistory::items() method
174 */
items()175 void tst_QWebHistory::items()
176 {
177 QList<QWebHistoryItem> items = hist->items();
178 //check count
179 QCOMPARE(histsize, items.count());
180
181 //check order
182 for (int i = 1;i <= histsize;i++) {
183 QCOMPARE(items.at(i - 1).title(), QString("page") + QString::number(i));
184 }
185 }
186
187 /**
188 * Check history state after serialization (pickle, persistent..) method
189 * Checks history size, history order
190 */
serialize_1()191 void tst_QWebHistory::serialize_1()
192 {
193 QByteArray tmp; //buffer
194 QDataStream save(&tmp, QIODevice::WriteOnly); //here data will be saved
195 QDataStream load(&tmp, QIODevice::ReadOnly); //from here data will be loaded
196
197 save << *hist;
198 QVERIFY(save.status() == QDataStream::Ok);
199 QCOMPARE(hist->count(), histsize);
200
201 //check size of history
202 //load next page to find differences
203 loadPage(6);
204 QCOMPARE(hist->count(), histsize + 1);
205 load >> *hist;
206 QVERIFY(load.status() == QDataStream::Ok);
207 QCOMPARE(hist->count(), histsize);
208
209 //check order of historyItems
210 QList<QWebHistoryItem> items = hist->items();
211 for (int i = 1;i <= histsize;i++) {
212 QCOMPARE(items.at(i - 1).title(), QString("page") + QString::number(i));
213 }
214 }
215
216 /**
217 * Check history state after serialization (pickle, persistent..) method
218 * Checks history currentIndex value
219 */
serialize_2()220 void tst_QWebHistory::serialize_2()
221 {
222 QByteArray tmp; //buffer
223 QDataStream save(&tmp, QIODevice::WriteOnly); //here data will be saved
224 QDataStream load(&tmp, QIODevice::ReadOnly); //from here data will be loaded
225
226 int oldCurrentIndex = hist->currentItemIndex();
227
228 hist->back();
229 waitForLoadFinished.exec();
230 hist->back();
231 waitForLoadFinished.exec();
232 //check if current index was changed (make sure that it is not last item)
233 QVERIFY(hist->currentItemIndex() != oldCurrentIndex);
234 //save current index
235 oldCurrentIndex = hist->currentItemIndex();
236
237 save << *hist;
238 QVERIFY(save.status() == QDataStream::Ok);
239 load >> *hist;
240 QVERIFY(load.status() == QDataStream::Ok);
241
242 //check current index
243 QCOMPARE(hist->currentItemIndex(), oldCurrentIndex);
244 }
245
246 /**
247 * Check history state after serialization (pickle, persistent..) method
248 * Checks QWebHistoryItem public property after serialization
249 */
serialize_3()250 void tst_QWebHistory::serialize_3()
251 {
252 QByteArray tmp; //buffer
253 QDataStream save(&tmp, QIODevice::WriteOnly); //here data will be saved
254 QDataStream load(&tmp, QIODevice::ReadOnly); //from here data will be loaded
255
256 //prepare two different history items
257 QWebHistoryItem a = hist->currentItem();
258 a.setUserData("A - user data");
259
260 //check properties BEFORE serialization
261 QString title(a.title());
262 QDateTime lastVisited(a.lastVisited());
263 QUrl originalUrl(a.originalUrl());
264 QUrl url(a.url());
265 QVariant userData(a.userData());
266
267 save << *hist;
268 QVERIFY(save.status() == QDataStream::Ok);
269 QVERIFY(!load.atEnd());
270 hist->clear();
271 QVERIFY(hist->count() == 1);
272 load >> *hist;
273 QVERIFY(load.status() == QDataStream::Ok);
274 QWebHistoryItem b = hist->currentItem();
275
276 //check properties AFTER serialization
277 QCOMPARE(b.title(), title);
278 QCOMPARE(b.lastVisited(), lastVisited);
279 QCOMPARE(b.originalUrl(), originalUrl);
280 QCOMPARE(b.url(), url);
281 QCOMPARE(b.userData(), userData);
282
283 //Check if all data was read
284 QVERIFY(load.atEnd());
285 }
286
287 /** Simple checks should be a bit redundant to streaming operators */
saveAndRestore_1()288 void tst_QWebHistory::saveAndRestore_1()
289 {
290 hist->back();
291 waitForLoadFinished.exec();
292 QByteArray buffer(hist->saveState());
293 hist->clear();
294 QVERIFY(hist->count() == 1);
295 hist->restoreState(buffer);
296
297 //check only few values, do not make full test
298 //because most of the code is shared with streaming operators
299 //and these are checked before
300 QCOMPARE(hist->count(), histsize);
301 QCOMPARE(hist->currentItemIndex(), histsize - 2);
302 QCOMPARE(hist->itemAt(0).title(), QString("page1"));
303 QCOMPARE(hist->itemAt(histsize - 1).title(), QString("page") + QString::number(histsize));
304 }
305
306 /** Check returns value if there are bad parameters. Actually, result
307 * is no so importent. The test shouldn't crash :-) */
saveAndRestore_2()308 void tst_QWebHistory::saveAndRestore_2()
309 {
310 QByteArray buffer;
311 hist->restoreState(buffer);
312 QVERIFY(hist->count() == 1);
313 QVERIFY(hist->itemAt(0).isValid());
314 }
315
316 /** Try to use bad version value */
saveAndRestore_3()317 void tst_QWebHistory::saveAndRestore_3()
318 {
319 QByteArray tmp = hist->saveState((QWebHistory::HistoryStateVersion)29999);
320 QVERIFY(hist->saveState((QWebHistory::HistoryStateVersion)29999).isEmpty());
321 QVERIFY(hist->count() == histsize);
322 QVERIFY(hist->itemAt(3).isValid());
323 }
324
325 QTEST_MAIN(tst_QWebHistory)
326 #include "tst_qwebhistory.moc"
327