~ubuntu-branches/ubuntu/vivid/mago/vivid

« back to all changes in this revision

Viewing changes to xpresser/tests/test_xutils.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Vogt
  • Date: 2011-02-08 13:32:13 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110208133213-m1og7ey0m990chg6
Tags: 0.3+bzr20-0ubuntu1
* debian/rules:
  - updated to debhelper 7
  - use dh_python2 instead of python-central
* debian/pycompat:
  - removed, no longer needed
* debian/control:
  - dropped cdbs and python-central dependencies
* bzr snapshot of the current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Copyright (c) 2010 Canonical
 
3
#
 
4
# Written by Gustavo Niemeyer <gustavo@niemeyer.net>
 
5
#
 
6
# This file is part of the Xpresser GUI automation library.
 
7
#
 
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.
 
11
#
 
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.
 
16
#
 
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/>.
 
19
#
 
20
import time
 
21
 
 
22
import gtk
 
23
 
 
24
from xpresser import xutils
 
25
from xpresser.image import Image
 
26
from xpresser.opencvfinder import OpenCVFinder
 
27
 
 
28
from xpresser.tests.images import get_image_path
 
29
from xpresser.lib.testing import TestCase
 
30
 
 
31
 
 
32
class XUtilsTestBase(TestCase):
 
33
 
 
34
    def setUp(self):
 
35
        xutils.hover(0, 0)
 
36
 
 
37
    def flush_gtk(self):
 
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():
 
43
            gtk.main_iteration()
 
44
        time.sleep(0.1) # Why oh why? :-(
 
45
        while gtk.events_pending():
 
46
            gtk.main_iteration()
 
47
        time.sleep(0.1)
 
48
        while gtk.events_pending():
 
49
            gtk.main_iteration()
 
50
 
 
51
    def create_window(self, child):
 
52
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
 
53
        window.connect("delete_event", lambda widget, event: False)
 
54
        window.add(child)
 
55
        child.show()
 
56
        window.show()
 
57
        return window
 
58
 
 
59
    def create_button_window(self, image_path=None):
 
60
        button = gtk.Button()
 
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)
 
65
 
 
66
    def create_image_window(self, image_path):
 
67
        image = gtk.image_new_from_file(image_path)
 
68
        return self.create_window(image)
 
69
 
 
70
 
 
71
class XUtilsTest(XUtilsTestBase):
 
72
 
 
73
    def test_type(self):
 
74
        entry = gtk.Entry()
 
75
        window = self.create_window(entry)
 
76
        try:
 
77
            window.present()
 
78
            entry.grab_focus()
 
79
            self.flush_gtk()
 
80
            xutils.type("Hello there!")
 
81
            self.flush_gtk()
 
82
            self.assertEquals(entry.get_text(), "Hello there!")
 
83
        finally:
 
84
            window.destroy()
 
85
 
 
86
    def test_take_screenshot(self):
 
87
        """Verify that take_screenshot works, with a real screenshot.
 
88
 
 
89
        For that, we will put a known image in the screen, and will then
 
90
        try to find it in the screenshot.
 
91
        """
 
92
        red_square = Image("red-square",
 
93
                           filename=get_image_path("red-square.png"))
 
94
        window = self.create_image_window(red_square.filename)
 
95
 
 
96
        self.flush_gtk()
 
97
 
 
98
        resolution = gtk.gdk.get_default_root_window().get_size()
 
99
 
 
100
        window_x, window_y = window.get_child().window.get_position()
 
101
        window_width, window_height = window.get_child().window.get_size()
 
102
 
 
103
        big_screenshot = xutils.take_screenshot()
 
104
        small_screenshot = xutils.take_screenshot(window_x, window_y,
 
105
                                                  window_width, window_height)
 
106
 
 
107
        window.destroy()
 
108
        self.flush_gtk()
 
109
 
 
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])
 
114
 
 
115
        self.assertEquals(small_screenshot.name, "screenshot")
 
116
        self.assertEquals(small_screenshot.width, window_width)
 
117
        self.assertEquals(small_screenshot.height, window_height)
 
118
 
 
119
        # Now verify the actual images taken.
 
120
        finder = OpenCVFinder()
 
121
 
 
122
        big_match = finder.find(big_screenshot, red_square)
 
123
        small_match = finder.find(small_screenshot, red_square)
 
124
 
 
125
        self.assertEquals(big_match.image, red_square)
 
126
        self.assertTrue(big_match.similarity > 0.95, big_match.similarity)
 
127
 
 
128
        self.assertEquals(small_match.image, red_square)
 
129
        self.assertTrue(small_match.similarity > 0.95, small_match.similarity)
 
130
 
 
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)
 
136
 
 
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)
 
141
 
 
142
 
 
143
 
 
144
class XUtilsButtonTest(XUtilsTestBase):
 
145
 
 
146
    def setUp(self):
 
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
 
153
 
 
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()
 
160
 
 
161
        def entered(widget):
 
162
            self.button_hovered = True
 
163
 
 
164
        self.button.connect("button_press_event", clicked)
 
165
        self.button.connect("enter", entered)
 
166
 
 
167
        self.flush_gtk()
 
168
 
 
169
    def tearDown(self):
 
170
        self.window.destroy()
 
171
 
 
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)
 
176
 
 
177
    def test_click(self):
 
178
        xutils.click(*self.get_button_center())
 
179
        self.flush_gtk()
 
180
        self.assertTrue(self.button_clicked)
 
181
 
 
182
    def test_right_click(self):
 
183
        xutils.right_click(*self.get_button_center())
 
184
        self.flush_gtk()
 
185
        self.assertTrue(self.button_rclicked)
 
186
 
 
187
    def test_hover(self):
 
188
        xutils.hover(*self.get_button_center())
 
189
        self.flush_gtk()
 
190
        self.assertTrue(self.button_hovered)
 
191
        self.assertFalse(self.button_clicked)