test that enums match their Qt equivalents

Also changes reloaded -> reload for consistency
(both to Qt and between the names).
This commit is contained in:
Florian Bruhin 2022-05-30 20:37:21 +02:00
parent 96fedff22a
commit 541d49d08b
5 changed files with 106 additions and 29 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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

View File

@ -0,0 +1,75 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2022 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# 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 <https://www.gnu.org/licenses/>.
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'(?<!^)(?=[A-Z])', '_', name).lower()
@pytest.mark.parametrize("naming, name, expected", [
(Naming(prefix="NavigationType"), "NavigationTypeLinkClicked", "link_clicked"),
(Naming(prefix="NavigationType"), "NavigationTypeTyped", "typed"),
(Naming(prefix="NavigationType"), "NavigationTypeBackForward", "back_forward"),
(Naming(suffix="MessageLevel"), "InfoMessageLevel", "info"),
])
def test_camel_to_snake(naming, name, expected):
assert camel_to_snake(naming, name) == expected
@pytest.mark.parametrize("enum_type, naming, mapping", [
(
QWebEnginePage.JavaScriptConsoleMessageLevel,
Naming(suffix="MessageLevel"),
webview.WebEnginePage._JS_LOG_LEVEL_MAPPING,
),
(
QWebEnginePage.NavigationType,
Naming(prefix="NavigationType"),
webview.WebEnginePage._NAVIGATION_TYPE_MAPPING,
)
])
def test_enum_mappings(enum_type, naming, mapping):
members = testutils.enum_members(QWebEnginePage, enum_type).items()
for name, val in members:
mapped = mapping[val]
assert camel_to_snake(naming, name) == mapped.name