Esempio n. 1
0
 def test_load_default_registries(self):
     registry = get_default_registry()
     self.assertIsInstance(registry, TargetRegistry)
     if not is_null():
         self.assertGreaterEqual(
             len(registry._interaction_registry._target_to_key_to_value),
             1,
         )
Esempio n. 2
0
class TestHandler(BaseTestMixin, TestCase):
    def setUp(self):
        BaseTestMixin.setUp(self)

    def tearDown(self):
        BaseTestMixin.tearDown(self)

    def test_perform_pyface_action(self):
        object = SampleObject()
        handler = SampleHandler()
        action = PyfaceAction()
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(action.performed)

    def test_perform_traitsui_action(self):
        object = SampleObject()
        handler = SampleHandler()
        action = TraitsUIAction()
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(action.performed)
        self.assertFalse(handler.action_performed)
        self.assertFalse(handler.info_action_performed)
        self.assertFalse(handler.click_performed)
        self.assertFalse(object.action_performed)
        self.assertFalse(object.info_action_performed)
        self.assertFalse(object.click_performed)

    def test_perform_action_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = TraitsUIAction(name="action", action="action_handler")
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(handler.action_performed)
        self.assertFalse(handler.info_action_performed)
        self.assertFalse(handler.click_performed)
        self.assertFalse(object.action_performed)
        self.assertFalse(object.info_action_performed)
        self.assertFalse(object.click_performed)
        self.assertFalse(action.performed)

    def test_perform_info_action_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = TraitsUIAction(name="action", action="info_action_handler")
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(handler.info_action_performed)
        self.assertFalse(handler.action_performed)
        self.assertFalse(handler.click_performed)
        self.assertFalse(object.action_performed)
        self.assertFalse(object.info_action_performed)
        self.assertFalse(object.click_performed)
        self.assertFalse(action.performed)

    def test_perform_click_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = TraitsUIAction(name="action", action="")
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(handler.click_performed)
        self.assertFalse(handler.action_performed)
        self.assertFalse(handler.info_action_performed)
        self.assertFalse(object.action_performed)
        self.assertFalse(object.info_action_performed)
        self.assertFalse(object.click_performed)
        self.assertFalse(action.performed)

    def test_perform_object_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = TraitsUIAction(name="action", action="object_action_handler")
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(object.object_action_performed)
        self.assertFalse(action.performed)

    def test_undo_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = UndoAction
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(handler.undo_performed)

    def test_redo_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = RedoAction
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(handler.redo_performed)

    def test_revert_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = RevertAction
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(handler.revert_performed)

    def test_close_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = CloseAction
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(handler.close_performed)

    def test_help_handler(self):
        object = SampleObject()
        handler = SampleHandler()
        action = HelpAction
        event = ActionEvent()
        ui = UI(handler=handler, context={"object": object})
        info = ui.info

        handler.perform(info, action, event)

        self.assertTrue(handler.help_performed)

    @skipIf(is_null(), "Null toolkit can't create UI")
    def test_handler_init_false(self):
        object = SampleObject()
        handler = SampleHandler(init_return_value=False)

        with self.assertRaises(TraitError):
            object.edit_traits(handler=handler)

    @skipIf(is_null(), "Null toolkit can't create UI")
    def test_handler_init_none(self):
        object = SampleObject()
        handler = SampleHandler(init_return_value=None)

        with self.assertWarns(DeprecationWarning):
            ui = object.edit_traits(handler=handler)

        ui.dispose()
Esempio n. 3
0
from traits.has_traits import HasTraits
from traits.trait_types import Bool
from traitsui.menu import Action, ActionGroup, Menu, MenuBar, ToolBar
from traitsui.item import Item
from traitsui.view import View

from traitsui.tests._tools import (
    create_ui,
    is_mac_os,
    is_null,
    requires_toolkit,
    reraise_exceptions,
    ToolkitName,
)

if is_null():
    raise unittest.SkipTest("Not supported using the null backend")

TestAction = Action(name="Test",
                    action="test_clicked",
                    tooltip="Click to test")


class DialogWithToolbar(HasTraits):
    """Test dialog with toolbar and menu."""

    action_successful = Bool(False)

    def test_clicked(self):
        self.action_successful = True