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 check_button_disabled(self, style): button_text_edit = ButtonTextEdit(button_enabled=False, ) view = View( Item( "play_button", editor=ButtonEditor(), enabled_when="button_enabled", style=style, ), ) tester = UITester() with tester.create_ui(button_text_edit, dict(view=view)) as ui: button = tester.find_by_name(ui, "play_button") self.assertFalse(button.inspect(IsEnabled())) with self.assertTraitDoesNotChange(button_text_edit, "play_button"): button.perform(MouseClick()) button_text_edit.button_enabled = True self.assertTrue(button.inspect(IsEnabled())) with self.assertTraitChanges(button_text_edit, "play_button", count=1): button.perform(MouseClick())
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 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_custom_check_list_editor_click(self): list_edit = ListModel() tester = UITester() with tester.create_ui(list_edit, dict(view=get_view("custom"))) as ui: self.assertEqual(list_edit.value, []) check_list = tester.find_by_name(ui, "value") item_1 = check_list.locate(Index(1)) item_1.perform(MouseClick()) self.assertEqual(list_edit.value, ["two"]) item_1.perform(MouseClick()) self.assertEqual(list_edit.value, [])
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_open_external_link_externally(self): model = HTMLModel(content=""" <html> <a href='test://testing' target='_blank' style='display:block; width: 100%; height: 100%'> External Link </a> </html> """) view = View( Item("content", editor=HTMLEditor(open_externally=True)) ) with self.tester.create_ui(model, dict(view=view)) as ui: html_view = self.tester.find_by_name(ui, "content") with mock.patch("webbrowser.open_new") as mocked_browser: html_view.perform(MouseClick()) self.assertIn( "External Link", html_view.inspect(HTMLContent()), ) is_webkit = _is_webkit_page(html_view._target.control.page()) if is_webkit: # This is the expected behavior. mocked_browser.assert_called_once_with("test://testing") else: # Expected failure: # See enthought/traitsui#1464 # This is the current unexpected behavior if QtWebEngine is used. mocked_browser.assert_not_called()
def test_open_internal_link_externally(self): # this test requires Qt because it relies on the link filling up # the entire page through the use of CSS, which isn't supported # by Wx. model = HTMLModel(content=""" <html> <a href='test://testing' target='_self' style='display:block; width: 100%; height: 100%'> Internal Link </a> </html> """) view = View( Item("content", editor=HTMLEditor(open_externally=True)) ) with self.tester.create_ui(model, dict(view=view)) as ui: html_view = self.tester.find_by_name(ui, "content") with mock.patch("webbrowser.open_new") as mocked_browser: html_view.perform(MouseClick()) self.assertIn( "Internal Link", html_view.inspect(HTMLContent()), ) mocked_browser.assert_called_once_with("test://testing")
def test_open_external_link(self): # this test requires Qt because it relies on the link filling up # the entire page through the use of CSS, which isn't supported # by Wx. model = HTMLModel(content=""" <html> <a href='test://testing' target='_blank' style='display:block; width: 100%; height: 100%'> External Link </a> </html> """) view = View( Item("content", editor=HTMLEditor()) ) with self.tester.create_ui(model, dict(view=view)) as ui: html_view = self.tester.find_by_name(ui, "content") with mock.patch("webbrowser.open_new") as mocked_browser: html_view.perform(MouseClick()) self.assertIn( "External Link", html_view.inspect(HTMLContent()), ) # See enthought/traitsui#1464 # This is the expected behaviour: # mocked_browser.assert_called_once_with("test://testing") # However, this is the current unexpected behaviour mocked_browser.assert_not_called()
def test_expand_all(self): bogus = Bogus(bogus_list=[BogusWrap()]) tree_editor_view = BogusTreeView(bogus=bogus) tester = UITester() with tester.create_ui(tree_editor_view) as ui: expand_all_button = tester.find_by_name(ui, "expand_all") expand_all_button.perform(MouseClick())
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_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_boolean_editor_simple_demo(self): demo = runpy.run_path(DEMO_PATH)["demo"] tester = UITester() with tester.create_ui(demo) as ui: simple = tester.find_by_id(ui, 'simple') readonly = tester.find_by_id(ui, 'readonly') text = tester.find_by_id(ui, 'text') count_changes = tester.find_by_name(ui, "count_changes") simple.perform(MouseClick()) self.assertEqual(demo.my_boolean_trait, True) for _ in range(4): text.perform(KeyClick("Backspace")) text.perform(KeySequence("False")) text.perform(KeyClick("Enter")) self.assertEqual(demo.my_boolean_trait, False) displayed_count_changes = count_changes.inspect(DisplayedText()) self.assertEqual(displayed_count_changes, '2') self.assertEqual(displayed_count_changes, str(demo.count_changes)) demo.my_boolean_trait = True displayed = readonly.inspect(DisplayedText()) self.assertEqual(displayed, "True") simple_is_checked = simple.inspect(IsChecked()) self.assertEqual(simple_is_checked, True)
def test_create_and_dispose_text_style(self): # Setting focus on the widget and then disposing the widget # should not cause errors. view = View(Item("font_trait", style="text")) tester = UITester() with tester.create_ui(ObjectWithFont(), dict(view=view)) as ui: wrapper = tester.find_by_name(ui, "font_trait") wrapper.perform(MouseClick())
def test_basic_stuff(ui_tester, button_editor, editor_app): button = ui_tester.find_by_name(editor_app, "my_button_trait") button.perform(MouseClick()) assert ui_tester.find_by_name(editor_app, 'click_counter').inspect( DisplayedText()) == '1' field = ui_tester.find_by_name(editor_app, "t_field") field._target.control.setText('hello') assert field.inspect(DisplayedText()) == 'hello'
def test_simple_editor(self): obj = ObjectWithInstance() tester = UITester() with tester.create_ui(obj, dict(view=get_view("simple"))) as ui: instance = tester.find_by_name(ui, "inst") instance.perform(MouseClick()) value_txt = instance.find_by_name("value") value_txt.perform(KeySequence("abc")) self.assertEqual(obj.inst.value, "abc")
def check_button_fired_event(self, view): button_text_edit = ButtonTextEdit() tester = UITester() with tester.create_ui(button_text_edit, dict(view=view)) as ui: button = tester.find_by_name(ui, "play_button") with self.assertTraitChanges( button_text_edit, "play_button", count=1): button.perform(MouseClick())
def test_table_editor_escape_retain_edit(self): object_list = ObjectListWithSelection(values=[ListItem(other_value=0)]) tester = UITester() with tester.create_ui(object_list, dict(view=select_row_view)) as ui: cell = tester.find_by_name(ui, "values").locate(Cell(0, 1)) cell.perform(MouseClick()) cell.perform(KeySequence("123")) cell.perform(KeyClick("Esc")) # exit edit mode, did not revert self.assertEqual(object_list.values[0].other_value, 123)
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 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 check_click_boolean_changes_trait(self, style): view = View(Item("true_or_false", style=style, editor=BooleanEditor())) obj = BoolModel() tester = UITester() with tester.create_ui(obj, dict(view=view)) as ui: # sanity check self.assertEqual(obj.true_or_false, False) checkbox = tester.find_by_name(ui, "true_or_false") checkbox.perform(MouseClick()) self.assertEqual(obj.true_or_false, True)
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_table_editor_modify_cell_with_tester(self): object_list = ObjectListWithSelection( values=[ListItem(value=str(i**2)) for i in range(10)]) tester = UITester() with tester.create_ui(object_list, dict(view=select_row_view)) as ui: wrapper = tester.find_by_name(ui, "values").locate(Cell(5, 0)) wrapper.perform(MouseClick()) # activate edit mode wrapper.perform(KeySequence("abc")) self.assertEqual(object_list.selected.value, "abc") # second column refers to an Int type original = object_list.selected.other_value wrapper = tester.find_by_name(ui, "values").locate(Cell(5, 1)) wrapper.perform(MouseClick()) wrapper.perform(KeySequence("abc")) # invalid self.assertEqual(object_list.selected.other_value, original) for _ in range(3): wrapper.perform(KeyClick("Backspace")) wrapper.perform(KeySequence("12")) # now ok self.assertEqual(object_list.selected.other_value, 12)
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_table_editor_select_row_index(self): object_list = ObjectListWithSelection( values=[ListItem(value=str(i**2)) for i in range(10)]) object_list.selected_index = 5 tester = UITester() with tester.create_ui(object_list, dict(view=select_row_index_view)) \ as ui: values_table = tester.find_by_name(ui, "values") values_table.locate(Cell(5, 0)).perform(MouseClick()) selected = values_table.inspect(SelectedIndices()) self.assertEqual(selected, [5])
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_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_table_editor_select_cell_index(self): object_list = ObjectListWithSelection( values=[ListItem(value=str(i**2)) for i in range(10)]) view = select_cell_index_view tester = UITester() with tester.create_ui(object_list, dict(view=view)) as ui: # click the cell at (5,1) values_table = tester.find_by_name(ui, "values") cell_5_1 = values_table.locate(Cell(5, 1)) cell_5_1.perform(MouseClick()) selected = values_table.inspect(SelectedIndices()) self.assertEqual(selected, [(5, 1)])
def test_simple_editor_resynch_editor(self): edited_inst = EditedInstance(value='hello') obj = ObjectWithInstance(inst=edited_inst) tester = UITester() with tester.create_ui(obj, dict(view=get_view("simple"))) as ui: instance = tester.find_by_name(ui, "inst") instance.perform(MouseClick()) value_txt = instance.find_by_name("value") displayed = value_txt.inspect(DisplayedText()) self.assertEqual(displayed, "hello") edited_inst.value = "bye" displayed = value_txt.inspect(DisplayedText()) self.assertEqual(displayed, "bye")
def test_boolean_editor_demo(self): demo = runpy.run_path(DEMO_PATH)["demo"] tester = UITester() with tester.create_ui(demo) as ui: simple = tester.find_by_id(ui, 'simple') custom = tester.find_by_id(ui, 'custom') text = tester.find_by_id(ui, 'text') readonly = tester.find_by_id(ui, 'readonly') simple.perform(MouseClick()) self.assertEqual(demo.boolean_trait, True) custom.perform(MouseClick()) self.assertEqual(demo.boolean_trait, False) for _ in range(5): text.perform(KeyClick("Backspace")) text.perform(KeySequence("True")) text.perform(KeyClick("Enter")) self.assertEqual(demo.boolean_trait, True) demo.boolean_trait = False displayed = readonly.inspect(DisplayedText()) self.assertEqual(displayed, "False")