Falkon Develop
Cross-platform Qt-based web browser
sitesettingstest.cpp
Go to the documentation of this file.
1/* ============================================================
2 * Falkon - Qt web browser
3 * Copyright (C) 2024 Juraj Oravec <jurajoravec@mailo.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
19#include "sitesettingstest.h"
20
21#include "mainapplication.h"
22#include "tabbedwebview.h"
23#include "webpage.h"
24
25#include "autotests.h"
26#include "tabwidget.h"
27
28
29void SiteSettingsTest::initTestCase()
30{
31 /* Wait until the initial tab (at index 0) in the window is created */
32 QTRY_COMPARE(mApp->getWindow()->tabCount(), 1);
33}
34
35void SiteSettingsTest::cleanupTestCase()
36{
37}
38
39void SiteSettingsTest::webAttributeTest()
40{
41 SiteSettingsManager *siteSettings = mApp->siteSettingsManager();
42 siteSettings->setOption(QWebEngineSettings::AutoLoadImages, QUrl(QSL("https://www.falkon.org/")), SiteSettingsManager::Deny);
43 siteSettings->setOption(QWebEngineSettings::JavascriptEnabled, QUrl(QSL("https://kde.org/")), SiteSettingsManager::Deny);
44 siteSettings->setOption(QWebEngineSettings::LocalStorageEnabled, QUrl(QSL("https://store.falkon.org/")), SiteSettingsManager::Deny);
45 siteSettings->setOption(QWebEngineSettings::PlaybackRequiresUserGesture, QUrl(QSL("https://planet.kde.org/")), SiteSettingsManager::Allow);
46
47
48 WebTab tab;
49
50 checkInternalPage(&tab, QUrl(QSL("falkon:start")));
51
52 checkExternalPage(&tab, QUrl(QSL("https://www.falkon.org/")));
53 checkExternalPage(&tab, QUrl(QSL("https://kde.org/")));
54 checkExternalPage(&tab, QUrl(QSL("https://store.falkon.org/")));
55
56 checkInternalPage(&tab, QUrl(QSL("falkon:about")));
57
58 checkExternalPage(&tab, QUrl(QSL("https://planet.kde.org/")));
59}
60
61bool SiteSettingsTest::checkWebAttributes(WebPage *page, QHash<QWebEngineSettings::WebAttribute, bool> webAttributes)
62{
63 for (auto it = webAttributes.begin(); it != webAttributes.end(); ++it) {
64 if (page->settings()->testAttribute(it.key()) != it.value()) {
65 return false;
66 }
67 }
68
69 return true;
70}
71
72void SiteSettingsTest::checkInternalPage(WebTab *tab, QUrl url)
73{
74 QMap<QWebEngineSettings::WebAttribute, bool> internalWebAttributes = {
75 {QWebEngineSettings::AutoLoadImages, true}
76 ,{QWebEngineSettings::JavascriptEnabled, true}
77 ,{QWebEngineSettings::JavascriptCanOpenWindows, false}
78 ,{QWebEngineSettings::JavascriptCanAccessClipboard, true}
79 ,{QWebEngineSettings::JavascriptCanPaste, false}
80 ,{QWebEngineSettings::AllowWindowActivationFromJavaScript, false}
81 ,{QWebEngineSettings::LocalStorageEnabled, true}
82 ,{QWebEngineSettings::FullScreenSupportEnabled, mApp->webSettings()->testAttribute(QWebEngineSettings::FullScreenSupportEnabled)}
83 ,{QWebEngineSettings::AllowRunningInsecureContent, false}
84 ,{QWebEngineSettings::AllowGeolocationOnInsecureOrigins, false}
85 ,{QWebEngineSettings::PlaybackRequiresUserGesture, mApp->webSettings()->testAttribute(QWebEngineSettings::PlaybackRequiresUserGesture)}
86 ,{QWebEngineSettings::WebRTCPublicInterfacesOnly, false}
87 };
88
89 QSignalSpy spy(tab, SIGNAL(loadingChanged(bool)));
90 tab->load(url);
91 QTRY_COMPARE(spy.count(), 3);
92
93 auto *page = tab->webView()->page();
94 for (auto it = internalWebAttributes.begin(); it != internalWebAttributes.end(); ++it) {
95 QCOMPARE(page->settings()->testAttribute(it.key()), it.value());
96 }
97}
98
99void SiteSettingsTest::checkExternalPage(WebTab *tab, QUrl url)
100{
101 SiteSettingsManager *siteSettings = mApp->siteSettingsManager();
102
103 QSignalSpy spy(tab, SIGNAL(loadingChanged(bool)));
104 tab->load(url);
105 QTRY_COMPARE_WITH_TIMEOUT(spy.count(), 3, 20000);
106
107 auto *page = tab->webView()->page();
108 QCOMPARE(checkWebAttributes(page, siteSettings->getWebAttributes(url)), true);
109}
110
#define FALKONTEST_MAIN(Test)
Definition: autotests.h:23
QHash< QWebEngineSettings::WebAttribute, bool > getWebAttributes(const QUrl &url)
void setOption(const QString &column, const QUrl &url, const int value)
Definition: webtab.h:40
void load(const LoadRequest &request)
Definition: webtab.cpp:391
TabbedWebView * webView() const
Definition: webtab.cpp:209
WebPage * page() const
Definition: webview.cpp:132
#define mApp
#define QSL(x)
Definition: qzcommon.h:40