~3v1n0/autopilot/badwindow-errors-protect

« back to all changes in this revision

Viewing changes to autopilot/emulators/unity/quicklist.py

  • Committer: Thomi Richards
  • Date: 2012-05-06 22:45:27 UTC
  • Revision ID: thomi.richards@canonical.com-20120506224527-xh6wixqiw0rarkmh
Imported code from 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.emulators.unity import UnityIntrospectionObject
 
14
from autopilot.emulators.X11 import Mouse
 
15
 
 
16
 
 
17
logger = logging.getLogger(__name__)
 
18
 
 
19
 
 
20
class Quicklist(UnityIntrospectionObject):
 
21
    """Represents a quicklist."""
 
22
 
 
23
    @property
 
24
    def items(self):
 
25
        """Individual items in the quicklist."""
 
26
        return self.get_children_by_type(QuicklistMenuItem, visible=True)
 
27
 
 
28
    @property
 
29
    def selectable_items(self):
 
30
        """Items that can be selected in the quicklist."""
 
31
        return self.get_children_by_type(QuicklistMenuItem, visible=True, selectable=True)
 
32
 
 
33
    def get_quicklist_item_by_text(self, text):
 
34
        """Returns a QuicklistMenuItemLabel object with the given text, or None."""
 
35
        if not self.active:
 
36
            raise RuntimeError("Cannot get quicklist items. Quicklist is inactive!")
 
37
 
 
38
        matches = filter(lambda i: i.text == text,
 
39
            self.get_children_by_type(QuicklistMenuItemLabel))
 
40
 
 
41
        return matches[0] if matches else None
 
42
 
 
43
    def get_quicklist_application_item(self, application_name):
 
44
        """Returns the QuicklistMenuItemLabel for the given application_name"""
 
45
        return self.get_quicklist_item_by_text("<b>"+application_name+"</b>")
 
46
 
 
47
    def click_item(self, item):
 
48
        """Click one of the quicklist items."""
 
49
        if not isinstance(item, QuicklistMenuItem):
 
50
            raise TypeError("Item must be a subclass of QuicklistMenuItem")
 
51
 
 
52
        item.mouse_click()
 
53
 
 
54
    def move_mouse_to_right(self):
 
55
        """Moves the mouse outside the quicklist"""
 
56
        logger.debug("Moving mouse outside the quicklist %r", self)
 
57
        target_x = self.x + self.width + 10
 
58
        target_y = self.y + self.height / 2
 
59
        Mouse().move(target_x, target_y, animate=False)
 
60
 
 
61
    @property
 
62
    def selected_item(self):
 
63
        items = self.get_children_by_type(QuicklistMenuItem, selected=True)
 
64
        assert(len(items) <= 1)
 
65
        return items[0] if items else None
 
66
 
 
67
    @property
 
68
    def geometry(self):
 
69
        """Returns a tuple of (x,y,w,h) for the quicklist."""
 
70
        return (self.x, self.y, self.width, self.height)
 
71
 
 
72
 
 
73
class QuicklistMenuItem(UnityIntrospectionObject):
 
74
    """Represents a single item in a quicklist."""
 
75
 
 
76
    def __init__(self, *args, **kwargs):
 
77
        super(QuicklistMenuItem, self).__init__(*args, **kwargs)
 
78
        self._mouse = Mouse()
 
79
 
 
80
    @property
 
81
    def geometry(self):
 
82
        """Returns a tuple of (x,y,w,h) for the quicklist item."""
 
83
        return (self.x, self.y, self.width, self.height)
 
84
 
 
85
    def mouse_move_to(self):
 
86
        assert(self.visible)
 
87
        logger.debug("Moving mouse over quicklist item %r", self)
 
88
        target_x = self.x + self.width / 2
 
89
        target_y = self.y + self.height / 2
 
90
        self._mouse.move(target_x, target_y, rate=20, time_between_events=0.005)
 
91
 
 
92
    def mouse_click(self, button=1):
 
93
        logger.debug("Clicking on quicklist item %r", self)
 
94
        self.mouse_move_to()
 
95
        self._mouse.click()
 
96
 
 
97
 
 
98
class QuicklistMenuItemLabel(QuicklistMenuItem):
 
99
    """Represents a text label inside a quicklist."""
 
100
 
 
101
 
 
102
class QuicklistMenuItemSeparator(QuicklistMenuItem):
 
103
    """Represents a separator in a quicklist."""