1
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2
# Copyright 2012 Canonical
3
# Author: Thomi Richards
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.
11
from time import sleep
13
from autopilot.emulators.unity import UnityIntrospectionObject
14
from autopilot.emulators.X11 import Mouse
17
logger = logging.getLogger(__name__)
20
class Quicklist(UnityIntrospectionObject):
21
"""Represents a quicklist."""
25
"""Individual items in the quicklist."""
26
return self.get_children_by_type(QuicklistMenuItem, visible=True)
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)
33
def get_quicklist_item_by_text(self, text):
34
"""Returns a QuicklistMenuItemLabel object with the given text, or None."""
36
raise RuntimeError("Cannot get quicklist items. Quicklist is inactive!")
38
matches = filter(lambda i: i.text == text,
39
self.get_children_by_type(QuicklistMenuItemLabel))
41
return matches[0] if matches else None
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>")
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")
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)
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
69
"""Returns a tuple of (x,y,w,h) for the quicklist."""
70
return (self.x, self.y, self.width, self.height)
73
class QuicklistMenuItem(UnityIntrospectionObject):
74
"""Represents a single item in a quicklist."""
76
def __init__(self, *args, **kwargs):
77
super(QuicklistMenuItem, self).__init__(*args, **kwargs)
82
"""Returns a tuple of (x,y,w,h) for the quicklist item."""
83
return (self.x, self.y, self.width, self.height)
85
def mouse_move_to(self):
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)
92
def mouse_click(self, button=1):
93
logger.debug("Clicking on quicklist item %r", self)
98
class QuicklistMenuItemLabel(QuicklistMenuItem):
99
"""Represents a text label inside a quicklist."""
102
class QuicklistMenuItemSeparator(QuicklistMenuItem):
103
"""Represents a separator in a quicklist."""