def _test_tree_editor_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. """ bogus = Bogus(bogus_list=[Bogus()]) tree_editor_view = BogusTreeView(bogus=bogus, hide_root=hide_root, nodes=nodes) with reraise_exceptions(), create_ui(tree_editor_view): # 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)) # 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_video_editor_basics(self): obj = MovieTheater() view = View( Item( 'url', editor=VideoEditor( state='state', position='position', duration='duration', video_error='error', media_status='status', buffer='buffer', muted='muted', volume='volume', playback_rate='playback_rate', image_func='image_func', notify_interval='notify_interval', ), ), ) # This should not fail. with create_ui(obj, {'view': view}): pass
def test_image_editor_array(self): try: import numpy as np from pyface.api import ArrayImage except ImportError: self.skipTest("NumPy is not available") gradient1 = np.empty(shape=(256, 256, 3), dtype='uint8') gradient1[:, :, 0] = np.arange(256).reshape(256, 1) gradient1[:, :, 1] = np.arange(256).reshape(1, 256) gradient1[:, :, 2] = np.arange(255, -1, -1).reshape(1, 256) gradient2 = np.empty(shape=(256, 256, 3), dtype='uint8') gradient2[:, :, 0] = np.arange(255, -1, -1).reshape(256, 1) gradient2[:, :, 1] = np.arange(256).reshape(1, 256) gradient2[:, :, 2] = np.arange(255, -1, -1).reshape(1, 256) obj1 = ImageDisplay(image=ArrayImage(data=gradient1)) view = View(Item('image', editor=ImageEditor())) # This should not fail. with create_ui(obj1, dict(view=view)) as ui: obj1.image = ArrayImage(data=gradient2)
def test_progress_column(self): 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(), \ create_ui(object_list, dict(view=progress_view)) as ui: gui.process_events() press_ok_button(ui) gui.process_events()
def test_wx_list_str_multi_selected_index(self): # behavior: when starting up, the obj = ListStrEditorWithSelectedIndex( values=["value1", "value2"], selected_indices=[1] ) with store_exceptions_on_all_threads(), \ create_ui(obj, dict(view=multi_select_view)) as ui: editor = ui.get_editors("values")[0] # the following is equivalent to setting the text in the text # control, then pressing OK selected_1 = get_selected_indices(editor) obj.selected_indices = [0] selected_2 = get_selected_indices(editor) # press the OK button and close the dialog press_ok_button(ui) # the number traits should be between 3 and 8 self.assertEqual(selected_1, [1]) self.assertEqual(selected_2, [0])
def test_code_editor_readonly(self): """Test readonly editor style for CodeEditor""" from pyface import qt code_model = CodeModel() code_view = CodeView(model=code_model, style="readonly") with reraise_exceptions(), create_ui(code_view) as ui: txt_ctrl = ui.control.findChild(qt.QtGui.QPlainTextEdit) self.assertTrue(txt_ctrl.isReadOnly()) # Test changing the object's text self.assertEqual(txt_ctrl.toPlainText(), code_model.code) code_model.code += "some more code" self.assertTrue(txt_ctrl.isReadOnly()) self.assertEqual(txt_ctrl.toPlainText(), code_model.code) # Test changing the underlying object code_model2 = CodeModel(code=code_model.code * 2) code_view.model = code_model2 self.assertTrue(txt_ctrl.isReadOnly()) self.assertEqual(txt_ctrl.toPlainText(), code_model.code) ui.control.close()
def test_wx_spin_control_editing_does_not_update(self): # Bug: when editing the text part of a spin control box, pressing # the OK button does not update the value of the HasTraits class # on Mac OS X # But under wx >= 3.0 this has been resolved import wx if wx.VERSION >= (3, 0): return num = NumberWithSpinnerEditor() with store_exceptions_on_all_threads(), create_ui(num) as ui: # the following is equivalent to clicking in the text control of # the range editor, enter a number, and clicking ok without # defocusing # SpinCtrl object spin = ui.control.FindWindowByName("wxSpinCtrl") spin.SetFocusFromKbd() # on Windows, a wxSpinCtrl does not have children, and we cannot do # the more fine-grained testing below if len(spin.GetChildren()) == 0: spin.SetValueString("4") else: # TextCtrl object of the spin control spintxt = spin.FindWindowByName("text") spintxt.SetValue("4") # press the OK button and close the dialog press_ok_button(ui) # if all went well, the number traits has been updated and its # value is 4 self.assertEqual(num.number, 4)
def test_qt_spin_control_editing(self): # Behavior: when editing the text part of a spin control box, pressing # the OK button updates the value of the HasTraits class from pyface import qt num = NumberWithSpinnerEditor() with store_exceptions_on_all_threads(), create_ui(num) as ui: # the following is equivalent to clicking in the text control of # the range editor, enter a number, and clicking ok without # defocusing # text element inside the spin control lineedit = ui.control.findChild(qt.QtGui.QLineEdit) lineedit.setFocus() lineedit.setText("4") # press the OK button and close the dialog press_ok_button(ui) # if all went well, the number traits has been updated and its value is # 4 self.assertEqual(num.number, 4)
def test_move_rows_invalid_index(self): # Test the last resort to prevent segfault obj = DummyHasTraits(names=["A", "B", "C"]) view = get_view(TabularAdapter(columns=["Name"])) with reraise_exceptions(), create_ui(obj, dict(view=view)) as ui: (editor, ) = ui.get_editors("names") model = editor.model # sanity check self.assertEqual(model.rowCount(None), 3) # when # -1 is an invalid row. This should not cause segfault. model.moveRows([1], -1) # then mime_data = model.mimeData([ model.createIndex(i, 0) for i in range(model.rowCount(None), ) ]) content = mime_data.instance() self.assertEqual(content, ["A", "C", "B"]) self.assertEqual(obj.names, content)
def launch_editor(self, view_factory): foo = Foo() with create_ui(foo, dict(view=view_factory())) as ui: editor, = ui._editors yield foo, editor
def test_no_spring_trait(self): obj = DisallowNewTraits() with create_ui(obj): pass self.assertTrue("spring" not in obj.traits())
def test_data_frame_editor_columns(self): viewer = sample_data() with reraise_exceptions(), \ create_ui(viewer, dict(view=columns_view)): pass
def test_data_frame_editor_font_mapping(self): viewer = sample_data() with reraise_exceptions(), \ create_ui(viewer, dict(view=font_mapping_view)): pass
def test_enable_when_flag(self): with reraise_exceptions(), \ create_ui(EnableWhenDialog()): pass
def test_horizontal_resizable_and_labels(self): with store_exceptions_on_all_threads(), \ create_ui(HResizeTestDialog()): pass
def test_value_update(self): # Regression test for #179 model = DummyModel() with create_ui(model) as ui: with self.assertTraitChanges(model, "data", count=1): model.data = (3, 4.6, "nono")
def test_value_update(self): # Regression test for #179 model = TupleEditor() with create_ui(model): with self.assertTraitChanges(model, "tup", count=1): model.tup = (3, 4, "nono")
def test_enable_when_flag(self): with store_exceptions_on_all_threads(), \ create_ui(EnableWhenDialog()): pass
def test_all_resizable_with_labels(self): with store_exceptions_on_all_threads(), \ create_ui(VResizeTestDialog()): pass
def check_init_and_dispose(self, view): with reraise_exceptions(), \ create_ui(Foo(), dict(view=view)): pass
def launch_editor(self, object, view): with create_ui(object, dict(view=view)) as ui: editor, = ui._editors yield editor
def test_horizontal_resizable_and_labels(self): with reraise_exceptions(), \ create_ui(HResizeTestDialog()): pass
def test_line_numbers_visibility(show=True): code_model = CodeModel() code_view = CodeView(model=code_model, show_line_numbers=show) with reraise_exceptions(), create_ui(code_view) as ui: self.assertEqual(is_line_numbers_visible(ui), show) ui.control.close()
def test_smoke_save_restore_prefs(self): bogus = Bogus(bogus_list=[Bogus()]) tree_editor_view = BogusTreeView(bogus=bogus) with create_ui(tree_editor_view) as ui: prefs = ui.get_prefs() ui.set_prefs(prefs)
def test_group_show_right_labels(self): with reraise_exceptions(), \ create_ui(ShowRightLabelsDialog()): pass
def test_smoke_word_wrap(self): bogus = Bogus(bogus_list=[Bogus()]) tree_editor_view = BogusTreeView(bogus=bogus, word_wrap=True) with create_ui(tree_editor_view): pass
def test_all_resizable_with_labels(self): with reraise_exceptions(), \ create_ui(VResizeTestDialog()): pass
def test_data_frame_editor_numerical_index(self): viewer = sample_data_numerical_index() with reraise_exceptions(), create_ui(viewer): pass
def setup_ui(self, model, view): with create_ui(model, dict(view=view)) as ui: yield ui.get_editors("value")[0]
def test_data_frame_editor_text_data(self): viewer = sample_text_data() with reraise_exceptions(), create_ui(viewer): pass