File: test_xrc.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 (265 lines) | stat: -rw-r--r-- 10,128 bytes parent folder | download | duplicates (2)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import unittest
from unittests import wtc
import wx
import wx.xrc as xrc
import os

xrcFile = os.path.join(os.path.dirname(__file__), 'xrctest.xrc')

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

class xrc_Tests(wtc.WidgetTestCase):

    def checkXmlRes(self, xmlres):
        assert isinstance(xmlres, xrc.XmlResource)
        f = xmlres.LoadFrame(self.frame, 'MainFrame')
        self.assertNotEqual(f, None)
        f.Show()

        self.myYield()

        id = xrc.XRCID('MainPanel')
        self.assertTrue(id != -1)
        self.assertTrue(isinstance(id, int))

        ctrl = xrc.XRCCTRL(f, 'TitleText')
        self.assertTrue(ctrl is not None)
        self.assertTrue(isinstance(ctrl, wx.StaticText))


    def test_xrc1(self):
        xmlres = xrc.XmlResource(xrcFile)
        self.checkXmlRes(xmlres)

    def test_xrc2(self):
        xmlres = xrc.XmlResource()
        xmlres.LoadFile(xrcFile)
        self.checkXmlRes(xmlres)

    def test_xrc3(self):
        xmlres = xrc.XmlResource()
        with open(xrcFile, 'rb') as f:
            data = f.read()
        xmlres.LoadFromBuffer(data)
        self.checkXmlRes(xmlres)

    def test_xrc4(self):
        xmlres = xrc.XmlResource(xrcFile)
        p = xmlres.LoadObjectRecursively(self.frame, 'MainPanel', 'wxPanel')
        self.assertNotEqual(p, None)
        self.frame.SendSizeEvent()
        self.myYield()

    #---------------------------------------------------------------------------
    # Tests for custom handlers

    # This test does not allow for 2-phase create or creating the instance of
    # the resource before filling it with widgets or etc. See also the next
    # test and try to keep the two of them in sync as much as possible.
    def test_xrc5(self):
        resource = b'''<?xml version="1.0"?>
            <resource>
            <object class="wxFrame" name="MainFrame">
                <size>400,250</size>
                <title>This is a test</title>
                <!-- Notice that the class is NOT a standard wx class -->
                <object class="MyCustomPanel" name="MyPanel">
                    <size>200,100</size>
                    <object class="wxStaticText" name="label1">
                        <label>This panel is a custom class derived from wx.Panel,\nand is loaded by a custom XmlResourceHandler.</label>
                        <pos>10,10</pos>
                    </object>
                </object>
            </object>
            </resource>'''

        # this is the class that will be created for the resource
        class MyCustomPanel(wx.Panel):
            def __init__(self, parent, id, pos, size, style, name):
                wx.Panel.__init__(self, parent, id, pos, size, style, name)

                # This is the little bit of customization that we do for this
                # silly example.
                self.Bind(wx.EVT_SIZE, self.OnSize)
                t = wx.StaticText(self, -1, "MyCustomPanel")
                f = t.GetFont()
                f.SetWeight(wx.BOLD)
                f.SetPointSize(f.GetPointSize()+2)
                t.SetFont(f)
                self.t = t

            def OnSize(self, evt):
                sz = self.GetSize()
                w, h = self.t.GetTextExtent(self.t.GetLabel())
                self.t.SetPosition(((sz.width-w)//2, (sz.height-h)//2))


        # this is the handler class that will create the resource item
        class MyCustomPanelXmlHandler(xrc.XmlResourceHandler):
            def __init__(self):
                xrc.XmlResourceHandler.__init__(self)
                # Specify the styles recognized by objects of this type
                self.AddStyle("wxTAB_TRAVERSAL", wx.TAB_TRAVERSAL)
                self.AddStyle("wxWS_EX_VALIDATE_RECURSIVELY", wx.WS_EX_VALIDATE_RECURSIVELY)
                self.AddStyle("wxCLIP_CHILDREN", wx.CLIP_CHILDREN)
                self.AddWindowStyles()

            def CanHandle(self, node):
                return self.IsOfClass(node, "MyCustomPanel")

            def DoCreateResource(self):
                # Ensure that the instance hasn't been created yet (since
                # we're not using 2-phase create)
                assert self.GetInstance() is None

                # Now create the object
                panel = MyCustomPanel(self.GetParentAsWindow(),
                                      self.GetID(),
                                      self.GetPosition(),
                                      self.GetSize(),
                                      self.GetStyle("style", wx.TAB_TRAVERSAL),
                                      self.GetName()
                                      )
                self.SetupWindow(panel)
                self.CreateChildren(panel)
                return panel

        # now load it
        xmlres = xrc.XmlResource()
        xmlres.InsertHandler( MyCustomPanelXmlHandler() )
        success = xmlres.LoadFromBuffer(resource)

        f = xmlres.LoadFrame(self.frame, 'MainFrame')
        self.assertNotEqual(f, None)
        f.Show()
        self.myYield()

        panel = xrc.XRCCTRL(f, 'MyPanel')
        self.assertNotEqual(panel, None)
        self.assertTrue(isinstance(panel, MyCustomPanel))



    # This test shows how to do basically the same as above while still
    # allowing the instance to be created before loading the content.

    def test_xrc6(self):
        resource = b'''<?xml version="1.0"?>
            <resource>
            <object class="wxFrame" name="MainFrame">
                <size>400,250</size>
                <title>This is a test</title>
                <!-- Notice that the class is NOT a standard wx class -->
                <object class="MyCustomPanel" name="MyPanel">
                    <size>200,100</size>
                    <object class="wxStaticText" name="label1">
                        <label>This panel is a custom class derived from wx.Panel,\nand is loaded by a custom XmlResourceHandler.</label>
                        <pos>10,10</pos>
                    </object>
                </object>
            </object>
            </resource>'''

        # this is the class that will be created for the resource
        class MyCustomPanel(wx.Panel):
            def __init__(self):
                wx.Panel.__init__(self)  # create only the instance, not the widget

            def Create(self, parent, id, pos, size, style, name):
                wx.Panel.Create(self, parent, id, pos, size, style, name)
                self.Bind(wx.EVT_SIZE, self.OnSize)
                t = wx.StaticText(self, -1, "MyCustomPanel")
                f = t.GetFont()
                f.SetWeight(wx.BOLD)
                f.SetPointSize(f.GetPointSize()+2)
                t.SetFont(f)
                self.t = t

            def OnSize(self, evt):
                sz = self.GetSize()
                w, h = self.t.GetTextExtent(self.t.GetLabel())
                self.t.SetPosition(((sz.width-w)//2, (sz.height-h)//2))


        # this is the handler class that will create the resource item
        class MyCustomPanelXmlHandler(xrc.XmlResourceHandler):
            def __init__(self):
                xrc.XmlResourceHandler.__init__(self)
                # Specify the styles recognized by objects of this type
                self.AddStyle("wxTAB_TRAVERSAL", wx.TAB_TRAVERSAL)
                self.AddStyle("wxWS_EX_VALIDATE_RECURSIVELY", wx.WS_EX_VALIDATE_RECURSIVELY)
                self.AddStyle("wxCLIP_CHILDREN", wx.CLIP_CHILDREN)
                self.AddWindowStyles()

            def CanHandle(self, node):
                return self.IsOfClass(node, "MyCustomPanel")

            def DoCreateResource(self):
                panel = self.GetInstance()
                if panel is None:
                    # if not, then create the instance (but not the window)
                    panel = MyCustomPanel()

                # Now create the UI object
                panel.Create(self.GetParentAsWindow(),
                             self.GetID(),
                             self.GetPosition(),
                             self.GetSize(),
                             self.GetStyle("style", wx.TAB_TRAVERSAL),
                             self.GetName()
                             )
                self.SetupWindow(panel)
                self.CreateChildren(panel)
                return panel

        # now load it
        xmlres = xrc.XmlResource()
        xmlres.InsertHandler( MyCustomPanelXmlHandler() )
        success = xmlres.LoadFromBuffer(resource)

        f = xmlres.LoadFrame(self.frame, 'MainFrame')
        self.assertNotEqual(f, None)
        f.Show()
        self.myYield()

        panel = xrc.XRCCTRL(f, 'MyPanel')
        self.assertNotEqual(panel, None)
        self.assertTrue(isinstance(panel, MyCustomPanel))



    #---------------------------------------------------------------------------
    # Tests for the Subclass Factory
    def test_xrc7(self):
        resource = b'''<?xml version="1.0"?>
            <resource>
                <!-- Notice that the class IS a standard wx class and that a subclass is specified -->
                <object class="wxPanel" name="MyPanel" subclass="unittests.xrcfactorytest.MyCustomPanel">
                    <size>200,100</size>
                    <object class="wxStaticText" name="label1">
                        <label>This panel is a custom class derived from wx.Panel,\nand is loaded by the Python SubclassFactory.</label>
                        <pos>10,10</pos>
                    </object>
                </object>
            </resource>'''


        # now load it
        xmlres = xrc.XmlResource()
        success = xmlres.LoadFromBuffer(resource)

        panel = xmlres.LoadPanel(self.frame, "MyPanel")
        self.frame.SendSizeEvent()
        self.myYield()

        self.assertNotEqual(panel, None)
        from unittests import xrcfactorytest
        self.assertTrue(isinstance(panel, xrcfactorytest.MyCustomPanel))



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


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