~pitti/autopilot-gtk/gtktextbuffer

« back to all changes in this revision

Viewing changes to tests/autopilot/tests/test_actions.py

  • Committer: Tarmac
  • Author(s): Martin Pitt
  • Date: 2013-06-26 17:03:00 UTC
  • mfrom: (43.2.25 ap-gtk-tests)
  • Revision ID: tarmac-20130626170300-1yfj1vyv21n257xr
Add integration test suite (LP: #1083612). Fixes: https://bugs.launchpad.net/bugs/1083612.

Approved by Francis Ginther, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
#
 
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.
 
8
#
 
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.
 
13
#
 
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/>.
 
16
 
 
17
import os.path
 
18
 
 
19
from autopilot.testcase import AutopilotTestCase
 
20
from autopilot.matchers import Eventually
 
21
from testtools.matchers import Equals, NotEquals
 
22
 
 
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')
 
26
 
 
27
 
 
28
class ActionsTest(AutopilotTestCase):
 
29
    """Test performing actions in the UI and verify results"""
 
30
 
 
31
    def setUp(self):
 
32
        super(ActionsTest, self).setUp()
 
33
        self.app = self.launch_test_application(test_app, app_type='gtk')
 
34
 
 
35
    def test_greeting_keyboard(self):
 
36
        """Greeting with keyboard navigation"""
 
37
 
 
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
 
42
        # assertion
 
43
        if entries[0].globalRect[1] < entries[1].globalRect[1]:
 
44
            (entry_name, entry_color) = entries
 
45
        else:
 
46
            (entry_color, entry_name) = entries
 
47
 
 
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)
 
52
 
 
53
        # type in name and color
 
54
        self.keyboard.type('Joe')
 
55
        self.keyboard.press_and_release('Tab')
 
56
        self.keyboard.type('red')
 
57
 
 
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')))
 
61
 
 
62
        # should not have any dialogs
 
63
        self.assertEqual(self.app.select_single('GtkMessageDialog'), None)
 
64
 
 
65
        # focus and activate the "Greet" button
 
66
        self.keyboard.press_and_release('Tab')
 
67
        self.keyboard.press_and_release('Enter')
 
68
 
 
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')
 
73
 
 
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.'),
 
77
                            None)
 
78
 
 
79
        # close the dialog
 
80
        self.keyboard.press_and_release('Enter')
 
81
        self.assertThat(lambda: self.app.select_single('GtkMessageDialog', visible=True),
 
82
                        Eventually(Equals(None)))
 
83
 
 
84
    def test_greeting_mouse(self):
 
85
        """Greeting with mouse navigation"""
 
86
 
 
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
 
91
        # assertion
 
92
        if entries[0].globalRect[1] < entries[1].globalRect[1]:
 
93
            (entry_name, entry_color) = entries
 
94
        else:
 
95
            (entry_color, entry_name) = entries
 
96
 
 
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)
 
101
 
 
102
        # type in name and color
 
103
        self.keyboard.type('Joe')
 
104
        self.mouse.click_object(entry_color)
 
105
        self.keyboard.type('blue')
 
106
 
 
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')))
 
110
 
 
111
        # should not have any dialogs
 
112
        self.assertEqual(self.app.select_single('GtkMessageDialog'), None)
 
113
 
 
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)
 
118
 
 
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')
 
123
 
 
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.'),
 
127
                            None)
 
128
 
 
129
        # close the dialog
 
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)))
 
134
 
 
135
    def test_clear(self):
 
136
        """Using Clear button with mouse"""
 
137
 
 
138
        # type in name and color
 
139
        self.keyboard.type('Joe')
 
140
        self.keyboard.press_and_release('Tab')
 
141
        self.keyboard.type('blue')
 
142
 
 
143
        # clear
 
144
        btn = self.app.select_single('GtkButton', label='gtk-delete')
 
145
        self.mouse.click_object(btn)
 
146
 
 
147
        # entries should be clear now
 
148
        entries = self.app.select_many('GtkEntry')
 
149
        self.assertEqual(len(entries), 2)
 
150
        for e in entries:
 
151
            self.assertThat(e.text, Eventually(Equals('')))
 
152
 
 
153
    def test_menu(self):
 
154
        """Browse the menu"""
 
155
 
 
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)
 
160
 
 
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)
 
167
 
 
168
        # the submenus are not visible by default
 
169
        m = self.app.select_single('GtkImageMenuItem', label='gtk-open')
 
170
        self.assertFalse(hasattr(m, 'globalRect'))
 
171
 
 
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
 
175
        # it doesn't work
 
176
        self.assertThat(lambda: hasattr(self.app.select_single('GtkImageMenuItem',
 
177
                                                               label='gtk-open'),
 
178
                                        'globalRect'),
 
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)