/usr/lib/python2.7/dist-packages/unity/emulators/launcher.py is in unity-autopilot 7.5.0+18.04.20180413-0ubuntu1.
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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
# Copyright 2012 Canonical
# Author: Thomi Richards
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
from __future__ import absolute_import
from autopilot.input import Mouse
from autopilot.display import Display, move_mouse_to_screen
from autopilot.keybindings import KeybindingsHelper
import logging
from testtools.matchers import NotEquals
from time import sleep
from unity.emulators import UnityIntrospectionObject
from unity.emulators import keys
from unity.emulators.icons import (
ApplicationLauncherIcon,
BFBLauncherIcon,
ExpoLauncherIcon,
SimpleLauncherIcon,
TrashLauncherIcon,
)
from unity.emulators.compiz import get_compiz_option
logger = logging.getLogger(__name__)
class IconDragType:
"""Define possible positions to drag an icon onto another"""
INSIDE = 0
OUTSIDE = 1
BEFORE = 3
AFTER = 4
class LauncherPosition:
"""Define launcher possible positions"""
LEFT = "Left"
BOTTOM = "Bottom"
class LauncherController(UnityIntrospectionObject):
"""The LauncherController class."""
def get_launcher_for_monitor(self, monitor_num):
"""Return an instance of Launcher for the specified monitor, or None."""
launchers = self.get_children_by_type(Launcher, monitor=monitor_num)
return launchers[0] if launchers else None
def get_launchers(self):
"""Return the available launchers, or None."""
return self.get_children_by_type(Launcher)
@property
def model(self):
"""Return the launcher model."""
models = self.get_children_by_type(LauncherModel)
assert(len(models) == 1)
return models[0]
class Launcher(UnityIntrospectionObject, KeybindingsHelper):
"""An individual launcher for a monitor."""
def __init__(self, *args, **kwargs):
super(Launcher, self).__init__(*args, **kwargs)
self.show_timeout = 1
self.hide_timeout = 1
self.in_keynav_mode = False
self.in_switcher_mode = False
self._mouse = Mouse.create()
self._display = Display.create()
def _perform_key_nav_binding(self, keybinding):
if not self.in_keynav_mode:
raise RuntimeError("Cannot perform key navigation when not in kaynav mode.")
self.keybinding(keybinding)
def _perform_key_nav_exit_binding(self, keybinding):
self._perform_key_nav_binding(keybinding)
self.in_keynav_mode = False
def _perform_switcher_binding(self, keybinding):
if not self.in_switcher_mode:
raise RuntimeError("Cannot interact with launcher switcher when not in switcher mode.")
self.keybinding(keybinding)
def _perform_switcher_exit_binding(self, keybinding):
# If we're doing a normal activation, all we need to do is release the
# keybinding. Otherwise, perform the keybinding specified *then* release
# the switcher keybinding.
if keybinding != "launcher/switcher":
self._perform_switcher_binding(keybinding)
self.keybinding_release("launcher/switcher")
self.in_switcher_mode = False
def _get_controller(self):
"""Get the launcher controller."""
controller = self.get_root_instance().select_single(LauncherController)
return controller
def move_mouse_to_screen_of_current_launcher(self):
"""Places the mouse on the screen of this launcher."""
move_mouse_to_screen(self.monitor)
def move_mouse_beside_launcher(self):
"""Places the mouse to the right of this launcher."""
move_mouse_to_screen(self.monitor)
(x, y, w, h) = self.geometry
if h > w:
target_x = x + w + 10
target_y = y + h / 2
else:
target_x = x + w / 2
target_y = y - 10
logger.debug("Moving mouse away from launcher.")
self._mouse.move(target_x, target_y, False)
sleep(self.show_timeout)
def move_mouse_over_launcher(self):
"""Move the mouse over this launcher."""
move_mouse_to_screen(self.monitor)
logger.debug("Moving mouse to center of launcher.")
self._mouse.move_to_object(self)
def move_mouse_to_icon(self, icon, autoscroll_offset=0):
"""Move the mouse to a specific icon."""
(x, y, w, h) = self.geometry
found = False
# Only try 10 times (5 secs.) before giving up.
for i in xrange(0, 10):
mouse_x = target_x = icon.center.x
mouse_y = target_y = icon.center.y
if target_y > h + y:
mouse_y = h + y - autoscroll_offset
elif target_y < 0:
mouse_y = y + autoscroll_offset
if self._mouse.x == target_x and self._mouse.y == target_y:
found = True
break
self._mouse.move(mouse_x, mouse_y)
sleep(0.5)
if not found:
raise RuntimeError("Could not move mouse to the icon")
def mouse_reveal_launcher(self):
"""Reveal this launcher with the mouse.
If the launcher is already visible calling this method does nothing.
"""
if self.is_showing:
return
move_mouse_to_screen(self.monitor)
(x, y, w, h) = self.geometry
if h > w:
target_x = x - 300 # this is the pressure we need to reveal the launcher.
target_y = y + h / 2
else:
target_x = x + w / 2
target_y = y + h + 300
logger.debug("Revealing launcher on monitor %d with mouse.", self.monitor)
self._mouse.move(target_x, target_y, True, 5, .002)
# Move the mouse back to the launcher for multi-monitor
self._mouse.move(x, target_y, True, 5, .002)
def keyboard_reveal_launcher(self):
"""Reveal this launcher using the keyboard."""
move_mouse_to_screen(self.monitor)
logger.debug("Revealing launcher with keyboard.")
self.keybinding_hold("launcher/reveal")
self.is_showing.wait_for(True)
def keyboard_unreveal_launcher(self):
"""Un-reveal this launcher using the keyboard."""
move_mouse_to_screen(self.monitor)
logger.debug("Un-revealing launcher with keyboard.")
self.keybinding_release("launcher/reveal")
# only wait if the launcher is set to autohide
if self.hidemode == 1:
self.is_showing.wait_for(False)
def keyboard_select_icon(self, launcher_position = LauncherPosition.LEFT, **kwargs):
"""Using either keynav mode or the switcher, select an icon in the launcher.
The desired mode (keynav or switcher) must be started already before
calling this methods or a RuntimeError will be raised.
This method won't activate the icon, it will only select it.
Icons are selected by passing keyword argument filters to this method.
For example:
>>> launcher.keyboard_select_icon(tooltip_text="Calculator")
...will select the *first* icon that has a 'tooltip_text' attribute equal
to 'Calculator'. If an icon is missing the attribute, it is treated as
not matching.
If no icon is found, this method will raise a ValueError.
"""
if not self.in_keynav_mode and not self.in_switcher_mode:
raise RuntimeError("Launcher must be in keynav or switcher mode")
launcher_model = self.get_root_instance().select_single(LauncherModel)
all_icons = launcher_model.get_launcher_icons()
logger.debug("all_icons = %r", [i.tooltip_text for i in all_icons])
for icon in all_icons:
# can't iterate over the model icons directly since some are hidden
# from the user.
if not icon.visible:
continue
logger.debug("Selected icon = %s", icon.tooltip_text)
matches = True
for arg,val in kwargs.iteritems():
if not hasattr(icon, arg) or getattr(icon, arg, None) != val:
matches = False
break
if matches:
return
if self.in_keynav_mode:
self.key_nav_next(launcher_position)
elif self.in_switcher_mode:
self.switcher_next()
raise ValueError("No icon found that matches: %r", kwargs)
def key_nav_start(self):
"""Start keyboard navigation mode by pressing Alt+F1."""
move_mouse_to_screen(self.monitor)
logger.debug("Initiating launcher keyboard navigation with Alt+F1.")
self.keybinding("launcher/keynav")
self._get_controller().key_nav_is_active.wait_for(True)
self.in_keynav_mode = True
def key_nav_cancel(self):
"""End the key navigation."""
logger.debug("Cancelling keyboard navigation mode.")
self._perform_key_nav_exit_binding("launcher/keynav/exit")
self._get_controller().key_nav_is_active.wait_for(False)
def key_nav_activate(self):
"""Activates the selected launcher icon. In the current implementation
this also exits key navigation"""
logger.debug("Ending keyboard navigation mode, activating icon.")
self._perform_key_nav_exit_binding("launcher/keynav/activate")
self._get_controller().key_nav_is_active.wait_for(False)
def key_nav_next(self, launcher_position = LauncherPosition.LEFT):
"""Moves the launcher keynav focus to the next launcher icon"""
logger.debug("Selecting next item in keyboard navigation mode.")
old_selection = self._get_controller().key_nav_selection
self._perform_key_nav_binding(keys[launcher_position + "/launcher/keynav/next"])
self._get_controller().key_nav_selection.wait_for(NotEquals(old_selection))
def key_nav_prev(self, launcher_position = LauncherPosition.LEFT):
"""Moves the launcher keynav focus to the previous launcher icon"""
logger.debug("Selecting previous item in keyboard navigation mode.")
old_selection = self._get_controller().key_nav_selection
self._perform_key_nav_binding(keys[launcher_position + "/launcher/keynav/prev"])
self._get_controller().key_nav_selection.wait_for(NotEquals(old_selection))
def key_nav_enter_quicklist(self, launcher_position = LauncherPosition.LEFT):
logger.debug("Opening quicklist for currently selected icon.")
self._perform_key_nav_binding(keys[launcher_position + "/launcher/keynav/open-quicklist"])
self.quicklist_open.wait_for(True)
def key_nav_exit_quicklist(self):
logger.debug("Closing quicklist for currently selected icon.")
self._perform_key_nav_binding("launcher/keynav/close-quicklist")
self.quicklist_open.wait_for(False)
def switcher_start(self):
"""Start the super+Tab switcher on this launcher."""
move_mouse_to_screen(self.monitor)
logger.debug("Starting Super+Tab switcher.")
self.keybinding_hold_part_then_tap("launcher/switcher")
self._get_controller().key_nav_is_active.wait_for(True)
self.in_switcher_mode = True
def switcher_cancel(self):
"""End the super+tab swithcer."""
logger.debug("Cancelling keyboard navigation mode.")
self._perform_switcher_exit_binding("launcher/switcher/exit")
self._get_controller().key_nav_is_active.wait_for(False)
def switcher_activate(self):
"""Activates the selected launcher icon. In the current implementation
this also exits the switcher"""
logger.debug("Ending keyboard navigation mode.")
self._perform_switcher_exit_binding("launcher/switcher")
self._get_controller().key_nav_is_active.wait_for(False)
def switcher_next(self):
logger.debug("Selecting next item in keyboard navigation mode.")
old_selection = self._get_controller().key_nav_selection
self._perform_switcher_binding("launcher/switcher/next")
self._get_controller().key_nav_selection.wait_for(NotEquals(old_selection))
def switcher_prev(self):
logger.debug("Selecting previous item in keyboard navigation mode.")
old_selection = self._get_controller().key_nav_selection
self._perform_switcher_binding("launcher/switcher/prev")
self._get_controller().key_nav_selection.wait_for(NotEquals(old_selection))
def switcher_up(self):
logger.debug("Selecting next item in keyboard navigation mode.")
old_selection = self._get_controller().key_nav_selection
self._perform_switcher_binding("launcher/switcher/up")
self._get_controller().key_nav_selection.wait_for(NotEquals(old_selection))
def switcher_down(self):
logger.debug("Selecting previous item in keyboard navigation mode.")
old_selection = self._get_controller().key_nav_selection
self._perform_switcher_binding("launcher/switcher/down")
self._get_controller().key_nav_selection.wait_for(NotEquals(old_selection))
def click_launcher_icon(self, icon, button=1, move_mouse_after=True):
"""Move the mouse over the launcher icon, and click it.
`icon` must be an instance of SimpleLauncherIcon or it's descendants.
`move_mouse_after` moves the mouse outside the launcher if true.
"""
if not isinstance(icon, SimpleLauncherIcon):
raise TypeError("icon must be a LauncherIcon, not %s" % type(icon))
logger.debug("Clicking launcher icon %r on monitor %d with mouse button %d",
icon, self.monitor, button)
self.mouse_reveal_launcher()
self.move_mouse_to_icon(icon)
self._mouse.click(button)
if (move_mouse_after):
self.move_mouse_beside_launcher()
def drag_icon_to_position(self, icon, pos, target, drag_type=IconDragType.INSIDE, launcher_position=LauncherPosition.LEFT):
"""Drag a launcher icon to a new position.
'icon' is the icon to move. It must be either a ApplicationLauncherIcon or an
ExpoLauncherIcon instance. Other values will result in a TypeError being
raised.
'pos' must be one of IconDragType.BEFORE or IconDragType.AFTER. If it is
not one of these, a ValueError will be raised.
'target' is the target icon.
'drag_type' must be one of IconDragType.INSIDE or IconDragType.OUTSIDE.
This specifies whether the icon is gragged inside the launcher, or to the
right/top of it. The default is to drag inside the launcher. If it is
specified, and not one of the allowed values, a ValueError will be raised.
'launcher_position' must be one of LauncherPosition.LEFT or LauncherPosition.BOTTOM.
This specifies the launcher position when dragging the icon. The default launcher
position is at left. If it is specified, and not one of the allowed values, a
ValueError will be raised.
For example:
>>> drag_icon_to_position(calc_icon, IconDragType.BEFORE, switcher_icon)
This will drag the calculator icon to just before the switcher icon.
Note: This method makes no attempt to sanity-check the requested move.
For example, it will happily try and move an icon to before the BFB icon,
if asked.
"""
if not isinstance(icon, ApplicationLauncherIcon) \
and not isinstance(icon, ExpoLauncherIcon):
raise TypeError("Icon to move must be a ApplicationLauncherIcon or ExpoLauncherIcon, not %s"
% type(icon).__name__)
if pos not in (IconDragType.BEFORE, IconDragType.AFTER):
raise ValueError("'pos' parameter must be one of IconDragType.BEFORE, IconDragType.AFTER")
if not isinstance(target, SimpleLauncherIcon):
raise TypeError("'target' must be a valid launcher icon, not %s"
% type(target).__name__)
if drag_type not in (IconDragType.INSIDE, IconDragType.OUTSIDE):
raise ValueError("'drag_type' parameter must be one of IconDragType.INSIDE, IconDragType.OUTSIDE")
icon_size = get_compiz_option("unityshell", "icon_size")
self.move_mouse_to_icon(icon)
self._mouse.press()
sleep(1)
if drag_type == IconDragType.OUTSIDE:
if launcher_position == LauncherPosition.LEFT:
shift_over = self._mouse.x + (icon_size * 3)
self._mouse.move(shift_over, self._mouse.y, rate=20, time_between_events=0.005)
else:
shift_over = self._mouse.y - (icon_size * 3)
self._mouse.move(self._mouse.x, shift_over, rate=20, time_between_events=0.005)
sleep(0.5)
self.move_mouse_to_icon(target)
if launcher_position == LauncherPosition.LEFT:
target_y = target.center.y
if target_y < icon.center.y:
target_y += icon_size
if pos == IconDragType.BEFORE:
target_y -= icon_size + (icon_size / 2)
self._mouse.move(self._mouse.x, target_y, rate=20, time_between_events=0.005)
else:
target_x = target.center.x
if target_x < icon.center.x:
target_x += icon_size
if pos == IconDragType.BEFORE:
target_x -= icon_size + (icon_size / 2)
self._mouse.move(target_x, self._mouse.y, rate=20, time_between_events=0.005)
sleep(1)
self._mouse.release()
self.move_mouse_beside_launcher()
def lock_to_launcher(self, icon):
"""lock 'icon' to the launcher, if it's not already.
`icon` must be an instance of ApplicationLauncherIcon.
"""
if not isinstance(icon, ApplicationLauncherIcon):
raise TypeError("Can only lock instances of ApplicationLauncherIcon")
if icon.sticky:
# Nothing to do.
return
logger.debug("Locking icon %r to launcher.", icon)
self.click_launcher_icon(icon, button=3)
quicklist = icon.get_quicklist()
pin_item = quicklist.get_quicklist_item_by_text('Lock to Launcher')
quicklist.click_item(pin_item)
def unlock_from_launcher(self, icon):
"""lock 'icon' to the launcher, if it's not already.
`icon` must be an instance of ApplicationLauncherIcon.
"""
if not isinstance(icon, ApplicationLauncherIcon):
raise TypeError("Can only unlock instances of ApplicationLauncherIcon, not %s" % type(icon).__name__)
if not icon.sticky:
# nothing to do.
return
logger.debug("Unlocking icon %r from launcher.")
self.click_launcher_icon(icon, button=3)
quicklist = icon.get_quicklist()
pin_item = quicklist.get_quicklist_item_by_text('Unlock from Launcher')
quicklist.click_item(pin_item)
@property
def geometry(self):
"""Returns a Rectangle (x,y,w,h) for the current launcher."""
return self.globalRect
class LauncherModel(UnityIntrospectionObject):
"""The launcher model. Contains all launcher icons as children."""
def get_bfb_icon(self):
icons = self.get_root_instance().select_many(BFBLauncherIcon)
assert(len(icons) == 1)
return icons[0]
def get_expo_icon(self):
icons = self.get_children_by_type(ExpoLauncherIcon)
assert(len(icons) == 1)
return icons[0]
def get_trash_icon(self):
icons = self.get_children_by_type(TrashLauncherIcon)
assert(len(icons) == 1)
return icons[0]
def get_launcher_icons(self, visible_only=True):
"""Get a list of launcher icons in this launcher."""
if visible_only:
icons = self.get_children_by_type(SimpleLauncherIcon, visible=True)
else:
icons = self.get_children_by_type(SimpleLauncherIcon)
return sorted(icons, key=lambda icon: icon.order)
def get_bamf_launcher_icons(self, visible_only=True):
"""Get a list of bamf launcher icons in this launcher."""
if visible_only:
return self.get_children_by_type(ApplicationLauncherIcon, visible=True)
else:
return self.get_children_by_type(ApplicationLauncherIcon)
def get_launcher_icons_for_monitor(self, monitor, visible_only=True):
"""Get a list of launcher icons for provided monitor."""
icons = []
for icon in self.get_launcher_icons(visible_only):
if icon.is_on_monitor(monitor):
icons.append(icon)
return icons
def get_icon(self, **kwargs):
"""Get a launcher icon from the model according to some filters.
This method accepts keyword argument that are the filters to use when
looking for an icon. For example, to find an icon with a particular
desktop_id, one might do this from within a test:
>>> self.launcher.model.get_icon(desktop_id="gcalctool.desktop")
This method returns only one icon. It is the callers responsibility to
ensure that the filter matches only one icon.
This method will attempt to get the launcher icon, and will retry several
times, so the caller can be assured that if this method doesn't find
the icon it really does not exist.
If no keyword arguments are specified, ValueError will be raised.
If no icons are matched, None is returned.
"""
if not kwargs:
raise ValueError("You must specify at least one keyword argument to ths method.")
for i in range(10):
icons = self.get_children_by_type(SimpleLauncherIcon, **kwargs)
if len(icons) > 1:
logger.warning("Got more than one icon returned using filters=%r. Returning first one", kwargs)
if icons:
return icons[0]
sleep(1)
return None
def num_launcher_icons(self):
"""Get the number of icons in the launcher model."""
return len(self.get_launcher_icons())
def num_bamf_launcher_icons(self, visible_only=True):
"""Get the number of bamf icons in the launcher model."""
return len(self.get_bamf_launcher_icons(visible_only))
|