def test_propagate_errors_switch_selection(self): obj = ObjectWithValidatedList() ui_tester = UITester() with ui_tester.create_ui(obj, {'view': selection_view}) as ui: something_ui = ui_tester.find_by_name(ui, "inst") something_ui.locate(Index(0)).perform(MouseClick()) some_string_field = something_ui.locate( TargetByName('some_string')) some_string_field.perform(KeySequence("bcde")) some_string_field.perform(KeyClick("Enter")) ok_button = ui_tester.find_by_id(ui, "OK") instance_editor_ui = something_ui._target._ui instance_editor_ui_parent = something_ui._target._ui.parent self.assertNotEqual(instance_editor_ui, ui) self.assertEqual(instance_editor_ui_parent, ui) self.assertEqual(instance_editor_ui.errors, ui.errors) self.assertFalse(ok_button.inspect(IsEnabled())) # change to a different selected that is not in an error state something_ui.locate(Index(1)).perform(MouseClick()) self.assertTrue(ok_button.inspect(IsEnabled()))
def test_get_person_name(self): person1 = Person() person2 = Person(name="Mary") phonebook = Phonebook(people=[person1, person2], ) tester = UITester() with tester.create_ui(phonebook, dict(view=notebook_view)) as ui: list_ = tester.find_by_name(ui, "people") list_.locate(Index(1)).perform(MouseClick()) name_field = list_.locate(Index(1)).find_by_name("name") actual = name_field.inspect(DisplayedText()) self.assertEqual(actual, "Mary")
def test_enum_editor_demo(self): demo = runpy.run_path(DEMO_PATH)["demo"] tester = UITester() with tester.create_ui(demo) as ui: simple_enum = tester.find_by_id(ui, "simple") simple_text_enum = tester.find_by_id(ui, "simple_text") radio_enum = tester.find_by_id(ui, "radio") list_enum = tester.find_by_id(ui, "list") text = tester.find_by_id(ui, "text") readonly = tester.find_by_id(ui, "readonly") self.assertEqual(demo.name_list, 'A-495') simple_enum.locate(Index(1)).perform(MouseClick()) self.assertEqual(demo.name_list, 'A-498') for _ in range(5): simple_text_enum.perform(KeyClick("Backspace")) simple_text_enum.perform(KeySequence("R-1226")) simple_text_enum.perform(KeyClick("Enter")) self.assertEqual(demo.name_list, 'R-1226') radio_enum.locate(Index(5)).perform(MouseClick()) self.assertEqual(demo.name_list, 'Foo') list_enum.locate(Index(3)).perform(MouseClick()) self.assertEqual(demo.name_list, 'TS-17') for _ in range(5): text.perform(KeyClick("Backspace")) text.perform(KeySequence("A-498")) text.perform(KeyClick("Enter")) self.assertEqual(demo.name_list, 'A-498') demo.name_list = 'Foo' displayed_simple = simple_enum.inspect(DisplayedText()) disp_simple_text = simple_text_enum.inspect(DisplayedText()) selected_radio = radio_enum.inspect(SelectedText()) selected_list = list_enum.inspect(SelectedText()) displayed_text = text.inspect(DisplayedText()) displayed_readonly = readonly.inspect(DisplayedText()) displayed_selected = [ displayed_simple, disp_simple_text, selected_radio, selected_list, displayed_text, displayed_readonly, ] for text in displayed_selected: self.assertEqual(text, 'Foo')
def test_modify_person_name(self): phonebook = Phonebook(people=get_people(), ) tester = UITester() with tester.create_ui(phonebook, dict(view=notebook_view)) as ui: list_ = tester.find_by_name(ui, "people") list_.locate(Index(1)).perform(MouseClick()) name_field = list_.locate(Index(1)).find_by_name("name") for _ in range(4): name_field.perform(KeyClick("Backspace")) name_field.perform(KeySequence("Pete")) self.assertEqual(phonebook.people[1].name, "Pete")
def test_custom_editor_with_selection(self): obj = ObjectWithList() tester = UITester() with tester.create_ui(obj, {'view': selection_view}) as ui: # test that the current object is None self.assertIsNone(obj.inst) # test that the displayed text is correct instance = tester.find_by_name(ui, "inst") text = instance.inspect(SelectedText()) self.assertEqual(text, obj.inst_list[0].name) # test that changing selection works instance = tester.find_by_name(ui, "inst") instance.locate(Index(1)).perform(MouseClick()) self.assertIs(obj.inst, obj.inst_list[1]) # test that the displayed text is correct text = instance.inspect(SelectedText()) self.assertEqual(text, obj.inst_list[1].name) # test editing the view works value_txt = instance.find_by_name("value") value_txt.perform(KeySequence("abc")) self.assertEqual(obj.inst.value, "twoabc")
def test_index_out_of_bound(self): phonebook = Phonebook(people=[], ) tester = UITester() with tester.create_ui(phonebook, dict(view=notebook_view)) as ui: with self.assertRaises(IndexError): tester.find_by_name(ui, "people").locate(Index(0)).perform( MouseClick())
def test_index_out_of_range(self): obj = ListTraitTest(people=get_people(), style="simple") tester = UITester() with tester.create_ui(obj) as ui: people_list = tester.find_by_name(ui, "people") with self.assertRaises(IndexError): people_list.locate(Index(10))
def check_enum_index_update(self, view): enum_edit = EnumModel() tester = UITester() with tester.create_ui(enum_edit, dict(view=view)) as ui: self.assertEqual(enum_edit.value, "one") list_editor = tester.find_by_name(ui, "value") list_editor.locate(Index(1)).perform(MouseClick()) self.assertEqual(enum_edit.value, "two")
def test_checklist_editor_simple_demo(self): demo = runpy.run_path(DEMO_PATH)["demo"] tester = UITester() with tester.create_ui(demo) as ui: checklist = tester.find_by_id(ui, "custom") item3 = checklist.locate(Index(2)) item3.perform(MouseClick()) self.assertEqual(demo.checklist, ["three"]) item3.perform(MouseClick()) self.assertEqual(demo.checklist, [])
def check_enum_text_update(self, view): enum_edit = EnumModel() tester = UITester() with tester.create_ui(enum_edit, dict(view=view)) as ui: combobox = tester.find_by_name(ui, "value") displayed = combobox.inspect(DisplayedText()) self.assertEqual(displayed, "one") combobox.locate(Index(1)).perform(MouseClick()) displayed = combobox.inspect(DisplayedText()) self.assertEqual(displayed, "two")
def test_custom_check_list_editor_click_initial_value(self): list_edit = ListModel(value=["two"]) tester = UITester() with tester.create_ui(list_edit, dict(view=get_view("custom"))) as ui: self.assertEqual(list_edit.value, ["two"]) check_list = tester.find_by_name(ui, "value") item_1 = check_list.locate(Index(1)) item_1.perform(MouseClick()) self.assertEqual(list_edit.value, [])
def test_list_editor_notebook_selection_demo(self): demo = runpy.run_path(DEMO_PATH)["demo"] tester = UITester() with tester.create_ui(demo) as ui: people_list = tester.find_by_name(ui, "people") person2 = people_list.locate(Index(2)) person2.perform(MouseClick()) self.assertEqual(demo.index, 2) age = person2.find_by_name("age") age.perform(KeyClick("Backspace")) age.perform(KeyClick("9")) self.assertEqual(demo.people[2].age, 39)
def test_list_editor_demo(self): demo = runpy.run_path(DEMO_PATH)["demo"] tester = UITester() with tester.create_ui(demo) as ui: custom_list = tester.find_by_id(ui, "custom") item1 = custom_list.locate(Index(1)) for _ in range(6): item1.perform(KeyClick("Backspace")) item1.perform(KeySequence("Othello")) item1.perform(KeyClick("Enter")) self.assertEqual(demo.play_list, ["The Merchant of Venice", "Othello", "MacBeth"])
def test_custom_check_list_editor_grid_layout(self): for cols in range(1, 8): list_edit = ListModel() tester = UITester() view = get_view_custom_cols(cols=cols) with tester.create_ui(list_edit, dict(view=view)) as ui: self.assertEqual(list_edit.value, []) check_list = tester.find_by_name(ui, "value") item = check_list.locate(Index(6)) item.perform(MouseClick()) self.assertEqual(list_edit.value, ["seven"]) item.perform(MouseClick()) self.assertEqual(list_edit.value, [])
def test_none_selected(self): obj = ObjectWithList() tester = UITester() with tester.create_ui(obj, {'view': none_view}) as ui: # test that the current object is None self.assertIsNone(obj.inst) # test that the displayed text is empty to start instance = tester.find_by_name(ui, "inst") text = instance.inspect(SelectedText()) self.assertEqual(text, '') # test that changing selection works and displayed text is correct instance.locate(Index(1)).perform(MouseClick()) self.assertIs(obj.inst, obj.inst_list[1]) text = instance.inspect(SelectedText()) self.assertEqual(text, obj.inst_list[1].name) # test resetting selection to None reset_to_none_button = tester.find_by_name(ui, "reset_to_none") reset_to_none_button.perform(MouseClick()) self.assertIsNone(obj.inst) text = instance.inspect(SelectedText()) self.assertEqual(text, '') # change selection again instance.locate(Index(1)).perform(MouseClick()) self.assertIs(obj.inst, obj.inst_list[1]) text = instance.inspect(SelectedText()) self.assertEqual(text, obj.inst_list[1].name) # test modifying list of selectable options returns current object # to None change_options_button = tester.find_by_name(ui, "change_options") change_options_button.perform(MouseClick()) self.assertIsNone(obj.inst) text = instance.inspect(SelectedText()) self.assertEqual(text, '')
def test_radio_enum_editor_pick(self): for cols in range(1, 4): for row_major in [True, False]: enum_edit = EnumModel() tester = UITester() view = get_radio_view(cols=cols) with tester.create_ui(enum_edit, dict(view=view)) as ui: # sanity check self.assertEqual(enum_edit.value, "one") radio_editor = tester.find_by_name(ui, "value") if is_qt(): radio_editor._target.row_major = row_major radio_editor._target.rebuild_editor() item = radio_editor.locate(Index(3)) item.perform(MouseClick()) self.assertEqual(enum_edit.value, "four")
def test_locate_element_and_edit(self): obj = ListTraitTest(people=get_people(), style="simple") tester = UITester() with tester.create_ui(obj) as ui: # sanity check self.assertEqual(obj.people[7].name, "Fields") people_list = tester.find_by_name(ui, "people") item = people_list.locate(Index(7)) item.perform(MouseClick()) name_field = item.find_by_name("name") for _ in range(6): name_field.perform(KeyClick("Backspace")) name_field.perform(KeySequence("David")) displayed = name_field.inspect(DisplayedText()) self.assertEqual(obj.people[7].name, "David") self.assertEqual(displayed, obj.people[7].name)
def test_custom_check_list_editor_invalid_current_values_str(self): class StrModel(HasTraits): value = Str() str_edit = StrModel(value="alpha, \ttwo, three,\n lambda, one") tester = UITester() with tester.create_ui(str_edit, dict(view=get_view("custom"))) as ui: self.assertEqual(str_edit.value, "two,three,one") check_list = tester.find_by_name(ui, "value") item_1 = check_list.locate(Index(1)) item_1.perform(MouseClick()) self.assertEqual(str_edit.value, "three,one")
def test_locate_element_and_edit(self): # varying the number of columns in the view tests the logic for # getting the correct nested ui for col in range(1, 5): obj = ListTraitTest(people=get_people(), num_columns=col) tester = UITester() with tester.create_ui(obj) as ui: # sanity check self.assertEqual(obj.people[7].name, "Fields") people_list = tester.find_by_name(ui, "people") item = people_list.locate(Index(7)) name_field = item.find_by_name("name") for _ in range(6): name_field.perform(KeyClick("Backspace")) name_field.perform(KeySequence("David")) displayed = name_field.inspect(DisplayedText()) self.assertEqual(obj.people[7].name, "David") self.assertEqual(displayed, obj.people[7].name)
def test_custom_editor_with_selection_change_option_name(self): obj = ObjectWithList() tester = UITester() with tester.create_ui(obj, {'view': selection_view}) as ui: # test that the current object is None self.assertIsNone(obj.inst) # actually select the first item instance = tester.find_by_name(ui, "inst") instance.locate(Index(0)).perform(MouseClick()) self.assertIs(obj.inst, obj.inst_list[0]) # test that the displayed text is correct after change name_txt = instance.find_by_name("name") for _ in range(3): name_txt.perform(KeyClick("Backspace")) name_txt.perform(KeySequence("Something New")) text = instance.inspect(SelectedText()) self.assertEqual(text, "Something New") self.assertEqual("Something New", obj.inst_list[0].name)
def test_scrollable_group_visible_when(self): from pyface.qt import QtGui obj = ScrollableGroupVisibleWhen() tester = UITester() with tester.create_ui(obj) as ui: bar_group = tester.find_by_id(ui, 'bar_group') baz_group = tester.find_by_id(ui, 'baz_group') # for a scrollable group the GroupEditors control should be a # QScrollArea not just the QWidget. We want the full area to be # not visible, not just the text box widget. self.assertIsInstance(bar_group._target.control, QtGui.QScrollArea) self.assertIsInstance(baz_group._target.control, QtGui.QScrollArea) self.assertTrue(bar_group.inspect(IsVisible())) self.assertFalse(baz_group.inspect(IsVisible())) enabled_box = tester.find_by_name(ui, 'enabled') baz_item = enabled_box.locate(Index(1)) baz_item.perform(MouseClick()) self.assertTrue(baz_group.inspect(IsVisible())) self.assertFalse(bar_group.inspect(IsVisible()))
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')