~smspillaz/unity/untiy.less-paint-insanity

« back to all changes in this revision

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

  • Committer: Daniel van Vugt
  • Date: 2012-03-14 06:24:18 UTC
  • mfrom: (2108 unity)
  • mto: This revision was merged to the branch mainline in revision 2146.
  • Revision ID: daniel.van.vugt@canonical.com-20120314062418-nprucpbr0m7qky5e
MergedĀ latestĀ lp:unity

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: Thomi Richards
 
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
import logging
 
11
from time import sleep
 
12
 
 
13
from autopilot.keybindings import KeybindingsHelper
 
14
from autopilot.emulators.unity import get_state_by_path, make_introspection_object
 
15
from autopilot.emulators.X11 import Keyboard, Mouse
 
16
 
 
17
# even though we don't use these directly, we need to make sure they've been
 
18
# imported so the classes contained are registered with the introspection API.
 
19
from autopilot.emulators.unity.icons import *
 
20
 
 
21
logger = logging.getLogger(__name__)
 
22
 
 
23
 
 
24
# TODO: THis class needs to be ported to the new-style emulator classes.
 
25
# See launcher.py or dash.py for reference.
 
26
class Switcher(KeybindingsHelper):
 
27
    """Interact with the Unity switcher."""
 
28
 
 
29
    def __init__(self):
 
30
        super(Switcher, self).__init__()
 
31
        self._keyboard = Keyboard()
 
32
        self._mouse = Mouse()
 
33
 
 
34
    def initiate(self):
 
35
        """Start the switcher with alt+tab."""
 
36
        logger.debug("Initiating switcher with Alt+Tab")
 
37
        self.keybinding_hold("switcher/reveal_normal")
 
38
        self.keybinding_tap("switcher/reveal_normal")
 
39
        sleep(1)
 
40
 
 
41
    def initiate_detail_mode(self):
 
42
        """Start detail mode with alt+`"""
 
43
        logger.debug("Initiating switcher detail mode with Alt+`")
 
44
        self.keybinding_hold("switcher/reveal_details")
 
45
        self.keybinding_tap("switcher/reveal_details")
 
46
        sleep(1)
 
47
 
 
48
    def initiate_all_mode(self):
 
49
        """Start switcher in 'all workspaces' mode.
 
50
 
 
51
        Shows apps from all workspaces, instead of just the current workspace.
 
52
        """
 
53
        logger.debug("Initiating switcher in 'all workspaces' mode.")
 
54
        self.keybinding_hold("switcher/reveal_all")
 
55
        self.keybinding_tap("switcher/reveal_all")
 
56
        sleep(1)
 
57
 
 
58
    def initiate_right_arrow(self):
 
59
        """Impropperly attempt to start switcher."""
 
60
        logger.debug("Initiating switcher with Alt+Right (should not work)")
 
61
        self.keybinding_hold("switcher/reveal_impropper")
 
62
        self.keybinding_tap("switcher/right")
 
63
        sleep(1)
 
64
 
 
65
    def terminate(self):
 
66
        """Stop switcher without activating the selected icon."""
 
67
        logger.debug("Terminating switcher.")
 
68
        self.keybinding("switcher/cancel")
 
69
        self.keybinding_release("switcher/reveal_normal")
 
70
 
 
71
    def cancel(self):
 
72
        """Stop switcher without activating the selected icon and releasing the keys."""
 
73
        logger.debug("Cancelling switcher.")
 
74
        self.keybinding("switcher/cancel")
 
75
 
 
76
    def stop(self):
 
77
        """Stop switcher and activate the selected icon."""
 
78
        logger.debug("Stopping switcher")
 
79
        self.keybinding_release("switcher/reveal_normal")
 
80
 
 
81
    def next_icon(self):
 
82
        """Move to the next icon."""
 
83
        logger.debug("Selecting next item in switcher.")
 
84
        self.keybinding("switcher/next")
 
85
 
 
86
    def previous_icon(self):
 
87
        """Move to the previous icon."""
 
88
        logger.debug("Selecting previous item in switcher.")
 
89
        self.keybinding("switcher/prev")
 
90
 
 
91
    def next_icon_mouse(self):
 
92
        """Move to the next icon using the mouse scroll wheel"""
 
93
        logger.debug("Selecting next item in switcher with mouse scroll wheel.")
 
94
        self._mouse.press(6)
 
95
        self._mouse.release(6)
 
96
 
 
97
    def previous_icon_mouse(self):
 
98
        """Move to the previous icon using the mouse scroll wheel"""
 
99
        logger.debug("Selecting previous item in switcher with mouse scroll wheel.")
 
100
        self._mouse.press(7)
 
101
        self._mouse.release(7)
 
102
 
 
103
    def show_details(self):
 
104
        """Show detail mode."""
 
105
        logger.debug("Showing details view.")
 
106
        self.keybinding("switcher/detail_start")
 
107
 
 
108
    def hide_details(self):
 
109
        """Hide detail mode."""
 
110
        logger.debug("Hiding details view.")
 
111
        self.keybinding("switcher/detail_stop")
 
112
 
 
113
    def next_detail(self):
 
114
        """Move to next detail in the switcher."""
 
115
        logger.debug("Selecting next item in details mode.")
 
116
        self.keybinding("switcher/detail_next")
 
117
 
 
118
    def previous_detail(self):
 
119
        """Move to the previous detail in the switcher."""
 
120
        logger.debug("Selecting previous item in details mode.")
 
121
        self.keybinding("switcher/detail_prev")
 
122
 
 
123
    def __get_icon(self, index):
 
124
        return self.__get_model()['Children'][index][1][0]
 
125
 
 
126
    @property
 
127
    def current_icon(self):
 
128
        """Get the currently-selected icon."""
 
129
        if not self.get_is_visible:
 
130
            return None
 
131
        model = self.__get_model()
 
132
        sel_idx = self.get_selection_index()
 
133
        try:
 
134
            return make_introspection_object(model['Children'][sel_idx])
 
135
        except KeyError:
 
136
            return None
 
137
 
 
138
    def get_icon_name(self, index):
 
139
        return self.__get_icon(index)['tooltip-text']
 
140
 
 
141
    def get_icon_desktop_file(self, index):
 
142
        try:
 
143
            return self.__get_icon(index)['desktop-file']
 
144
        except:
 
145
            return None
 
146
 
 
147
    def get_model_size(self):
 
148
        return len(self.__get_model()['Children'])
 
149
 
 
150
    def get_selection_index(self):
 
151
        return int(self.__get_model()['selection-index'])
 
152
 
 
153
    def get_last_selection_index(self):
 
154
        return bool(self.__get_model()['last-selection-index'])
 
155
 
 
156
    def get_is_visible(self):
 
157
        return bool(self.__get_controller()['visible'])
 
158
 
 
159
    def get_is_in_details_mode(self):
 
160
        """Return True if the SwitcherView is in details mode."""
 
161
        return bool(self.__get_model()['detail-selection'])
 
162
 
 
163
    def get_switcher_icons(self):
 
164
        """Get all icons in the switcher model.
 
165
 
 
166
        The switcher needs to be initiated in order to get the model.
 
167
 
 
168
        """
 
169
        icons = []
 
170
        model = get_state_by_path('//SwitcherModel')[0]
 
171
        for child in model['Children']:
 
172
            icon = make_introspection_object(child)
 
173
            if icon:
 
174
                icons.append(icon)
 
175
        return icons
 
176
 
 
177
    def __get_model(self):
 
178
        return get_state_by_path('/Unity/SwitcherController/SwitcherModel')[0]
 
179
 
 
180
    def __get_controller(self):
 
181
        return get_state_by_path('/Unity/SwitcherController')[0]