###############################################################################
# File:        UnitTest_pgTools.py
# Appointment: unittests pgTools
# Project:     
# Author:      AR
# History:
# 27.10.2013 Created
###############################################################################
import unittest
import sys
import pygame
import threading

sys.path.append("../")
import pgTools
import zx

###############################################################################
class Test_pgTools(unittest.TestCase):
    def setUp(self):
        pass

    #//////////////////////////////////////////////////////////////////////////
    # BlurImage tests
    #//////////////////////////////////////////////////////////////////////////
    
    #--------------------------------------------------------------------------
    def test_BlurImage(self):
        # BlurImage with None returns None
        self.assertTrue(pgTools.BlurImage(None) is None)

        # BlurImage with not None succeeds
        image = pygame.Surface((5, 5))
        self.assertTrue(pgTools.BlurImage(image) is not None)

        # BlurImage with delay
        self.assertTrue(pgTools.BlurImage(image, zx.CDelay(0.00001)) is not None)

        # BlurImage with cancelled delay - fails
        delay = zx.CDelay(0.00001)
        delay.Cancel()
        self.assertTrue(pgTools.BlurImage(image, delay) is None)

    #--------------------------------------------------------------------------
    def test_BlurImage_async(self):
        # Thread to blur inmage
        class TestThread(threading.Thread):
            def __init__(self, aDelay):
                threading.Thread.__init__(self)
                self.Image = None
                self.Delay = aDelay

            def run(self):
                print 'generating...'
                self.Image = pgTools.BlurImage(pygame.Surface((5, 5)), self.Delay)
                print 'generating...ok'
                
        # Thread with a small delay
        t1 = TestThread(zx.CDelay(0.00001))
        # Thread with a big delay
        t2 = TestThread(zx.CDelay(10))
        # Run threads and cancel the 2nd one
        t1.start()
        t2.start()
        t2.Delay.Cancel()
        t1.join()
        t2.join()
        self.assertTrue(t1.Image is not None)
        self.assertTrue(t2.Image is None) # because cancelled
        


################################################################################
if __name__ == "__main__":
    unittest.main()
