| 1 |
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX |
| 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 under |
| 6 |
# 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 |
|
| 11 |
""" |
| 12 |
This example demonstrates how to test interacting with a textbox created |
| 13 |
using FileEditor. |
| 14 |
|
| 15 |
The GUI being tested is written in the demo under the same name (minus the |
| 16 |
preceding 'test') in the outer directory. |
| 17 |
""" |
| 18 |
|
| 19 |
import os |
| 20 |
import runpy |
| 21 |
import unittest |
| 22 |
|
| 23 |
from traitsui.testing.api import DisplayedText, KeyClick, KeySequence, UITester |
| 24 |
|
| 25 |
#: Filename of the demo script |
| 26 |
FILENAME = "FileEditor_demo.py" |
| 27 |
|
| 28 |
#: Path of the demo script |
| 29 |
DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME) |
| 30 |
|
| 31 |
|
| 32 |
class TestFileEditorDemo(unittest.TestCase): |
| 33 |
def test_run_demo(self): |
| 34 |
demo = runpy.run_path(DEMO_PATH)["demo"] |
| 35 |
|
| 36 |
tester = UITester() |
| 37 |
with tester.create_ui(demo) as ui: |
| 38 |
# simple FileEditor |
| 39 |
simple_file_field = tester.find_by_id(ui, "simple_file") |
| 40 |
|
| 41 |
# Modify the value on the GUI |
| 42 |
# The path does not exist, which is allowed by the trait. |
| 43 |
# The custom FileEditor will ignore nonexisting file path. |
| 44 |
simple_file_field.perform(KeySequence("some_path")) |
| 45 |
simple_file_field.perform(KeyClick("Enter")) |
| 46 |
self.assertEqual(demo.file_name, "some_path") |
| 47 |
|
| 48 |
# Modify the value on the object |
| 49 |
demo.file_name = FILENAME |
| 50 |
self.assertEqual( |
| 51 |
simple_file_field.inspect(DisplayedText()), FILENAME |
| 52 |
) |
| 53 |
|
| 54 |
# On wx, an assertion error occurs upon disposing the UI. |
| 55 |
# That error is linked to the custom FileEditor. |
| 56 |
# See enthought/traitsui#752 |
| 57 |
|
| 58 |
|
| 59 |
# Run the test(s) |
| 60 |
unittest.TextTestRunner().run( |
| 61 |
unittest.TestLoader().loadTestsFromTestCase(TestFileEditorDemo) |
| 62 |
) |