~ubuntu-branches/ubuntu/natty/mago/natty

« back to all changes in this revision

Viewing changes to xpresser/tests/test_xp.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 threading
 
21
import time
 
22
 
 
23
import gtk
 
24
 
 
25
from xpresser import Xpresser, ImageNotFound
 
26
from xpresser.image import Image
 
27
from xpresser.imagematch import ImageMatch
 
28
 
 
29
from xpresser.tests.images import get_image_path
 
30
from xpresser.tests.test_xutils import XUtilsTestBase
 
31
 
 
32
 
 
33
SLEEP_DELAY = 1.5
 
34
 
 
35
 
 
36
class XpresserTestBase(XUtilsTestBase):
 
37
 
 
38
    def setUp(self):
 
39
        super(XpresserTestBase, self).setUp()
 
40
        self.xp = Xpresser()
 
41
        self.xp.load_images(get_image_path())
 
42
 
 
43
 
 
44
class XpresserTest(XpresserTestBase):
 
45
 
 
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")
 
50
 
 
51
    def test_type(self):
 
52
        entry = gtk.Entry()
 
53
        window = self.create_window(entry)
 
54
        try:
 
55
            window.present()
 
56
            entry.grab_focus()
 
57
            self.flush_gtk()
 
58
            self.xp.type("Hello there!")
 
59
            self.flush_gtk()
 
60
            self.assertEquals(entry.get_text(), "Hello there!")
 
61
        finally:
 
62
            window.destroy()
 
63
 
 
64
 
 
65
class XpresserButtonTest(XpresserTestBase):
 
66
 
 
67
    def setUp(self):
 
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
 
74
 
 
75
        def clicked(widget, event):
 
76
            if event.button == 1:
 
77
                self.button_clicked = True
 
78
            elif event.button == 3:
 
79
                self.button_rclicked = True
 
80
            self.window.destroy()
 
81
 
 
82
        def entered(widget):
 
83
            self.button_hovered = True
 
84
 
 
85
        self.button.connect("button_press_event", clicked)
 
86
        self.button.connect("enter", entered)
 
87
 
 
88
        self.flush_gtk()
 
89
 
 
90
    def tearDown(self):
 
91
        self.window.destroy()
 
92
 
 
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)
 
97
 
 
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())
 
102
 
 
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())
 
108
 
 
109
    def test_find_with_delay(self):
 
110
        self.window.hide()
 
111
        self.flush_gtk()
 
112
        def show_window():
 
113
            time.sleep(SLEEP_DELAY)
 
114
            self.window.show()
 
115
            self.flush_gtk()
 
116
        thread = threading.Thread(target=show_window)
 
117
        thread.start()
 
118
        match = self.xp.find("red-square")
 
119
        self.assertEquals(type(match), ImageMatch)
 
120
        self.assertEquals(match.focus_point, self.get_button_center())
 
121
 
 
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)
 
127
 
 
128
    def test_wait(self):
 
129
        self.window.hide()
 
130
        self.flush_gtk()
 
131
        def show_window():
 
132
            time.sleep(SLEEP_DELAY)
 
133
            self.window.show()
 
134
            self.flush_gtk()
 
135
        thread = threading.Thread(target=show_window)
 
136
        started = time.time()
 
137
        thread.start()
 
138
        self.xp.wait("red-square")
 
139
        self.assertTrue(time.time() - started > SLEEP_DELAY)
 
140
 
 
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)
 
146
 
 
147
    def test_click_position(self):
 
148
        self.xp.click(*self.get_button_center())
 
149
        self.flush_gtk()
 
150
        self.assertTrue(self.button_clicked)
 
151
 
 
152
    def test_hover_position(self):
 
153
        self.xp.hover(*self.get_button_center())
 
154
        self.flush_gtk()
 
155
        self.assertTrue(self.button_hovered)
 
156
        self.assertFalse(self.button_clicked)
 
157
 
 
158
    def test_click_image_name(self):
 
159
        self.xp.click("red-square")
 
160
        self.flush_gtk()
 
161
        self.assertTrue(self.button_clicked)
 
162
 
 
163
    def test_right_click_image_name(self):
 
164
        self.xp.right_click("red-square")
 
165
        self.flush_gtk()
 
166
        self.assertTrue(self.button_rclicked)
 
167
 
 
168
    def test_hover_image_name(self):
 
169
        self.xp.hover("red-square")
 
170
        self.flush_gtk()
 
171
        self.assertTrue(self.button_hovered)
 
172
        self.assertFalse(self.button_clicked)
 
173
 
 
174
    def test_click_image_match(self):
 
175
        match = self.xp.find("red-square")
 
176
        self.xp.click(match)
 
177
        self.flush_gtk()
 
178
        self.assertTrue(self.button_clicked)
 
179
 
 
180
    def test_right_click_image_match(self):
 
181
        match = self.xp.find("red-square")
 
182
        self.xp.right_click(match)
 
183
        self.flush_gtk()
 
184
        self.assertTrue(self.button_rclicked)
 
185
 
 
186
    def test_hover_image_match(self):
 
187
        match = self.xp.find("red-square")
 
188
        self.xp.hover(match)
 
189
        self.flush_gtk()
 
190
        self.assertTrue(self.button_hovered)
 
191
        self.assertFalse(self.button_clicked)
 
192
 
 
193
    def test_click_waits(self):
 
194
        self.window.hide()
 
195
        self.flush_gtk()
 
196
        def show_window():
 
197
            time.sleep(SLEEP_DELAY)
 
198
            self.window.show()
 
199
            self.flush_gtk()
 
200
        thread = threading.Thread(target=show_window)
 
201
        started = time.time()
 
202
        thread.start()
 
203
        self.xp.click("red-square")
 
204
        self.assertTrue(time.time() - started > SLEEP_DELAY)
 
205
        self.flush_gtk()
 
206
        self.assertTrue(self.button_clicked)
 
207
 
 
208
    def test_right_click_waits(self):
 
209
        self.window.hide()
 
210
        self.flush_gtk()
 
211
        def show_window():
 
212
            time.sleep(SLEEP_DELAY)
 
213
            self.window.show()
 
214
            self.flush_gtk()
 
215
        thread = threading.Thread(target=show_window)
 
216
        started = time.time()
 
217
        thread.start()
 
218
        self.xp.right_click("red-square")
 
219
        self.assertTrue(time.time() - started > SLEEP_DELAY)
 
220
        self.flush_gtk()
 
221
        self.assertTrue(self.button_rclicked)
 
222
 
 
223
    def test_hover_waits(self):
 
224
        self.window.hide()
 
225
        self.flush_gtk()
 
226
        def show_window():
 
227
            time.sleep(SLEEP_DELAY)
 
228
            self.window.show()
 
229
            self.flush_gtk()
 
230
        thread = threading.Thread(target=show_window)
 
231
        started = time.time()
 
232
        thread.start()
 
233
        self.xp.hover("red-square")
 
234
        self.assertTrue(time.time() - started > SLEEP_DELAY)
 
235
        self.flush_gtk()
 
236
        self.assertTrue(self.button_hovered)