1
# blackbox testing of autopilot API against our hello_color.py test GTK program
2
# Author: Martin Pitt <martin.pitt@ubuntu.com>
3
# Copyright (C) 2013 Canonical Ltd
5
# This program is free software: you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License version 3 as
7
# published by the Free Software Foundation.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
from autopilot.testcase import AutopilotTestCase
20
from autopilot.matchers import Eventually
21
from testtools.matchers import Equals, NotEquals
23
tests_dir = os.path.dirname(os.path.dirname(os.path.dirname(
24
os.path.realpath(__file__))))
25
test_app = os.path.join(tests_dir, 'hello_color.py')
28
class ActionsTest(AutopilotTestCase):
29
"""Test performing actions in the UI and verify results"""
32
super(ActionsTest, self).setUp()
33
self.app = self.launch_test_application(test_app, app_type='gtk')
35
def test_greeting_keyboard(self):
36
"""Greeting with keyboard navigation"""
38
entries = self.app.select_many('GtkEntry')
39
self.assertEqual(len(entries), 2)
40
# the upper entry is for the name, the lower for the color
41
# FIXME: once we have proper names (LP# 1082391), replace this with an
43
if entries[0].globalRect[1] < entries[1].globalRect[1]:
44
(entry_name, entry_color) = entries
46
(entry_color, entry_name) = entries
48
# FIXME: This isn't necessary for real X, but under Xvfb there is no
49
# default focus sometimes
50
if not entry_name.has_focus:
51
self.mouse.click_object(entry_name)
53
# type in name and color
54
self.keyboard.type('Joe')
55
self.keyboard.press_and_release('Tab')
56
self.keyboard.type('red')
58
# entries should now have the typed text
59
self.assertThat(entry_name.text, Eventually(Equals('Joe')))
60
self.assertThat(entry_color.text, Eventually(Equals('red')))
62
# should not have any dialogs
63
self.assertEqual(self.app.select_single('GtkMessageDialog'), None)
65
# focus and activate the "Greet" button
66
self.keyboard.press_and_release('Tab')
67
self.keyboard.press_and_release('Enter')
69
# should get the greeting dialog
70
self.assertThat(lambda: self.app.select_single('GtkMessageDialog', visible=True),
71
Eventually(NotEquals(None)))
72
md = self.app.select_single('GtkMessageDialog')
74
# we expect the message dialog to show the corresponding greeting
75
self.assertNotEqual(md.select_single('GtkLabel',
76
label=u'Hello Joe, you like red.'),
80
self.keyboard.press_and_release('Enter')
81
self.assertThat(lambda: self.app.select_single('GtkMessageDialog', visible=True),
82
Eventually(Equals(None)))
84
def test_greeting_mouse(self):
85
"""Greeting with mouse navigation"""
87
entries = self.app.select_many('GtkEntry')
88
self.assertEqual(len(entries), 2)
89
# the upper entry is for the name, the lower for the color
90
# FIXME: once we have proper names (LP# 1082391), replace this with an
92
if entries[0].globalRect[1] < entries[1].globalRect[1]:
93
(entry_name, entry_color) = entries
95
(entry_color, entry_name) = entries
97
# FIXME: This isn't necessary for real X, but under Xvfb there is no
98
# default focus sometimes
99
if not entry_name.has_focus:
100
self.mouse.click_object(entry_name)
102
# type in name and color
103
self.keyboard.type('Joe')
104
self.mouse.click_object(entry_color)
105
self.keyboard.type('blue')
107
# entries should now have the typed text
108
self.assertThat(entry_name.text, Eventually(Equals('Joe')))
109
self.assertThat(entry_color.text, Eventually(Equals('blue')))
111
# should not have any dialogs
112
self.assertEqual(self.app.select_single('GtkMessageDialog'), None)
114
# focus and activate the "Greet" button
115
btn = self.app.select_single('GtkButton', label='Greet')
116
self.assertNotEqual(btn, None)
117
self.mouse.click_object(btn)
119
# should get the greeting dialog
120
self.assertThat(lambda: self.app.select_single('GtkMessageDialog', visible=True),
121
Eventually(NotEquals(None)))
122
md = self.app.select_single('GtkMessageDialog')
124
# we expect the message dialog to show the corresponding greeting
125
self.assertNotEqual(md.select_single('GtkLabel',
126
label=u'Hello Joe, you like blue.'),
130
btn = md.select_single('GtkButton', label='gtk-close')
131
self.mouse.click_object(btn)
132
self.assertThat(lambda: self.app.select_single('GtkMessageDialog', visible=True),
133
Eventually(Equals(None)))
135
def test_clear(self):
136
"""Using Clear button with mouse"""
138
# type in name and color
139
self.keyboard.type('Joe')
140
self.keyboard.press_and_release('Tab')
141
self.keyboard.type('blue')
144
btn = self.app.select_single('GtkButton', label='gtk-delete')
145
self.mouse.click_object(btn)
147
# entries should be clear now
148
entries = self.app.select_many('GtkEntry')
149
self.assertEqual(len(entries), 2)
151
self.assertThat(e.text, Eventually(Equals('')))
154
"""Browse the menu"""
156
file_menu = self.app.select_single('GtkMenuItem', label='_File')
157
help_menu = self.app.select_single('GtkMenuItem', label='_Help')
158
self.assertNotEqual(file_menu, None)
159
self.assertNotEqual(help_menu, None)
161
# the top-level menus should be visible and thus have a rect
162
for m in (file_menu, help_menu):
163
self.assertGreaterEqual(m.globalRect[0], 0)
164
self.assertGreaterEqual(m.globalRect[1], 0)
165
self.assertGreater(m.globalRect[2], 0)
166
self.assertGreater(m.globalRect[3], 0)
168
# the submenus are not visible by default
169
m = self.app.select_single('GtkImageMenuItem', label='gtk-open')
170
self.assertFalse(hasattr(m, 'globalRect'))
172
# after opening, submenus should become visible
173
self.mouse.click_object(file_menu)
174
# FIXME: getting a reference to this object once and then just querying
176
self.assertThat(lambda: hasattr(self.app.select_single('GtkImageMenuItem',
179
Eventually(Equals(True)))
180
m = self.app.select_single('GtkImageMenuItem', label='gtk-open')
181
self.assertGreaterEqual(m.globalRect[0], 0)
182
self.assertGreaterEqual(m.globalRect[1], 0)
183
self.assertGreater(m.globalRect[2], 0)
184
self.assertGreater(m.globalRect[3], 0)