def test_buttons_exist(self): tester = ModalDialogTester( partial(self.ui.edit_traits, kind="modal", handler=ContributedUIHandler())) tester.open_and_run( when_opened=lambda x: x.click_widget("Run Workflow")) tester.open_and_run( when_opened=lambda x: x.click_widget("Update Workflow"))
def test_dialog_was_not_opened_on_traitsui_dialog(self): my_class = MyClass() tester = ModalDialogTester(my_class.do_not_show_dialog) # it runs okay tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertTrue(tester.value_assigned()) self.assertEqual(tester.result, True) # but no dialog is opened self.assertFalse(tester.dialog_was_opened)
def test_lattice_show(self): lattice = make_cubic_lattice( 'test', 0.2, (10, 10, 1), origin=(0.2, -2.4, 0.)) def function(): show(lattice) return True tester = ModalDialogTester(function) tester.open_and_run(when_opened=lambda x: x.close(accept=False)) self.assertTrue(tester.result)
def test_on_message_dialog(self): dialog = MessageDialog() tester = ModalDialogTester(dialog.open) # accept tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertTrue(tester.value_assigned()) self.assertEqual(tester.result, OK) # reject tester.open_and_run(when_opened=lambda x: x.close()) self.assertTrue(tester.value_assigned()) self.assertEqual(tester.result, CANCEL)
def test_on_traitsui_dialog(self): my_class = MyClass() tester = ModalDialogTester(my_class.run) # accept tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertTrue(tester.value_assigned()) self.assertEqual(tester.result, 'accepted') # reject tester.open_and_run(when_opened=lambda x: x.close()) self.assertTrue(tester.value_assigned()) self.assertEqual(tester.result, 'rejected')
def test_error_animate_fired_with_mayavi_engine_undefined(self): # given self.panel = RunAndAnimatePanel(engine=DummyEngine()) # when def animate(): self.panel._RunAndAnimatePanel__animate_fired() return True # then tester = ModalDialogTester(animate) tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertTrue(tester.result)
def test_error_if_number_of_time_steps_not_found(self): engine = DummyEngine() mayavi_engine = NullEngine() engine.CM.pop(CUBA.NUMBER_OF_TIME_STEPS) # when def init(): RunAndAnimatePanel(engine=engine, mayavi_engine=mayavi_engine) return True # then tester = ModalDialogTester(init) tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertTrue(tester.result)
def test_error_add_dataset_when_engine_is_empty(self): # when self.engine.datasets = {} def function(): self.panel._AddSourcePanel__add_dataset_fired() return True # then # a message dialog is displayed and the pending tester = ModalDialogTester(function) tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertTrue(tester.result) # number of EngineSource is unchanged self.assertEqual(len(self.panel._pending_engine_sources), 0)
def test_error_animate_fired_with_objects_not_from_current_engine(self): # given self._setUp() self.panel.engine = DummyEngine() self.panel.mayavi_engine.add_source(VTKDataSource()) # when def animate(): self.panel._RunAndAnimatePanel__animate_fired() return True # then tester = ModalDialogTester(animate) tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertTrue(tester.result)
def test_find_widget(self): dialog = Dialog() tester = ModalDialogTester(dialog.open) def check_and_close(tester): try: with tester.capture_error(): widget = tester.find_qt_widget( type_=QtGui.QAbstractButton, test=lambda x: x.text() == 'OK') self.assertIsInstance(widget, QtGui.QPushButton) finally: tester.close() tester.open_and_run(when_opened=check_and_close)
def test_has_widget(self): dialog = Dialog() tester = ModalDialogTester(dialog.open) def check_and_close(tester): try: with tester.capture_error(): self.assertTrue( tester.has_widget('OK', QtGui.QAbstractButton)) self.assertFalse( tester.has_widget(text='I am a virtual button')) finally: tester.close() tester.open_and_run(when_opened=check_and_close)
def test_ok_default(self): tester = ModalDialogTester(self.dialog.main) def press_enter(t): dialog = t.click_button(9) #enter_press = QKeyEvent(QEvent.KeyRelease, Qt.Key_Enter, # Qt.NoModifier) #dialog.keyPressEvent(enter_press) #enter_release = QKeyEvent(QEvent.KeyRelease, Qt.Key_Enter, # Qt.NoModifier) #dialog.keyReleaseEvent(enter_release) tester.open_and_run(press_enter) self.assertEqual(tester.result, OK)
def test_on_message_dialog(self): dialog = MessageDialog() tester = ModalDialogTester(dialog.open) # accept tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertTrue(tester.value_assigned()) self.assertEqual(tester.result, OK) self.assertTrue(tester.dialog_was_opened) # reject tester.open_and_run(when_opened=lambda x: x.close()) self.assertTrue(tester.value_assigned()) self.assertEqual(tester.result, CANCEL) self.assertTrue(tester.dialog_was_opened)
def test_on_traitsui_dialog(self): my_class = MyClass() tester = ModalDialogTester(my_class.run) # accept tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertTrue(tester.value_assigned()) self.assertEqual(tester.result, 'accepted') self.assertTrue(tester.dialog_was_opened) # reject tester.open_and_run(when_opened=lambda x: x.close()) self.assertTrue(tester.value_assigned()) self.assertEqual(tester.result, 'rejected') self.assertTrue(tester.dialog_was_opened)
def test_ok_cancel_selection(self): contributed_ui = DummyContributedUI() other_contributed_ui = DummyContributedUI2() self.ui_modal = UISelectModal( contributed_uis=[contributed_ui, other_contributed_ui], available_plugins=self.plugins) # OK self.ui_modal.selected_ui_name = "DummyUI" tester = ModalDialogTester(self.ui_modal.edit_traits) tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertIsInstance(self.ui_modal.selected_ui, DummyContributedUI) # Cancel self.ui_modal.selected_ui_name = "DummyUI" tester = ModalDialogTester(self.ui_modal.edit_traits) tester.open_and_run(when_opened=lambda x: x.close(accept=False)) self.assertIsNone(self.ui_modal.selected_ui)
def test_find_widget(self): my_class = MyClass() tester = ModalDialogTester(my_class.run) def check_and_close(tester): try: with tester.capture_error(): widget = tester.find_qt_widget( type_=QtGui.QPushButton, test=lambda x: x.text() == 'OK' ) self.assertIsInstance(widget, QtGui.QPushButton) finally: tester.close() tester.open_and_run(when_opened=check_and_close)
def test_capture_errors_on_error(self): dialog = MessageDialog() tester = ModalDialogTester(dialog.open) def raise_error(tester): try: with tester.capture_error(): # this error will appear in the console and get recorded 1 / 0 finally: tester.close() with self.assertRaises(ZeroDivisionError): alt_stderr = cStringIO.StringIO with silence_output(err=alt_stderr): tester.open_and_run(when_opened=raise_error) self.assertIn('ZeroDivisionError', alt_stderr)
def test_capture_errors_on_failure(self): dialog = MessageDialog() tester = ModalDialogTester(dialog.open) def failure(tester): try: with tester.capture_error(): # this failure will appear in the console and get recorded self.fail() finally: tester.close() with self.assertRaises(AssertionError): alt_stderr = cStringIO.StringIO with silence_output(err=alt_stderr): tester.open_and_run(when_opened=failure) self.assertIn('raise self.failureException(msg)', alt_stderr)
def test_has_widget(self): dialog = Dialog() tester = ModalDialogTester(dialog.open) def check_and_close(tester): try: with tester.capture_error(): self.assertTrue( tester.has_widget('OK', QtGui.QAbstractButton) ) self.assertFalse( tester.has_widget(text='I am a virtual button') ) finally: tester.close() tester.open_and_run(when_opened=check_and_close)
def test_has_widget(self): my_class = MyClass() tester = ModalDialogTester(my_class.run) def check_and_close(tester): try: with tester.capture_error(): self.assertTrue( tester.has_widget('OK', QtGui.QPushButton) ) self.assertFalse( tester.has_widget(text='I am a virtual button') ) finally: tester.close() tester.open_and_run(when_opened=check_and_close)
def test_capture_errors_on_error(self): dialog = MessageDialog() tester = ModalDialogTester(dialog.open) def raise_error(tester): try: with tester.capture_error(): # this error will appear in the console and get recorded 1 / 0 finally: tester.close() with self.assertRaises(ZeroDivisionError): alt_stderr = io.StringIO with silence_output(err=alt_stderr): tester.open_and_run(when_opened=raise_error) self.assertIn('ZeroDivisionError', alt_stderr)
def test_capture_errors_on_failure(self): dialog = MessageDialog() tester = ModalDialogTester(dialog.open) def failure(tester): try: with tester.capture_error(): # this failure will appear in the console and get recorded self.fail() finally: tester.close() with self.assertRaises(AssertionError): alt_stderr = io.StringIO with silence_output(err=alt_stderr): tester.open_and_run(when_opened=failure) self.assertIn('raise self.failureException(msg)', alt_stderr)
def test_mesh_show(self): points = numpy.array([ [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [2, 0, 0], [3, 0, 0], [3, 1, 0], [2, 1, 0], [2, 0, 1], [3, 0, 1], [3, 1, 1], [2, 1, 1]], 'f') mesh = Mesh('test') point_iter = (Point(coordinates=point) for point in points) mesh.add(point_iter) def function(): show(mesh) return True tester = ModalDialogTester(function) tester.open_and_run(when_opened=lambda x: x.close(accept=False)) self.assertTrue(tester.result)
def test_particles_show(self): coordinates = numpy.array([ [0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1], [2, 0, 0], [3, 0, 0], [3, 1, 0], [2, 1, 0], [2, 0, 1], [3, 0, 1], [3, 1, 1], [2, 1, 1]], 'f') particles = Particles('test') particle_iter = (Particle(coordinates=point+3) for point in coordinates) particles.add(particle_iter) def function(): show(particles) return True tester = ModalDialogTester(function) tester.open_and_run(when_opened=lambda x: x.close(accept=False)) self.assertTrue(tester.result)
def test_load_ui_file(self): class _Model(HasStrictTraits): text = Unicode() def default_traits_view(self): _ValidatedField = TextField(validator=QtGui.QIntValidator()) traits_view = View( Bound( UIFile( localfile('form.ui'), overrides={ 'lineEdit': _ValidatedField, }, ), 'lineEdit.value := object.text', ), ) return traits_view m = _Model() tester = ModalDialogTester(lambda: m.edit_traits(kind='livemodal')) def _test(tester): # Send valid and invalid data through the model to see if it # propagates to our overridden widget correctly. try: w = tester.find_qt_widget( type_=QtGui.QLineEdit, test=lambda w: w.objectName() == u'lineEdit', ) self.assertEqual(m.text, w.text()) m.text = u'10' self.assertEqual(w.text(), u'10') self.assertEqual(w.property('valid'), True) m.text = u'abc' self.assertEqual(w.text(), u'abc') self.assertEqual(w.property('valid'), False) finally: tester.close(accept=True) tester.open_and_run(when_opened=_test)
def test_warning_mayavi_engine_invalid(self): # given ui = self.panel._AddSourcePanel__add_dataset_fired() press_button_by_label(ui, "Confirm") ui = self.panel.show_config() # when self.panel.mayavi_engine = None def function(): press_button_by_label(ui, "Send to Scene") return True # then # a message dialog is displayed and the pending tester = ModalDialogTester(function) tester.open_and_run(when_opened=lambda x: x.close(accept=True)) self.assertTrue(tester.result) # number of EngineSource is unchanged self.assertEqual(len(self.panel._pending_engine_sources), 1)
def test_load_ui_file(self): class _Model(HasStrictTraits): text = Unicode() number = Int() # Only for testing. Not a good idea for production, otherwise # multiple views will conflict with each other. ui_file = Instance(UIFile) def default_traits_view(self): traits_view = View( Bound( self.ui_file, 'lineEdit.value := object.text', 'widget.value := object.number', ), ) return traits_view def _ui_file_default(self): _ValidatedField = TextField(validator=QtGui.QIntValidator()) _IntSlider = IntSlider(range=(0, 10)) ui_file = UIFile( localfile('form.ui'), overrides={ 'lineEdit': _ValidatedField, }, insertions={ 'widget': _IntSlider, }, ) return ui_file m = _Model() tester = ModalDialogTester(lambda: m.edit_traits(kind='livemodal')) def _test(tester): with tester.capture_error(): # Send valid and invalid data through the model to see if it # propagates to our overridden widget correctly. try: w = tester.find_qt_widget( type_=QtGui.QLineEdit, test=lambda w: w.objectName() == u'lineEdit', ) self.assertEqual(m.text, w.text()) m.text = u'10' self.assertEqual(w.text(), u'10') self.assertEqual(w.property('valid'), True) m.text = u'abc' self.assertEqual(w.text(), u'abc') self.assertEqual(w.property('valid'), False) w = tester.find_qt_widget( type_=QtGui.QSlider, test=lambda w: getattr( w.parent(), 'objectName')() == u'widget', ) self.assertEqual(m.number, w.value()) m.number = 5 self.assertEqual(w.value(), 5) self.assertIsInstance(w, QtGui.QSlider) self.assertIs(m.ui_file.widget.qobj, w) finally: tester.close(accept=True) tester.open_and_run(when_opened=_test)
def test_load_ui_file(self): class _Model(HasStrictTraits): text = Unicode() number = Int() # Only for testing. Not a good idea for production, otherwise # multiple views will conflict with each other. ui_file = Instance(UIFile) def default_traits_view(self): traits_view = View( Bound( self.ui_file, 'lineEdit.value := object.text', 'widget.value := object.number', ), ) return traits_view def _ui_file_default(self): _ValidatedField = TextField(validator=QtGui.QIntValidator()) _IntSlider = IntSlider(range=(0, 10)) ui_file = UIFile( localfile('form.ui'), overrides={ 'lineEdit': _ValidatedField, }, insertions={ 'widget': _IntSlider, }, ) return ui_file m = _Model() tester = ModalDialogTester(lambda: m.edit_traits(kind='livemodal')) def _test(tester): with tester.capture_error(): # Send valid and invalid data through the model to see if it # propagates to our overridden widget correctly. try: w = tester.find_qt_widget( type_=QtGui.QLineEdit, test=lambda w: w.objectName() == u'lineEdit', ) self.assertEqual(m.text, w.text()) m.text = u'10' self.assertEqual(w.text(), u'10') self.assertEqual(w.property('valid'), True) m.text = u'abc' self.assertEqual(w.text(), u'abc') self.assertEqual(w.property('valid'), False) w = tester.find_qt_widget( type_=QtGui.QSlider, test=lambda w: getattr(w.parent(), 'objectName') () == u'widget', ) self.assertEqual(m.number, w.value()) m.number = 5 self.assertEqual(w.value(), 5) self.assertIsInstance(w, QtGui.QSlider) self.assertIs(m.ui_file.widget.qobj, w) finally: tester.close(accept=True) tester.open_and_run(when_opened=_test)