| 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 boolean |
| 13 |
using BooleanEditor. |
| 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 ( |
| 24 |
DisplayedText, |
| 25 |
KeyClick, |
| 26 |
KeySequence, |
| 27 |
MouseClick, |
| 28 |
UITester, |
| 29 |
) |
| 30 |
|
| 31 |
#: Filename of the demo script |
| 32 |
FILENAME = "BooleanEditor_demo.py" |
| 33 |
|
| 34 |
#: Path of the demo script |
| 35 |
DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME) |
| 36 |
|
| 37 |
|
| 38 |
class TestBooleanEditorDemo(unittest.TestCase): |
| 39 |
def test_boolean_editor_demo(self): |
| 40 |
demo = runpy.run_path(DEMO_PATH)["demo"] |
| 41 |
|
| 42 |
tester = UITester() |
| 43 |
with tester.create_ui(demo) as ui: |
| 44 |
simple = tester.find_by_id(ui, 'simple') |
| 45 |
custom = tester.find_by_id(ui, 'custom') |
| 46 |
text = tester.find_by_id(ui, 'text') |
| 47 |
readonly = tester.find_by_id(ui, 'readonly') |
| 48 |
|
| 49 |
simple.perform(MouseClick()) |
| 50 |
self.assertEqual(demo.boolean_trait, True) |
| 51 |
custom.perform(MouseClick()) |
| 52 |
self.assertEqual(demo.boolean_trait, False) |
| 53 |
|
| 54 |
for _ in range(5): |
| 55 |
text.perform(KeyClick("Backspace")) |
| 56 |
text.perform(KeySequence("True")) |
| 57 |
text.perform(KeyClick("Enter")) |
| 58 |
self.assertEqual(demo.boolean_trait, True) |
| 59 |
|
| 60 |
demo.boolean_trait = False |
| 61 |
displayed = readonly.inspect(DisplayedText()) |
| 62 |
self.assertEqual(displayed, "False") |
| 63 |
|
| 64 |
|
| 65 |
# Run the test(s) |
| 66 |
unittest.TextTestRunner().run( |
| 67 |
unittest.TestLoader().loadTestsFromTestCase(TestBooleanEditorDemo) |
| 68 |
) |