/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

  • Committer: Curtis Hovey
  • Date: 2012-02-27 22:07:36 UTC
  • mfrom: (776.2.11 ui-factory)
  • mto: This revision was merged to the branch mainline in revision 782.
  • Revision ID: sinzui.is@verizon.net-20120227220736-zjme9rpbk952hg23
Merged UI and test goodness.

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 PromptDialogTestCase(tests.TestCase):
 
57
 
 
58
    def test_init(self):
 
59
        # The text and buttons are created, then shown.
 
60
        dialog = ui.PromptDialog('test 123')
 
61
        self.assertEqual('test 123', dialog.props.text)
 
62
        self.assertEqual(Gtk.MessageType.QUESTION, dialog.props.message_type)
 
63
        buttons = dialog.get_action_area().get_children()
 
64
        self.assertEqual('gtk-yes', buttons[0].props.label)
 
65
        self.assertEqual('gtk-no', buttons[1].props.label)
 
66
 
 
67
 
 
68
class InfoDialogTestCase(tests.TestCase):
 
69
 
 
70
    def test_init(self):
 
71
        # The text and buttons are created, then shown.
 
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
 
 
79
class WarningDialogTestCase(tests.TestCase):
 
80
 
 
81
    def test_init(self):
 
82
        # The tezt and buttons are created, then shown.
 
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
 
 
90
class ErrorDialogTestCase(tests.TestCase):
 
91
 
 
92
    def test_init(self):
 
93
        # The tezt and buttons are created, then shown.
 
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
 
 
101
class PasswordDialogTestCase(tests.TestCase):
 
102
 
 
103
    def test_init(self):
 
104
        # The label, password entry, and buttons are created, then shown.
 
105
        MockMethod.bind(self, Gtk.Box, 'show_all')
 
106
        dialog = ui.PasswordDialog('test password')
 
107
        content_area = dialog.get_content_area()
 
108
        self.assertIs(True, dialog.get_content_area().show_all.called)
 
109
        widgets = content_area.get_children()
 
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(
 
115
            Gtk.ResponseType.CANCEL,
 
116
            dialog.get_response_for_widget(buttons[0]))
 
117
        self.assertEqual('gtk-ok', buttons[1].props.label)
 
118
        self.assertEqual(
 
119
            Gtk.ResponseType.OK,
 
120
            dialog.get_response_for_widget(buttons[1]))
 
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):
 
132
        # tick() shows the widget, does one pulse, then handles the pending
 
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)
 
139
        self.assertEqual('with_main_iteration', progress_bar.tick.__name__)
 
140
 
 
141
    def test_update_with_data(self):
 
142
        # update() shows the widget, sets the fraction, then handles the
 
143
        # pending events in the main loop.
 
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)
 
151
        self.assertEqual('with_main_iteration', progress_bar.update.__name__)
 
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)
 
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)
 
184
 
 
185
 
 
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
 
 
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
 
 
206
    def test_finished(self):
 
207
        progress_widget = self.progress_container()
 
208
        MockMethod.bind(self, progress_widget, 'hide')
 
209
        MockMethod.bind(self, progress_widget.pb, 'finished')
 
210
        progress_widget.finished()
 
211
        self.assertIs(True, progress_widget.hide.called)
 
212
        self.assertIs(True, progress_widget.pb.finished.called)
 
213
 
 
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
 
 
222
 
 
223
class ProgressBarWindowTestCase(ProgressContainerMixin, tests.TestCase):
 
224
 
 
225
    progress_container = ui.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)
 
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()
 
241
        self.assertEqual(
 
242
            Gtk.Orientation.HORIZONTAL, pb_window.props.orientation)
 
243
        self.assertEqual(5, pb_window.props.spacing)
 
244
        self.assertIsInstance(pb_window.pb, ui.GtkProgressBar)
 
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)
 
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
 
 
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
 
 
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
 
 
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)