Falkon Develop
Cross-platform Qt-based web browser
webtabtest.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "webtabtest.h"
19#include "autotests.h"
20#include "webtab.h"
21#include "tabwidget.h"
22#include "tabbedwebview.h"
23#include "mainapplication.h"
24#include "browserwindow.h"
25
26#include <QWebEngineHistory>
27
28void WebTabTest::initTestCase()
29{
30}
31
32void WebTabTest::cleanupTestCase()
33{
35}
36
37void WebTabTest::init()
38{
40}
41
42void WebTabTest::parentChildTabsTest()
43{
44 WebTab tab1;
45 WebTab tab2;
46 WebTab tab3;
47 WebTab tab4;
48 WebTab tab5;
49 WebTab tab6;
50
51 tab1.addChildTab(&tab2);
52 QCOMPARE(tab1.childTabs(), QVector<WebTab*>{&tab2});
53 QCOMPARE(tab2.parentTab(), &tab1);
54 QCOMPARE(tab2.childTabs(), QVector<WebTab*>{});
55
56 tab1.addChildTab(&tab3);
57 QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab2, &tab3}));
58 QCOMPARE(tab3.parentTab(), &tab1);
59 QCOMPARE(tab3.childTabs(), QVector<WebTab*>{});
60
61 tab1.addChildTab(&tab4, 1);
62 QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab2, &tab4, &tab3}));
63 QCOMPARE(tab4.parentTab(), &tab1);
64 QCOMPARE(tab4.childTabs(), QVector<WebTab*>{});
65
66 tab4.addChildTab(&tab5);
67 tab4.addChildTab(&tab6);
68
69 tab4.attach(mApp->getWindow());
70 tab4.detach();
71
72 QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab2, &tab5, &tab6, &tab3}));
73 QCOMPARE(tab4.parentTab(), nullptr);
74 QCOMPARE(tab4.childTabs(), QVector<WebTab*>{});
75
76 tab3.addChildTab(&tab4);
77 tab3.setParentTab(nullptr);
78 tab1.addChildTab(&tab3, 0);
79
80 QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab2, &tab5, &tab6}));
81 QCOMPARE(tab3.parentTab(), &tab1);
82 QCOMPARE(tab3.childTabs(), QVector<WebTab*>{&tab4});
83 QCOMPARE(tab4.parentTab(), &tab3);
84
85 tab3.addChildTab(&tab2);
86 QCOMPARE(tab3.childTabs(), (QVector<WebTab*>{&tab4, &tab2}));
87 QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab5, &tab6}));
88}
89
90void WebTabTest::prependChildTabsTest()
91{
93
94 WebTab tab1;
95 WebTab tab2;
96 WebTab tab3;
97 WebTab tab4;
98 WebTab tab5;
99 WebTab tab6;
100
101 tab1.addChildTab(&tab2);
102 QCOMPARE(tab1.childTabs(), QVector<WebTab*>{&tab2});
103 QCOMPARE(tab2.parentTab(), &tab1);
104 QCOMPARE(tab2.childTabs(), QVector<WebTab*>{});
105
106 tab1.addChildTab(&tab3);
107 QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab2}));
108 QCOMPARE(tab3.parentTab(), &tab1);
109 QCOMPARE(tab3.childTabs(), QVector<WebTab*>{});
110
111 tab1.addChildTab(&tab4, 1);
112 QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab4, &tab2}));
113 QCOMPARE(tab4.parentTab(), &tab1);
114 QCOMPARE(tab4.childTabs(), QVector<WebTab*>{});
115
116 tab4.addChildTab(&tab5);
117 tab4.addChildTab(&tab6);
118
119 QCOMPARE(tab4.childTabs(), (QVector<WebTab*>{&tab6, &tab5}));
120
121 tab4.attach(mApp->getWindow());
122 tab4.detach();
123
124 QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab6, &tab5, &tab2}));
125 QCOMPARE(tab4.parentTab(), nullptr);
126 QCOMPARE(tab4.childTabs(), QVector<WebTab*>{});
127
128 tab3.addChildTab(&tab4);
129 tab3.setParentTab(nullptr);
130 tab1.addChildTab(&tab3, 0);
131
132 QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab6, &tab5, &tab2}));
133 QCOMPARE(tab3.parentTab(), &tab1);
134 QCOMPARE(tab3.childTabs(), QVector<WebTab*>{&tab4});
135 QCOMPARE(tab4.parentTab(), &tab3);
136
137 tab3.addChildTab(&tab2);
138 QCOMPARE(tab3.childTabs(), (QVector<WebTab*>{&tab2, &tab4}));
139 QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab6, &tab5}));
140}
141
142void WebTabTest::moveTabTest()
143{
144 BrowserWindow *w = mApp->createWindow(Qz::BW_NewWindow);
145
146 QSignalSpy movedSpy(w->tabWidget(), &TabWidget::tabMoved);
147
148 w->tabWidget()->addView(QUrl());
149 w->tabWidget()->addView(QUrl());
150 w->tabWidget()->addView(QUrl());
151 w->tabWidget()->addView(QUrl());
152 QTRY_COMPARE(w->tabWidget()->count(), 5);
153
154 WebTab *tab0 = w->tabWidget()->webTab(0);
155 WebTab *tab1 = w->tabWidget()->webTab(1);
156 WebTab *tab2 = w->tabWidget()->webTab(2);
157 WebTab *tab3 = w->tabWidget()->webTab(3);
158 WebTab *tab4 = w->tabWidget()->webTab(4);
159
160 movedSpy.clear();
161 tab0->moveTab(2); // 1, 2, 0, 3, 4
162 QCOMPARE(movedSpy.count(), 1);
163 QCOMPARE(movedSpy.at(0).at(0).toInt(), 0);
164 QCOMPARE(movedSpy.at(0).at(1).toInt(), 2);
165
166 QCOMPARE(w->tabWidget()->webTab(0), tab1);
167 QCOMPARE(w->tabWidget()->webTab(1), tab2);
168 QCOMPARE(w->tabWidget()->webTab(2), tab0);
169 QCOMPARE(w->tabWidget()->webTab(3), tab3);
170 QCOMPARE(w->tabWidget()->webTab(4), tab4);
171
172 movedSpy.clear();
173 tab4->moveTab(0); // 4, 1, 2, 0, 3
174 QCOMPARE(movedSpy.count(), 1);
175 QCOMPARE(movedSpy.at(0).at(0).toInt(), 4);
176 QCOMPARE(movedSpy.at(0).at(1).toInt(), 0);
177
178 tab4->togglePinned(); // [4], 1, 2, 0, 3
179
180 movedSpy.clear();
181 tab2->moveTab(0); // [2, 4], 1, 0, 3
182 QCOMPARE(tab2->isPinned(), true);
183 QCOMPARE(movedSpy.count(), 1);
184 QCOMPARE(movedSpy.at(0).at(0).toInt(), 2);
185 QCOMPARE(movedSpy.at(0).at(1).toInt(), 0);
186
187 movedSpy.clear();
188 tab0->moveTab(1); // [2, 0, 4], 1, 3
189 QCOMPARE(tab0->isPinned(), true);
190 QCOMPARE(movedSpy.count(), 1);
191 QCOMPARE(movedSpy.at(0).at(0).toInt(), 3);
192 QCOMPARE(movedSpy.at(0).at(1).toInt(), 1);
193
194 QCOMPARE(w->tabWidget()->webTab(0), tab2);
195 QCOMPARE(w->tabWidget()->webTab(1), tab0);
196 QCOMPARE(w->tabWidget()->webTab(2), tab4);
197 QCOMPARE(w->tabWidget()->webTab(3), tab1);
198 QCOMPARE(w->tabWidget()->webTab(4), tab3);
199
200 movedSpy.clear();
201 tab0->moveTab(0); // [0, 2, 4], 1, 3
202 QCOMPARE(movedSpy.count(), 1);
203 QCOMPARE(movedSpy.at(0).at(0).toInt(), 1);
204 QCOMPARE(movedSpy.at(0).at(1).toInt(), 0);
205
206 QCOMPARE(w->tabWidget()->webTab(0), tab0);
207 QCOMPARE(w->tabWidget()->webTab(1), tab2);
208 QCOMPARE(w->tabWidget()->webTab(2), tab4);
209 QCOMPARE(w->tabWidget()->webTab(3), tab1);
210 QCOMPARE(w->tabWidget()->webTab(4), tab3);
211
212 movedSpy.clear();
213 tab0->moveTab(3); // [2, 4], 1, 0, 3
214 QCOMPARE(tab0->isPinned(), false);
215 QCOMPARE(movedSpy.count(), 1);
216 QCOMPARE(movedSpy.at(0).at(0).toInt(), 0);
217 QCOMPARE(movedSpy.at(0).at(1).toInt(), 3);
218
219 QCOMPARE(w->tabWidget()->webTab(0), tab2);
220 QCOMPARE(w->tabWidget()->webTab(1), tab4);
221 QCOMPARE(w->tabWidget()->webTab(2), tab1);
222 QCOMPARE(w->tabWidget()->webTab(3), tab0);
223 QCOMPARE(w->tabWidget()->webTab(4), tab3);
224
225 movedSpy.clear();
226 tab2->moveTab(4); // [4], 1, 0, 3, 2
227 QCOMPARE(tab0->isPinned(), false);
228 QCOMPARE(movedSpy.count(), 1);
229 QCOMPARE(movedSpy.at(0).at(0).toInt(), 0);
230 QCOMPARE(movedSpy.at(0).at(1).toInt(), 4);
231
232 movedSpy.clear();
233 tab4->moveTab(2); // 1, 0, 4, 3, 2
234 QCOMPARE(tab0->isPinned(), false);
235 QCOMPARE(movedSpy.count(), 1);
236 QCOMPARE(movedSpy.at(0).at(0).toInt(), 0);
237 QCOMPARE(movedSpy.at(0).at(1).toInt(), 2);
238
239 QCOMPARE(w->tabWidget()->webTab(0), tab1);
240 QCOMPARE(w->tabWidget()->webTab(1), tab0);
241 QCOMPARE(w->tabWidget()->webTab(2), tab4);
242 QCOMPARE(w->tabWidget()->webTab(3), tab3);
243 QCOMPARE(w->tabWidget()->webTab(4), tab2);
244
245 // Invalid moves
246 movedSpy.clear();
247 tab4->moveTab(tab4->tabIndex());
248 tab4->moveTab(-1);
249 tab4->moveTab(5);
250 tab4->moveTab(6);
251 QCOMPARE(movedSpy.count(), 0);
252
253 delete w;
254}
255
256void WebTabTest::loadNotRestoredTabTest()
257{
258 WebTab tab;
259
260 tab.load(QUrl(QSL("qrc:autotests/data/basic_page.html")));
261 QVERIFY(waitForLoadfinished(&tab));
262 QTRY_COMPARE(tab.webView()->history()->count(), 1);
263
264 tab.unload();
265 QVERIFY(!tab.isRestored());
266
267 tab.load(QUrl(QSL("qrc:autotests/data/basic_page2.html")));
268 QVERIFY(waitForLoadfinished(&tab));
269 QTRY_COMPARE(tab.webView()->history()->count(), 2);
270
271 QCOMPARE(tab.url(), QUrl(QSL("qrc:autotests/data/basic_page2.html")));
272 QCOMPARE(tab.webView()->history()->currentItem().url(), QUrl(QSL("qrc:autotests/data/basic_page2.html")));
273 QCOMPARE(tab.webView()->history()->backItem().url(), QUrl(QSL("qrc:autotests/data/basic_page.html")));
274}
275
276void WebTabTest::saveNotRestoredTabTest()
277{
278 WebTab tab;
279
280 tab.load(QUrl(QSL("qrc:autotests/data/basic_page.html")));
281 QVERIFY(waitForLoadfinished(&tab));
282 QTRY_COMPARE(tab.webView()->history()->count(), 1);
283
284 tab.unload();
285 QVERIFY(!tab.isRestored());
286
287 WebTab::SavedTab saved(&tab);
288 QVERIFY(saved.isValid());
289 QCOMPARE(saved.url, QUrl(QSL("qrc:autotests/data/basic_page.html")));
290}
291
#define FALKONTEST_MAIN(Test)
Definition: autotests.h:23
bool waitForLoadfinished(QObject *object)
Definition: autotests.h:34
TabWidget * tabWidget() const
WebTab * webTab(int index=-1) const
Definition: tabwidget.cpp:570
int addView(const LoadRequest &req, const Qz::NewTabPositionFlags &openFlags, bool selectLine=false, bool pinned=false)
Definition: tabwidget.cpp:314
void tabMoved(int from, int to)
Definition: webtab.h:40
int tabIndex() const
Definition: webtab.cpp:710
bool isRestored() const
Definition: webtab.cpp:548
void setParentTab(WebTab *tab)
Definition: webtab.cpp:474
QUrl url() const
Definition: webtab.cpp:263
static void setAddChildBehavior(AddChildBehavior behavior)
Definition: webtab.cpp:655
void load(const LoadRequest &request)
Definition: webtab.cpp:391
QVector< WebTab * > childTabs() const
Definition: webtab.cpp:533
void unload()
Definition: webtab.cpp:401
bool isPinned() const
Definition: webtab.cpp:414
void addChildTab(WebTab *tab, int index=-1)
Definition: webtab.cpp:502
@ PrependChild
Definition: webtab.h:66
@ AppendChild
Definition: webtab.h:65
TabbedWebView * webView() const
Definition: webtab.cpp:209
void togglePinned()
Definition: webtab.cpp:715
void moveTab(int to)
Definition: webtab.cpp:703
void attach(BrowserWindow *window)
Definition: webtab.cpp:345
WebTab * parentTab() const
Definition: webtab.cpp:469
void detach()
Definition: webtab.cpp:314
#define mApp
@ BW_NewWindow
Definition: qzcommon.h:67
#define QSL(x)
Definition: qzcommon.h:40