/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz

« back to all changes in this revision

Viewing changes to tests/test_ui.py

Merged UI work from progressbar branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2012 Curtis Hovey <sinzui.is@verizon.net>
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Test the ui functionality."""
 
18
 
 
19
from gi.repository import Gtk
 
20
 
 
21
from bzrlib import (
 
22
    tests,
 
23
    )
 
24
 
 
25
from bzrlib.plugins.gtk import ui
 
26
from bzrlib.plugins.gtk.tests import (
 
27
    MockMethod,
 
28
    MockProperty,
 
29
    )
 
30
from bzrlib.progress import ProgressTask
 
31
 
 
32
 
 
33
class MainIterationTestCase(tests.TestCase):
 
34
 
 
35
    def test_main_iteration(self):
 
36
        # The main_iteration decorator iterates over the pending Gtk events
 
37
        # after calling its function so that the UI is updated too.
 
38
        button = Gtk.ToggleButton(label='before')
 
39
 
 
40
        def event_listener(button):
 
41
            button.props.label = 'after'
 
42
 
 
43
        button.connect('clicked', event_listener)
 
44
 
 
45
        def test_func(self):
 
46
            button.emit('clicked')
 
47
            return True
 
48
 
 
49
        decorated_func = ui.main_iteration(test_func)
 
50
        result = decorated_func(object())
 
51
        self.assertIs(True, result)
 
52
        self.assertIs(False, Gtk.events_pending())
 
53
        self.assertEqual('after', button.props.label)
 
54
 
 
55
 
 
56
class GtkUIFactoryTestCase(tests.TestCase):
 
57
 
 
58
    def test__init(self):
 
59
        ui_factory = ui.GtkUIFactory()
 
60
        self.assertIs(None, ui_factory._progress_bar_widget)
 
61
 
 
62
    def test_set_progress_bar_widget(self):
 
63
        ui_factory = ui.GtkUIFactory()
 
64
        progress_widget = ui.ProgressPanel()
 
65
        ui_factory.set_progress_bar_widget(progress_widget)
 
66
        self.assertIs(progress_widget, ui_factory._progress_bar_widget)
 
67
 
 
68
    def test_get_boolean_true(self):
 
69
        ui_factory = ui.GtkUIFactory()
 
70
        MockMethod.bind(self, ui.PromptDialog, 'run', Gtk.ResponseType.YES)
 
71
        boolean_value = ui_factory.get_boolean('test')
 
72
        self.assertIs(True, ui.PromptDialog.run.called)
 
73
        self.assertIs(True, boolean_value)
 
74
 
 
75
    def test_get_boolean_false(self):
 
76
        ui_factory = ui.GtkUIFactory()
 
77
        MockMethod.bind(self, ui.PromptDialog, 'run', Gtk.ResponseType.NO)
 
78
        boolean_value = ui_factory.get_boolean('test')
 
79
        self.assertIs(True, ui.PromptDialog.run.called)
 
80
        self.assertIs(False, boolean_value)
 
81
 
 
82
    def test_get_password(self):
 
83
        ui_factory = ui.GtkUIFactory()
 
84
        MockMethod.bind(self, ui.PasswordDialog, 'run', Gtk.ResponseType.OK)
 
85
        mock_property = MockProperty.bind(
 
86
            self, ui.PasswordDialog, 'passwd', 'secret')
 
87
        password = ui_factory.get_password('test')
 
88
        self.assertIs(True, ui.PasswordDialog.run.called)
 
89
        self.assertIs(True, mock_property.called)
 
90
        self.assertEqual('secret', password)
 
91
 
 
92
    def test_progress_all_finished_with_widget(self):
 
93
        ui_factory = ui.GtkUIFactory()
 
94
        progress_widget = ui.ProgressPanel()
 
95
        MockMethod.bind(self, progress_widget, 'finished')
 
96
        ui_factory.set_progress_bar_widget(progress_widget)
 
97
        self.assertIs(None, ui_factory._progress_all_finished())
 
98
        self.assertIs(True, progress_widget.finished.called)
 
99
 
 
100
    def test_progress_all_finished_without_widget(self):
 
101
        ui_factory = ui.GtkUIFactory()
 
102
        self.assertIs(None, ui_factory._progress_all_finished())
 
103
 
 
104
    def test_progress_updated_with_widget(self):
 
105
        ui_factory = ui.GtkUIFactory()
 
106
        progress_widget = ui.ProgressPanel()
 
107
        MockMethod.bind(self, progress_widget, 'update')
 
108
        ui_factory.set_progress_bar_widget(progress_widget)
 
109
        task = ProgressTask()
 
110
        task.msg = 'test'
 
111
        task.current_cnt = 1
 
112
        task.total_cnt = 2
 
113
        self.assertIs(None, ui_factory._progress_updated(task))
 
114
        self.assertIs(True, progress_widget.update.called)
 
115
        self.assertEqual(
 
116
            ('test', 1, 2), progress_widget.update.args)
 
117
 
 
118
    def test_progress_updated_without_widget(self):
 
119
        ui_factory = ui.GtkUIFactory()
 
120
        MockMethod.bind(self, ui.ProgressBarWindow, 'update')
 
121
        task = ProgressTask()
 
122
        task.msg = 'test'
 
123
        task.current_cnt = 1
 
124
        task.total_cnt = 2
 
125
        self.assertIs(None, ui_factory._progress_updated(task))
 
126
        self.assertIsInstance(
 
127
            ui_factory._progress_bar_widget, ui.ProgressBarWindow)
 
128
        self.assertIs(True, ui_factory._progress_bar_widget.update.called)
 
129
        self.assertEqual(
 
130
            ('test', 1, 2), ui_factory._progress_bar_widget.update.args)
 
131
 
 
132
    def test_report_transport_activity_with_widget(self):
 
133
        ui_factory = ui.GtkUIFactory()
 
134
        progress_widget = ui.ProgressPanel()
 
135
        MockMethod.bind(self, progress_widget, 'tick')
 
136
        ui_factory.set_progress_bar_widget(progress_widget)
 
137
        self.assertIs(
 
138
            None, ui_factory.report_transport_activity(None, None, None))
 
139
        self.assertIs(True, progress_widget.tick.called)
 
140
 
 
141
    def test_report_transport_activity_without_widget(self):
 
142
        ui_factory = ui.GtkUIFactory()
 
143
        MockMethod.bind(self, ui.ProgressBarWindow, 'tick')
 
144
        self.assertIs(
 
145
            None, ui_factory.report_transport_activity(None, None, None))
 
146
        self.assertIsInstance(
 
147
            ui_factory._progress_bar_widget, ui.ProgressBarWindow)
 
148
        self.assertIs(True, ui.ProgressBarWindow.tick.called)
 
149
 
 
150
 
 
151
class PromptDialogTestCase(tests.TestCase):
 
152
 
 
153
    def test_init(self):
 
154
        # tthe label and buttons are created, then shown.
 
155
        MockMethod.bind(self, Gtk.Box, 'show_all')
 
156
        dialog = ui.PromptDialog('test 123')
 
157
        content_area = dialog.get_content_area()
 
158
        self.assertIs(True, dialog.get_content_area().show_all.called)
 
159
        self.assertIs(1, dialog.get_content_area().show_all.call_count)
 
160
        label = content_area.get_children()[0]
 
161
        self.assertEqual('test 123', label.props.label)
 
162
        buttons = dialog.get_action_area().get_children()
 
163
        self.assertEqual('gtk-no', buttons[0].props.label)
 
164
        self.assertEqual(
 
165
            Gtk.ResponseType.NO, dialog.get_response_for_widget(buttons[0]))
 
166
        self.assertEqual('gtk-yes', buttons[1].props.label)
 
167
        self.assertEqual(
 
168
            Gtk.ResponseType.YES, dialog.get_response_for_widget(buttons[1]))
 
169
 
 
170
 
 
171
class PasswordDialogTestCase(tests.TestCase):
 
172
 
 
173
    def test_init(self):
 
174
        # The label, password entry, and buttons are created, then shown.
 
175
        MockMethod.bind(self, Gtk.Box, 'show_all')
 
176
        dialog = ui.PasswordDialog('test password')
 
177
        content_area = dialog.get_content_area()
 
178
        self.assertIs(True, dialog.get_content_area().show_all.called)
 
179
        widgets = content_area.get_children()
 
180
        self.assertEqual('test password', widgets[0].props.label)
 
181
        self.assertEqual(False, widgets[1].props.visibility)
 
182
        buttons = dialog.get_action_area().get_children()
 
183
        self.assertEqual('gtk-cancel', buttons[0].props.label)
 
184
        self.assertEqual(
 
185
            Gtk.ResponseType.CANCEL,
 
186
            dialog.get_response_for_widget(buttons[0]))
 
187
        self.assertEqual('gtk-ok', buttons[1].props.label)
 
188
        self.assertEqual(
 
189
            Gtk.ResponseType.OK,
 
190
            dialog.get_response_for_widget(buttons[1]))
 
191
 
 
192
 
 
193
class GtkProgressBarTestCase(tests.TestCase):
 
194
 
 
195
    def test_init(self):
 
196
        progress_bar = ui.GtkProgressBar()
 
197
        self.assertEqual(0.0, progress_bar.props.fraction)
 
198
        self.assertIs(None, progress_bar.total)
 
199
        self.assertIs(None, progress_bar.current)
 
200
 
 
201
    def test_tick(self):
 
202
        # tick() shows the widget, does one pulse, then handles the pending
 
203
        # events in the main loop.
 
204
        MockMethod.bind(self, ui.GtkProgressBar, 'show')
 
205
        MockMethod.bind(self, ui.GtkProgressBar, 'pulse')
 
206
        progress_bar = ui.GtkProgressBar()
 
207
        progress_bar.tick()
 
208
        self.assertIs(True, progress_bar.show.called)
 
209
        self.assertEqual('with_main_iteration', progress_bar.tick.__name__)
 
210
 
 
211
    def test_update_with_data(self):
 
212
        # update() shows the widget, sets the fraction, then handles the
 
213
        # pending events in the main loop.
 
214
        MockMethod.bind(self, ui.GtkProgressBar, 'show')
 
215
        progress_bar = ui.GtkProgressBar()
 
216
        progress_bar.update(msg='test', current_cnt=5, total_cnt=10)
 
217
        self.assertIs(True, progress_bar.show.called)
 
218
        self.assertEqual(0.5, progress_bar.props.fraction)
 
219
        self.assertEqual(10, progress_bar.total)
 
220
        self.assertEqual(5, progress_bar.current)
 
221
        self.assertEqual('with_main_iteration', progress_bar.update.__name__)
 
222
 
 
223
    def test_update_without_data(self):
 
224
        progress_bar = ui.GtkProgressBar()
 
225
        progress_bar.update(current_cnt=5, total_cnt=None)
 
226
        self.assertEqual(0.0, progress_bar.props.fraction)
 
227
        self.assertIs(None, progress_bar.total)
 
228
        self.assertEqual(5, progress_bar.current)
 
229
 
 
230
    def test_update_with_insane_data(self):
 
231
        # The fraction must be between 0.0 and 1.0.
 
232
        progress_bar = ui.GtkProgressBar()
 
233
        self.assertRaises(
 
234
            ValueError, progress_bar.update, None, 20, 2)
 
235
 
 
236
    def test_finished(self):
 
237
        # finished() hides the widget, resets the state, then handles the
 
238
        # pending events in the main loop.
 
239
        MockMethod.bind(self, ui.GtkProgressBar, 'hide')
 
240
        progress_bar = ui.GtkProgressBar()
 
241
        progress_bar.finished()
 
242
        self.assertIs(True, progress_bar.hide.called)
 
243
        self.assertEqual(0.0, progress_bar.props.fraction)
 
244
        self.assertIs(None, progress_bar.total)
 
245
        self.assertIs(None, progress_bar.current)
 
246
        self.assertEqual('with_main_iteration', progress_bar.finished.__name__)
 
247
 
 
248
    def test_clear(self):
 
249
        # clear() is synonymous with finished.
 
250
        MockMethod.bind(self, ui.GtkProgressBar, 'finished')
 
251
        progress_bar = ui.GtkProgressBar()
 
252
        progress_bar.finished()
 
253
        self.assertIs(True, progress_bar.finished.called)
 
254
 
 
255
 
 
256
class ProgressContainerMixin:
 
257
 
 
258
    def test_tick(self):
 
259
        progress_widget = self.progress_container()
 
260
        MockMethod.bind(self, progress_widget, 'show_all')
 
261
        MockMethod.bind(self, progress_widget.pb, 'tick')
 
262
        progress_widget.tick()
 
263
        self.assertIs(True, progress_widget.show_all.called)
 
264
        self.assertIs(True, progress_widget.pb.tick.called)
 
265
 
 
266
    def test_update(self):
 
267
        progress_widget = self.progress_container()
 
268
        MockMethod.bind(self, progress_widget, 'show_all')
 
269
        MockMethod.bind(self, progress_widget.pb, 'update')
 
270
        progress_widget.update('test', 5, 10)
 
271
        self.assertIs(True, progress_widget.show_all.called)
 
272
        self.assertIs(True, progress_widget.pb.update.called)
 
273
        self.assertEqual(
 
274
            ('test', 5, 10), progress_widget.pb.update.args)
 
275
 
 
276
    def test_finished(self):
 
277
        progress_widget = self.progress_container()
 
278
        MockMethod.bind(self, progress_widget, 'hide')
 
279
        MockMethod.bind(self, progress_widget.pb, 'finished')
 
280
        progress_widget.finished()
 
281
        self.assertIs(True, progress_widget.hide.called)
 
282
        self.assertIs(True, progress_widget.pb.finished.called)
 
283
 
 
284
    def test_clear(self):
 
285
        progress_widget = self.progress_container()
 
286
        MockMethod.bind(self, progress_widget, 'hide')
 
287
        MockMethod.bind(self, progress_widget.pb, 'clear')
 
288
        progress_widget.clear()
 
289
        self.assertIs(True, progress_widget.hide.called)
 
290
        self.assertIs(True, progress_widget.pb.clear.called)
 
291
 
 
292
 
 
293
class ProgressBarWindowTestCase(ProgressContainerMixin, tests.TestCase):
 
294
 
 
295
    progress_container = ui.ProgressBarWindow
 
296
 
 
297
    def test_init(self):
 
298
        pb_window = ui.ProgressBarWindow()
 
299
        self.assertEqual('Progress', pb_window.props.title)
 
300
        self.assertEqual(
 
301
            Gtk.WindowPosition.CENTER_ALWAYS, pb_window.props.window_position)
 
302
        self.assertIsInstance(pb_window.pb, ui.GtkProgressBar)
 
303
 
 
304
 
 
305
class ProgressPanelTestCase(ProgressContainerMixin, tests.TestCase):
 
306
 
 
307
    progress_container = ui.ProgressPanel
 
308
 
 
309
    def test_init(self):
 
310
        pb_window = ui.ProgressPanel()
 
311
        self.assertEqual(
 
312
            Gtk.Orientation.HORIZONTAL, pb_window.props.orientation)
 
313
        self.assertEqual(5, pb_window.props.spacing)
 
314
        self.assertIsInstance(pb_window.pb, ui.GtkProgressBar)
 
315
        widgets = pb_window.get_children()
 
316
        # The image's stock and icon_name properties are always None?
 
317
        self.assertIsInstance(widgets[0], Gtk.Image)