###############################################################################
# File:        UnitTest_wxTools.py
# Appointment: test wx tools
# Project:     
# Author:      AR
# History:
# 11.12.2014 Created
###############################################################################
import unittest
import sys
import wx

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

###############################################################################
class Test_CColDataBuilder(unittest.TestCase):
    #--------------------------------------------------------------------------
    def test_Empty(self):
        b = wxTools.CColDataBuilder()
        self.assertEqual(0, b.GetColumnsCount())
        self.assertEqual(0, b.GetRowsCount())

        self.assertIsNone(b.GetColumnDesc(0))
        self.assertIsNone(b.GetColumnDesc(1))
        self.assertIsNone(b.GetColumnDesc(-1))

        self.assertIsNone(b.GetValue(0, 0))
        self.assertIsNone(b.GetValue(1, 1))
        self.assertIsNone(b.GetValue(-1, -1))


    #--------------------------------------------------------------------------
    def test_AddingColumns(self):
        b = wxTools.CColDataBuilder()
        
        # Add columns
        b.AddColumn("c1", lambda val: val[0], wx.LIST_FORMAT_LEFT, True);
        b.AddColumn("c2", lambda val: val[1], wx.LIST_FORMAT_RIGHT, False);
        b.AddColumn("c3", lambda val: val[0], wx.LIST_FORMAT_CENTER);

        self.assertEqual(3, b.GetColumnsCount())
        
        # Check columns
        c = b.GetColumnDesc(0)
        self.assertIsNotNone(c)
        self.assertEqual("c1", c.Title)
        self.assertEqual(wx.LIST_FORMAT_LEFT, c.Align)
        self.assertEqual(True, c.IsNumeric)
        self.assertEqual(0, c.MaxWidth) # not set yet

        c = b.GetColumnDesc(1)
        self.assertIsNotNone(c)
        self.assertEqual("c2", c.Title)
        self.assertEqual(wx.LIST_FORMAT_RIGHT, c.Align)
        self.assertEqual(False, c.IsNumeric)
        self.assertEqual(0, c.MaxWidth)
        
        c = b.GetColumnDesc(2)
        self.assertIsNotNone(c)
        self.assertEqual("c3", c.Title)
        self.assertEqual(wx.LIST_FORMAT_CENTER, c.Align)
        self.assertEqual(False, c.IsNumeric)
        self.assertEqual(0, c.MaxWidth)

    #--------------------------------------------------------------------------
    def test_AddingColumnsWithTheSameName(self):
        b = wxTools.CColDataBuilder()
        
        # Add columns
        b.AddColumn("c1", lambda val: val[0], wx.LIST_FORMAT_LEFT, True);
        b.AddColumn("c1", lambda val: val[1], wx.LIST_FORMAT_RIGHT, False);

        self.assertEqual(2, b.GetColumnsCount())
        
        # Check columns
        c = b.GetColumnDesc(0)
        self.assertIsNotNone(c)
        self.assertEqual("c1", c.Title)
        self.assertEqual(wx.LIST_FORMAT_LEFT, c.Align)
        self.assertEqual(True, c.IsNumeric)
        self.assertEqual(0, c.MaxWidth) # not set yet

        c = b.GetColumnDesc(1)
        self.assertIsNotNone(c)
        self.assertEqual("c1", c.Title)
        self.assertEqual(wx.LIST_FORMAT_RIGHT, c.Align)
        self.assertEqual(False, c.IsNumeric)
        self.assertEqual(0, c.MaxWidth) # not set yet

    #--------------------------------------------------------------------------
    def test_AddingRowsAndCheckingData(self):
        b = wxTools.CColDataBuilder()
        
        # Add columns
        b.AddColumn("c1", lambda val: val[0], wx.LIST_FORMAT_LEFT, True);
        b.AddColumn("c2", lambda val: val[1], wx.LIST_FORMAT_RIGHT, False);
        b.AddColumn("c3", lambda val: val[2], wx.LIST_FORMAT_CENTER);

        self.assertEqual(3, b.GetColumnsCount())
        self.assertEqual(0, b.GetRowsCount()) # no rows yet
        
        # Prepare data
        testData = [(1,2,3), (4,5,6)] # some dummy data
        b.Prepare(testData)
        self.assertEqual(2, b.GetRowsCount()) # now should be 2
        
        # Check data
        # 1st row
        self.assertEqual(1, b.GetValue(0, 0))
        self.assertEqual(2, b.GetValue(0, 1))
        self.assertEqual(3, b.GetValue(0, 2))
        # 2nd row
        self.assertEqual(4, b.GetValue(1, 0))
        self.assertEqual(5, b.GetValue(1, 1))
        self.assertEqual(6, b.GetValue(1, 2))
        # bads
        self.assertIsNone(b.GetValue(-1, 0))
        self.assertIsNone(b.GetValue(1, -1))
        self.assertIsNone(b.GetValue(0, 10))
        self.assertIsNone(b.GetValue(10, 0))

    #--------------------------------------------------------------------------
    def test_CWindowsCollection(self):
        wc = wxTools.CWindowsCollection()
        self.assertEqual(0, len(wc.GetCreators()))

        # Add a simple window
        w1 = wc.RegisterAndShow(lambda: wx.Frame(parent = None, title = "1"))
        self.assertEqual(1, len(wc.GetCreators()))
        self.assertEqual(w1.GetTitle(), wc.FindWindow("1").GetTitle())

        # Add window with the same name - nothing changed
        wc.RegisterAndShow(lambda: wx.Frame(parent = None, title = "1"))
        self.assertEqual(1, len(wc.GetCreators()))

        # Add another window
        w2 = wc.RegisterAndShow(lambda: wx.Frame(parent = None, title = "2"))
        self.assertEqual(2, len(wc.GetCreators()))
        self.assertEqual(w2.GetTitle(), wc.FindWindow("2").GetTitle())

        # Unregister window 1
        wc.Unregister(w1)
        self.assertEqual(1, len(wc.GetCreators()))
        self.assertIsNone(wc.FindWindow("1"))

        # Close All
        w1 = wc.RegisterAndShow(lambda: wx.Frame(parent = None, title = "1"))
        self.assertEqual(2, len(wc.GetCreators()))
        wc.CloseAll()
        self.assertEqual(0, len(wc.GetCreators()))

        # Recreate
        w1 = wc.RegisterAndShow(lambda: wx.Frame(parent = None, title = "1"))
        self.assertEqual(1, len(wc.GetCreators()))
        wc.RegisterAndShow(lambda: wx.Frame(parent = None, title = "1"))
        wc.RegisterAndShow(lambda: wx.Frame(parent = None, title = "2"))
        col = wc.GetCreators()
        self.assertEqual(2, len(col))
        wc.CloseAll()
        self.assertEqual(0, len(wc.GetCreators()))
        self.assertEqual(2, len(col)) # separate collection
        wc.ReCreate(col)
        self.assertEqual(2, len(wc.GetCreators()))
        self.assertIsNotNone(wc.FindWindow("1"))
        self.assertIsNotNone(wc.FindWindow("2"))

        # Bad creator
        self.assertIsNone(wc.RegisterAndShow(w1))
        self.assertIsNone(wc.RegisterAndShow(None))
        self.assertIsNone(wc.RegisterAndShow(123))
        self.assertEqual(2, len(wc.GetCreators())) # not changed


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