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/>.
24
from xpresser import xutils
25
from xpresser.image import Image
26
from xpresser.opencvfinder import OpenCVFinder
28
from xpresser.tests.images import get_image_path
29
from xpresser.lib.testing import TestCase
32
class XUtilsTestBase(TestCase):
38
# Why do we need to do so much to get the button in place before
39
# actually trying to click on it? :-( If we just run until there
40
# are no more events, and without sleep, the button will simply
41
# return (0, 0) as its position.
42
while gtk.events_pending():
44
time.sleep(0.1) # Why oh why? :-(
45
while gtk.events_pending():
48
while gtk.events_pending():
51
def create_window(self, child):
52
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
53
window.connect("delete_event", lambda widget, event: False)
59
def create_button_window(self, image_path=None):
61
if image_path is None:
62
image_path = get_image_path("red-square.png")
63
button.set_image(gtk.image_new_from_file(image_path))
64
return self.create_window(button)
66
def create_image_window(self, image_path):
67
image = gtk.image_new_from_file(image_path)
68
return self.create_window(image)
71
class XUtilsTest(XUtilsTestBase):
75
window = self.create_window(entry)
80
xutils.type("Hello there!")
82
self.assertEquals(entry.get_text(), "Hello there!")
86
def test_take_screenshot(self):
87
"""Verify that take_screenshot works, with a real screenshot.
89
For that, we will put a known image in the screen, and will then
90
try to find it in the screenshot.
92
red_square = Image("red-square",
93
filename=get_image_path("red-square.png"))
94
window = self.create_image_window(red_square.filename)
98
resolution = gtk.gdk.get_default_root_window().get_size()
100
window_x, window_y = window.get_child().window.get_position()
101
window_width, window_height = window.get_child().window.get_size()
103
big_screenshot = xutils.take_screenshot()
104
small_screenshot = xutils.take_screenshot(window_x, window_y,
105
window_width, window_height)
110
# Check the basic attributes set
111
self.assertEquals(big_screenshot.name, "screenshot")
112
self.assertEquals(big_screenshot.width, resolution[0])
113
self.assertEquals(big_screenshot.height, resolution[1])
115
self.assertEquals(small_screenshot.name, "screenshot")
116
self.assertEquals(small_screenshot.width, window_width)
117
self.assertEquals(small_screenshot.height, window_height)
119
# Now verify the actual images taken.
120
finder = OpenCVFinder()
122
big_match = finder.find(big_screenshot, red_square)
123
small_match = finder.find(small_screenshot, red_square)
125
self.assertEquals(big_match.image, red_square)
126
self.assertTrue(big_match.similarity > 0.95, big_match.similarity)
128
self.assertEquals(small_match.image, red_square)
129
self.assertTrue(small_match.similarity > 0.95, small_match.similarity)
131
# The match we found in the big screenshot should be in the same
132
# position as the window we created. Note that this may fail if
133
# you have the image opened elsewhere. ;-)
134
self.assertEquals(big_match.x, window_x)
135
self.assertEquals(big_match.y, window_y)
137
# With the small match, it should be in the origin, since the
138
# screenshot was taken on the precise area.
139
self.assertEquals(small_match.x, 0)
140
self.assertEquals(small_match.y, 0)
144
class XUtilsButtonTest(XUtilsTestBase):
147
super(XUtilsButtonTest, self).setUp()
148
self.window = self.create_button_window()
149
self.button = self.window.get_child()
150
self.button_clicked = False
151
self.button_rclicked = False
152
self.button_hovered = False
154
def clicked(widget, event):
155
if event.button == 1:
156
self.button_clicked = True
157
elif event.button == 3:
158
self.button_rclicked = True
159
self.window.destroy()
162
self.button_hovered = True
164
self.button.connect("button_press_event", clicked)
165
self.button.connect("enter", entered)
170
self.window.destroy()
172
def get_button_center(self):
173
button_x, button_y = self.button.window.get_position()
174
button_width, button_height = self.button.window.get_size()
175
return (button_x + button_width//2, button_y + button_height//2)
177
def test_click(self):
178
xutils.click(*self.get_button_center())
180
self.assertTrue(self.button_clicked)
182
def test_right_click(self):
183
xutils.right_click(*self.get_button_center())
185
self.assertTrue(self.button_rclicked)
187
def test_hover(self):
188
xutils.hover(*self.get_button_center())
190
self.assertTrue(self.button_hovered)
191
self.assertFalse(self.button_clicked)