def test_modify_slider_log_range_slider(self): model = RangeModel() view = View( Item( "float_value", editor=RangeEditor(low=.1, high=1000000000, mode='logslider') ) ) tester = UITester() with tester.create_ui(model, dict(view=view)) as ui: number_field = tester.find_by_name(ui, "float_value") slider = number_field.locate(Slider()) text = number_field.locate(Textbox()) # 10 steps is equivalent to 1 page step # on this scale either of those is equivalent to increasing the # trait's value from 10^n to 10^(n+1) for _ in range(10): slider.perform(KeyClick("Right")) displayed = text.inspect(DisplayedText()) self.assertEqual(model.float_value, 1.0) self.assertEqual(displayed, str(model.float_value)) slider.perform(KeyClick("Page Up")) displayed = text.inspect(DisplayedText()) self.assertEqual(model.float_value, 10.0) self.assertEqual(displayed, str(model.float_value))
def check_modify_slider(self, mode): model = RangeModel(value=0) view = View( Item( "value", editor=RangeEditor(low=0, high=10, mode=mode) ) ) tester = UITester() with tester.create_ui(model, dict(view=view)) as ui: number_field = tester.find_by_name(ui, "value") slider = number_field.locate(Slider()) text = number_field.locate(Textbox()) # slider values are converted to a [0, 10000] scale. A single # step is a change of 100 on that scale and a page step is 1000. # Our range in [0, 10] so these correspond to changes of .1 and 1. # Note: when tested manually, the step size seen on OSX and Wx is # different. for _ in range(10): slider.perform(KeyClick("Right")) displayed = text.inspect(DisplayedText()) self.assertEqual(model.value, 1) self.assertEqual(displayed, str(model.value)) slider.perform(KeyClick("Page Up")) displayed = text.inspect(DisplayedText()) self.assertEqual(model.value, 2) self.assertEqual(displayed, str(model.value))
def test_useful_err_message(self): obj = ListTraitTest(people=get_people(), style="simple") tester = UITester() with tester.create_ui(obj) as ui: with self.assertRaises(LocationNotSupported) as exc: people_list = tester.find_by_name(ui, "people") people_list.locate(Textbox()) self.assertIn(Index, exc.exception.supported)
def check_slider_set_with_text_valid(self, mode): model = RangeModel() view = View( Item("value", editor=RangeEditor(low=1, high=12, mode=mode))) tester = UITester() with tester.create_ui(model, dict(view=view)) as ui: # sanity check self.assertEqual(model.value, 1) number_field = tester.find_by_name(ui, "value") text = number_field.locate(Textbox()) text.perform(KeyClick("0")) text.perform(KeyClick("Enter")) displayed = text.inspect(DisplayedText()) self.assertEqual(model.value, 10) self.assertEqual(displayed, str(model.value))
def test_modify_out_of_range(self): obj = RangeExcludeLow() tester = UITester(auto_process_events=False) with tester.create_ui(obj) as ui: number_field = tester.find_by_name(ui, "x") text = number_field.locate(Textbox()) # should not fail def set_out_of_range(): text.perform(KeyClick("Backspace")) text.perform(KeyClick("0")) text.perform(KeyClick("Enter")) mdtester = ModalDialogTester(set_out_of_range) mdtester.open_and_run(lambda x: x.close(accept=True))
def test_editor_factory_format(self): """ format trait on RangeEditor editor factory has been deprecated in favor of format_str. However, behavior should be unchanged. """ model = RangeModel() with self.assertWarns(DeprecationWarning): view = View( Item("float_value", editor=RangeEditor(format="%s ..."))) tester = UITester() with tester.create_ui(model, dict(view=view)) as ui: float_value_field = tester.find_by_name(ui, "float_value") float_value_text = float_value_field.locate(Textbox()) self.assertEqual(float_value_text.inspect(DisplayedText()), "0.1 ...")
def check_slider_set_with_text_after_empty(self, mode): model = RangeModel() view = View( Item("value", editor=RangeEditor(low=1, high=12, mode=mode))) tester = UITester() with tester.create_ui(model, dict(view=view)) as ui: number_field = tester.find_by_name(ui, "value") text = number_field.locate(Textbox()) # Delete all contents of textbox for _ in range(5): text.perform(KeyClick("Backspace")) text.perform(KeySequence("11")) text.perform(KeyClick("Enter")) displayed = text.inspect(DisplayedText()) self.assertEqual(model.value, 11) self.assertEqual(displayed, str(model.value))
def test_editor_format(self): """ The format trait on an Editor instance previously potentially could override the factory. Now that is not the case. """ model = RangeModel() with self.assertWarns(DeprecationWarning): view = View( Item("float_value", editor=RangeEditor(format="%s ..."))) tester = UITester() with tester.create_ui(model, dict(view=view)) as ui: float_value_field = tester.find_by_name(ui, "float_value") float_value_field._target.format = "%s +++" model.float_value = 0.2 float_value_text = float_value_field.locate(Textbox()) self.assertEqual(float_value_text.inspect(DisplayedText()), "0.2 ...")
def test_editor_error_msg(self): from pyface.qt import QtCore, QtGui class Foo(HasTraits): x = Range(low=0.0, high=1.0, value=0.5, exclude_low=True) foo = Foo() tester = UITester(auto_process_events=False) with tester.create_ui(foo) as ui: x_range = tester.find_by_name(ui, "x") x_range_textbox = x_range.locate(Textbox()) x_range_textbox.perform(KeyClick('Backspace')) x_range_textbox.perform(KeySequence('0')) def trigger_error(): x_range_textbox.perform(KeyClick('Enter')) def check_and_close(mdtester): try: with mdtester.capture_error(): self.assertTrue( mdtester.has_widget( text="The 'x' trait of a Foo instance must be " "0.0 < a floating point number <= 1.0, " "but a value of 0.0 <class 'float'> was " "specified.", type_=QtGui.QMessageBox, ) ) self.assertEqual( mdtester.get_dialog_widget().textFormat(), QtCore.Qt.TextFormat.PlainText, ) finally: mdtester.close(accept=True) self.assertTrue(mdtester.dialog_was_opened) mdtester = ModalDialogTester(trigger_error) mdtester.open_and_run(check_and_close) self.assertTrue(mdtester.dialog_was_opened)
def test_format_func(self): def num_to_time(num): minutes = int(num / 60) if minutes < 10: minutes_str = '0' + str(minutes) else: minutes_str = str(minutes) seconds = num % 60 if seconds < 10: seconds_str = '0' + str(seconds) else: seconds_str = str(seconds) return minutes_str + ':' + seconds_str model = RangeModel() view = View( Item("float_value", editor=RangeEditor(format_func=num_to_time))) tester = UITester() with tester.create_ui(model, dict(view=view)) as ui: float_value_field = tester.find_by_name(ui, "float_value") float_value_text = float_value_field.locate(Textbox()) self.assertEqual(float_value_text.inspect(DisplayedText()), "00:00.1")
def test_run_demo(self): demo = runpy.run_path(DEMO_PATH)["demo"] tester = UITester() with tester.create_ui(demo) as ui: simple_small = tester.find_by_id(ui, 'simple_small') custom_small = tester.find_by_id(ui, 'custom_small') text_small = tester.find_by_id(ui, 'text_small') readonly_small = tester.find_by_id(ui, 'readonly_small') simple_medium = tester.find_by_id(ui, 'simple_medium') custom_medium = tester.find_by_id(ui, 'custom_medium') text_medium = tester.find_by_id(ui, 'text_medium') readonly_medium = tester.find_by_id(ui, 'readonly_medium') # Testing for SimpleSpinEditor is not supported yet so the simple # and custom styles for the large_range_int are not included here text_large = tester.find_by_id(ui, 'text_large') readonly_large = tester.find_by_id(ui, 'readonly_large') simple_float = tester.find_by_id(ui, 'simple_float') custom_float = tester.find_by_id(ui, 'custom_float') text_float = tester.find_by_id(ui, 'text_float') readonly_float = tester.find_by_id(ui, 'readonly_float') # Tests for the small_int_range ################################## simple_small_slider = simple_small.locate(Slider()) simple_small_slider.perform(KeyClick("Page Up")) self.assertEqual(demo.small_int_range, 2) simple_small_text = simple_small.locate(Textbox()) simple_small_text.perform(KeyClick("Backspace")) simple_small_text.perform(KeyClick("3")) simple_small_text.perform(KeyClick("Enter")) self.assertEqual(demo.small_int_range, 3) custom_small.locate(Index(0)).perform(MouseClick()) self.assertEqual(demo.small_int_range, 1) text_small.perform(KeyClick("0")) text_small.perform(KeyClick("Enter")) self.assertEqual(demo.small_int_range, 10) demo.small_int_range = 7 displayed_small = readonly_small.inspect(DisplayedText()) self.assertEqual(displayed_small, '7') # Tests for the medium_int_range ################################# simple_medium_slider = simple_medium.locate(Slider()) # on this range, page up/down corresponds to a change of 2. simple_medium_slider.perform(KeyClick("Page Up")) self.assertEqual(demo.medium_int_range, 3) simple_medium_text = simple_medium.locate(Textbox()) simple_medium_text.perform(KeyClick("Backspace")) simple_medium_text.perform(KeyClick("4")) simple_medium_text.perform(KeyClick("Enter")) self.assertEqual(demo.medium_int_range, 4) custom_medium_slider = custom_medium.locate(Slider()) custom_medium_slider.perform(KeyClick("Page Down")) self.assertEqual(demo.medium_int_range, 2) custom_medium_text = custom_medium.locate(Textbox()) custom_medium_text.perform(KeyClick("Backspace")) custom_medium_text.perform(KeyClick("1")) custom_medium_text.perform(KeyClick("Enter")) self.assertEqual(demo.medium_int_range, 1) text_medium.perform(KeyClick("0")) text_medium.perform(KeyClick("Enter")) self.assertEqual(demo.medium_int_range, 10) demo.medium_int_range = 7 displayed_medium = readonly_medium.inspect(DisplayedText()) self.assertEqual(displayed_medium, '7') # Tests for the large_int_range ################################## # Testing for SimpleSpinEditor is not supported yet text_large.perform(KeySequence("00")) text_large.perform(KeyClick("Enter")) self.assertEqual(demo.large_int_range, 100) demo.large_int_range = 77 displayed_large = readonly_large.inspect(DisplayedText()) self.assertEqual(displayed_large, '77') # Tests for the float_range ###################################### simple_float_slider = simple_float.locate(Slider()) # on this range, page up/down corresponds to a change of 1.000. simple_float_slider.perform(KeyClick("Page Up")) self.assertEqual(demo.float_range, 1.000) simple_float_text = simple_float.locate(Textbox()) for _ in range(3): simple_float_text.perform(KeyClick("Backspace")) simple_float_text.perform(KeyClick("5")) simple_float_text.perform(KeyClick("Enter")) self.assertEqual(demo.float_range, 1.5) custom_float_slider = custom_float.locate(Slider()) # after the trait is set to 1.5 above, the active range shown by # the LargeRangeSliderEditor for the custom style is [0,11.500] # so a page down is now a decrement of 1.15 custom_float_slider.perform(KeyClick("Page Down")) self.assertEqual(round(demo.float_range, 2), 0.35) custom_float_text = custom_float.locate(Textbox()) for _ in range(5): custom_float_text.perform(KeyClick("Backspace")) custom_float_text.perform(KeySequence("50.0")) custom_float_text.perform(KeyClick("Enter")) self.assertEqual(demo.float_range, 50.0) text_float.perform(KeyClick("5")) text_float.perform(KeyClick("Enter")) self.assertEqual(demo.float_range, 50.05) demo.float_range = 72.0 displayed_float = readonly_float.inspect(DisplayedText()) self.assertEqual(displayed_float, '72.0')