/b-gtk/fix-viz

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/b-gtk/fix-viz
776.1.6 by Curtis Hovey
Added a test module for ui.
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
776.1.9 by Curtis Hovey
Added tests for get_boolean.
19
from gi.repository import Gtk
20
776.1.6 by Curtis Hovey
Added a test module for ui.
21
from bzrlib import (
22
    tests,
23
    )
24
25
from bzrlib.plugins.gtk import ui
776.1.10 by Curtis Hovey
Added MockProperty to test that a dialog property is called by get_password.
26
from bzrlib.plugins.gtk.tests import (
27
    MockMethod,
28
    MockProperty,
29
    )
776.1.13 by Curtis Hovey
Added tests for _progress_updated_with_widget().
30
from bzrlib.progress import ProgressTask
776.1.6 by Curtis Hovey
Added a test module for ui.
31
32
776.1.34 by Curtis Hovey
Added test for main_iteration() and fixed decorator to return the result.
33
class MainIterationTestCase(tests.TestCase):
34
35
    def test_main_iteration(self):
776.1.35 by Curtis Hovey
Verify that pending events are processed.
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
776.1.34 by Curtis Hovey
Added test for main_iteration() and fixed decorator to return the result.
45
        def test_func(self):
776.1.35 by Curtis Hovey
Verify that pending events are processed.
46
            button.emit('clicked')
776.1.34 by Curtis Hovey
Added test for main_iteration() and fixed decorator to return the result.
47
            return True
776.1.35 by Curtis Hovey
Verify that pending events are processed.
48
776.1.34 by Curtis Hovey
Added test for main_iteration() and fixed decorator to return the result.
49
        decorated_func = ui.main_iteration(test_func)
50
        result = decorated_func(object())
51
        self.assertIs(True, result)
776.1.35 by Curtis Hovey
Verify that pending events are processed.
52
        self.assertIs(False, Gtk.events_pending())
53
        self.assertEqual('after', button.props.label)
776.1.34 by Curtis Hovey
Added test for main_iteration() and fixed decorator to return the result.
54
55
776.1.15 by Curtis Hovey
Added test for PromptDialog.
56
class PromptDialogTestCase(tests.TestCase):
57
776.1.20 by Curtis Hovey
Added a test for tick().
58
    def test_init(self):
776.2.12 by Curtis Hovey
Fixed spelling, hushed lint, updated dummy cmd_build_i18n to actually run to tell
59
        # The text and buttons are created.
776.1.15 by Curtis Hovey
Added test for PromptDialog.
60
        dialog = ui.PromptDialog('test 123')
776.2.6 by Curtis Hovey
Change PromptDialog to extend GtkMessageDialog that provides icons and text handling for
61
        self.assertEqual('test 123', dialog.props.text)
62
        self.assertEqual(Gtk.MessageType.QUESTION, dialog.props.message_type)
776.1.16 by Curtis Hovey
Added a tests for the dialog setup.
63
        buttons = dialog.get_action_area().get_children()
776.2.6 by Curtis Hovey
Change PromptDialog to extend GtkMessageDialog that provides icons and text handling for
64
        self.assertEqual('gtk-yes', buttons[0].props.label)
65
        self.assertEqual('gtk-no', buttons[1].props.label)
776.1.17 by Curtis Hovey
Added a test for PasswordDialog.
66
67
776.2.7 by Curtis Hovey
Added InfoDialog and implemented show_message().
68
class InfoDialogTestCase(tests.TestCase):
69
70
    def test_init(self):
776.2.12 by Curtis Hovey
Fixed spelling, hushed lint, updated dummy cmd_build_i18n to actually run to tell
71
        # The text and buttons are created.
776.2.7 by Curtis Hovey
Added InfoDialog and implemented show_message().
72
        dialog = ui.InfoDialog('test 123')
73
        self.assertEqual('test 123', dialog.props.text)
74
        self.assertEqual(Gtk.MessageType.INFO, dialog.props.message_type)
75
        buttons = dialog.get_action_area().get_children()
76
        self.assertEqual('gtk-close', buttons[0].props.label)
77
78
776.2.8 by Curtis Hovey
Added WarningDialog and implementd show_warning.
79
class WarningDialogTestCase(tests.TestCase):
80
81
    def test_init(self):
776.2.12 by Curtis Hovey
Fixed spelling, hushed lint, updated dummy cmd_build_i18n to actually run to tell
82
        # The text and buttons are created.
776.2.8 by Curtis Hovey
Added WarningDialog and implementd show_warning.
83
        dialog = ui.WarningDialog('test 123')
84
        self.assertEqual('test 123', dialog.props.text)
85
        self.assertEqual(Gtk.MessageType.WARNING, dialog.props.message_type)
86
        buttons = dialog.get_action_area().get_children()
87
        self.assertEqual('gtk-close', buttons[0].props.label)
88
89
776.2.10 by Curtis Hovey
Added ErrorDialog and implented show_error.
90
class ErrorDialogTestCase(tests.TestCase):
91
92
    def test_init(self):
776.2.12 by Curtis Hovey
Fixed spelling, hushed lint, updated dummy cmd_build_i18n to actually run to tell
93
        # The text and buttons are created, then shown.
776.2.10 by Curtis Hovey
Added ErrorDialog and implented show_error.
94
        dialog = ui.ErrorDialog('test 123')
95
        self.assertEqual('test 123', dialog.props.text)
96
        self.assertEqual(Gtk.MessageType.ERROR, dialog.props.message_type)
97
        buttons = dialog.get_action_area().get_children()
98
        self.assertEqual('gtk-close', buttons[0].props.label)
99
100
776.1.17 by Curtis Hovey
Added a test for PasswordDialog.
101
class PasswordDialogTestCase(tests.TestCase):
102
776.1.20 by Curtis Hovey
Added a test for tick().
103
    def test_init(self):
776.1.19 by Curtis Hovey
Added comment about what the dialogs need to do.
104
        # The label, password entry, and buttons are created, then shown.
776.1.18 by Curtis Hovey
Added test for call to show_all because dialog.run() requires the that to be called first.
105
        MockMethod.bind(self, Gtk.Box, 'show_all')
776.1.17 by Curtis Hovey
Added a test for PasswordDialog.
106
        dialog = ui.PasswordDialog('test password')
776.1.18 by Curtis Hovey
Added test for call to show_all because dialog.run() requires the that to be called first.
107
        content_area = dialog.get_content_area()
108
        self.assertIs(True, dialog.get_content_area().show_all.called)
109
        widgets = content_area.get_children()
776.1.17 by Curtis Hovey
Added a test for PasswordDialog.
110
        self.assertEqual('test password', widgets[0].props.label)
111
        self.assertEqual(False, widgets[1].props.visibility)
112
        buttons = dialog.get_action_area().get_children()
113
        self.assertEqual('gtk-cancel', buttons[0].props.label)
114
        self.assertEqual(
776.1.18 by Curtis Hovey
Added test for call to show_all because dialog.run() requires the that to be called first.
115
            Gtk.ResponseType.CANCEL,
116
            dialog.get_response_for_widget(buttons[0]))
776.1.17 by Curtis Hovey
Added a test for PasswordDialog.
117
        self.assertEqual('gtk-ok', buttons[1].props.label)
118
        self.assertEqual(
119
            Gtk.ResponseType.OK,
120
            dialog.get_response_for_widget(buttons[1]))
776.1.20 by Curtis Hovey
Added a test for tick().
121
122
123
class GtkProgressBarTestCase(tests.TestCase):
124
125
    def test_init(self):
126
        progress_bar = ui.GtkProgressBar()
127
        self.assertEqual(0.0, progress_bar.props.fraction)
128
        self.assertIs(None, progress_bar.total)
129
        self.assertIs(None, progress_bar.current)
130
131
    def test_tick(self):
776.1.24 by Curtis Hovey
Added tests for finshed() and clear.
132
        # tick() shows the widget, does one pulse, then handles the pending
776.1.20 by Curtis Hovey
Added a test for tick().
133
        # events in the main loop.
134
        MockMethod.bind(self, ui.GtkProgressBar, 'show')
135
        MockMethod.bind(self, ui.GtkProgressBar, 'pulse')
136
        progress_bar = ui.GtkProgressBar()
137
        progress_bar.tick()
138
        self.assertIs(True, progress_bar.show.called)
776.1.24 by Curtis Hovey
Added tests for finshed() and clear.
139
        self.assertEqual('with_main_iteration', progress_bar.tick.__name__)
776.1.21 by Curtis Hovey
Added a test for the common case of update().
140
141
    def test_update_with_data(self):
776.1.24 by Curtis Hovey
Added tests for finshed() and clear.
142
        # update() shows the widget, sets the fraction, then handles the
143
        # pending events in the main loop.
776.1.21 by Curtis Hovey
Added a test for the common case of update().
144
        MockMethod.bind(self, ui.GtkProgressBar, 'show')
145
        progress_bar = ui.GtkProgressBar()
146
        progress_bar.update(msg='test', current_cnt=5, total_cnt=10)
147
        self.assertIs(True, progress_bar.show.called)
148
        self.assertEqual(0.5, progress_bar.props.fraction)
149
        self.assertEqual(10, progress_bar.total)
150
        self.assertEqual(5, progress_bar.current)
776.1.24 by Curtis Hovey
Added tests for finshed() and clear.
151
        self.assertEqual('with_main_iteration', progress_bar.update.__name__)
776.1.22 by Curtis Hovey
Added tests for partial and insane update() data.
152
153
    def test_update_without_data(self):
154
        progress_bar = ui.GtkProgressBar()
155
        progress_bar.update(current_cnt=5, total_cnt=None)
156
        self.assertEqual(0.0, progress_bar.props.fraction)
157
        self.assertIs(None, progress_bar.total)
158
        self.assertEqual(5, progress_bar.current)
159
160
    def test_update_with_insane_data(self):
161
        # The fraction must be between 0.0 and 1.0.
162
        progress_bar = ui.GtkProgressBar()
163
        self.assertRaises(
164
            ValueError, progress_bar.update, None, 20, 2)
776.1.24 by Curtis Hovey
Added tests for finshed() and clear.
165
166
    def test_finished(self):
167
        # finished() hides the widget, resets the state, then handles the
168
        # pending events in the main loop.
169
        MockMethod.bind(self, ui.GtkProgressBar, 'hide')
170
        progress_bar = ui.GtkProgressBar()
171
        progress_bar.finished()
172
        self.assertIs(True, progress_bar.hide.called)
173
        self.assertEqual(0.0, progress_bar.props.fraction)
174
        self.assertIs(None, progress_bar.total)
175
        self.assertIs(None, progress_bar.current)
176
        self.assertEqual('with_main_iteration', progress_bar.finished.__name__)
177
178
    def test_clear(self):
179
        # clear() is synonymous with finished.
180
        MockMethod.bind(self, ui.GtkProgressBar, 'finished')
181
        progress_bar = ui.GtkProgressBar()
182
        progress_bar.finished()
183
        self.assertIs(True, progress_bar.finished.called)
776.1.25 by Curtis Hovey
Added a test for ProgressBarWindow.
184
185
776.1.26 by Curtis Hovey
Added test for progress container tick().
186
class ProgressContainerMixin:
187
188
    def test_tick(self):
189
        progress_widget = self.progress_container()
190
        MockMethod.bind(self, progress_widget, 'show_all')
191
        MockMethod.bind(self, progress_widget.pb, 'tick')
192
        progress_widget.tick()
193
        self.assertIs(True, progress_widget.show_all.called)
194
        self.assertIs(True, progress_widget.pb.tick.called)
195
776.1.27 by Curtis Hovey
Added test for progress container update.
196
    def test_update(self):
197
        progress_widget = self.progress_container()
198
        MockMethod.bind(self, progress_widget, 'show_all')
199
        MockMethod.bind(self, progress_widget.pb, 'update')
200
        progress_widget.update('test', 5, 10)
201
        self.assertIs(True, progress_widget.show_all.called)
202
        self.assertIs(True, progress_widget.pb.update.called)
203
        self.assertEqual(
204
            ('test', 5, 10), progress_widget.pb.update.args)
205
776.1.28 by Curtis Hovey
Added test for progress container finished.
206
    def test_finished(self):
207
        progress_widget = self.progress_container()
776.1.29 by Curtis Hovey
Added test for progress container hide.
208
        MockMethod.bind(self, progress_widget, 'hide')
776.1.28 by Curtis Hovey
Added test for progress container finished.
209
        MockMethod.bind(self, progress_widget.pb, 'finished')
210
        progress_widget.finished()
776.1.29 by Curtis Hovey
Added test for progress container hide.
211
        self.assertIs(True, progress_widget.hide.called)
776.1.28 by Curtis Hovey
Added test for progress container finished.
212
        self.assertIs(True, progress_widget.pb.finished.called)
213
776.1.29 by Curtis Hovey
Added test for progress container hide.
214
    def test_clear(self):
215
        progress_widget = self.progress_container()
216
        MockMethod.bind(self, progress_widget, 'hide')
217
        MockMethod.bind(self, progress_widget.pb, 'clear')
218
        progress_widget.clear()
219
        self.assertIs(True, progress_widget.hide.called)
220
        self.assertIs(True, progress_widget.pb.clear.called)
221
776.1.26 by Curtis Hovey
Added test for progress container tick().
222
223
class ProgressBarWindowTestCase(ProgressContainerMixin, tests.TestCase):
224
225
    progress_container = ui.ProgressBarWindow
776.1.25 by Curtis Hovey
Added a test for ProgressBarWindow.
226
227
    def test_init(self):
228
        pb_window = ui.ProgressBarWindow()
229
        self.assertEqual('Progress', pb_window.props.title)
230
        self.assertEqual(
231
            Gtk.WindowPosition.CENTER_ALWAYS, pb_window.props.window_position)
232
        self.assertIsInstance(pb_window.pb, ui.GtkProgressBar)
776.1.31 by Curtis Hovey
Added a testcase for ProgressPanel.
233
234
235
class ProgressPanelTestCase(ProgressContainerMixin, tests.TestCase):
236
237
    progress_container = ui.ProgressPanel
238
239
    def test_init(self):
240
        pb_window = ui.ProgressPanel()
776.1.33 by Curtis Hovey
Updated ProgressPanel to Gtk 3.0 Gtk.Box.
241
        self.assertEqual(
242
            Gtk.Orientation.HORIZONTAL, pb_window.props.orientation)
243
        self.assertEqual(5, pb_window.props.spacing)
776.1.31 by Curtis Hovey
Added a testcase for ProgressPanel.
244
        self.assertIsInstance(pb_window.pb, ui.GtkProgressBar)
776.1.33 by Curtis Hovey
Updated ProgressPanel to Gtk 3.0 Gtk.Box.
245
        widgets = pb_window.get_children()
246
        # The image's stock and icon_name properties are always None?
247
        self.assertIsInstance(widgets[0], Gtk.Image)
776.2.9 by Curtis Hovey
Chnge test order to match the order of classes in the module under test.
248
249
250
class GtkUIFactoryTestCase(tests.TestCase):
251
252
    def test__init(self):
253
        ui_factory = ui.GtkUIFactory()
254
        self.assertIs(None, ui_factory._progress_bar_widget)
255
256
    def test_set_progress_bar_widget(self):
257
        ui_factory = ui.GtkUIFactory()
258
        progress_widget = ui.ProgressPanel()
259
        ui_factory.set_progress_bar_widget(progress_widget)
260
        self.assertIs(progress_widget, ui_factory._progress_bar_widget)
261
262
    def test_get_boolean_true(self):
263
        ui_factory = ui.GtkUIFactory()
264
        MockMethod.bind(self, ui.PromptDialog, 'run', Gtk.ResponseType.YES)
265
        boolean_value = ui_factory.get_boolean('test')
266
        self.assertIs(True, ui.PromptDialog.run.called)
267
        self.assertIs(True, boolean_value)
268
269
    def test_get_boolean_false(self):
270
        ui_factory = ui.GtkUIFactory()
271
        MockMethod.bind(self, ui.PromptDialog, 'run', Gtk.ResponseType.NO)
272
        boolean_value = ui_factory.get_boolean('test')
273
        self.assertIs(True, ui.PromptDialog.run.called)
274
        self.assertIs(False, boolean_value)
275
276
    def test_show_message(self):
277
        ui_factory = ui.GtkUIFactory()
278
        MockMethod.bind(self, ui.InfoDialog, 'run', Gtk.ResponseType.CLOSE)
279
        ui_factory.show_message('test')
280
        self.assertIs(True, ui.InfoDialog.run.called)
281
282
    def test_show_warning(self):
283
        ui_factory = ui.GtkUIFactory()
284
        MockMethod.bind(self, ui.WarningDialog, 'run', Gtk.ResponseType.CLOSE)
285
        ui_factory.show_warning('test')
286
        self.assertIs(True, ui.WarningDialog.run.called)
287
776.2.10 by Curtis Hovey
Added ErrorDialog and implented show_error.
288
    def test_show_Error(self):
289
        ui_factory = ui.GtkUIFactory()
290
        MockMethod.bind(self, ui.ErrorDialog, 'run', Gtk.ResponseType.CLOSE)
291
        ui_factory.show_error('test')
292
        self.assertIs(True, ui.ErrorDialog.run.called)
293
776.2.11 by Curtis Hovey
Added show_user_warning implementation.
294
    def test_show_user_warning(self):
295
        ui_factory = ui.GtkUIFactory()
296
        MockMethod.bind(self, ui.WarningDialog, 'run', Gtk.ResponseType.CLOSE)
297
        ui_factory.show_user_warning(
298
            'recommend_upgrade', current_format_name='1.0', basedir='./test')
299
        self.assertIs(True, ui.WarningDialog.run.called)
300
301
    def test_show_user_warning_supressed(self):
302
        ui_factory = ui.GtkUIFactory()
303
        ui_factory.suppressed_warnings.add('recommend_upgrade')
304
        MockMethod.bind(self, ui.WarningDialog, 'run', Gtk.ResponseType.CLOSE)
305
        ui_factory.show_user_warning(
306
            'recommend_upgrade', current_format_name='1.0', basedir='./test')
307
        self.assertIs(False, ui.WarningDialog.run.called)
308
776.2.9 by Curtis Hovey
Chnge test order to match the order of classes in the module under test.
309
    def test_get_password(self):
310
        ui_factory = ui.GtkUIFactory()
311
        MockMethod.bind(self, ui.PasswordDialog, 'run', Gtk.ResponseType.OK)
312
        mock_property = MockProperty.bind(
313
            self, ui.PasswordDialog, 'passwd', 'secret')
314
        password = ui_factory.get_password('test')
315
        self.assertIs(True, ui.PasswordDialog.run.called)
316
        self.assertIs(True, mock_property.called)
317
        self.assertEqual('secret', password)
318
319
    def test_progress_all_finished_with_widget(self):
320
        ui_factory = ui.GtkUIFactory()
321
        progress_widget = ui.ProgressPanel()
322
        MockMethod.bind(self, progress_widget, 'finished')
323
        ui_factory.set_progress_bar_widget(progress_widget)
324
        self.assertIs(None, ui_factory._progress_all_finished())
325
        self.assertIs(True, progress_widget.finished.called)
326
327
    def test_progress_all_finished_without_widget(self):
328
        ui_factory = ui.GtkUIFactory()
329
        self.assertIs(None, ui_factory._progress_all_finished())
330
331
    def test_progress_updated_with_widget(self):
332
        ui_factory = ui.GtkUIFactory()
333
        progress_widget = ui.ProgressPanel()
334
        MockMethod.bind(self, progress_widget, 'update')
335
        ui_factory.set_progress_bar_widget(progress_widget)
336
        task = ProgressTask()
337
        task.msg = 'test'
338
        task.current_cnt = 1
339
        task.total_cnt = 2
340
        self.assertIs(None, ui_factory._progress_updated(task))
341
        self.assertIs(True, progress_widget.update.called)
342
        self.assertEqual(
343
            ('test', 1, 2), progress_widget.update.args)
344
345
    def test_progress_updated_without_widget(self):
346
        ui_factory = ui.GtkUIFactory()
347
        MockMethod.bind(self, ui.ProgressBarWindow, 'update')
348
        task = ProgressTask()
349
        task.msg = 'test'
350
        task.current_cnt = 1
351
        task.total_cnt = 2
352
        self.assertIs(None, ui_factory._progress_updated(task))
353
        self.assertIsInstance(
354
            ui_factory._progress_bar_widget, ui.ProgressBarWindow)
355
        self.assertIs(True, ui_factory._progress_bar_widget.update.called)
356
        self.assertEqual(
357
            ('test', 1, 2), ui_factory._progress_bar_widget.update.args)
358
359
    def test_report_transport_activity_with_widget(self):
360
        ui_factory = ui.GtkUIFactory()
361
        progress_widget = ui.ProgressPanel()
362
        MockMethod.bind(self, progress_widget, 'tick')
363
        ui_factory.set_progress_bar_widget(progress_widget)
364
        self.assertIs(
365
            None, ui_factory.report_transport_activity(None, None, None))
366
        self.assertIs(True, progress_widget.tick.called)
367
368
    def test_report_transport_activity_without_widget(self):
369
        ui_factory = ui.GtkUIFactory()
370
        MockMethod.bind(self, ui.ProgressBarWindow, 'tick')
371
        self.assertIs(
372
            None, ui_factory.report_transport_activity(None, None, None))
373
        self.assertIsInstance(
374
            ui_factory._progress_bar_widget, ui.ProgressBarWindow)
375
        self.assertIs(True, ui.ProgressBarWindow.tick.called)