diff options
Diffstat (limited to '')
| -rw-r--r-- | spec/BrowserManagerSpec.cpp | 271 | ||||
| -rw-r--r-- | spec/WebViewStackSpec.cpp | 273 |
2 files changed, 273 insertions, 271 deletions
diff --git a/spec/BrowserManagerSpec.cpp b/spec/BrowserManagerSpec.cpp deleted file mode 100644 index efa1ca3..0000000 --- a/spec/BrowserManagerSpec.cpp +++ /dev/null @@ -1,271 +0,0 @@ -#include <QWebEngineNewWindowRequest> -#include <QWebEngineProfile> -#include <vector> - -#include "testUtils.h" -#include "widgets/BrowserManager.hpp" - -class BrowserManagerSpec : 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 testInitialState() { - context("when initialized"); - it("opens a single tab") { - BrowserManager browserManager; - - QCOMPARE(browserManager.webViewCount(), 1); - QCOMPARE(browserManager.currentUrl(), BrowserManager::NewtabURL); - browserManager.deleteLater(); - } - } - - void testCreateNewWebView() { - context("when creating a new background webview without a url"); - it("opens new tab url as a background view") { - BrowserManager browserManager; - - browserManager.createNewWebView(); - - QCOMPARE(browserManager.webViewCount(), 2); - std::vector<QUrl> urls = {QUrl(BrowserManager::NewtabURL), - QUrl(BrowserManager::NewtabURL)}; - QCOMPARE(browserManager.webViewUrls(), urls); - QCOMPARE(browserManager.currentWebViewIndex(), 0); - QCOMPARE(browserManager.currentUrl(), BrowserManager::NewtabURL); - } - - context("when creating a new webview with a url and focus is false"); - it("opens the given url in background") { - BrowserManager browserManager; - - browserManager.createNewWebView(QUrl("https://duckduckgo.com"), false); - - QCOMPARE(browserManager.webViewCount(), 2); - std::vector<QUrl> urls = {QUrl(BrowserManager::NewtabURL), - QUrl("https://duckduckgo.com")}; - QCOMPARE(browserManager.webViewUrls(), urls); - QCOMPARE(browserManager.currentWebViewIndex(), 0); - QCOMPARE(browserManager.currentUrl(), BrowserManager::NewtabURL); - } - - context("when creating a new webview with a url and focus is true"); - it("opens the given url as current view") { - BrowserManager browserManager; - - browserManager.createNewWebView(QUrl("https://duckduckgo.com"), true); - - QCOMPARE(browserManager.webViewCount(), 2); - std::vector<QUrl> urls = {QUrl(BrowserManager::NewtabURL), - QUrl("https://duckduckgo.com")}; - QCOMPARE(browserManager.webViewUrls(), urls); - QCOMPARE(browserManager.currentWebViewIndex(), 1); - QCOMPARE(browserManager.currentUrl(), QUrl("https://duckduckgo.com")); - } - } - - void testNextNavigation() { - context("when nextWebView is called"); - context("- and there is only 1 tab"); - it("does nothing") { - BrowserManager browserManager; - - browserManager.nextWebView(); - - QCOMPARE(browserManager.currentWebViewIndex(), 0); - QCOMPARE(browserManager.currentUrl(), BrowserManager::NewtabURL); - } - - context("when nextWebView is called"); - context("- and there are tabs after the current tab"); - it("goes to the next tab") { - BrowserManager browserManager; - browserManager.createNewWebView(QUrl("http://a1.com")); - browserManager.createNewWebView(QUrl("http://a2.com")); - - browserManager.nextWebView(); - - QCOMPARE(browserManager.currentWebViewIndex(), 1); - QCOMPARE(browserManager.currentUrl(), QUrl("http://a1.com")); - } - - context("when nextWebView is called"); - context("- and current tab is the last tab"); - it("jumps to the first tab") { - BrowserManager browserManager; - browserManager.createNewWebView(QUrl("http://a1.com")); - browserManager.createNewWebView(QUrl("http://a2.com"), true); - QCOMPARE(browserManager.currentWebViewIndex(), 2); - - browserManager.nextWebView(); - - QCOMPARE(browserManager.currentWebViewIndex(), 0); - QCOMPARE(browserManager.currentUrl(), BrowserManager::NewtabURL); - } - } - - void testPreviousNavigation() { - context("when previousWebView is called"); - context("- and there is only 1 tab"); - it("does nothing") { - BrowserManager browserManager; - - browserManager.previousWebView(); - - QCOMPARE(browserManager.currentWebViewIndex(), 0); - QCOMPARE(browserManager.currentUrl(), BrowserManager::NewtabURL); - } - - context("when previousWebView is called"); - context("- and there are tabs before the current tab"); - it("goes to the next tab") { - BrowserManager browserManager; - browserManager.createNewWebView(QUrl("http://a1.com")); - browserManager.createNewWebView(QUrl("http://a2.com"), true); - QCOMPARE(browserManager.currentWebViewIndex(), 2); - - browserManager.previousWebView(); - - QCOMPARE(browserManager.currentWebViewIndex(), 1); - QCOMPARE(browserManager.currentUrl(), QUrl("http://a1.com")); - } - - context("when previousWebView is called"); - context("- and current tab is the last tab"); - it("jumps to the last tab") { - BrowserManager browserManager; - browserManager.createNewWebView(QUrl("http://a1.com")); - browserManager.createNewWebView(QUrl("http://a2.com")); - - browserManager.previousWebView(); - - QCOMPARE(browserManager.currentWebViewIndex(), 2); - QCOMPARE(browserManager.currentUrl(), QUrl("http://a2.com")); - } - } - - void testCloseWebView() { - context("when closeWebView is called"); - context("- with out of bounds index"); - it("does nothing") { - BrowserManager browserManager; - browserManager.setCurrentUrl(QUrl("https://a.com")); - - browserManager.closeWebView(1); - - QCOMPARE(browserManager.webViewCount(), 1); - QCOMPARE(browserManager.webViewUrls(), - (std::vector<QUrl>{QUrl("https://a.com")})); - QCOMPARE(browserManager.currentWebViewIndex(), 0); - QCOMPARE(browserManager.currentUrl(), QUrl("https://a.com")); - } - - context("when closeWebView is called"); - context("- and there is only 1 tab"); - it("closes the tab and opens empty tab in its place") { - BrowserManager browserManager; - browserManager.setCurrentUrl(QUrl("https://a.com")); - QCOMPARE(browserManager.webViewCount(), 1); - - browserManager.closeWebView(0); - - QCOMPARE(browserManager.webViewCount(), 1); - QCOMPARE(browserManager.webViewUrls(), - (std::vector<QUrl>{BrowserManager::NewtabURL})); - QCOMPARE(browserManager.currentWebViewIndex(), 0); - QCOMPARE(browserManager.currentUrl(), BrowserManager::NewtabURL); - } - - context("when closeWebView is called"); - context("- with the current tab index"); - context("- and there are some tabs after"); - it("closes the tab and focuses the next tab") { - BrowserManager browserManager; - browserManager.createNewWebView(QUrl("https://a1.com"), true); - browserManager.createNewWebView(QUrl("https://a2.com")); - QCOMPARE(browserManager.webViewCount(), 3); - QCOMPARE(browserManager.currentWebViewIndex(), 1); - - browserManager.closeWebView(1); - - QCOMPARE(browserManager.webViewCount(), 2); - QCOMPARE(browserManager.webViewUrls(), - (std::vector<QUrl>{BrowserManager::NewtabURL, - QUrl("https://a2.com")})); - QCOMPARE(browserManager.currentWebViewIndex(), 1); - QCOMPARE(browserManager.currentUrl(), QUrl("https://a2.com")); - } - - context("when closeWebView is called"); - context("- with the current tab index"); - context("- which is the last tab"); - it("closes the tab and focusses previous tab") { - BrowserManager browserManager; - browserManager.createNewWebView(QUrl("https://a1.com")); - browserManager.createNewWebView(QUrl("https://a2.com"), true); - QCOMPARE(browserManager.webViewCount(), 3); - QCOMPARE(browserManager.currentWebViewIndex(), 2); - - browserManager.closeWebView(2); - - QCOMPARE(browserManager.webViewCount(), 2); - QCOMPARE(browserManager.webViewUrls(), - (std::vector<QUrl>{BrowserManager::NewtabURL, - QUrl("https://a1.com")})); - QCOMPARE(browserManager.currentWebViewIndex(), 1); - QCOMPARE(browserManager.currentUrl(), QUrl("https://a1.com")); - } - } - - void testNewWindowRequestSignal() { - context("when webview emits a newWindowRequested signal"); - context("- of type new tab"); - it("opens a new web view and focusses it") { - BrowserManager browserManager; - browserManager.setCurrentUrl(QUrl("https://a.com")); - auto webview = browserManager.findChild<QWebEngineView *>(); - - FakeNewWindowRequest windowRequest( - FakeNewWindowRequest::DestinationType::InNewTab, QRect(0, 0, 0, 0), - QUrl("https://new.com"), true); - emit webview->page()->newWindowRequested(windowRequest); - - QCOMPARE(browserManager.webViewCount(), 2); - QCOMPARE( - browserManager.webViewUrls(), - (std::vector<QUrl>{QUrl("https://a.com"), QUrl("https://new.com")})); - QCOMPARE(browserManager.currentWebViewIndex(), 1); - QCOMPARE(browserManager.currentUrl(), QUrl("https://new.com")); - } - - context("when webview emits a newWindowRequested signal"); - context("- of type new background tab"); - it("opens a new web view in the background") { - BrowserManager browserManager; - browserManager.setCurrentUrl(QUrl("https://a.com")); - auto webview = browserManager.findChild<QWebEngineView *>(); - - FakeNewWindowRequest windowRequest( - FakeNewWindowRequest::DestinationType::InNewBackgroundTab, - QRect(0, 0, 0, 0), QUrl("https://new.com"), true); - emit webview->page()->newWindowRequested(windowRequest); - - QCOMPARE(browserManager.webViewCount(), 2); - QCOMPARE( - browserManager.webViewUrls(), - (std::vector<QUrl>{QUrl("https://a.com"), QUrl("https://new.com")})); - QCOMPARE(browserManager.currentWebViewIndex(), 0); - QCOMPARE(browserManager.currentUrl(), QUrl("https://a.com")); - } - } -}; - -QTEST_REGISTER(BrowserManagerSpec) -#include "BrowserManagerSpec.moc" diff --git a/spec/WebViewStackSpec.cpp b/spec/WebViewStackSpec.cpp new file mode 100644 index 0000000..5824437 --- /dev/null +++ b/spec/WebViewStackSpec.cpp @@ -0,0 +1,273 @@ +#include <QWebEngineNewWindowRequest> +#include <QWebEngineProfile> +#include <vector> + +#include "testUtils.h" +#include "widgets/WebViewStack.hpp" + +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 testInitialState() { + context("when initialized"); + it("opens a single tab") { + WebViewStack webViewStack; + + QCOMPARE(webViewStack.count(), 1); + QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL); + webViewStack.deleteLater(); + } + } + + void testOpenUrl() { + context("when openUrl is called without an open type"); + it("replaces current tab url with newtab url") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("http://a.com"), OpenType::OpenUrl); + + webViewStack.openUrl(); + + QCOMPARE(webViewStack.count(), 1); + std::vector<QUrl> urls = {QUrl(WebViewStack::NewtabURL)}; + QCOMPARE(webViewStack.urls(), urls); + QCOMPARE(webViewStack.currentWebViewIndex(), 0); + QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL); + } + + context("when creating a new webview with a url and focus is false"); + it("opens the given url in background") { + WebViewStack webViewStack; + + webViewStack.openUrl(QUrl("https://duckduckgo.com"), + OpenType::OpenUrlInBgTab); + + QCOMPARE(webViewStack.count(), 2); + std::vector<QUrl> urls = {QUrl(WebViewStack::NewtabURL), + QUrl("https://duckduckgo.com")}; + QCOMPARE(webViewStack.urls(), urls); + QCOMPARE(webViewStack.currentWebViewIndex(), 0); + QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL); + } + + context("when creating a new webview with a url and focus is true"); + it("opens the given url as current view") { + WebViewStack webViewStack; + + webViewStack.openUrl(QUrl("https://duckduckgo.com"), + OpenType::OpenUrlInTab); + + QCOMPARE(webViewStack.count(), 2); + std::vector<QUrl> urls = {QUrl(WebViewStack::NewtabURL), + QUrl("https://duckduckgo.com")}; + QCOMPARE(webViewStack.urls(), urls); + QCOMPARE(webViewStack.currentWebViewIndex(), 1); + QCOMPARE(webViewStack.currentUrl(), QUrl("https://duckduckgo.com")); + } + } + + void testNextNavigation() { + context("when nextWebView is called"); + context("- and there is only 1 tab"); + it("does nothing") { + WebViewStack webViewStack; + + webViewStack.next(); + + QCOMPARE(webViewStack.currentWebViewIndex(), 0); + QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL); + } + + context("when nextWebView is called"); + context("- and there are tabs after the current tab"); + it("goes to the next tab") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("http://a1.com"), OpenType::OpenUrlInBgTab); + webViewStack.openUrl(QUrl("http://a2.com"), OpenType::OpenUrlInBgTab); + + webViewStack.next(); + + QCOMPARE(webViewStack.currentWebViewIndex(), 1); + QCOMPARE(webViewStack.currentUrl(), QUrl("http://a1.com")); + } + + context("when nextWebView is called"); + context("- and current tab is the last tab"); + it("jumps to the first tab") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("http://a1.com"), OpenType::OpenUrlInBgTab); + webViewStack.openUrl(QUrl("http://a2.com"), OpenType::OpenUrlInTab); + QCOMPARE(webViewStack.currentWebViewIndex(), 2); + + webViewStack.next(); + + QCOMPARE(webViewStack.currentWebViewIndex(), 0); + QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL); + } + } + + void testPreviousNavigation() { + context("when previousWebView is called"); + context("- and there is only 1 tab"); + it("does nothing") { + WebViewStack webViewStack; + + webViewStack.previous(); + + QCOMPARE(webViewStack.currentWebViewIndex(), 0); + QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL); + } + + context("when previousWebView is called"); + context("- and there are tabs before the current tab"); + it("goes to the next tab") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("http://a1.com"), OpenType::OpenUrlInBgTab); + webViewStack.openUrl(QUrl("http://a2.com"), OpenType::OpenUrlInTab); + QCOMPARE(webViewStack.currentWebViewIndex(), 2); + + webViewStack.previous(); + + QCOMPARE(webViewStack.currentWebViewIndex(), 1); + QCOMPARE(webViewStack.currentUrl(), QUrl("http://a1.com")); + } + + context("when previousWebView is called"); + context("- and current tab is the last tab"); + it("jumps to the last tab") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("http://a1.com"), OpenType::OpenUrlInBgTab); + webViewStack.openUrl(QUrl("http://a2.com"), OpenType::OpenUrlInBgTab); + + webViewStack.previous(); + + QCOMPARE(webViewStack.currentWebViewIndex(), 2); + QCOMPARE(webViewStack.currentUrl(), QUrl("http://a2.com")); + } + } + + void testCloseWebView() { + context("when closeWebView is called"); + context("- with out of bounds index"); + it("does nothing") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("https://a.com"), OpenType::OpenUrl); + + webViewStack.close(1); + + QCOMPARE(webViewStack.count(), 1); + QCOMPARE(webViewStack.urls(), (std::vector<QUrl>{QUrl("https://a.com")})); + QCOMPARE(webViewStack.currentWebViewIndex(), 0); + QCOMPARE(webViewStack.currentUrl(), QUrl("https://a.com")); + } + + context("when closeWebView is called"); + context("- and there is only 1 tab"); + it("closes the tab and opens empty tab in its place") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("https://a.com"), OpenType::OpenUrl); + QCOMPARE(webViewStack.count(), 1); + + webViewStack.close(0); + + QCOMPARE(webViewStack.count(), 1); + QCOMPARE(webViewStack.urls(), + (std::vector<QUrl>{WebViewStack::NewtabURL})); + QCOMPARE(webViewStack.currentWebViewIndex(), 0); + QCOMPARE(webViewStack.currentUrl(), WebViewStack::NewtabURL); + } + + context("when closeWebView is called"); + context("- with the current tab index"); + context("- and there are some tabs after"); + it("closes the tab and focuses the next tab") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("https://a1.com"), OpenType::OpenUrlInTab); + webViewStack.openUrl(QUrl("https://a2.com"), OpenType::OpenUrlInBgTab); + QCOMPARE(webViewStack.count(), 3); + QCOMPARE(webViewStack.currentWebViewIndex(), 1); + + webViewStack.close(1); + + QCOMPARE(webViewStack.count(), 2); + QCOMPARE( + webViewStack.urls(), + (std::vector<QUrl>{WebViewStack::NewtabURL, QUrl("https://a2.com")})); + QCOMPARE(webViewStack.currentWebViewIndex(), 1); + QCOMPARE(webViewStack.currentUrl(), QUrl("https://a2.com")); + } + + context("when closeWebView is called"); + context("- with the current tab index"); + context("- which is the last tab"); + it("closes the tab and focusses previous tab") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("https://a1.com"), OpenType::OpenUrlInBgTab); + webViewStack.openUrl(QUrl("https://a2.com"), OpenType::OpenUrlInTab); + QCOMPARE(webViewStack.count(), 3); + QCOMPARE(webViewStack.currentWebViewIndex(), 2); + + webViewStack.close(2); + + QCOMPARE(webViewStack.count(), 2); + QCOMPARE( + webViewStack.urls(), + (std::vector<QUrl>{WebViewStack::NewtabURL, QUrl("https://a1.com")})); + QCOMPARE(webViewStack.currentWebViewIndex(), 1); + QCOMPARE(webViewStack.currentUrl(), QUrl("https://a1.com")); + } + } + + void testNewWindowRequestSignal() { + context("when webview emits a newWindowRequested signal"); + context("- of type new tab"); + it("opens a new web view and focusses it") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("https://a.com"), OpenType::OpenUrl); + auto webview = webViewStack.findChild<QWebEngineView *>(); + QCOMPARE(webViewStack.count(), 1); + + FakeNewWindowRequest windowRequest( + FakeNewWindowRequest::DestinationType::InNewTab, QRect(0, 0, 0, 0), + QUrl("https://new.com"), true); + emit webview->page()->newWindowRequested(windowRequest); + + QCOMPARE(webViewStack.count(), 2); + QCOMPARE( + webViewStack.urls(), + (std::vector<QUrl>{QUrl("https://a.com"), QUrl("https://new.com")})); + QCOMPARE(webViewStack.currentWebViewIndex(), 1); + QCOMPARE(webViewStack.currentUrl(), QUrl("https://new.com")); + } + + context("when webview emits a newWindowRequested signal"); + context("- of type new background tab"); + it("opens a new web view in the background") { + WebViewStack webViewStack; + webViewStack.openUrl(QUrl("https://a.com"), OpenType::OpenUrl); + auto webview = webViewStack.findChild<QWebEngineView *>(); + + FakeNewWindowRequest windowRequest( + FakeNewWindowRequest::DestinationType::InNewBackgroundTab, + QRect(0, 0, 0, 0), QUrl("https://new.com"), true); + emit webview->page()->newWindowRequested(windowRequest); + + QCOMPARE(webViewStack.count(), 2); + QCOMPARE( + webViewStack.urls(), + (std::vector<QUrl>{QUrl("https://a.com"), QUrl("https://new.com")})); + QCOMPARE(webViewStack.currentWebViewIndex(), 0); + QCOMPARE(webViewStack.currentUrl(), QUrl("https://a.com")); + } + } +}; + +QTEST_REGISTER(WebViewStackSpec) +#include "WebViewStackSpec.moc" |
