1#  Copyright (c) 2005-2020, Enthought, Inc.
2#  All rights reserved.
3#
4#  This software is provided without warranty under the terms of the BSD
5#  license included in LICENSE.txt and may be redistributed only
6#  under the conditions described in the aforementioned license.  The license
7#  is also available online at http://www.enthought.com/licenses/BSD.txt
8#
9#  Thanks for using Enthought open source!
10
11import unittest
12
13from traits.api import HasTraits, Str
14from traitsui.api import HTMLEditor, Item, View
15from traitsui.tests._tools import (
16    BaseTestMixin,
17    create_ui,
18    requires_toolkit,
19    reraise_exceptions,
20    ToolkitName,
21)
22
23
24class HTMLModel(HasTraits):
25    """ Dummy class for testing HTMLEditor."""
26
27    content = Str()
28
29    model_base_url = Str()
30
31
32def get_view(base_url_name):
33    return View(
34        Item(
35            "content",
36            editor=HTMLEditor(
37                format_text=True,
38                base_url_name=base_url_name,
39            )
40        )
41    )
42
43
44# Run this against wx as well once enthought/traitsui#752 is fixed.
45@requires_toolkit([ToolkitName.qt])
46class TestHTMLEditor(BaseTestMixin, unittest.TestCase):
47    """ Test HTMLEditor """
48
49    def setUp(self):
50        BaseTestMixin.setUp(self)
51
52    def tearDown(self):
53        BaseTestMixin.tearDown(self)
54
55    def test_init_and_dispose(self):
56        # Smoke test to check init and dispose do not fail.
57        model = HTMLModel()
58        view = get_view(base_url_name="")
59        with reraise_exceptions(), \
60                create_ui(model, dict(view=view)):
61            pass
62
63    def test_base_url_changed(self):
64        # Test if the base_url is changed after the UI closes, nothing
65        # fails because sync_value is unhooked in the base class.
66        model = HTMLModel()
67        view = get_view(base_url_name="model_base_url")
68        with reraise_exceptions():
69            with create_ui(model, dict(view=view)):
70                pass
71            # It is okay to modify base_url after the UI is closed
72            model.model_base_url = "/new_dir"
73