def test_data_frame_editor_multi_select(): view = View(Item('data', editor=DataFrameEditor(multi_select=True), width=400)) viewer = sample_data() with store_exceptions_on_all_threads(): ui = viewer.edit_traits(view=view) ui.dispose()
def _test_tree_node_object_releases_listeners(self, hide_root, nodes=None, trait='bogus_list', expected_listeners=1): """ The TreeEditor should release the listener to the root node's children when it's disposed of. """ with store_exceptions_on_all_threads(): bogus = BogusTreeNodeObject(bogus_list=[BogusTreeNodeObject()]) tree_editor_view = BogusTreeNodeObjectView( bogus=bogus, hide_root=hide_root, nodes=nodes, ) ui = tree_editor_view.edit_traits() # The TreeEditor sets a listener on the bogus object's # children list notifiers_list = bogus.trait(trait)._notifiers(False) self.assertEqual(expected_listeners, len(notifiers_list)) # Manually close the UI press_ok_button(ui) # The listener should be removed after the UI has been closed notifiers_list = bogus.trait(trait)._notifiers(False) self.assertEqual(0, len(notifiers_list))
def test_table_editor_select_cells(): gui = GUI() object_list = ObjectListWithSelection( values=[ListItem(value=str(i**2)) for i in range(10)] ) object_list.selected_cells = [ (object_list.values[5], 'value'), (object_list.values[6], 'other value'), (object_list.values[8], 'value'), ] with store_exceptions_on_all_threads(): ui = object_list.edit_traits(view=select_cells_view) editor = ui.get_editors('values')[0] gui.process_events() if is_current_backend_qt4(): selected = editor.selected elif is_current_backend_wx(): selected = editor.selected_cells press_ok_button(ui) gui.process_events() assert selected == [ (object_list.values[5], 'value'), (object_list.values[6], 'other value'), (object_list.values[8], 'value'), ]
def test_selection_listener_disconnected(): """ Check that selection listeners get correctly disconnected """ from pyface.api import GUI from pyface.qt.QtGui import QApplication, QItemSelectionModel from pyface.ui.qt4.util.event_loop_helper import EventLoopHelper from pyface.ui.qt4.util.testing import event_loop obj = ListStrEditorWithSelectedIndex(values=['value1', 'value2']) with store_exceptions_on_all_threads(): qt_app = QApplication.instance() if qt_app is None: qt_app = QApplication([]) helper = EventLoopHelper(gui=GUI(), qt_app=qt_app) # open the UI and run until the dialog is closed ui = obj.edit_traits(view=single_select_item_view) with helper.delete_widget(ui.control): press_ok_button(ui) # now run again and change the selection ui = obj.edit_traits(view=single_select_item_view) with event_loop(): editor = ui.get_editors('values')[0] list_view = editor.list_view mi = editor.model.index(1) list_view.selectionModel().select(mi, QItemSelectionModel.ClearAndSelect) obj.selected = 'value2'
def test_progress_column(): from traitsui.extras.progress_column import ProgressColumn progress_view = View( Item( 'values', show_label=False, editor=TableEditor( columns=[ ObjectColumn(name='value'), ProgressColumn(name='other_value'), ], ) ), buttons=['OK'], ) gui = GUI() object_list = ObjectList( values=[ListItem(value=str(i**2)) for i in range(10)] ) with store_exceptions_on_all_threads(): ui = object_list.edit_traits(view=progress_view) gui.process_events() press_ok_button(ui) gui.process_events()
def test_styles(self): # simple smoke test of buttons gui = GUI() button_text_edit = ButtonTextEdit() with store_exceptions_on_all_threads(): ui = button_text_edit.edit_traits() self.addCleanup(ui.dispose) gui.process_events()
def test_table_editor(): gui = GUI() object_list = ObjectList(values=[ListItem(value=str(i ** 2)) for i in range(10)]) with store_exceptions_on_all_threads(): ui = object_list.edit_traits(view=simple_view) gui.process_events() press_ok_button(ui) gui.process_events()
def test_simple_editor_parent_closed(): with store_exceptions_on_all_threads(): obj = NonmodalInstanceEditor() ui = obj.edit_traits() editor = ui.get_editors('inst')[0] # make the dialog appear editor._button.click() # close the main ui press_ok_button(ui)
def test_color_column(self): # Behaviour: column ui should display without error with store_exceptions_on_all_threads(): d1 = MyEntry(name='a', value=2, color=(1.0, 0.3, 0.1)) d2 = MyEntry(name='b', value=3, color=(0.1, 0.0, 0.9)) data = MyData(data_list=[d1, d2]) ui = data.edit_traits() ui.dispose()
def test_text_check_list_object_list(self): list_edit = ListModel() with store_exceptions_on_all_threads(): gui, _, line_edit = self.setup_gui(list_edit, get_view("text")) self.assertEqual(list_edit.value, []) set_text_in_line_edit(line_edit, "['one', 'two']") gui.process_events() self.assertEqual(list_edit.value, ["one", "two"])
def test_custom_check_list_editor_click_initial_value(self): list_edit = ListModel(value=["two"]) with store_exceptions_on_all_threads(): gui, _, widget = self.setup_gui(list_edit, get_view("custom")) self.assertEqual(list_edit.value, ["two"]) click_checkbox_button(widget, 1) gui.process_events() self.assertEqual(list_edit.value, [])
def test_simple_check_list_editor_text(self): list_edit = ListModel(value=["one"]) with store_exceptions_on_all_threads(): gui, _, combobox = self.setup_gui(list_edit, get_view("simple")) self.assertEqual(get_combobox_text(combobox), "One") list_edit.value = ["two"] gui.process_events() self.assertEqual(get_combobox_text(combobox), "Two")
def test_data_frame_editor_alternate_adapter(): class AlternateAdapter(DataFrameAdapter): pass alternate_adapter_view = View( Item('data', editor=DataFrameEditor(adapter=AlternateAdapter()), width=400) ) viewer = sample_data() with store_exceptions_on_all_threads(): ui = viewer.edit_traits(view=alternate_adapter_view) ui.dispose()
def check_enum_text_bad_update(self, view): enum_edit = EnumModel() with store_exceptions_on_all_threads(): gui, combobox = self.setup_gui(enum_edit, view) self.assertEqual(enum_edit.value, "one") set_combobox_text(combobox, "t") gui.process_events() self.assertEqual(enum_edit.value, "one")
def test_data_frame_editor_alternate_adapter(): class AlternateAdapter(DataFrameAdapter): pass alternate_adapter_view = View( Item('data', editor=DataFrameEditor(adapter=AlternateAdapter()), width=400)) viewer = sample_data() with store_exceptions_on_all_threads(): ui = viewer.edit_traits(view=alternate_adapter_view) ui.dispose()
def test_simple_editor_rebuild_editor_evaluate(self): # Smoke test for `wx.enum_editor.SimpleEditor.rebuild_editor` enum_editor_factory = EnumEditor( evaluate=True, values=["one", "two", "three", "four"], ) view = View(UItem("value", editor=enum_editor_factory, style="simple")) with store_exceptions_on_all_threads(): gui, combobox = self.setup_gui(EnumModel(), view) enum_editor_factory.values = ["one", "two", "three"]
def check_enum_text_update(self, view): enum_edit = EnumModel() with store_exceptions_on_all_threads(): gui, list_widget = self.setup_gui(enum_edit, view) self.assertEqual(get_list_widget_text(list_widget), "one") enum_edit.value = "two" gui.process_events() self.assertEqual(get_list_widget_text(list_widget), "two")
def check_enum_index_update(self, view): enum_edit = EnumModel() with store_exceptions_on_all_threads(): gui, list_widget = self.setup_gui(enum_edit, view) self.assertEqual(enum_edit.value, "one") set_list_widget_selected_index(list_widget, 1) gui.process_events() self.assertEqual(enum_edit.value, "two")
def test_simple_editor(self): obj = NonmodalInstanceEditor() with store_exceptions_on_all_threads(), create_ui(obj) as ui: editor = ui.get_editors("inst")[0] # make the dialog appear editor._button.click() # close the ui dialog press_ok_button(editor._dialog_ui) # close the main ui press_ok_button(ui)
def test_radio_enum_editor_pick(self): enum_edit = EnumModel() with store_exceptions_on_all_threads(): gui, widget = self.setup_gui(enum_edit, get_view("custom")) self.assertEqual(enum_edit.value, "one") # The layout is: one, three, four \n two click_radio_button(widget, 3) gui.process_events() self.assertEqual(enum_edit.value, "two")
def test_data_frame_editor_with_refresh(self): class DataFrameViewer(HasTraits): data = Instance(DataFrame) df_refreshed = Event() view = View( Item("data", editor=DataFrameEditor(refresh="df_refreshed"))) df = DataFrame(DATA, index=["one", "two", "three", "four"], columns=["X", "Y", "Z"]) viewer = DataFrameViewer(data=df) with store_exceptions_on_all_threads(), create_ui(viewer) as ui: viewer.df_refreshed = True
def test_simple_set_editor_default_selection_unused(self): with store_exceptions_on_all_threads(): gui, editor = self.setup_gui(ListModel(), get_view()) self.assertEqual(get_list_items(editor._unused), ["four", "three"]) self.assertEqual(get_list_items(editor._used), ["one", "two"]) click_button(editor._use) gui.process_events() # Button inserts at the top self.assertEqual(get_list_items(editor._unused), ["three"]) self.assertEqual(get_list_items(editor._used), ["four", "one", "two"])
def test_simple_set_editor_unuse_dclick(self): with store_exceptions_on_all_threads(): gui, editor = self.setup_gui(ListModel(), get_view()) self.assertEqual(get_list_items(editor._unused), ["four", "three"]) self.assertEqual(get_list_items(editor._used), ["one", "two"]) double_click_on_item(editor, 0, in_used=True) gui.process_events() # Inserts at the top self.assertEqual(get_list_items(editor._unused), ["one", "four", "three"]) self.assertEqual(get_list_items(editor._used), ["two"])
def test_simple_editor(): with store_exceptions_on_all_threads(): obj = NonmodalInstanceEditor() ui = obj.edit_traits() editor = ui.get_editors("inst")[0] # make the dialog appear editor._button.click() # close the ui dialog press_ok_button(editor._dialog_ui) # close the main ui press_ok_button(ui)
def test_data_frame_editor_with_update_refresh(): class DataFrameViewer(HasTraits): data = Instance(DataFrame) df_updated = Event view = View(Item("data", editor=DataFrameEditor(update="df_updated"))) df = DataFrame(DATA, index=["one", "two", "three", "four"], columns=["X", "Y", "Z"]) viewer = DataFrameViewer(data=df) with store_exceptions_on_all_threads(): ui = viewer.edit_traits() viewer.df_updated = True ui.dispose()
def test_data_frame_editor_with_update_refresh(): class DataFrameViewer(HasTraits): data = Instance(DataFrame) df_updated = Event view = View(Item('data', editor=DataFrameEditor(update="df_updated"))) df = DataFrame(DATA, index=['one', 'two', 'three', 'four'], columns=['X', 'Y', 'Z']) viewer = DataFrameViewer(data=df) with store_exceptions_on_all_threads(): ui = viewer.edit_traits() viewer.df_updated = True ui.dispose()
def test_radio_enum_editor_button_update(self): enum_edit = EnumModel() with store_exceptions_on_all_threads(): gui, widget = self.setup_gui(enum_edit, get_view("custom")) # The layout is: one, three, four \n two self.assertEqual(get_all_button_status(widget), [True, False, False, False]) enum_edit.value = "two" gui.process_events() self.assertEqual(get_all_button_status(widget), [False, False, False, True])
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") with store_exceptions_on_all_threads(): gui, _, widget = self.setup_gui(str_edit, get_view("custom")) self.assertEqual(str_edit.value, "two,three,one") click_checkbox_button(widget, 1) gui.process_events() self.assertEqual(str_edit.value, "three,one")
def test_text_check_list_object_str(self): class StrModel(HasTraits): value = Str() str_edit = StrModel(value="three, four") with store_exceptions_on_all_threads(): gui, _, line_edit = self.setup_gui(str_edit, get_view("text")) self.assertEqual(str_edit.value, "three, four") set_text_in_line_edit(line_edit, "one, two") gui.process_events() self.assertEqual(str_edit.value, "one, two")
def _test_actions(self, trigger_action_func): """Template test for wx, qt4, menu, and toolbar testing. """ # Behavior: when clicking on a menu or toolbar action, # the corresponding function should be executed # create dialog with toolbar adn menu dialog = DialogWithToolbar() with store_exceptions_on_all_threads(), create_ui(dialog) as ui: # press toolbar or menu button trigger_action_func(ui) # verify that the action was triggered self.assertTrue(dialog.action_successful)
def test_qt_resizable_in_hgroup(self): # Behavior: Item.resizable controls whether a component can resize # along the non-layout axis of its group. In a HGroup, resizing should # work only in the vertical direction. from pyface import qt with store_exceptions_on_all_threads(), \ create_ui(HResizeDialog()) as ui: editor, = ui.get_editors("txt") text = editor.control # vertical size should be large self.assertGreater(text.height(), _DIALOG_HEIGHT - 100)
def test_filtered_table_editor(): gui = GUI() object_list = ObjectListWithSelection( values=[ListItem(value=str(i**2)) for i in range(10)]) with store_exceptions_on_all_threads(): ui = object_list.edit_traits(view=filtered_view) gui.process_events() filter = ui.get_editors("values")[0].filter press_ok_button(ui) gui.process_events() assert filter is not None
def test_data_frame_editor_with_refresh(): class DataFrameViewer(HasTraits): data = Instance(DataFrame) df_updated = Event view = View( Item('data', editor=DataFrameEditor()) ) df = DataFrame(DATA, index=['one', 'two', 'three', 'four'], columns=['X', 'Y', 'Z']) viewer = DataFrameViewer(data=df) with store_exceptions_on_all_threads(): ui = viewer.edit_traits() viewer.df_updated = True ui.dispose()
def test_filtered_table_editor(self): gui = GUI() object_list = ObjectListWithSelection( values=[ListItem(value=str(i**2)) for i in range(10)]) with store_exceptions_on_all_threads(), \ create_ui(object_list, dict(view=filtered_view)) as ui: gui.process_events() filter = ui.get_editors("values")[0].filter press_ok_button(ui) gui.process_events() self.assertIsNotNone(filter)
def test_readonly_editor_value_changed(self): enum_edit = EnumModel() with store_exceptions_on_all_threads(): with patch(cache_to_patch, wraps=image_cache) as patched_cache: gui, control = self.setup_gui(enum_edit, get_view("readonly")) self.assertEqual(enum_edit.value, 'top left') self.assertEqual(patched_cache.call_args[0][0], "@icons:top left_origin") enum_edit.value = 'top right' gui.process_events() self.assertEqual(patched_cache.call_args[0][0], "@icons:top right_origin")
def check_button_text_update(self, view): gui = GUI() button_text_edit = ButtonTextEdit() with store_exceptions_on_all_threads(): ui = button_text_edit.edit_traits(view=view) self.addCleanup(ui.dispose) gui.process_events() editor, = ui.get_editors("play_button") button = editor.control self.assertEqual(get_button_text(button), "I'm a play button") button_text_edit.play_button_label = "New Label" self.assertEqual(get_button_text(button), "New Label")
def test_filtered_table_editor(): gui = GUI() object_list = ObjectListWithSelection( values=[ListItem(value=str(i**2)) for i in range(10)] ) with store_exceptions_on_all_threads(): ui = object_list.edit_traits(view=filtered_view) gui.process_events() filter = ui.get_editors('values')[0].filter press_ok_button(ui) gui.process_events() assert filter is not None
def test_csv_editor_disposal(self): # Bug: CSVListEditor does not un-hook the traits notifications after # its disposal, causing errors when the hooked data is accessed after # the window is closed (Issue #48) list_of_floats = ListOfFloats(data=[1, 2, 3]) csv_view = ListOfFloatsWithCSVEditor(model=list_of_floats) try: with store_exceptions_on_all_threads(), create_ui(csv_view) as ui: press_ok_button(ui) # raise an exception if still hooked list_of_floats.data.append(2) except AttributeError: # if all went well, we should not be here self.fail("AttributeError raised")
def test_qt_show_labels_right_without_colon(self): # Behavior: traitsui should not append a colon ':' to labels # that are shown to the *right* of the corresponding elements from pyface import qt dialog = ShowRightLabelsDialog() with store_exceptions_on_all_threads(), \ create_ui(dialog) as ui: # get reference to label objects labels = ui.control.findChildren(qt.QtGui.QLabel) # the first is shown to the right, so no colon self.assertFalse(labels[0].text().endswith(":")) # the second is shown to the right, it should have a colon self.assertTrue(labels[1].text().endswith(":"))
def test_simple_check_list_editor_index(self): list_edit = ListModel(value=["one"]) with store_exceptions_on_all_threads(): gui, editor, _ = self.setup_gui(list_edit, get_view("simple")) self.assertEqual(list_edit.value, ["one"]) set_combobox_index(editor, 1) gui.process_events() self.assertEqual(list_edit.value, ["two"]) set_combobox_index(editor, 0) gui.process_events() self.assertEqual(list_edit.value, ["one"])
def test_simple_editor_combobox(self): enum_edit = EnumModel() with store_exceptions_on_all_threads(): gui, combobox = self.setup_gui(enum_edit, get_view("simple")) self.assertEqual(enum_edit.value, 'top left') # Smoke test for ImageEnumItemDelegate painting combobox.showPopup() gui.process_events() combobox.setCurrentIndex(1) combobox.hidePopup() gui.process_events() self.assertEqual(enum_edit.value, 'top right')
def test_custom_editor_selection(self): enum_edit = EnumModel() with store_exceptions_on_all_threads(): gui, control = self.setup_gui(enum_edit, get_view("custom")) self.assertEqual( get_button_strings(control), ['top left', 'top right', 'bottom left', 'bottom right']) self.assertEqual(enum_edit.value, 'top left') self.assertEqual(get_all_button_selected_status(control), [True, False, False, False]) click_on_image(get_button_control(control, 1)) gui.process_events() self.assertEqual(enum_edit.value, 'top right')
def check_enum_text_update(self, view): gui = GUI() enum_edit = EnumModel() with store_exceptions_on_all_threads(): ui = enum_edit.edit_traits(view=view) self.addCleanup(ui.dispose) gui.process_events() editor = ui.get_editors("value")[0] combobox = editor.control self.assertEqual(get_combobox_text(combobox), "one") enum_edit.value = "two" gui.process_events() self.assertEqual(get_combobox_text(combobox), "two")
def test_table_editor_select_column_index(): gui = GUI() object_list = ObjectListWithSelection( values=[ListItem(value=str(i**2)) for i in range(10)] ) object_list.selected_index = 1 with store_exceptions_on_all_threads(): ui = object_list.edit_traits(view=select_column_index_view) editor = ui.get_editors('values')[0] gui.process_events() if is_current_backend_qt4(): selected = editor.selected_indices elif is_current_backend_wx(): selected = editor.selected_column_index press_ok_button(ui) gui.process_events() assert selected == 1
def test_wx_list_str_selected_index(): # behavior: when starting up, the with store_exceptions_on_all_threads(): obj = ListStrEditorWithSelectedIndex( values=['value1', 'value2'], selected_index=1) ui = obj.edit_traits(view=single_select_view) # the following is equivalent to setting the text in the text control, # then pressing OK liststrctrl = ui.control.FindWindowByName('listCtrl') selected_1 = get_selected(liststrctrl) obj.selected_index = 0 selected_2 = get_selected(liststrctrl) # press the OK button and close the dialog press_ok_button(ui) # the number traits should be between 3 and 8 assert selected_1 == [1] assert selected_2 == [0]
def test_data_frame_editor_numerical_index(): viewer = sample_data_numerical_index() with store_exceptions_on_all_threads(): ui = viewer.edit_traits() ui.dispose()
def test_data_frame_editor_font_mapping(): viewer = sample_data() with store_exceptions_on_all_threads(): ui = viewer.edit_traits(view=font_mapping_view) ui.dispose()
def test_data_frame_editor_columns(): viewer = sample_data() with store_exceptions_on_all_threads(): ui = viewer.edit_traits(view=columns_view) ui.dispose()