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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
#include <QWebEngineNewWindowRequest>
#include <QWebEngineProfile>
#include <vector>
#include "Configuration.hpp"
#include "testUtils.h"
#include "widgets/WebView.hpp"
#include "widgets/WebViewStack.hpp"
// NOLINTBEGIN
class WebViewStackSpec : public QObject {
Q_OBJECT
class FakeNewWindowRequest : public QWebEngineNewWindowRequest {
public:
FakeNewWindowRequest(DestinationType t, const QRect &r, const QUrl &u,
bool b)
: QWebEngineNewWindowRequest(t, r, u, b, nullptr) {}
};
private slots:
void test_initial_state() {
context("when initialized");
it("opens a single tab") {
Configuration configuration;
WebViewStack webview_stack(&configuration);
QCOMPARE(webview_stack.count(), 1);
QCOMPARE(webview_stack.current_url(), configuration.new_tab_url);
webview_stack.deleteLater();
}
}
void test_open_url() {
context("when openUrl is called without an open type");
it("replaces current tab url with newtab url") {
Configuration configuration;
WebViewStack webview_stack(&configuration);
webview_stack.open_url(QUrl("http://a.com"), OpenType::OpenUrl);
webview_stack.open_url(QUrl(configuration.new_tab_url));
QCOMPARE(webview_stack.count(), 1);
std::vector<QUrl> urls = {QUrl(configuration.new_tab_url)};
QCOMPARE(webview_stack.urls(), urls);
QCOMPARE(webview_stack.current_webview_index(), 0);
QCOMPARE(webview_stack.current_url(), configuration.new_tab_url);
}
context("when creating a new webview with a url and focus is false");
it("opens the given url in background") {
Configuration configuration;
WebViewStack webview_stack(&configuration);
webview_stack.open_url(QUrl("https://duckduckgo.com"),
OpenType::OpenUrlInBgTab);
QCOMPARE(webview_stack.count(), 2);
std::vector<QUrl> urls = {QUrl(configuration.new_tab_url),
QUrl("https://duckduckgo.com")};
QCOMPARE(webview_stack.urls(), urls);
QCOMPARE(webview_stack.current_webview_index(), 0);
QCOMPARE(webview_stack.current_url(), configuration.new_tab_url);
}
context("when creating a new webview with a url and focus is true");
it("opens the given url as current view") {
Configuration configuration;
WebViewStack webview_stack(&configuration);
webview_stack.open_url(QUrl("https://duckduckgo.com"),
OpenType::OpenUrlInTab);
QCOMPARE(webview_stack.count(), 2);
std::vector<QUrl> urls = {QUrl(configuration.new_tab_url),
QUrl("https://duckduckgo.com")};
QCOMPARE(webview_stack.urls(), urls);
QCOMPARE(webview_stack.current_webview_index(), 1);
QCOMPARE(webview_stack.current_url(), QUrl("https://duckduckgo.com"));
}
}
// void test_next_navigation() {
// context("when nextWebView is called");
// context("- and there is only 1 tab");
// it("does nothing") {
// Configuration configuration;
// WebViewStack webview_stack(&configuration);
//
// webview_stack.next();
//
// QCOMPARE(webview_stack.current_webview_index(), 0);
// QCOMPARE(webview_stack.current_url(), configuration.new_tab_url);
// }
//
// context("when nextWebView is called");
// context("- and there are tabs after the current tab");
// it("goes to the next tab") {
// Configuration configuration;
// WebViewStack webview_stack(&configuration);
// webview_stack.open_url(QUrl("http://a1.com"),
// OpenType::OpenUrlInBgTab);
// webview_stack.open_url(QUrl("http://a2.com"),
// OpenType::OpenUrlInBgTab);
//
// webview_stack.next();
//
// QCOMPARE(webview_stack.current_webview_index(), 1);
// QCOMPARE(webview_stack.current_url(), QUrl("http://a1.com"));
// }
//
// context("when nextWebView is called");
// context("- and current tab is the last tab");
// it("jumps to the first tab") {
// Configuration configuration;
// WebViewStack webview_stack(&configuration);
// webview_stack.open_url(QUrl("http://a1.com"),
// OpenType::OpenUrlInBgTab);
// webview_stack.open_url(QUrl("http://a2.com"), OpenType::OpenUrlInTab);
// QCOMPARE(webview_stack.current_webview_index(), 2);
//
// webview_stack.next();
//
// QCOMPARE(webview_stack.current_webview_index(), 0);
// QCOMPARE(webview_stack.current_url(), configuration.new_tab_url);
// }
// }
//
// void test_previous_navigation() {
// context("when previousWebView is called");
// context("- and there is only 1 tab");
// it("does nothing") {
// Configuration configuration;
// WebViewStack webview_stack(&configuration);
//
// webview_stack.previous();
//
// QCOMPARE(webview_stack.current_webview_index(), 0);
// QCOMPARE(webview_stack.current_url(), configuration.new_tab_url);
// }
//
// context("when previousWebView is called");
// context("- and there are tabs before the current tab");
// it("goes to the next tab") {
// Configuration configuration;
// WebViewStack webview_stack(&configuration);
// webview_stack.open_url(QUrl("http://a1.com"),
// OpenType::OpenUrlInBgTab);
// webview_stack.open_url(QUrl("http://a2.com"), OpenType::OpenUrlInTab);
// QCOMPARE(webview_stack.current_webview_index(), 2);
//
// webview_stack.previous();
//
// QCOMPARE(webview_stack.current_webview_index(), 1);
// QCOMPARE(webview_stack.current_url(), QUrl("http://a1.com"));
// }
//
// context("when previousWebView is called");
// context("- and current tab is the last tab");
// it("jumps to the last tab") {
// Configuration configuration;
// WebViewStack webview_stack(&configuration);
// webview_stack.open_url(QUrl("http://a1.com"),
// OpenType::OpenUrlInBgTab);
// webview_stack.open_url(QUrl("http://a2.com"),
// OpenType::OpenUrlInBgTab);
//
// webview_stack.previous();
//
// QCOMPARE(webview_stack.current_webview_index(), 2);
// QCOMPARE(webview_stack.current_url(), QUrl("http://a2.com"));
// }
// }
void test_close() {
context("when close is called");
context("- with invalid id");
xit("does nothing") {
Configuration configuration;
WebViewStack webview_stack(&configuration);
webview_stack.open_url(QUrl("https://a.com"), OpenType::OpenUrl);
webview_stack.close(99);
QCOMPARE(webview_stack.count(), 1);
QCOMPARE(webview_stack.urls(),
(std::vector<QUrl>{QUrl("https://a.com")}));
QCOMPARE(webview_stack.current_webview_index(), 0);
QCOMPARE(webview_stack.current_url(), QUrl("https://a.com"));
}
context("when close is called on current webview");
context("- and there is only 1 tab");
it("closes the tab and opens empty tab in its place") {
Configuration configuration;
WebViewStack webview_stack(&configuration);
webview_stack.open_url(QUrl("https://a.com"), OpenType::OpenUrl);
QCOMPARE(webview_stack.count(), 1);
webview_stack.close(webview_stack.current_webview_id());
QCOMPARE(webview_stack.count(), 1);
QCOMPARE(webview_stack.urls(),
(std::vector<QUrl>{configuration.new_tab_url}));
QCOMPARE(webview_stack.current_webview_index(), 0);
QCOMPARE(webview_stack.current_url(), configuration.new_tab_url);
}
context("when close is called");
context("- with the current tab id");
context("- and there are some tabs after");
it("closes the tab and focuses the next tab") {
Configuration configuration;
WebViewStack webview_stack(&configuration);
webview_stack.open_url(QUrl("https://a1.com"), OpenType::OpenUrlInTab);
webview_stack.open_url(QUrl("https://a2.com"), OpenType::OpenUrlInBgTab);
QCOMPARE(webview_stack.count(), 3);
QCOMPARE(webview_stack.current_webview_index(), 1);
webview_stack.close(webview_stack.current_webview_id());
QCOMPARE(webview_stack.count(), 2);
QCOMPARE(webview_stack.urls(),
(std::vector<QUrl>{configuration.new_tab_url,
QUrl("https://a2.com")}));
QCOMPARE(webview_stack.current_webview_index(), 1);
QCOMPARE(webview_stack.current_url(), QUrl("https://a2.com"));
}
context("when close is called");
context("- with the current tab id");
context("- which is the last tab");
it("closes the tab and focuses previous tab") {
Configuration configuration;
WebViewStack webview_stack(&configuration);
webview_stack.open_url(QUrl("https://a1.com"), OpenType::OpenUrlInBgTab);
webview_stack.open_url(QUrl("https://a2.com"), OpenType::OpenUrlInTab);
QCOMPARE(webview_stack.count(), 3);
QCOMPARE(webview_stack.current_webview_index(), 2);
webview_stack.close(webview_stack.current_webview_id());
QCOMPARE(webview_stack.count(), 2);
QCOMPARE(webview_stack.urls(),
(std::vector<QUrl>{configuration.new_tab_url,
QUrl("https://a1.com")}));
QCOMPARE(webview_stack.current_webview_index(), 1);
QCOMPARE(webview_stack.current_url(), QUrl("https://a1.com"));
}
}
void test_new_window_request_signal() {
context("when webview emits a newWindowRequested signal");
context("- of type new tab");
xit("opens a new web view and focusses it") {
Configuration configuration;
WebViewStack webview_stack(&configuration);
webview_stack.open_url(QUrl("https://a.com"), OpenType::OpenUrl);
auto *webview = webview_stack.findChild<WebView *>();
QCOMPARE(webview_stack.count(), 1);
FakeNewWindowRequest window_request(
FakeNewWindowRequest::DestinationType::InNewTab, QRect(0, 0, 0, 0),
QUrl("https://new.com"), true);
emit webview->page()->newWindowRequested(window_request);
QCOMPARE(webview_stack.count(), 2);
QCOMPARE(
webview_stack.urls(),
(std::vector<QUrl>{QUrl("https://a.com"), QUrl("https://new.com")}));
QCOMPARE(webview_stack.current_webview_index(), 1);
QCOMPARE(webview_stack.current_url(), QUrl("https://new.com"));
}
context("when webview emits a newWindowRequested signal");
context("- of type new background tab");
xit("opens a new web view in the background") {
Configuration configuration;
WebViewStack webview_stack(&configuration);
webview_stack.open_url(QUrl("https://a.com"), OpenType::OpenUrl);
auto *webview = webview_stack.findChild<WebView *>();
FakeNewWindowRequest window_request(
FakeNewWindowRequest::DestinationType::InNewBackgroundTab,
QRect(0, 0, 0, 0), QUrl("https://new.com"), true);
emit webview->page()->newWindowRequested(window_request);
QCOMPARE(webview_stack.count(), 2);
QCOMPARE(
webview_stack.urls(),
(std::vector<QUrl>{QUrl("https://a.com"), QUrl("https://new.com")}));
QCOMPARE(webview_stack.current_webview_index(), 0);
QCOMPARE(webview_stack.current_url(), QUrl("https://a.com"));
}
}
};
QTEST_REGISTER(WebViewStackSpec)
#include "WebViewStackSpec.moc"
// NOLINTEND
|