1
# Copyright (C) 2006 by Canonical Ltd
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.
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.
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
17
from StringIO import StringIO
19
from bzrlib.progress import (
20
DummyProgress, ChildProgress,
25
from bzrlib.tests import TestCase
28
def __init__(self, top):
34
class InstrumentedProgress(TTYProgressBar):
35
"""TTYProgress variant that tracks outcomes"""
37
def __init__(self, *args, **kwargs):
38
self.always_throttled = True
39
TTYProgressBar.__init__(self, *args, **kwargs)
41
def throttle(self, old_message):
42
result = TTYProgressBar.throttle(self, old_message)
44
self.always_throttled = False
47
class TestProgress(TestCase):
50
self.top = ChildProgress(_stack=FakeStack(q))
52
def test_propogation(self):
53
self.top.update('foobles', 1, 2)
54
self.assertEqual(self.top.message, 'foobles')
55
self.assertEqual(self.top.current, 1)
56
self.assertEqual(self.top.total, 2)
57
self.assertEqual(self.top.child_fraction, 0)
58
child = ChildProgress(_stack=FakeStack(self.top))
59
child.update('baubles', 2, 4)
60
self.assertEqual(self.top.message, 'foobles')
61
self.assertEqual(self.top.current, 1)
62
self.assertEqual(self.top.total, 2)
63
self.assertEqual(self.top.child_fraction, 0.5)
64
grandchild = ChildProgress(_stack=FakeStack(child))
65
grandchild.update('barbells', 1, 2)
66
self.assertEqual(self.top.child_fraction, 0.625)
67
self.assertEqual(child.child_fraction, 0.5)
68
child.update('baubles', 3, 4)
69
self.assertEqual(child.child_fraction, 0)
70
self.assertEqual(self.top.child_fraction, 0.75)
71
grandchild.update('barbells', 1, 2)
72
self.assertEqual(self.top.child_fraction, 0.875)
73
grandchild.update('barbells', 2, 2)
74
self.assertEqual(self.top.child_fraction, 1)
75
child.update('baubles', 4, 4)
76
self.assertEqual(self.top.child_fraction, 1)
78
grandchild.update('barbells', 2, 2)
79
self.assertEqual(self.top.child_fraction, 1)
81
def test_implementations(self):
82
for implementation in (TTYProgressBar, DotsProgressBar,
84
self.check_parent_handling(implementation)
86
def check_parent_handling(self, parentclass):
87
top = parentclass(to_file=StringIO())
88
top.update('foobles', 1, 2)
89
child = ChildProgress(_stack=FakeStack(top))
90
child.update('baubles', 4, 4)
91
top.update('lala', 2, 2)
92
child.update('baubles', 4, 4)
94
def test_stacking(self):
95
self.check_stack(TTYProgressBar, ChildProgress)
96
self.check_stack(DotsProgressBar, ChildProgress)
97
self.check_stack(DummyProgress, DummyProgress)
99
def check_stack(self, parent_class, child_class):
100
stack = ProgressBarStack(klass=parent_class, to_file=StringIO())
101
parent = stack.get_nested()
103
self.assertIs(parent.__class__, parent_class)
104
child = stack.get_nested()
106
self.assertIs(child.__class__, child_class)
112
def test_throttling(self):
113
pb = InstrumentedProgress(to_file=StringIO())
114
# instantaneous updates should be squelched
115
pb.update('me', 1, 1)
116
self.assertTrue(pb.always_throttled)
117
pb = InstrumentedProgress(to_file=StringIO())
118
# It's like an instant sleep(1)!
120
# Updates after a second should not be squelched
121
pb.update('me', 1, 1)
122
self.assertFalse(pb.always_throttled)