| 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 the various styles of |
| 13 |
RangeEditor. |
| 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 |
Index, |
| 26 |
KeyClick, |
| 27 |
KeySequence, |
| 28 |
MouseClick, |
| 29 |
Slider, |
| 30 |
Textbox, |
| 31 |
UITester, |
| 32 |
) |
| 33 |
|
| 34 |
#: Filename of the demo script |
| 35 |
FILENAME = "RangeEditor_demo.py" |
| 36 |
|
| 37 |
#: Path of the demo script |
| 38 |
DEMO_PATH = os.path.join(os.path.dirname(__file__), "..", FILENAME) |
| 39 |
|
| 40 |
|
| 41 |
class TestRangeEditorDemo(unittest.TestCase): |
| 42 |
def test_run_demo(self): |
| 43 |
demo = runpy.run_path(DEMO_PATH)["demo"] |
| 44 |
|
| 45 |
tester = UITester() |
| 46 |
with tester.create_ui(demo) as ui: |
| 47 |
simple_small = tester.find_by_id(ui, 'simple_small') |
| 48 |
custom_small = tester.find_by_id(ui, 'custom_small') |
| 49 |
text_small = tester.find_by_id(ui, 'text_small') |
| 50 |
readonly_small = tester.find_by_id(ui, 'readonly_small') |
| 51 |
|
| 52 |
simple_medium = tester.find_by_id(ui, 'simple_medium') |
| 53 |
custom_medium = tester.find_by_id(ui, 'custom_medium') |
| 54 |
text_medium = tester.find_by_id(ui, 'text_medium') |
| 55 |
readonly_medium = tester.find_by_id(ui, 'readonly_medium') |
| 56 |
|
| 57 |
# Testing for SimpleSpinEditor is not supported yet so the simple |
| 58 |
# and custom styles for the large_range_int are not included here |
| 59 |
text_large = tester.find_by_id(ui, 'text_large') |
| 60 |
readonly_large = tester.find_by_id(ui, 'readonly_large') |
| 61 |
|
| 62 |
simple_float = tester.find_by_id(ui, 'simple_float') |
| 63 |
custom_float = tester.find_by_id(ui, 'custom_float') |
| 64 |
text_float = tester.find_by_id(ui, 'text_float') |
| 65 |
readonly_float = tester.find_by_id(ui, 'readonly_float') |
| 66 |
|
| 67 |
# Tests for the small_int_range ################################## |
| 68 |
simple_small_slider = simple_small.locate(Slider()) |
| 69 |
simple_small_slider.perform(KeyClick("Page Up")) |
| 70 |
self.assertEqual(demo.small_int_range, 2) |
| 71 |
simple_small_text = simple_small.locate(Textbox()) |
| 72 |
simple_small_text.perform(KeyClick("Backspace")) |
| 73 |
simple_small_text.perform(KeyClick("3")) |
| 74 |
simple_small_text.perform(KeyClick("Enter")) |
| 75 |
self.assertEqual(demo.small_int_range, 3) |
| 76 |
|
| 77 |
custom_small.locate(Index(0)).perform(MouseClick()) |
| 78 |
self.assertEqual(demo.small_int_range, 1) |
| 79 |
|
| 80 |
text_small.perform(KeyClick("0")) |
| 81 |
text_small.perform(KeyClick("Enter")) |
| 82 |
self.assertEqual(demo.small_int_range, 10) |
| 83 |
|
| 84 |
demo.small_int_range = 7 |
| 85 |
displayed_small = readonly_small.inspect(DisplayedText()) |
| 86 |
self.assertEqual(displayed_small, '7') |
| 87 |
|
| 88 |
# Tests for the medium_int_range ################################# |
| 89 |
simple_medium_slider = simple_medium.locate(Slider()) |
| 90 |
# on this range, page up/down corresponds to a change of 2. |
| 91 |
simple_medium_slider.perform(KeyClick("Page Up")) |
| 92 |
self.assertEqual(demo.medium_int_range, 3) |
| 93 |
simple_medium_text = simple_medium.locate(Textbox()) |
| 94 |
simple_medium_text.perform(KeyClick("Backspace")) |
| 95 |
simple_medium_text.perform(KeyClick("4")) |
| 96 |
simple_medium_text.perform(KeyClick("Enter")) |
| 97 |
self.assertEqual(demo.medium_int_range, 4) |
| 98 |
|
| 99 |
custom_medium_slider = custom_medium.locate(Slider()) |
| 100 |
custom_medium_slider.perform(KeyClick("Page Down")) |
| 101 |
self.assertEqual(demo.medium_int_range, 2) |
| 102 |
custom_medium_text = custom_medium.locate(Textbox()) |
| 103 |
custom_medium_text.perform(KeyClick("Backspace")) |
| 104 |
custom_medium_text.perform(KeyClick("1")) |
| 105 |
custom_medium_text.perform(KeyClick("Enter")) |
| 106 |
self.assertEqual(demo.medium_int_range, 1) |
| 107 |
|
| 108 |
text_medium.perform(KeyClick("0")) |
| 109 |
text_medium.perform(KeyClick("Enter")) |
| 110 |
self.assertEqual(demo.medium_int_range, 10) |
| 111 |
|
| 112 |
demo.medium_int_range = 7 |
| 113 |
displayed_medium = readonly_medium.inspect(DisplayedText()) |
| 114 |
self.assertEqual(displayed_medium, '7') |
| 115 |
|
| 116 |
# Tests for the large_int_range ################################## |
| 117 |
# Testing for SimpleSpinEditor is not supported yet |
| 118 |
text_large.perform(KeySequence("00")) |
| 119 |
text_large.perform(KeyClick("Enter")) |
| 120 |
self.assertEqual(demo.large_int_range, 100) |
| 121 |
|
| 122 |
demo.large_int_range = 77 |
| 123 |
displayed_large = readonly_large.inspect(DisplayedText()) |
| 124 |
self.assertEqual(displayed_large, '77') |
| 125 |
|
| 126 |
# Tests for the float_range ###################################### |
| 127 |
simple_float_slider = simple_float.locate(Slider()) |
| 128 |
# on this range, page up/down corresponds to a change of 1.000. |
| 129 |
simple_float_slider.perform(KeyClick("Page Up")) |
| 130 |
self.assertEqual(demo.float_range, 1.000) |
| 131 |
simple_float_text = simple_float.locate(Textbox()) |
| 132 |
for _ in range(3): |
| 133 |
simple_float_text.perform(KeyClick("Backspace")) |
| 134 |
simple_float_text.perform(KeyClick("5")) |
| 135 |
simple_float_text.perform(KeyClick("Enter")) |
| 136 |
self.assertEqual(demo.float_range, 1.5) |
| 137 |
|
| 138 |
custom_float_slider = custom_float.locate(Slider()) |
| 139 |
# after the trait is set to 1.5 above, the active range shown by |
| 140 |
# the LargeRangeSliderEditor for the custom style is [0,11.500] |
| 141 |
# so a page down is now a decrement of 1.15 |
| 142 |
custom_float_slider.perform(KeyClick("Page Down")) |
| 143 |
self.assertEqual(round(demo.float_range, 2), 0.35) |
| 144 |
custom_float_text = custom_float.locate(Textbox()) |
| 145 |
for _ in range(5): |
| 146 |
custom_float_text.perform(KeyClick("Backspace")) |
| 147 |
custom_float_text.perform(KeySequence("50.0")) |
| 148 |
custom_float_text.perform(KeyClick("Enter")) |
| 149 |
self.assertEqual(demo.float_range, 50.0) |
| 150 |
|
| 151 |
text_float.perform(KeyClick("5")) |
| 152 |
text_float.perform(KeyClick("Enter")) |
| 153 |
self.assertEqual(demo.float_range, 50.05) |
| 154 |
|
| 155 |
demo.float_range = 72.0 |
| 156 |
displayed_float = readonly_float.inspect(DisplayedText()) |
| 157 |
self.assertEqual(displayed_float, '72.0') |
| 158 |
|
| 159 |
|
| 160 |
# Run the test(s) |
| 161 |
unittest.TextTestRunner().run( |
| 162 |
unittest.TestLoader().loadTestsFromTestCase(TestRangeEditorDemo) |
| 163 |
) |