From 9e7a09cec751d526d2a9217c648419ae4dcc4505 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 30 May 2022 20:37:21 +0200 Subject: test that enums match their Qt equivalents Also changes reloaded -> reload for consistency (both to Qt and between the names). --- qutebrowser/browser/webengine/webview.py | 54 +++++++++---------- qutebrowser/browser/webkit/webpage.py | 4 +- qutebrowser/utils/usertypes.py | 2 +- tests/unit/browser/webengine/test_webview.py | 75 +++++++++++++++++++++++++++ tests/unit/browser/webkit/test_webkit_view.py | 32 ++++++++++++ tests/unit/browser/webkit/test_webview.py | 32 ------------ 6 files changed, 138 insertions(+), 61 deletions(-) create mode 100644 tests/unit/browser/webengine/test_webview.py create mode 100644 tests/unit/browser/webkit/test_webkit_view.py delete mode 100644 tests/unit/browser/webkit/test_webview.py diff --git a/qutebrowser/browser/webengine/webview.py b/qutebrowser/browser/webengine/webview.py index 739732efc..61dfafb30 100644 --- a/qutebrowser/browser/webengine/webview.py +++ b/qutebrowser/browser/webengine/webview.py @@ -163,6 +163,32 @@ class WebEnginePage(QWebEnginePage): shutting_down = pyqtSignal() navigation_request = pyqtSignal(usertypes.NavigationRequest) + _JS_LOG_LEVEL_MAPPING = { + QWebEnginePage.JavaScriptConsoleMessageLevel.InfoMessageLevel: + usertypes.JsLogLevel.info, + QWebEnginePage.JavaScriptConsoleMessageLevel.WarningMessageLevel: + usertypes.JsLogLevel.warning, + QWebEnginePage.JavaScriptConsoleMessageLevel.ErrorMessageLevel: + usertypes.JsLogLevel.error, + } + + _NAVIGATION_TYPE_MAPPING = { + QWebEnginePage.NavigationType.NavigationTypeLinkClicked: + usertypes.NavigationRequest.Type.link_clicked, + QWebEnginePage.NavigationType.NavigationTypeTyped: + usertypes.NavigationRequest.Type.typed, + QWebEnginePage.NavigationType.NavigationTypeFormSubmitted: + usertypes.NavigationRequest.Type.form_submitted, + QWebEnginePage.NavigationType.NavigationTypeBackForward: + usertypes.NavigationRequest.Type.back_forward, + QWebEnginePage.NavigationType.NavigationTypeReload: + usertypes.NavigationRequest.Type.reload, + QWebEnginePage.NavigationType.NavigationTypeOther: + usertypes.NavigationRequest.Type.other, + QWebEnginePage.NavigationType.NavigationTypeRedirect: + usertypes.NavigationRequest.Type.redirect, + } + def __init__(self, *, theme_color, profile, parent=None): super().__init__(profile, parent) self._is_shutting_down = False @@ -230,40 +256,16 @@ class WebEnginePage(QWebEnginePage): def javaScriptConsoleMessage(self, level, msg, line, source): """Log javascript messages to qutebrowser's log.""" - # FIXME:qt6 Add tests to ensure this is complete - level_map = { - QWebEnginePage.JavaScriptConsoleMessageLevel.InfoMessageLevel: usertypes.JsLogLevel.info, - QWebEnginePage.JavaScriptConsoleMessageLevel.WarningMessageLevel: usertypes.JsLogLevel.warning, - QWebEnginePage.JavaScriptConsoleMessageLevel.ErrorMessageLevel: usertypes.JsLogLevel.error, - } - shared.javascript_log_message(level_map[level], source, line, msg) + shared.javascript_log_message(self._JS_LOG_LEVEL_MAPPING[level], source, line, msg) def acceptNavigationRequest(self, url: QUrl, typ: QWebEnginePage.NavigationType, is_main_frame: bool) -> bool: """Override acceptNavigationRequest to forward it to the tab API.""" - # FIXME:qt6 Add tests to ensure this is complete - type_map = { - QWebEnginePage.NavigationType.NavigationTypeLinkClicked: - usertypes.NavigationRequest.Type.link_clicked, - QWebEnginePage.NavigationType.NavigationTypeTyped: - usertypes.NavigationRequest.Type.typed, - QWebEnginePage.NavigationType.NavigationTypeFormSubmitted: - usertypes.NavigationRequest.Type.form_submitted, - QWebEnginePage.NavigationType.NavigationTypeBackForward: - usertypes.NavigationRequest.Type.back_forward, - QWebEnginePage.NavigationType.NavigationTypeReload: - usertypes.NavigationRequest.Type.reloaded, - QWebEnginePage.NavigationType.NavigationTypeOther: - usertypes.NavigationRequest.Type.other, - QWebEnginePage.NavigationType.NavigationTypeRedirect: - usertypes.NavigationRequest.Type.redirect, - } - navigation = usertypes.NavigationRequest( url=url, - navigation_type=type_map.get( + navigation_type=self._NAVIGATION_TYPE_MAPPING.get( typ, usertypes.NavigationRequest.Type.other), is_main_frame=is_main_frame) self.navigation_request.emit(navigation) diff --git a/qutebrowser/browser/webkit/webpage.py b/qutebrowser/browser/webkit/webpage.py index 297c6ac88..1a6e7ea82 100644 --- a/qutebrowser/browser/webkit/webpage.py +++ b/qutebrowser/browser/webkit/webpage.py @@ -519,7 +519,7 @@ class BrowserPage(QWebPage): QWebPage.NavigationType.NavigationTypeBackOrForward: usertypes.NavigationRequest.Type.back_forward, QWebPage.NavigationType.NavigationTypeReload: - usertypes.NavigationRequest.Type.reloaded, + usertypes.NavigationRequest.Type.reload, QWebPage.NavigationType.NavigationTypeOther: usertypes.NavigationRequest.Type.other, } @@ -528,7 +528,7 @@ class BrowserPage(QWebPage): navigation_type=type_map[typ], is_main_frame=is_main_frame) - if navigation.navigation_type == navigation.Type.reloaded: + if navigation.navigation_type == navigation.Type.reload: self.reloading.emit(navigation.url) self.navigation_request.emit(navigation) diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index c1e61583a..b84af4524 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -546,7 +546,7 @@ class NavigationRequest: #: Navigation initiated by a history action. back_forward = 5 #: Navigation initiated by refreshing the page. - reloaded = 6 + reload = 6 #: Navigation triggered automatically by page content or remote server #: (QtWebEngine >= 5.14 only) redirect = 7 diff --git a/tests/unit/browser/webengine/test_webview.py b/tests/unit/browser/webengine/test_webview.py new file mode 100644 index 000000000..2ddcc8733 --- /dev/null +++ b/tests/unit/browser/webengine/test_webview.py @@ -0,0 +1,75 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2022 Florian Bruhin (The Compiler) +# +# This file is part of qutebrowser. +# +# qutebrowser 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. +# +# qutebrowser 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 qutebrowser. If not, see . + +import re +import dataclasses + +import pytest +webview = pytest.importorskip('qutebrowser.browser.webengine.webview') + +from qutebrowser.qt.webenginecore import QWebEnginePage + +from helpers import testutils + + +@dataclasses.dataclass +class Naming: + + prefix: str = "" + suffix: str = "" + + +def camel_to_snake(naming, name): + if naming.prefix: + assert name.startswith(naming.prefix) + name = name[len(naming.prefix):] + if naming.suffix: + assert name.endswith(naming.suffix) + name = name[:-len(naming.suffix)] + # https://stackoverflow.com/a/1176023 + return re.sub(r'(? +# +# This file is part of qutebrowser. +# +# qutebrowser 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. +# +# qutebrowser 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 qutebrowser. If not, see . + +import pytest +webview = pytest.importorskip('qutebrowser.browser.webkit.webview') + + +@pytest.fixture +def real_webview(webkit_tab, qtbot): + wv = webview.WebView(win_id=0, tab_id=0, tab=webkit_tab, private=False) + qtbot.add_widget(wv) + return wv + + +def test_background_color_none(config_stub, real_webview): + config_stub.val.colors.webpage.bg = None diff --git a/tests/unit/browser/webkit/test_webview.py b/tests/unit/browser/webkit/test_webview.py deleted file mode 100644 index fcaaa2256..000000000 --- a/tests/unit/browser/webkit/test_webview.py +++ /dev/null @@ -1,32 +0,0 @@ -# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: - -# Copyright 2019-2021 Florian Bruhin (The Compiler) -# -# This file is part of qutebrowser. -# -# qutebrowser 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. -# -# qutebrowser 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 qutebrowser. If not, see . - -import pytest -webview = pytest.importorskip('qutebrowser.browser.webkit.webview') - - -@pytest.fixture -def real_webview(webkit_tab, qtbot): - wv = webview.WebView(win_id=0, tab_id=0, tab=webkit_tab, private=False) - qtbot.add_widget(wv) - return wv - - -def test_background_color_none(config_stub, real_webview): - config_stub.val.colors.webpage.bg = None -- cgit v1.2.3-54-g00ecf