#!/www/python/bin/python

#
# Test script for codecs.py
#

__revision__ = "$Id: test_codecs.py,v 1.5 2002/02/21 20:27:17 akuchlin Exp $"

import os
from PIL import Image

from sancho.unittest import TestScenario, parse_args, run_scenarios
from mems.instrument import protocols
from mems.instrument.server import codecs

tested_modules = [ "mems.instrument.server.codecs" ]

class CodecTest (TestScenario):

    def setup (self):
        path = os.path.dirname(codecs.__file__)
        filename = os.path.join(path, 'test/uscope-image.jpg')
        self.image = Image.open(filename)

    def shutdown (self):
        del self.image


    def check_null (self):
        "Test the null codec: 9"

        self.test_stmt('codecs.NullSerializer(self.image)')
        self.test_stmt('codecs.NullUnserializer()')
        ser = codecs.NullSerializer(self.image)
        unser = codecs.NullUnserializer()

        # Try getting a few packets
        self.test_stmt('ser.next()')
        self.test_stmt('ser.next()')

        # Try assembling them
        packet = ser.next()
        fmt, data, keywords = protocols.parse_packet(packet)
        self.test_val('unser.num_frames', 0)
        self.test_stmt('unser.process(data, **keywords)')
        self.test_val('unser.num_frames', 1)
        packet = ser.next()
        fmt, data, keywords = protocols.parse_packet(packet)
        self.test_stmt('unser.process(data, **keywords)')
        
        for i in range(5):
            packet = ser.next()

        # Set a new image
        self.test_stmt('ser.set_image(self.image)')
        
        for i in range(5):
            packet = ser.next()


    def check_tiling (self):
        "Test the tiling codec: 9"

        self.test_stmt('codecs.TiledSerializer(self.image)')
        self.test_stmt('codecs.TiledUnserializer()')
        ser = codecs.TiledSerializer(self.image)
        unser = codecs.TiledUnserializer()

        # Try getting a few packets
        self.test_stmt('ser.next()')
        self.test_stmt('ser.next()')

        # Try assembling them
        packet = ser.next()
        fmt, data, keywords = protocols.parse_packet(packet)
        self.test_val('unser.num_frames', 0)
        self.test_stmt('unser.process(data, **keywords)')
        self.test_val('unser.num_frames', 1)
        packet = ser.next()
        fmt, data, keywords = protocols.parse_packet(packet)
        self.test_stmt('unser.process(data, **keywords)')

        
        for i in range(100):
            packet = ser.next()

        # Set a new image
        self.test_stmt('ser.set_image(self.image)')
        
        for i in range(100):
            packet = ser.next()


if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios(scenarios, options)
