~3v1n0/unity/light-shortcuts

« back to all changes in this revision

Viewing changes to tests/autopilot/unity/emulators/panel.py

  • Committer: Marco Trevisan (Treviño)
  • Date: 2013-04-26 12:41:09 UTC
  • Revision ID: mail@3v1n0.net-20130426124109-t3b2shjah2omiqa2
Unity: Remove all the views, but the Shortcuts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
 
# Copyright 2012 Canonical
3
 
# Author: Marco Trevisan (Treviño)
4
 
#
5
 
# This program is free software: you can redistribute it and/or modify it
6
 
# under the terms of the GNU General Public License version 3, as published
7
 
# by the Free Software Foundation.
8
 
#
9
 
 
10
 
from __future__ import absolute_import
11
 
 
12
 
import logging
13
 
from time import sleep
14
 
 
15
 
from autopilot.emulators.X11 import Mouse
16
 
from autopilot.keybindings import KeybindingsHelper
17
 
 
18
 
from unity.emulators import UnityIntrospectionObject
19
 
logger = logging.getLogger(__name__)
20
 
 
21
 
 
22
 
class PanelController(UnityIntrospectionObject):
23
 
    """The PanelController class."""
24
 
 
25
 
    def get_panel_for_monitor(self, monitor_num):
26
 
        """Return an instance of panel for the specified monitor, or None."""
27
 
        panels = self.get_children_by_type(UnityPanel, monitor=monitor_num)
28
 
        assert(len(panels) == 1)
29
 
        return panels[0]
30
 
 
31
 
    def get_active_panel(self):
32
 
        """Return the active panel, or None."""
33
 
        panels = self.get_children_by_type(UnityPanel, active=True)
34
 
        assert(len(panels) == 1)
35
 
        return panels[0]
36
 
 
37
 
    def get_active_indicator(self):
38
 
        for panel in self.get_panels:
39
 
            active = panel.get_active_indicator()
40
 
            if active:
41
 
                return active
42
 
 
43
 
        return None
44
 
 
45
 
    @property
46
 
    def get_panels(self):
47
 
        """Return the available panels, or None."""
48
 
        return self.get_children_by_type(UnityPanel)
49
 
 
50
 
 
51
 
class UnityPanel(UnityIntrospectionObject, KeybindingsHelper):
52
 
    """An individual panel for a monitor."""
53
 
 
54
 
    def __init__(self, *args, **kwargs):
55
 
        super(UnityPanel, self).__init__(*args, **kwargs)
56
 
        self._mouse = Mouse()
57
 
 
58
 
    def __get_menu_view(self):
59
 
        """Return the menu view."""
60
 
        menus = self.get_children_by_type(MenuView)
61
 
        assert(len(menus) == 1)
62
 
        return menus[0]
63
 
 
64
 
    def __get_window_buttons(self):
65
 
        """Return the window buttons view."""
66
 
        buttons = self.menus.get_children_by_type(WindowButtons)
67
 
        assert(len(buttons) == 1)
68
 
        return buttons[0]
69
 
 
70
 
    def __get_grab_area(self):
71
 
        """Return the panel grab area."""
72
 
        grab_areas = self.menus.get_children_by_type(GrabArea)
73
 
        assert(len(grab_areas) == 1)
74
 
        return grab_areas[0]
75
 
 
76
 
    def __get_indicators_view(self):
77
 
        """Return the menu view."""
78
 
        indicators = self.get_children_by_type(Indicators)
79
 
        assert(len(indicators) == 1)
80
 
        return indicators[0]
81
 
 
82
 
    def move_mouse_below_the_panel(self):
83
 
        """Places the mouse to bottom of this panel."""
84
 
        (x, y, w, h) = self.geometry
85
 
        target_x = x + w / 2
86
 
        target_y = y + h + 10
87
 
 
88
 
        logger.debug("Moving mouse away from panel.")
89
 
        self._mouse.move(target_x, target_y)
90
 
 
91
 
    def move_mouse_over_menus(self):
92
 
        """Move the mouse over the menu area for this panel."""
93
 
        (x, y, w, h) = self.menus.geometry
94
 
        target_x = x + w / 2
95
 
        target_y = y + h / 2
96
 
 
97
 
        # The menu view has bigger geometry than the real layout
98
 
        menu_entries = self.menus.get_entries()
99
 
        if len(menu_entries) > 0:
100
 
            first_x = menu_entries[0].x
101
 
            last_x = menu_entries[-1].x + menu_entries[-1].width / 2
102
 
 
103
 
            target_x = first_x + (last_x - first_x) / 2
104
 
 
105
 
        logger.debug("Moving mouse to center of menu area.")
106
 
        self._mouse.move(target_x, target_y)
107
 
 
108
 
    def move_mouse_over_grab_area(self):
109
 
        """Move the mouse over the grab area for this panel."""
110
 
        (x, y, w, h) = self.grab_area.geometry
111
 
        target_x = x + w / 2
112
 
        target_y = y + h / 2
113
 
 
114
 
        logger.debug("Moving mouse to center of grab area.")
115
 
        self._mouse.move(target_x, target_y)
116
 
 
117
 
    def move_mouse_over_window_buttons(self):
118
 
        """Move the mouse over the center of the window buttons area for this panel."""
119
 
        (x, y, w, h) = self.window_buttons.geometry
120
 
        target_x = x + w / 2
121
 
        target_y = y + h / 2
122
 
 
123
 
        logger.debug("Moving mouse to center of the window buttons.")
124
 
        self._mouse.move(target_x, target_y)
125
 
 
126
 
    def move_mouse_over_indicators(self):
127
 
        """Move the mouse over the center of the indicators area for this panel."""
128
 
        (x, y, w, h) = self.indicators.geometry
129
 
        target_x = x + w / 2
130
 
        target_y = y + h / 2
131
 
 
132
 
        logger.debug("Moving mouse to center of the indicators area.")
133
 
        self._mouse.move(target_x, target_y)
134
 
 
135
 
    def get_indicator_entries(self, visible_only=True, include_hidden_menus=False):
136
 
        """Returns a list of entries for this panel including both menus and indicators"""
137
 
        entries = []
138
 
        if include_hidden_menus or self.menus_shown:
139
 
            entries = self.menus.get_entries()
140
 
        entries += self.indicators.get_ordered_entries(visible_only)
141
 
        return entries
142
 
 
143
 
    def get_active_indicator(self):
144
 
        """Returns the indicator entry that is currently active"""
145
 
        entries = self.get_indicator_entries(False, True)
146
 
        entries = filter(lambda e: e.active == True, entries)
147
 
        assert(len(entries) <= 1)
148
 
        return entries[0] if entries else None
149
 
 
150
 
    def get_indicator_entry(self, entry_id):
151
 
        """Returns the indicator entry for the given ID or None"""
152
 
        entries = self.get_indicator_entries(False, True)
153
 
        entries = filter(lambda e: e.entry_id == entry_id, entries)
154
 
        assert(len(entries) <= 1)
155
 
        return entries[0] if entries else None
156
 
 
157
 
    @property
158
 
    def title(self):
159
 
        return self.menus.panel_title
160
 
 
161
 
    @property
162
 
    def desktop_is_active(self):
163
 
        return self.menus.desktop_active
164
 
 
165
 
    @property
166
 
    def menus_shown(self):
167
 
        return self.active and self.menus.draw_menus
168
 
 
169
 
    @property
170
 
    def window_buttons_shown(self):
171
 
        return self.menus.draw_window_buttons
172
 
 
173
 
    @property
174
 
    def window_buttons(self):
175
 
        return self.__get_window_buttons()
176
 
 
177
 
    @property
178
 
    def menus(self):
179
 
        return self.__get_menu_view()
180
 
 
181
 
    @property
182
 
    def grab_area(self):
183
 
        return self.__get_grab_area()
184
 
 
185
 
    @property
186
 
    def indicators(self):
187
 
        return self.__get_indicators_view()
188
 
 
189
 
    @property
190
 
    def geometry(self):
191
 
        """Returns a tuple of (x,y,w,h) for the current panel."""
192
 
        return (self.x, self.y, self.width, self.height)
193
 
 
194
 
 
195
 
class MenuView(UnityIntrospectionObject):
196
 
    """The Menu View class."""
197
 
 
198
 
    def get_entries(self):
199
 
        """Return a list of menu entries"""
200
 
        entries = self.get_children_by_type(IndicatorEntry)
201
 
        # We need to filter out empty entries, which are seperators - those
202
 
        # are not valid, visible and working entries
203
 
        # For instance, gedit adds some of those, breaking our tests
204
 
        entries = [e for e in entries if (e.label != "")]
205
 
        return entries
206
 
 
207
 
    def get_menu_by_label(self, entry_label):
208
 
        """Return the first indicator entry found with the given label"""
209
 
        indicators = self.get_children_by_type(IndicatorEntry, label=entry_label)
210
 
        return indicators[0] if indicators else None
211
 
 
212
 
    @property
213
 
    def geometry(self):
214
 
        """Returns a tuple of (x,y,w,h) for the current menu view."""
215
 
        return (self.x, self.y, self.width, self.height)
216
 
 
217
 
 
218
 
class WindowButtons(UnityIntrospectionObject):
219
 
    """The window buttons class"""
220
 
 
221
 
    def get_buttons(self, visible_only=True):
222
 
        """Return a list of window buttons"""
223
 
        if visible_only:
224
 
            return self.get_children_by_type(WindowButton, visible=True)
225
 
        else:
226
 
            return self.get_children_by_type(WindowButton)
227
 
 
228
 
    def get_button(self, type):
229
 
        buttons = self.get_children_by_type(WindowButton, type=type)
230
 
        assert(len(buttons) == 1)
231
 
        return buttons[0]
232
 
 
233
 
    @property
234
 
    def visible(self):
235
 
        return len(self.get_buttons()) != 0
236
 
 
237
 
    @property
238
 
    def close(self):
239
 
        return self.get_button("Close")
240
 
 
241
 
    @property
242
 
    def minimize(self):
243
 
        return self.get_button("Minimize")
244
 
 
245
 
    @property
246
 
    def unmaximize(self):
247
 
        return self.get_button("Unmaximize")
248
 
 
249
 
    @property
250
 
    def maximize(self):
251
 
        return self.get_button("Maximize")
252
 
 
253
 
    @property
254
 
    def geometry(self):
255
 
        """Returns a tuple of (x,y,w,h) for the current panel."""
256
 
        return (self.x, self.y, self.width, self.height)
257
 
 
258
 
 
259
 
class WindowButton(UnityIntrospectionObject):
260
 
    """The Window WindowButton class."""
261
 
 
262
 
    def __init__(self, *args, **kwargs):
263
 
        super(WindowButton, self).__init__(*args, **kwargs)
264
 
        self._mouse = Mouse()
265
 
 
266
 
    def mouse_move_to(self):
267
 
        target_x = self.x + self.width / 2
268
 
        target_y = self.y + self.height / 2
269
 
        self._mouse.move(target_x, target_y, rate=20, time_between_events=0.005)
270
 
 
271
 
    def mouse_click(self):
272
 
        self.mouse_move_to()
273
 
        sleep(.2)
274
 
        self._mouse.click(press_duration=.1)
275
 
        sleep(.01)
276
 
 
277
 
    @property
278
 
    def geometry(self):
279
 
        """Returns a tuple of (x,y,w,h) for the window button."""
280
 
        return (self.x, self.y, self.width, self.height)
281
 
 
282
 
 
283
 
class GrabArea(UnityIntrospectionObject):
284
 
    """The grab area class"""
285
 
 
286
 
    @property
287
 
    def geometry(self):
288
 
        """Returns a tuple of (x,y,w,h) for the grab area."""
289
 
        return (self.x, self.y, self.width, self.height)
290
 
 
291
 
 
292
 
class Indicators(UnityIntrospectionObject):
293
 
    """The Indicators View class."""
294
 
 
295
 
    def get_ordered_entries(self, visible_only=True):
296
 
        """Return a list of indicators, ordered by their priority"""
297
 
 
298
 
        if visible_only:
299
 
            entries = self.get_children_by_type(IndicatorEntry, visible=True)
300
 
        else:
301
 
            entries = self.get_children_by_type(IndicatorEntry)
302
 
 
303
 
        return sorted(entries, key=lambda entry: entry.priority)
304
 
 
305
 
    def get_indicator_by_name_hint(self, name_hint):
306
 
        """Return the IndicatorEntry with the name_hint"""
307
 
        indicators = self.get_children_by_type(IndicatorEntry, name_hint=name_hint)
308
 
        assert(len(indicators) == 1)
309
 
        return indicators[0]
310
 
 
311
 
    @property
312
 
    def geometry(self):
313
 
        """Returns a tuple of (x,y,w,h) for the indicators area."""
314
 
        return (self.x, self.y, self.width, self.height)
315
 
 
316
 
 
317
 
class IndicatorEntry(UnityIntrospectionObject):
318
 
    """The IndicatorEntry View class."""
319
 
 
320
 
    def __init__(self, *args, **kwargs):
321
 
        super(IndicatorEntry, self).__init__(*args, **kwargs)
322
 
        self._mouse = Mouse()
323
 
 
324
 
    def mouse_move_to(self):
325
 
        target_x = self.x + self.width / 2
326
 
        target_y = self.y + self.height / 2
327
 
        self._mouse.move(target_x, target_y, rate=20, time_between_events=0.005)
328
 
 
329
 
    def mouse_click(self, button=1):
330
 
        self.mouse_move_to()
331
 
        sleep(.2)
332
 
        assert(self.visible)
333
 
        self._mouse.click(press_duration=.1)
334
 
        sleep(.01)
335
 
 
336
 
    @property
337
 
    def geometry(self):
338
 
        """Returns a tuple of (x,y,w,h) for the indicator entry."""
339
 
        return (self.x, self.y, self.width, self.height)
340
 
 
341
 
    @property
342
 
    def menu_geometry(self):
343
 
        """Returns a tuple of (x,y,w,h) for the opened menu geometry."""
344
 
        return (self.menu_x, self.menu_y, self.menu_width, self.menu_height)
345
 
 
346
 
    def __repr__(self):
347
 
        with self.no_automatic_refreshing():
348
 
            return "<IndicatorEntry 0x%x (%s)>" % (id(self), self.label)
349
 
 
350
 
 
351
 
class Tray(UnityIntrospectionObject):
352
 
    """A panel tray object."""