1#!/usr/bin/python
2# Copyright (C) 2010 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8#     * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10#     * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14#     * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30"""Unit tests for metered_stream.py."""
31
32import os
33import optparse
34import pdb
35import sys
36import unittest
37
38from webkitpy.common.array_stream import ArrayStream
39from webkitpy.layout_tests.layout_package import metered_stream
40
41
42class TestMeteredStream(unittest.TestCase):
43    def test_regular(self):
44        a = ArrayStream()
45        m = metered_stream.MeteredStream(verbose=False, stream=a)
46        self.assertTrue(a.empty())
47
48        # basic test - note that the flush() is a no-op, but we include it
49        # for coverage.
50        m.write("foo")
51        m.flush()
52        exp = ['foo']
53        self.assertEquals(a.get(), exp)
54
55        # now check that a second write() does not overwrite the first.
56        m.write("bar")
57        exp.append('bar')
58        self.assertEquals(a.get(), exp)
59
60        m.update("batter")
61        exp.append('batter')
62        self.assertEquals(a.get(), exp)
63
64        # The next update() should overwrite the laste update() but not the
65        # other text. Note that the cursor is effectively positioned at the
66        # end of 'foo', even though we had to erase three more characters.
67        m.update("foo")
68        exp.append('\b\b\b\b\b\b      \b\b\b\b\b\b')
69        exp.append('foo')
70        self.assertEquals(a.get(), exp)
71
72        m.progress("progress")
73        exp.append('\b\b\b   \b\b\b')
74        exp.append('progress')
75        self.assertEquals(a.get(), exp)
76
77        # now check that a write() does overwrite the progress bar
78        m.write("foo")
79        exp.append('\b\b\b\b\b\b\b\b        \b\b\b\b\b\b\b\b')
80        exp.append('foo')
81        self.assertEquals(a.get(), exp)
82
83        # Now test that we only back up to the most recent newline.
84
85        # Note also that we do not back up to erase the most recent write(),
86        # i.e., write()s do not get erased.
87        a.reset()
88        m.update("foo\nbar")
89        m.update("baz")
90        self.assertEquals(a.get(), ['foo\nbar', '\b\b\b   \b\b\b', 'baz'])
91
92    def test_verbose(self):
93        a = ArrayStream()
94        m = metered_stream.MeteredStream(verbose=True, stream=a)
95        self.assertTrue(a.empty())
96        m.write("foo")
97        self.assertEquals(a.get(), ['foo'])
98
99        import logging
100        b = ArrayStream()
101        logger = logging.getLogger()
102        handler = logging.StreamHandler(b)
103        logger.addHandler(handler)
104        m.update("bar")
105        logger.handlers.remove(handler)
106        self.assertEquals(a.get(), ['foo'])
107        self.assertEquals(b.get(), ['bar\n'])
108
109        m.progress("dropped")
110        self.assertEquals(a.get(), ['foo'])
111        self.assertEquals(b.get(), ['bar\n'])
112
113
114if __name__ == '__main__':
115    unittest.main()
116