| 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 button created |
| 13 |
using ButtonEditor. |
| 14 |
It also demonstarates the use of UI Tester, together with pyface's |
| 15 |
ModalDialogTester. |
| 16 |
|
| 17 |
The GUI being tested is written in the demo under the same name (minus the |
| 18 |
preceding 'test') in the outer directory. |
| 19 |
""" |
| 20 |
|
| 21 |
import os |
| 22 |
import runpy |
| 23 |
import unittest |
| 24 |
|
| 25 |
from pyface.toolkit import toolkit_object |
| 26 |
from pyface.constant import OK |
| 27 |
|
| 28 |
from traitsui.testing.api import MouseClick, UITester |
| 29 |
|
| 30 |
ModalDialogTester = toolkit_object( |
| 31 |
"util.modal_dialog_tester:ModalDialogTester" |
| 32 |
) |
| 33 |
no_modal_dialog_tester = ModalDialogTester.__name__ == "Unimplemented" |
| 34 |
|
| 35 |
#: Filename of the demo script |
| 36 |
FILENAME = "ButtonEditor_demo.py" |
| 37 |
|
| 38 |
#: Path of the demo script |
| 39 |
DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME) |
| 40 |
|
| 41 |
|
| 42 |
@unittest.skipIf(no_modal_dialog_tester, "ModalDialogTester unavailable") |
| 43 |
class TestButtonEditorDemo(unittest.TestCase): |
| 44 |
def test_button_editor_demo_simple(self): |
| 45 |
demo = runpy.run_path(DEMO_PATH)["demo"] |
| 46 |
|
| 47 |
tester = UITester() |
| 48 |
with tester.create_ui(demo) as ui: |
| 49 |
|
| 50 |
simple_button = tester.find_by_id(ui, "simple") |
| 51 |
|
| 52 |
# funcion object for instantiating ModalDialogTester should be a |
| 53 |
# function that opens the dialog |
| 54 |
# we want clicking the buttons to do that |
| 55 |
def click_simple_button(): |
| 56 |
simple_button.perform(MouseClick()) |
| 57 |
|
| 58 |
mdtester_simple = ModalDialogTester(click_simple_button) |
| 59 |
mdtester_simple.open_and_run(lambda x: x.click_button(OK)) |
| 60 |
self.assertTrue(mdtester_simple.dialog_was_opened) |
| 61 |
|
| 62 |
def test_button_editor_demo_custom(self): |
| 63 |
demo = runpy.run_path(DEMO_PATH)["demo"] |
| 64 |
|
| 65 |
tester = UITester() |
| 66 |
with tester.create_ui(demo) as ui: |
| 67 |
|
| 68 |
custom_button = tester.find_by_id(ui, "custom") |
| 69 |
|
| 70 |
# funcion object for instantiating ModalDialogTester should be a |
| 71 |
# function that opens the dialog |
| 72 |
# we want clicking the buttons to do that |
| 73 |
def click_custom_button(): |
| 74 |
custom_button.perform(MouseClick()) |
| 75 |
|
| 76 |
mdtester_custom = ModalDialogTester(click_custom_button) |
| 77 |
mdtester_custom.open_and_run(lambda x: x.click_button(OK)) |
| 78 |
self.assertTrue(mdtester_custom.dialog_was_opened) |
| 79 |
|
| 80 |
|
| 81 |
# Run the test(s) |
| 82 |
unittest.TextTestRunner().run( |
| 83 |
unittest.TestLoader().loadTestsFromTestCase(TestButtonEditorDemo) |
| 84 |
) |