Set up custom profile for webengine_tab fixture

Make sure webenginesettings.default_profile is set before creating a
webengine tab. The webengine_tab fixture is used in the web_tab and
tabbed_browser fixtures.

Previously when `WebEngineView.__init__()` ran default_profile was set
to None which was causing the tabs to be created pointing to the Qt
default profile. Which was working fine until I moved the common JS
scripts used for hint mode etc up to the profile, now they just weren't
getting run.

Unfortunately using a fully set up default profile was too tricky for
me. The stuff in webenginesettings around setting up the default profile
is written specifically for setting up global stuff when the real
application. Apart from init_profile() depending on a bunch of other
global singletons (more to set up) the user agent setting is hard to
work with. There is an assert in `_init_default_profile()` if the global
user agent string is already set. If you bypass the global user agent
string then websettings chokes on it not being set (websettings is
called from webenginesettings).

That's probably all possible to unpick, just need to go through the
codepaths noting down what the regiments for the application path are
and what needs to be operable for tests. We will probably make it a bit
better when moving more towards containers anyway.

Alternatively we could have a session/request scope autouse=True fixture
to set up all the real global stuff. That might be benefitial to make
the tests faster anyway. Going through the full profile setup stuff for
every test might make them a bit slower.

A couple of the caret tests are still failing. Not sure why.
This commit is contained in:
toofar 2023-05-21 15:32:32 +12:00
parent 025757a9c1
commit 8f89499cd5
1 changed files with 29 additions and 1 deletions

View File

@ -40,6 +40,7 @@ import py.path
from qutebrowser.qt.core import QSize, Qt
from qutebrowser.qt.widgets import QWidget, QHBoxLayout, QVBoxLayout
from qutebrowser.qt.network import QNetworkCookieJar
from qutebrowser.qt.webenginecore import QWebEngineProfile # TODO: will this break on webkit?
import helpers.stubs as stubsmod
import qutebrowser
@ -229,10 +230,37 @@ def webkit_tab(web_tab_setup, qtbot, cookiejar_and_cache, mode_manager,
tab.private_api.shutdown()
@pytest.fixture
def default_profile(testdata_scheme, monkeypatch):
"""Make sure the qutebrowser default profile is set.
This profile is not currently fully set up the same as the real one would
be.
webenginesettings._init_default_profile() does more setup, for example
setting cache and data dirs, but that method can't be called multiple
times which makes it hard to use in a fixture. Maybe we should make that
method re-runnable or pull out the non-global stuff when called with an
existing profile.
There is a similar fixture in
tests/unit/browser/webengine/test_webenginesettings.py which we should
look at merging into this.
"""
webenginesettings = pytest.importorskip(
'qutebrowser.browser.webengine.webenginesettings')
profile = QWebEngineProfile()
_qute_scheme_handler.install(profile)
webenginesettings._init_scripts_for_profile(profile)
monkeypatch.setattr(webenginesettings, 'default_profile', profile)
@pytest.fixture
def webengine_tab(web_tab_setup, qtbot, redirect_webengine_data,
tabbed_browser_stubs, mode_manager, widget_container,
monkeypatch):
monkeypatch, default_profile):
monkeypatch.setattr(objects, 'backend', usertypes.Backend.QtWebEngine)
tabwidget = tabbed_browser_stubs[0].widget