"""
$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/ui/test/utest_browse.py $
$Id: utest_browse.py 31153 2008-09-18 15:09:10Z dbinger $
"""
from dulcinea.common import set_test_environment, CommonTest
from dulcinea.ui.browse import ArchiveDirectory, ImageArchiveDirectory
from dulcinea.ui.browse import IndexArchiveDirectory, format_browse_css
from dulcinea.ui.thumbnail import Image
from dulcinea.util import get_module_directory
from durus.utils import as_bytes
import dulcinea.common

def get_test_archive(type):
    test_directory = get_module_directory(dulcinea.ui) + '/test/'
    return test_directory + 'browse_test.%s' % type

def decorate (obj, content, title=None):
    return content

class ArchiveDirectoryTest (CommonTest):

    def _pre(self):
        CommonTest._pre(self)
        def nothing():
            return None
        dulcinea.common.header = nothing
        dulcinea.common.footer = nothing

    def test_misc(self):
        format_browse_css()

    def test_archive_directory_with_zip(self):
        archive_dir = ArchiveDirectory(get_test_archive('zip'),
                                       decorate=decorate)
        set_test_environment()
        assert archive_dir._q_translate('') == '_q_index'
        archive_dir()
        archive_dir._q_traverse(['browse_test', ''])
        archive_dir._q_traverse(['browse_test', 'trek.jpg'])
        archive_dir._q_traverse(['browse_test', 'a', 'trek.jpg'])
        contents = archive_dir._q_traverse(['browse_test', 'b', 'c', 'd.txt'])
        assert contents == as_bytes('Congratulations!\n')
        if Image is not None:
            set_test_environment(QUERY_STRING='40')
            archive_dir._q_traverse(['browse_test', 'a', 'trek.jpg'])

    def test_image_archive_directory_with_zip(self):
        archive_dir = ImageArchiveDirectory(get_test_archive('zip'))
        if Image is not None:
            set_test_environment(QUERY_STRING='40')
            archive_dir._q_traverse(['browse_test', 'a', 'trek.jpg'])

    def test_image_archive_directory_with_tgz(self):
        archive_dir = ImageArchiveDirectory(get_test_archive('tgz'))
        set_test_environment()
        assert archive_dir._q_translate('') == '_q_index'
        archive_dir._q_traverse(['browse_test', ''])
        archive_dir._q_traverse(['browse_test', 'trek.jpg'])
        archive_dir._q_traverse(['browse_test', 'a', 'trek.jpg'])
        fs = archive_dir._q_traverse(['browse_test', 'b', 'c', 'd.txt'])
        contents = [chunk for chunk in fs]
        assert contents == [as_bytes('Congratulations!\n')]
        if Image is not None:
            set_test_environment(QUERY_STRING='40')
            archive_dir._q_traverse(['browse_test', 'a', 'trek.jpg'])

    def test_index_archive_directory_with_tar(self):
        archive_dir = IndexArchiveDirectory(get_test_archive('tar'))
        set_test_environment()
        assert archive_dir._q_translate('') == '_q_index'
        archive_dir._q_traverse(['browse_test', ''])
        archive_dir._q_traverse(['browse_test', 'trek.jpg'])
        archive_dir._q_traverse(['browse_test', 'a', 'trek.jpg'])
        fs = archive_dir._q_traverse(['browse_test', 'b', 'c', 'd.txt'])
        contents = [chunk for chunk in fs]
        assert contents == [as_bytes('Congratulations!\n')]

if __name__ == "__main__":
    ArchiveDirectoryTest()
