This file is indexed.

/usr/lib/python2.7/dist-packages/traitsui/tests/test_actions.py is in python-traitsui 4.4.0-1.3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#------------------------------------------------------------------------------
#
#  Copyright (c) 2012, Enthought, Inc.
#  All rights reserved.
#
#  This software is provided without warranty under the terms of the BSD
#  license included in enthought/LICENSE.txt and may be redistributed only
#  under the conditions described in the aforementioned license.  The license
#  is also available online at http://www.enthought.com/licenses/BSD.txt
#
#  Author: Pietro Berkes
#  Date:   Feb 2012
#
#------------------------------------------------------------------------------

"""
Test that menu and toolbar actions are triggered.
"""

import pyface

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 *


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

    menubar = MenuBar(
        Menu(
            ActionGroup(TestAction),
            name='&Test menu'
        ),
    )

    toolbar = ToolBar(
        ActionGroup(TestAction),
    )

    traits_view = View(
        Item(label="Click the button on the toolbar or the menu item.\n"
                   "The 'Action successful' element should turn to True."),
        Item('action_successful', style='readonly'),
        menubar = menubar,
        toolbar = toolbar,
        buttons = ['OK']
    )


def _test_actions(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

    with store_exceptions_on_all_threads():
        # create dialog with toolbar adn menu
        dialog = DialogWithToolbar()
        ui = dialog.edit_traits()

        # press toolbar or menu button
        trigger_action_func(ui)

        # verify that the action was triggered
        nose.tools.assert_true(dialog.action_successful)


# ----- Qt4 tests

def _qt_trigger_action(container_class, ui):
    toolbar = ui.control.findChild(container_class)
    action = toolbar.actions()[0]
    action.trigger()


@skip_if_not_qt4
def test_qt_toolbar_action():
    # Behavior: when clicking on a toolbar action, the corresponding function
    # should be executed

    # Bug: in the Qt4 backend, a
    # TypeError: perform() takes exactly 2 arguments (1 given) was raised
    # instead

    qt_trigger_toolbar_action = partial(
        _qt_trigger_action, pyface.ui.qt4.action.tool_bar_manager._ToolBar)

    _test_actions(qt_trigger_toolbar_action)


@skip_if_not_qt4
def test_qt_menu_action():
    # Behavior: when clicking on a menu action, the corresponding function
    # should be executed

    # Bug: in the Qt4 backend, a
    # TypeError: perform() takes exactly 2 arguments (1 given) was raised
    # instead

    qt_trigger_menu_action = partial(
        _qt_trigger_action, pyface.ui.qt4.action.menu_manager._Menu)

    _test_actions(qt_trigger_menu_action)


# ----- wx tests

@skip_if_not_wx
def test_wx_toolbar_action():
    # Behavior: when clicking on a toolbar action, the corresponding function
    # should be executed

    import wx

    def _wx_trigger_toolbar_action(ui):
        # long road to get at the Id of the toolbar button
        toolbar_item = ui.view.toolbar.groups[0].items[0]
        toolbar_item_wrapper = toolbar_item._wrappers[0]
        control_id = toolbar_item_wrapper.control_id

        # build event that clicks the button
        click_event = wx.CommandEvent(wx.wxEVT_COMMAND_TOOL_CLICKED,
                                      control_id)

        # send the event to the toolbar
        toolbar = ui.control.FindWindowByName('toolbar')
        toolbar.ProcessEvent(click_event)

    _test_actions(_wx_trigger_toolbar_action)


# TODO: I couldn't find a way to press menu items programmatically for wx


if __name__ == '__main__':
    # Execute from command line for manual testing
    vw = DialogWithToolbar()
    vw.configure_traits()