| 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 |
IsChecked, |
| 26 |
KeyClick, |
| 27 |
KeySequence, |
| 28 |
MouseClick, |
| 29 |
UITester, |
| 30 |
) |
| 31 |
|
| 32 |
#: Filename of the demo script |
| 33 |
FILENAME = "BooleanEditor_simple_demo.py" |
| 34 |
|
| 35 |
#: Path of the demo script |
| 36 |
DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME) |
| 37 |
|
| 38 |
|
| 39 |
class TestBooleanEditorSimpleDemo(unittest.TestCase): |
| 40 |
def test_boolean_editor_simple_demo(self): |
| 41 |
demo = runpy.run_path(DEMO_PATH)["demo"] |
| 42 |
|
| 43 |
tester = UITester() |
| 44 |
with tester.create_ui(demo) as ui: |
| 45 |
simple = tester.find_by_id(ui, 'simple') |
| 46 |
readonly = tester.find_by_id(ui, 'readonly') |
| 47 |
text = tester.find_by_id(ui, 'text') |
| 48 |
count_changes = tester.find_by_name(ui, "count_changes") |
| 49 |
|
| 50 |
simple.perform(MouseClick()) |
| 51 |
self.assertEqual(demo.my_boolean_trait, True) |
| 52 |
|
| 53 |
for _ in range(4): |
| 54 |
text.perform(KeyClick("Backspace")) |
| 55 |
text.perform(KeySequence("False")) |
| 56 |
text.perform(KeyClick("Enter")) |
| 57 |
self.assertEqual(demo.my_boolean_trait, False) |
| 58 |
|
| 59 |
displayed_count_changes = count_changes.inspect(DisplayedText()) |
| 60 |
self.assertEqual(displayed_count_changes, '2') |
| 61 |
self.assertEqual(displayed_count_changes, str(demo.count_changes)) |
| 62 |
|
| 63 |
demo.my_boolean_trait = True |
| 64 |
displayed = readonly.inspect(DisplayedText()) |
| 65 |
self.assertEqual(displayed, "True") |
| 66 |
|
| 67 |
simple_is_checked = simple.inspect(IsChecked()) |
| 68 |
self.assertEqual(simple_is_checked, True) |
| 69 |
|
| 70 |
|
| 71 |
# Run the test(s) |
| 72 |
unittest.TextTestRunner().run( |
| 73 |
unittest.TestLoader().loadTestsFromTestCase(TestBooleanEditorSimpleDemo) |
| 74 |
) |