2
# Copyright (c) 2010 Canonical
4
# Written by Gustavo Niemeyer <gustavo@niemeyer.net>
6
# This file is part of the Xpresser GUI automation library.
8
# Xpresser is free software; you can redistribute it and/or modify
9
# it under the terms of the GNU Lesser General Public License version 3,
10
# as published by the Free Software Foundation.
12
# Xpresser is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Lesser General Public License for more details.
17
# You should have received a copy of the GNU Lesser General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
25
from xpresser import Xpresser, ImageNotFound
26
from xpresser.image import Image
27
from xpresser.imagematch import ImageMatch
29
from xpresser.tests.images import get_image_path
30
from xpresser.tests.test_xutils import XUtilsTestBase
36
class XpresserTestBase(XUtilsTestBase):
39
super(XpresserTestBase, self).setUp()
41
self.xp.load_images(get_image_path())
44
class XpresserTest(XpresserTestBase):
46
def test_load_images_and_get_image(self):
47
image = self.xp.get_image("red-circle")
48
self.assertEquals(type(image), Image)
49
self.assertEquals(image.name, "red-circle")
53
window = self.create_window(entry)
58
self.xp.type("Hello there!")
60
self.assertEquals(entry.get_text(), "Hello there!")
65
class XpresserButtonTest(XpresserTestBase):
68
super(XpresserButtonTest, self).setUp()
69
self.window = self.create_button_window()
70
self.button = self.window.get_child()
71
self.button_clicked = False
72
self.button_rclicked = False
73
self.button_hovered = False
75
def clicked(widget, event):
77
self.button_clicked = True
78
elif event.button == 3:
79
self.button_rclicked = True
83
self.button_hovered = True
85
self.button.connect("button_press_event", clicked)
86
self.button.connect("enter", entered)
93
def get_button_center(self):
94
button_x, button_y = self.button.window.get_position()
95
button_width, button_height = self.button.window.get_size()
96
return (button_x + button_width//2, button_y + button_height//2)
98
def test_find_image_name(self):
99
match = self.xp.find("red-square")
100
self.assertEquals(type(match), ImageMatch)
101
self.assertEquals(match.focus_point, self.get_button_center())
103
def test_find_image(self):
104
image = self.xp.get_image("red-square")
105
match = self.xp.find(image)
106
self.assertEquals(type(match), ImageMatch)
107
self.assertEquals(match.focus_point, self.get_button_center())
109
def test_find_with_delay(self):
113
time.sleep(SLEEP_DELAY)
116
thread = threading.Thread(target=show_window)
118
match = self.xp.find("red-square")
119
self.assertEquals(type(match), ImageMatch)
120
self.assertEquals(match.focus_point, self.get_button_center())
122
def test_find_failed(self):
123
started = time.time()
124
self.assertRaises(ImageNotFound,
125
self.xp.find, "blue-square", timeout=SLEEP_DELAY)
126
self.assertTrue(time.time() - started > SLEEP_DELAY)
132
time.sleep(SLEEP_DELAY)
135
thread = threading.Thread(target=show_window)
136
started = time.time()
138
self.xp.wait("red-square")
139
self.assertTrue(time.time() - started > SLEEP_DELAY)
141
def test_wait_failed(self):
142
started = time.time()
143
self.assertRaises(ImageNotFound,
144
self.xp.wait, "blue-square", timeout=SLEEP_DELAY)
145
self.assertTrue(time.time() - started > SLEEP_DELAY)
147
def test_click_position(self):
148
self.xp.click(*self.get_button_center())
150
self.assertTrue(self.button_clicked)
152
def test_hover_position(self):
153
self.xp.hover(*self.get_button_center())
155
self.assertTrue(self.button_hovered)
156
self.assertFalse(self.button_clicked)
158
def test_click_image_name(self):
159
self.xp.click("red-square")
161
self.assertTrue(self.button_clicked)
163
def test_right_click_image_name(self):
164
self.xp.right_click("red-square")
166
self.assertTrue(self.button_rclicked)
168
def test_hover_image_name(self):
169
self.xp.hover("red-square")
171
self.assertTrue(self.button_hovered)
172
self.assertFalse(self.button_clicked)
174
def test_click_image_match(self):
175
match = self.xp.find("red-square")
178
self.assertTrue(self.button_clicked)
180
def test_right_click_image_match(self):
181
match = self.xp.find("red-square")
182
self.xp.right_click(match)
184
self.assertTrue(self.button_rclicked)
186
def test_hover_image_match(self):
187
match = self.xp.find("red-square")
190
self.assertTrue(self.button_hovered)
191
self.assertFalse(self.button_clicked)
193
def test_click_waits(self):
197
time.sleep(SLEEP_DELAY)
200
thread = threading.Thread(target=show_window)
201
started = time.time()
203
self.xp.click("red-square")
204
self.assertTrue(time.time() - started > SLEEP_DELAY)
206
self.assertTrue(self.button_clicked)
208
def test_right_click_waits(self):
212
time.sleep(SLEEP_DELAY)
215
thread = threading.Thread(target=show_window)
216
started = time.time()
218
self.xp.right_click("red-square")
219
self.assertTrue(time.time() - started > SLEEP_DELAY)
221
self.assertTrue(self.button_rclicked)
223
def test_hover_waits(self):
227
time.sleep(SLEEP_DELAY)
230
thread = threading.Thread(target=show_window)
231
started = time.time()
233
self.xp.hover("red-square")
234
self.assertTrue(time.time() - started > SLEEP_DELAY)
236
self.assertTrue(self.button_hovered)