File: sitesettingstest.cpp

package info (click to toggle)
falkon 25.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,344 kB
  • sloc: cpp: 66,939; javascript: 21,781; sh: 578; xml: 564; python: 496; sql: 75; makefile: 26
file content (109 lines) | stat: -rw-r--r-- 4,267 bytes parent folder | download | duplicates (2)
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
/* ============================================================
 * Falkon - Qt web browser
 * Copyright (C) 2024  Juraj Oravec <jurajoravec@mailo.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * ============================================================ */

#include "sitesettingstest.h"

#include "mainapplication.h"
#include "tabbedwebview.h"
#include "webpage.h"

#include "autotests.h"
#include "tabwidget.h"


void SiteSettingsTest::initTestCase()
{
    /* Wait until the initial tab (at index 0) in the window is created */
    QTRY_COMPARE(mApp->getWindow()->tabCount(), 1);
}

void SiteSettingsTest::cleanupTestCase()
{
}

void SiteSettingsTest::webAttributeTest()
{
    SiteSettingsManager *siteSettings = mApp->siteSettingsManager();
    siteSettings->setOption(QWebEngineSettings::AutoLoadImages, QUrl(QSL("https://www.falkon.org/")), SiteSettingsManager::Deny);
    siteSettings->setOption(QWebEngineSettings::JavascriptEnabled, QUrl(QSL("https://kde.org/")), SiteSettingsManager::Deny);
    siteSettings->setOption(QWebEngineSettings::PlaybackRequiresUserGesture, QUrl(QSL("https://planet.kde.org/")), SiteSettingsManager::Allow);


    checkInternalPage(QUrl(QSL("falkon:start")));

    checkExternalPage(QUrl(QSL("https://www.falkon.org/")));
    checkExternalPage(QUrl(QSL("https://kde.org/")));

    checkInternalPage(QUrl(QSL("falkon:about")));

    checkExternalPage(QUrl(QSL("https://planet.kde.org/")));
}

bool SiteSettingsTest::checkWebAttributes(WebPage *page, QHash<QWebEngineSettings::WebAttribute, bool> webAttributes)
{
    for (auto it = webAttributes.begin(); it != webAttributes.end(); ++it) {
        if (page->settings()->testAttribute(it.key()) != it.value()) {
            return false;
        }
    }

    return true;
}

void SiteSettingsTest::checkInternalPage(QUrl url)
{
    WebTab tab;
    QMap<QWebEngineSettings::WebAttribute, bool> internalWebAttributes = {
         {QWebEngineSettings::AutoLoadImages, true}
        ,{QWebEngineSettings::JavascriptEnabled, true}
        ,{QWebEngineSettings::JavascriptCanOpenWindows, false}
        ,{QWebEngineSettings::JavascriptCanAccessClipboard, true}
        ,{QWebEngineSettings::JavascriptCanPaste, false}
        ,{QWebEngineSettings::AllowWindowActivationFromJavaScript, false}
        ,{QWebEngineSettings::LocalStorageEnabled, true}
        ,{QWebEngineSettings::FullScreenSupportEnabled, mApp->webSettings()->testAttribute(QWebEngineSettings::FullScreenSupportEnabled)}
        ,{QWebEngineSettings::AllowRunningInsecureContent, false}
        ,{QWebEngineSettings::AllowGeolocationOnInsecureOrigins, false}
        ,{QWebEngineSettings::PlaybackRequiresUserGesture, mApp->webSettings()->testAttribute(QWebEngineSettings::PlaybackRequiresUserGesture)}
        ,{QWebEngineSettings::WebRTCPublicInterfacesOnly, mApp->webSettings()->testAttribute(QWebEngineSettings::WebRTCPublicInterfacesOnly)}
    };

    QSignalSpy spy(tab.webView(), &WebView::loadFinished);
    tab.load(url);
    QTRY_COMPARE(spy.count(), 1);

    auto *page = tab.webView()->page();
    for (auto it = internalWebAttributes.begin(); it != internalWebAttributes.end(); ++it) {
        QCOMPARE(page->settings()->testAttribute(it.key()), it.value());
    }
}

void SiteSettingsTest::checkExternalPage(QUrl url)
{
    WebTab tab;
    SiteSettingsManager *siteSettings = mApp->siteSettingsManager();

    QSignalSpy spy(tab.webView(), &WebView::loadFinished);
    tab.load(url);
    QTRY_COMPARE_WITH_TIMEOUT(spy.count(), 1, 20000);

    auto *page = tab.webView()->page();
    QCOMPARE(checkWebAttributes(page, siteSettings->getWebAttributes(url)), true);
}

FALKONTEST_MAIN(SiteSettingsTest)