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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
import unittest
from unittest import mock
try:
from pyface.qt import QtWebkit # noqa: F401
NO_WEBKIT_OR_WEBENGINE = False
except ImportError:
try:
from pyface.qt import QtWebEngine # noqa: F401
NO_WEBKIT_OR_WEBENGINE = False
except ImportError:
NO_WEBKIT_OR_WEBENGINE = True
from traits.api import HasTraits, Str
from traitsui.api import HTMLEditor, Item, View
from traitsui.tests._tools import (
BaseTestMixin,
is_qt,
requires_toolkit,
ToolkitName,
)
from traitsui.testing.api import MouseClick, TargetRegistry, UITester
class HTMLModel(HasTraits):
"""Dummy class for testing HTMLEditor."""
content = Str()
model_base_url = Str()
def get_view(base_url_name):
return View(
Item(
"content",
editor=HTMLEditor(
format_text=True,
base_url_name=base_url_name,
),
)
)
class HTMLContent:
"""Action to retrieve the HTML content currently displayed.
Implementation should return a str, whose content conforms to HTML markup.
"""
pass
def _is_webkit_page(page):
"""Return true if the given page is a QWebPage from QtWebKit.
Intended for handling the compatibility between QtWebKit and QtWebEngine.
Parameters
----------
page : QWebEnginePage or QWebPage
"""
return hasattr(page, "setLinkDelegationPolicy")
def qt_get_page_html_content(page):
"""Return the HTML content currently being viewed.
Parameters
----------
page : QWebEnginePage or QWebPage
Returns
-------
html : str
"""
qt_allow_view_to_load(page)
if _is_webkit_page(page):
return page.mainFrame().toHtml()
content = []
page.toHtml(content.append)
qt_allow_view_to_load(page)
return "".join(content)
def wait_for_qt_signal(qt_signal, timeout):
"""Wait for the given Qt signal to fire, or timeout.
A mock implementation of QSignalSpy.wait, which is one of the missing
bindings in PySide2, and is not available in Qt4.
Parameters
----------
qt_signal : signal
Qt signal to wait for
timeout : int
Timeout in milliseconds, to match Qt API.
Raises
------
RuntimeError
"""
from pyface.qt import QtCore
# QEventLoop is used instead of QApplication due to observed
# hangs with Qt4.
event_loop = QtCore.QEventLoop()
def exit(*args, **kwargs):
event_loop.quit()
timeout_timer = QtCore.QTimer()
timeout_timer.setSingleShot(True)
timeout_timer.setInterval(timeout)
timeout_timer.timeout.connect(exit)
qt_signal.connect(exit)
timeout_timer.start()
event_loop.exec_()
qt_signal.disconnect(exit)
if timeout_timer.isActive():
timeout_timer.stop()
else:
raise RuntimeError("Timeout waiting for signal.")
def qt_allow_view_to_load(loadable, timeout=0.5):
"""Allow QWebView/QWebPage/QWebEngineView/QWebEnginePage to finish
loading.
Out of context, this function does not know if the page has started
loading. Therefore no timeout error is raised.
For most testing purposes, this function should be good enough to
avoid interacting with the Qt web page before it has finished loading, at
a cost of a slower test.
Parameters
----------
loadable : QWebView or QWebPage or QWebEngineView or QWebEnginePage
The view / page to allow loading to finish. Any object with the
loadFinished signal can be used.
timeout : float
Timeout in seconds for each signal being observed.
"""
timeout_ms = round(timeout * 1000)
try:
wait_for_qt_signal(loadable.loadFinished, timeout=timeout_ms)
except RuntimeError:
return
def qt_mouse_click_web_view(view, delay):
"""Perform a mouse click at the center of the web view.
Note that the page is allowed time to load before and after the mouse
click.
Parameters
----------
view : QWebView or QWebEngineView
"""
from pyface.qt import QtCore
from pyface.qt.QtTest import QTest
qt_allow_view_to_load(view)
if view.focusProxy() is not None:
# QWebEngineView
widget = view.focusProxy()
else:
# QWebView
widget = view
try:
QTest.mouseClick(
widget,
QtCore.Qt.MouseButton.LeftButton,
QtCore.Qt.KeyboardModifier.NoModifier,
delay=delay,
)
finally:
qt_allow_view_to_load(view)
def qt_target_registry():
"""Return an instance of TargetRegistry for testing Qt + HTMLEditor
Returns
-------
target_registry : TargetRegistry
"""
from traitsui.qt.html_editor import SimpleEditor
registry = TargetRegistry()
registry.register_interaction(
target_class=SimpleEditor,
interaction_class=MouseClick,
handler=lambda wrapper, _: qt_mouse_click_web_view(
wrapper._target.control, wrapper.delay
),
)
registry.register_interaction(
target_class=SimpleEditor,
interaction_class=HTMLContent,
handler=lambda wrapper, _: (
qt_get_page_html_content(wrapper._target.control.page())
),
)
return registry
def get_custom_ui_tester():
"""Return an instance of UITester that contains extended testing
functionality for HTMLEditor. These implementations are used by
TraitsUI only, are more ad hoc than they would have been if they were made
public.
"""
if is_qt():
return UITester(registries=[qt_target_registry()])
return UITester()
# Run this against wx as well once enthought/traitsui#752 is fixed.
@requires_toolkit([ToolkitName.qt])
@unittest.skipIf(
NO_WEBKIT_OR_WEBENGINE, "Tests require either QtWebKit or QtWebEngine"
)
class TestHTMLEditor(BaseTestMixin, unittest.TestCase):
"""Test HTMLEditor"""
def setUp(self):
BaseTestMixin.setUp(self)
self.tester = get_custom_ui_tester()
def tearDown(self):
BaseTestMixin.tearDown(self)
def test_init_and_dispose(self):
# Smoke test to check init and dispose do not fail.
model = HTMLModel()
view = get_view(base_url_name="")
with self.tester.create_ui(model, dict(view=view)):
pass
def test_base_url_changed(self):
# Test if the base_url is changed after the UI closes, nothing
# fails because sync_value is unhooked in the base class.
model = HTMLModel()
view = get_view(base_url_name="model_base_url")
with self.tester.create_ui(model, dict(view=view)):
pass
# It is okay to modify base_url after the UI is closed
model.model_base_url = "/new_dir"
@requires_toolkit([ToolkitName.qt])
def test_open_internal_link(self):
# this test requires Qt because it relies on the link filling up
# the entire page through the use of CSS, which isn't supported
# by Wx.
model = HTMLModel(
content="""
<html>
<a
href='/#'
target='_self'
style='display:block; width: 100%; height: 100%'>
Internal Link
</a>
</html>
"""
)
view = View(Item("content", editor=HTMLEditor()))
with self.tester.create_ui(model, dict(view=view)) as ui:
html_view = self.tester.find_by_name(ui, "content")
# when
with mock.patch("webbrowser.open_new") as mocked_browser:
html_view.perform(MouseClick())
mocked_browser.assert_not_called()
@requires_toolkit([ToolkitName.qt])
def test_open_external_link(self):
# this test requires Qt because it relies on the link filling up
# the entire page through the use of CSS, which isn't supported
# by Wx.
model = HTMLModel(
content="""
<html>
<a
href='test://testing'
target='_blank'
style='display:block; width: 100%; height: 100%'>
External Link
</a>
</html>
"""
)
view = View(Item("content", editor=HTMLEditor()))
with self.tester.create_ui(model, dict(view=view)) as ui:
html_view = self.tester.find_by_name(ui, "content")
with mock.patch("webbrowser.open_new") as mocked_browser:
html_view.perform(MouseClick())
self.assertIn(
"External Link",
html_view.inspect(HTMLContent()),
)
# See enthought/traitsui#1464
# This is the expected behaviour:
# mocked_browser.assert_called_once_with("test://testing")
# However, this is the current unexpected behaviour
mocked_browser.assert_not_called()
@requires_toolkit([ToolkitName.qt])
def test_open_internal_link_externally(self):
# this test requires Qt because it relies on the link filling up
# the entire page through the use of CSS, which isn't supported
# by Wx.
model = HTMLModel(
content="""
<html>
<a
href='test://testing'
target='_self'
style='display:block; width: 100%; height: 100%'>
Internal Link
</a>
</html>
"""
)
view = View(Item("content", editor=HTMLEditor(open_externally=True)))
with self.tester.create_ui(model, dict(view=view)) as ui:
html_view = self.tester.find_by_name(ui, "content")
with mock.patch("webbrowser.open_new") as mocked_browser:
html_view.perform(MouseClick())
self.assertIn(
"Internal Link",
html_view.inspect(HTMLContent()),
)
mocked_browser.assert_called_once_with("test://testing")
@requires_toolkit([ToolkitName.qt])
def test_open_external_link_externally(self):
model = HTMLModel(
content="""
<html>
<a
href='test://testing'
target='_blank'
style='display:block; width: 100%; height: 100%'>
External Link
</a>
</html>
"""
)
view = View(Item("content", editor=HTMLEditor(open_externally=True)))
with self.tester.create_ui(model, dict(view=view)) as ui:
html_view = self.tester.find_by_name(ui, "content")
with mock.patch("webbrowser.open_new") as mocked_browser:
html_view.perform(MouseClick())
self.assertIn(
"External Link",
html_view.inspect(HTMLContent()),
)
is_webkit = _is_webkit_page(html_view._target.control.page())
if is_webkit:
# This is the expected behavior.
mocked_browser.assert_called_once_with("test://testing")
else:
# Expected failure:
# See enthought/traitsui#1464
# This is the current unexpected behavior if QtWebEngine is used.
mocked_browser.assert_not_called()
|