File: test_vlbox.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (128 lines) | stat: -rw-r--r-- 3,924 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import unittest
from unittests import wtc
import wx

#---------------------------------------------------------------------------

class vlbox_Tests(wtc.WidgetTestCase):

    def test_vlbox1(self):
        with self.assertRaises(TypeError):
            lb = wx.VListBox()

    def test_vlbox2(self):
        with self.assertRaises(TypeError):
            lb = wx.VListBox(self.frame)


    def test_vlbox3(self):
        panel = wx.Panel(self.frame)
        self.frame.SendSizeEvent()

        lb = MyVListBox(panel, pos=(10,10), size=(100,150), style=wx.BORDER_THEME)
        lb.data = ['zero', 'one two', 'three four', 'five six', 'seven eight', 'nine ten']
        lb.SetItemCount(len(lb.data))
        self.waitFor(100)

        # check the ItemCount property
        self.assertEqual(len(lb.data), lb.ItemCount)

        # check that the overridden virtuals were called
        self.assertTrue(len(lb.drawItemCalls) > 0)
        self.assertTrue(len(lb.drawBackgroundCalls) > 0)
        self.assertTrue(len(lb.drawSeparatorCalls) > 0)
        self.assertTrue(len(lb.measureItemCalls) > 0)

        lb.SetSelection(2)
        self.waitFor(50)
        self.assertEqual(lb.GetSelectedCount(), 1)
        self.assertEqual(lb.GetSelection(), 2)
        self.assertTrue(lb.IsSelected(2))
        self.assertFalse(lb.IsSelected(3))



    def test_vlbox4(self):
        panel = wx.Panel(self.frame)
        self.frame.SendSizeEvent()

        lb = MyVListBox(panel, pos=(10,10), size=(100,150),
                        style=wx.BORDER_SIMPLE|wx.LB_MULTIPLE)
        lb.data = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
        lb.SetItemCount(len(lb.data))
        self.waitFor(50)

        lb.Select(2)
        lb.Select(5)
        lb.Select(7)
        lb.Select(8)
        self.waitFor(50)

        self.assertTrue(lb.IsSelected(2))
        self.assertTrue(lb.IsSelected(8))
        self.assertFalse(lb.IsSelected(3))

        sel = list()
        idx, cookie = lb.GetFirstSelected()
        while idx != wx.NOT_FOUND:
            sel.append(idx)
            idx, cookie = lb.GetNextSelected(cookie)

        self.assertEqual(sel, [2,5,7,8])



class MyVListBox(wx.VListBox):
    def __init__(self, *args, **kw):
        wx.VListBox.__init__(self, *args, **kw)
        self.data = list()
        self.drawItemCalls = list()
        self.drawBackgroundCalls = list()
        self.drawSeparatorCalls = list()
        self.measureItemCalls = list()

    # overridable methods
    def OnDrawItem(self, dc, rect, idx):
        self.drawItemCalls.append(idx)
        assert isinstance(dc, wx.DC)
        color = 'black'
        if self.IsSelected(idx):
            color = 'white'
        dc.SetTextForeground(color)
        dc.DrawLabel(self.data[idx], rect)

    def OnDrawBackground(self, dc, rect, idx):
        self.drawBackgroundCalls.append(idx)
        assert isinstance(dc, wx.DC)
        color = 'white'
        if self.IsSelected(idx):
            color = self.GetSelectionBackground()
            if not color.IsOk():
                color = 'navy'
        dc.SetPen(wx.Pen(color, 1))
        dc.SetBrush(wx.Brush(color))
        dc.DrawRectangle(rect)

    def OnDrawSeparator(self, dc, rect, idx):
        self.drawSeparatorCalls.append(idx)
        if idx == 0:
            return
        assert isinstance(dc, wx.DC)
        dc.SetPen(wx.Pen('#c0c0c0', 1, wx.PENSTYLE_DOT_DASH))
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
        assert isinstance(rect, wx.Rect)
        dc.DrawLine(rect.x, rect.y, rect.right, rect.y)
        rect.y += 1
        rect.height -= 2

    def OnMeasureItem(self, idx):
        self.measureItemCalls.append(idx)
        #dc = wx.ClientDC(self)
        w, h = self.GetTextExtent(self.data[idx])
        return h + 6


#---------------------------------------------------------------------------

if __name__ == '__main__':
    unittest.main()