/usr/lib/python2.7/dist-packages/autopilot/input/__init__.py is in python-autopilot 1.4.1+15.10.20150911-0ubuntu2.
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 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2012-2013 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Autopilot unified input system.
===============================
This package provides input methods for various platforms. Autopilot aims to
provide an appropriate implementation for the currently running system. For
example, not all systems have an X11 stack running: on those systems, autopilot
will instantiate input classes class that use something other than X11 to
generate events (possibly UInput).
Test authors should instantiate the appropriate class using the ``create``
method on each class. Calling ``create()`` with no arguments will get an
instance of the specified class that suits the current platform. In this case,
autopilot will do it's best to pick a suitable backend. Calling ``create``
with a backend name will result in that specific backend type being returned,
or, if it cannot be created, an exception will be raised. For more information
on creating backends, see :ref:`tut-picking-backends`
There are three basic input types available:
* :class:`Keyboard` - traditional keyboard devices.
* :class:`Mouse` - traditional mouse devices (Currently only avaialble on the
desktop).
* :class:`Touch` - single point-of-contact touch device.
The :class:`Pointer` class is a wrapper that unifies the API of the
:class:`Mouse` and :class:`Touch` classes, which can be helpful if you want to
write a test that can use either a mouse of a touch device. A common pattern is
to use a Touch device when running on a mobile device, and a Mouse device when
running on a desktop.
.. seealso::
Module :mod:`autopilot.gestures`
Multitouch and gesture support for touch devices.
"""
from collections import OrderedDict
from contextlib import contextmanager
from autopilot.utilities import _pick_backend, CleanupRegistered
from autopilot.input._common import get_center_point
import logging
_logger = logging.getLogger(__name__)
class Keyboard(CleanupRegistered):
"""A simple keyboard device class.
The keyboard class is used to generate key events while in an autopilot
test. This class should not be instantiated directly. To get an
instance of the keyboard class, call :py:meth:`create` instead.
"""
@staticmethod
def create(preferred_backend=''):
"""Get an instance of the :py:class:`Keyboard` class.
For more infomration on picking specific backends, see
:ref:`tut-picking-backends`
For details regarding backend limitations please see:
:ref:`Keyboard backend limitations<keyboard_backend_limitations>`
.. warning:: The **OSK** (On Screen Keyboard) backend option does not
implement either :py:meth:`press` or :py:meth:`release` methods due to
technical implementation details and will raise a NotImplementedError
exception if used.
:param preferred_backend: A string containing a hint as to which
backend you would like. Possible backends are:
* ``X11`` - Generate keyboard events using the X11 client
libraries.
* ``UInput`` - Use UInput kernel-level device driver.
* ``OSK`` - Use the graphical On Screen Keyboard as a backend.
:raises: RuntimeError if autopilot cannot instantate any of the
possible backends.
:raises: RuntimeError if the preferred_backend is specified and is not
one of the possible backends for this device class.
:raises: :class:`~autopilot.BackendException` if the preferred_backend
is set, but that backend could not be instantiated.
"""
def get_x11_kb():
from autopilot.input._X11 import Keyboard
return Keyboard()
def get_uinput_kb():
from autopilot.input._uinput import Keyboard
return Keyboard()
def get_osk_kb():
try:
from autopilot.input._osk import Keyboard
return Keyboard()
except ImportError as e:
e.args += ("Unable to import the OSK backend",)
raise
backends = OrderedDict()
backends['X11'] = get_x11_kb
backends['UInput'] = get_uinput_kb
backends['OSK'] = get_osk_kb
return _pick_backend(backends, preferred_backend)
@contextmanager
def focused_type(self, input_target, pointer=None):
"""Type into an input widget.
This context manager takes care of making sure a particular
*input_target* UI control is selected before any text is entered.
Some backends extend this method to perform cleanup actions at the end
of the context manager block. For example, the OSK backend dismisses
the keyboard.
If the *pointer* argument is None (default) then either a Mouse or
Touch pointer will be created based on the current platform.
An example of using the context manager (with an OSK backend)::
from autopilot.input import Keyboard
text_area = self._launch_test_input_area()
keyboard = Keyboard.create('OSK')
with keyboard.focused_type(text_area) as kb:
kb.type("Hello World.")
self.assertThat(text_area.text, Equals("Hello World"))
# Upon leaving the context managers scope the keyboard is dismissed
# with a swipe
"""
if pointer is None:
from autopilot.platform import model
if model() == 'Desktop':
pointer = Pointer(Mouse.create())
else:
pointer = Pointer(Touch.create())
pointer.click_object(input_target)
yield self
def press(self, keys, delay=0.2):
"""Send key press events only.
:param keys: Keys you want pressed.
:param delay: The delay (in Seconds) after pressing the keys before
returning control to the caller.
:raises: NotImplementedError If called when using the OSK Backend.
.. warning:: The **OSK** backend does not implement the press method
and will raise a NotImplementedError if called.
Example::
press('Alt+F2')
presses the 'Alt' and 'F2' keys, but does not release them.
"""
raise NotImplementedError("You cannot use this class directly.")
def release(self, keys, delay=0.2):
"""Send key release events only.
:param keys: Keys you want released.
:param delay: The delay (in Seconds) after releasing the keys before
returning control to the caller.
:raises: NotImplementedError If called when using the OSK Backend.
.. warning:: The **OSK** backend does not implement the press method
and will raise a NotImplementedError if called.
Example::
release('Alt+F2')
releases the 'Alt' and 'F2' keys.
"""
raise NotImplementedError("You cannot use this class directly.")
def press_and_release(self, keys, delay=0.2):
"""Press and release all items in 'keys'.
This is the same as calling 'press(keys);release(keys)'.
:param keys: Keys you want pressed and released.
:param delay: The delay (in Seconds) after pressing and releasing each
key.
Example::
press_and_release('Alt+F2')
presses both the 'Alt' and 'F2' keys, and then releases both keys.
"""
raise NotImplementedError("You cannot use this class directly.")
def type(self, string, delay=0.1):
"""Simulate a user typing a string of text.
:param string: The string to text to type.
:param delay: The delay (in Seconds) after pressing and releasing each
key. Note that the default value here is shorter than for the
press, release and press_and_release methods.
.. note:: Only 'normal' keys can be typed with this method. Control
characters (such as 'Alt' will be interpreted as an 'A', and 'l',
and a 't').
"""
raise NotImplementedError("You cannot use this class directly.")
class Mouse(CleanupRegistered):
"""A simple mouse device class.
The mouse class is used to generate mouse events while in an autopilot
test. This class should not be instantiated directly however. To get an
instance of the mouse class, call :py:meth:`create` instead.
For example, to create a mouse object and click at (100,50)::
mouse = Mouse.create()
mouse.move(100, 50)
mouse.click()
"""
@staticmethod
def create(preferred_backend=''):
"""Get an instance of the :py:class:`Mouse` class.
For more infomration on picking specific backends, see
:ref:`tut-picking-backends`
:param preferred_backend: A string containing a hint as to which
backend you would like. Possible backends are:
* ``X11`` - Generate mouse events using the X11 client libraries.
:raises: RuntimeError if autopilot cannot instantate any of the
possible backends.
:raises: RuntimeError if the preferred_backend is specified and is not
one of the possible backends for this device class.
:raises: :class:`~autopilot.BackendException` if the preferred_backend
is set, but that backend could not be instantiated.
"""
def get_x11_mouse():
from autopilot.input._X11 import Mouse
return Mouse()
from autopilot.platform import model
if model() != 'Desktop':
_logger.info(
"You cannot create a Mouse on the devices where X11 is not "
"available. consider using a Touch or Pointer device. "
"For more information, see: "
"http://unity.ubuntu.com/autopilot/api/input.html"
"#autopilot-unified-input-system"
)
raise RuntimeError(
"Cannot create a Mouse on devices where X11 is not available."
)
backends = OrderedDict()
backends['X11'] = get_x11_mouse
return _pick_backend(backends, preferred_backend)
@property
def x(self):
"""Mouse position X coordinate."""
raise NotImplementedError("You cannot use this class directly.")
@property
def y(self):
"""Mouse position Y coordinate."""
raise NotImplementedError("You cannot use this class directly.")
def press(self, button=1):
"""Press mouse button at current mouse location."""
raise NotImplementedError("You cannot use this class directly.")
def release(self, button=1):
"""Releases mouse button at current mouse location."""
raise NotImplementedError("You cannot use this class directly.")
def click(self, button=1, press_duration=0.10):
"""Click mouse at current location."""
raise NotImplementedError("You cannot use this class directly.")
def click_object(self, object_proxy, button=1, press_duration=0.10):
"""Click the center point of a given object.
It does this by looking for several attributes, in order. The first
attribute found will be used. The attributes used are (in order):
* globalRect (x,y,w,h)
* center_x, center_y
* x, y, w, h
:raises: **ValueError** if none of these attributes are found, or if an
attribute is of an incorrect type.
"""
self.move_to_object(object_proxy)
self.click(button, press_duration)
def move(self, x, y, animate=True, rate=10, time_between_events=0.01):
"""Moves mouse to location (x,y).
Callers should avoid specifying the *rate* or *time_between_events*
parameters unless they need a specific rate of movement.
"""
raise NotImplementedError("You cannot use this class directly.")
def move_to_object(self, object_proxy):
"""Attempts to move the mouse to 'object_proxy's centre point.
It does this by looking for several attributes, in order. The first
attribute found will be used. The attributes used are (in order):
* globalRect (x,y,w,h)
* center_x, center_y
* x, y, w, h
:raises: **ValueError** if none of these attributes are found, or if an
attribute is of an incorrect type.
"""
raise NotImplementedError("You cannot use this class directly.")
def position(self):
"""
Returns the current position of the mouse pointer.
:return: (x,y) tuple
"""
raise NotImplementedError("You cannot use this class directly.")
def drag(self, x1, y1, x2, y2, rate=10, time_between_events=0.01):
"""Perform a press, move and release.
This is to keep a common API between Mouse and Finger as long as
possible.
The pointer will be dragged from the starting point to the ending point
with multiple moves. The number of moves, and thus the time that it
will take to complete the drag can be altered with the `rate`
parameter.
:param x1: The point on the x axis where the drag will start from.
:param y1: The point on the y axis where the drag will starts from.
:param x2: The point on the x axis where the drag will end at.
:param y2: The point on the y axis where the drag will end at.
:param rate: The number of pixels the mouse will be moved per
iteration. Default is 10 pixels. A higher rate will make the drag
faster, and lower rate will make it slower.
:param time_between_events: The number of seconds that the drag will
wait between iterations.
"""
raise NotImplementedError("You cannot use this class directly.")
class Touch(object):
"""A simple touch driver class.
This class can be used for any touch events that require a single active
touch at once. If you want to do complex gestures (including multi-touch
gestures), look at the :py:mod:`autopilot.gestures` module.
"""
@staticmethod
def create(preferred_backend=''):
"""Get an instance of the :py:class:`Touch` class.
:param preferred_backend: A string containing a hint as to which
backend you would like. If left blank, autopilot will pick a
suitable backend for you. Specifying a backend will guarantee that
either that backend is returned, or an exception is raised.
possible backends are:
* ``UInput`` - Use UInput kernel-level device driver.
:raises: RuntimeError if autopilot cannot instantate any of the
possible backends.
:raises: RuntimeError if the preferred_backend is specified and is not
one of the possible backends for this device class.
:raises: :class:`~autopilot.BackendException` if the preferred_backend
is set, but that backend could not be instantiated.
"""
def get_uinput_touch():
from autopilot.input._uinput import Touch
return Touch()
backends = OrderedDict()
backends['UInput'] = get_uinput_touch
return _pick_backend(backends, preferred_backend)
@property
def pressed(self):
"""Return True if this touch is currently in use (i.e.- pressed on the
'screen').
"""
raise NotImplementedError("You cannot use this class directly.")
def tap(self, x, y):
"""Click (or 'tap') at given x,y coordinates."""
raise NotImplementedError("You cannot use this class directly.")
def tap_object(self, object):
"""Tap the center point of a given object.
It does this by looking for several attributes, in order. The first
attribute found will be used. The attributes used are (in order):
* globalRect (x,y,w,h)
* center_x, center_y
* x, y, w, h
:raises: **ValueError** if none of these attributes are found, or if an
attribute is of an incorrect type.
"""
raise NotImplementedError("You cannot use this class directly.")
def press(self, x, y):
"""Press and hold at the given x,y coordinates."""
raise NotImplementedError("You cannot use this class directly.")
def move(self, x, y):
"""Move the pointer coords to (x,y).
.. note:: The touch 'finger' must be pressed for a call to this
method to be successful. (see :py:meth:`press` for further details on
touch presses.)
:raises: **RuntimeError** if called and the touch 'finger' isn't
pressed.
"""
raise NotImplementedError("You cannot use this class directly.")
def release(self):
"""Release a previously pressed finger"""
raise NotImplementedError("You cannot use this class directly.")
def drag(self, x1, y1, x2, y2, rate=10, time_between_events=0.01):
"""Perform a drag gesture.
The finger will be dragged from the starting point to the ending point
with multiple moves. The number of moves, and thus the time that it
will take to complete the drag can be altered with the `rate`
parameter.
:param x1: The point on the x axis where the drag will start from.
:param y1: The point on the y axis where the drag will starts from.
:param x2: The point on the x axis where the drag will end at.
:param y2: The point on the y axis where the drag will end at.
:param rate: The number of pixels the finger will be moved per
iteration. Default is 10 pixels. A higher rate will make the drag
faster, and lower rate will make it slower.
:param time_between_events: The number of seconds that the drag will
wait between iterations.
:raises RuntimeError: if the finger is already pressed.
:raises RuntimeError: if no more finger slots are available.
"""
raise NotImplementedError("You cannot use this class directly.")
class Pointer(object):
"""A wrapper class that represents a pointing device which can either be a
mouse or a touch, and provides a unified API.
This class is useful if you want to run tests with either a mouse or a
touch device, and want to write your tests to use a single API. Create
this wrapper by passing it either a mouse or a touch device, like so::
pointer_device = Pointer(Mouse.create())
or, like so::
pointer_device = Pointer(Touch.create())
.. warning::
Some operations only make sense for certain devices. This class
attempts to minimise the differences between the Mouse and Touch APIs,
but there are still some operations that will cause exceptions to be
raised. These are documented in the specific methods below.
"""
def __init__(self, device):
if not isinstance(device, Mouse) and not isinstance(device, Touch):
raise TypeError(
"`device` must be either a Touch or a Mouse instance.")
self._device = device
self._x = 0
self._y = 0
@property
def x(self):
"""Pointer X coordinate.
If the wrapped device is a :class:`Touch` device, this will return the
last known X coordinate, which may not be a sensible value.
"""
if isinstance(self._device, Mouse):
return self._device.x
else:
return self._x
@property
def y(self):
"""Pointer Y coordinate.
If the wrapped device is a :class:`Touch` device, this will return the
last known Y coordinate, which may not be a sensible value.
"""
if isinstance(self._device, Mouse):
return self._device.y
else:
return self._y
def press(self, button=1):
"""Press the pointer at it's current location.
If the wrapped device is a mouse, you may pass a button specification.
If it is a touch device, passing anything other than 1 will raise a
ValueError exception.
"""
if isinstance(self._device, Mouse):
self._device.press(button)
else:
if button != 1:
raise ValueError(
"Touch devices do not have button %d" % (button))
self._device.press(self._x, self._y)
def release(self, button=1):
"""Releases the pointer at it's current location.
If the wrapped device is a mouse, you may pass a button specification.
If it is a touch device, passing anything other than 1 will raise a
ValueError exception.
"""
if isinstance(self._device, Mouse):
self._device.release(button)
else:
if button != 1:
raise ValueError(
"Touch devices do not have button %d" % (button))
self._device.release()
def click(self, button=1, press_duration=0.10):
"""Press and release at the current pointer location.
If the wrapped device is a mouse, the button specification is used. If
it is a touch device, passing anything other than 1 will raise a
ValueError exception.
"""
if isinstance(self._device, Mouse):
self._device.click(button, press_duration)
else:
if button != 1:
raise ValueError(
"Touch devices do not have button %d" % (button))
self._device.tap(self._x, self._y)
def move(self, x, y):
"""Moves the pointer to the specified coordinates.
If the wrapped device is a mouse, the mouse will animate to the
specified coordinates. If the wrapped device is a touch device, this
method will determine where the next :meth:`press`, :meth:`release` or
:meth:`click` will occur.
"""
if isinstance(self._device, Mouse):
self._device.move(x, y)
else:
self._x = x
self._y = y
def click_object(self, object_proxy, button=1, press_duration=0.10):
"""
Attempts to move the pointer to 'object_proxy's centre point.
and click a button
It does this by looking for several attributes, in order. The first
attribute found will be used. The attributes used are (in order):
* globalRect (x,y,w,h)
* center_x, center_y
* x, y, w, h
If the wrapped device is a mouse, the button specification is used. If
it is a touch device, passing anything other than 1 will raise a
ValueError exception.
"""
self.move_to_object(object_proxy)
self.click(button, press_duration)
def move_to_object(self, object_proxy):
"""Attempts to move the pointer to 'object_proxy's centre point.
It does this by looking for several attributes, in order. The first
attribute found will be used. The attributes used are (in order):
* globalRect (x,y,w,h)
* center_x, center_y
* x, y, w, h
:raises: **ValueError** if none of these attributes are found, or if an
attribute is of an incorrect type.
"""
x, y = get_center_point(object_proxy)
self.move(x, y)
def position(self):
"""
Returns the current position of the pointer.
:return: (x,y) tuple
"""
if isinstance(self._device, Mouse):
return self._device.position()
else:
return (self._x, self._y)
def drag(self, x1, y1, x2, y2, rate=10, time_between_events=0.01):
"""Perform a press, move and release.
This is to keep a common API between Mouse and Finger as long as
possible.
The pointer will be dragged from the starting point to the ending point
with multiple moves. The number of moves, and thus the time that it
will take to complete the drag can be altered with the `rate`
parameter.
:param x1: The point on the x axis where the drag will start from.
:param y1: The point on the y axis where the drag will starts from.
:param x2: The point on the x axis where the drag will end at.
:param y2: The point on the y axis where the drag will end at.
:param rate: The number of pixels the mouse will be moved per
iteration. Default is 10 pixels. A higher rate will make the drag
faster, and lower rate will make it slower.
:param time_between_events: The number of seconds that the drag will
wait between iterations.
"""
self._device.drag(
x1, y1, x2, y2, rate=rate, time_between_events=time_between_events)
if isinstance(self._device, Touch):
self._x = x2
self._y = y2
|