Fix crash when auto-sizing a wxDataViewCtrl column.
[wxWidgets.git] / tests / xml / xrctest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/xml/xrctest.cpp
3 // Purpose: XRC classes unit test
4 // Author: wxWidgets team
5 // Created: 2010-10-30
6 // Copyright: (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ----------------------------------------------------------------------------
10 // headers
11 // ----------------------------------------------------------------------------
12
13 #include "testprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif // WX_PRECOMP
22
23 #include "wx/xml/xml.h"
24 #include "wx/sstream.h"
25 #include "wx/wfstream.h"
26 #include "wx/xrc/xmlres.h"
27
28 #include <stdarg.h>
29
30 // ----------------------------------------------------------------------------
31 // helpers to create/save some xrc
32 // ----------------------------------------------------------------------------
33
34 namespace
35 {
36
37 static const char *TEST_XRC_FILE = "test.xrc";
38
39 // I'm hard-wiring the xrc into this function for now
40 // If different xrcs are wanted for future tests, it'll be easy to refactor
41 void CreateXrc()
42 {
43 const char *xrcText =
44 "<?xml version=\"1.0\" ?>"
45 "<resource>"
46 " <object class=\"wxDialog\" name=\"dialog\">"
47 " <object class=\"wxBoxSizer\">"
48 " <orient>wxVERTICAL</orient>"
49 " <object class=\"sizeritem\">"
50 " <object class=\"wxPanel\" name=\"panel1\">"
51 " <object class=\"wxBoxSizer\">"
52 " <object class=\"sizeritem\">"
53 " <object class=\"wxBoxSizer\">"
54 " <orient>wxVERTICAL</orient>"
55 " <object class=\"sizeritem\">"
56 " <object class=\"wxButton\" name=\"FirstCol[0]\">"
57 " <label>0</label>"
58 " </object>"
59 " </object>"
60 " <object class=\"sizeritem\">"
61 " <object class=\"wxButton\" name=\"FirstCol[1]\">"
62 " <label>1</label>"
63 " </object>"
64 " </object>"
65 " <object class=\"sizeritem\">"
66 " <object class=\"wxButton\" name=\"FirstCol[2]\">"
67 " <label>2</label>"
68 " </object>"
69 " </object>"
70 " <object class=\"sizeritem\">"
71 " <object class=\"wxButton\" name=\"FirstCol[3]\">"
72 " <label>3</label>"
73 " </object>"
74 " </object>"
75 " </object>"
76 " </object>"
77 " <object class=\"sizeritem\">"
78 " <object class=\"wxBoxSizer\">"
79 " <orient>wxVERTICAL</orient>"
80 " <object class=\"sizeritem\">"
81 " <object class=\"wxButton\" name=\"SecondCol[start]\">"
82 " <label>0</label>"
83 " </object>"
84 " </object>"
85 " <object class=\"sizeritem\">"
86 " <object class=\"wxButton\" name=\"SecondCol[1]\">"
87 " <label>1</label>"
88 " </object>"
89 " </object>"
90 " <object class=\"sizeritem\">"
91 " <object class=\"wxButton\" name=\"SecondCol[2]\">"
92 " <label>2</label>"
93 " </object>"
94 " </object>"
95 " <object class=\"sizeritem\">"
96 " <object class=\"wxButton\" name=\"SecondCol[end]\">"
97 " <label>3</label>"
98 " </object>"
99 " </object>"
100 " </object>"
101 " </object>"
102 " <orient>wxHORIZONTAL</orient>"
103 " </object>"
104 " </object>"
105 " </object>"
106 " <object class=\"sizeritem\">"
107 " <object class=\"wxPanel\" name=\"ref_of_panel1\">"
108 " <object_ref ref=\"panel1\"/>"
109 " </object>"
110 " </object>"
111 " </object>"
112 " <title>test</title>"
113 " </object>"
114 " <ids-range name=\"FirstCol\" size=\"2\" start=\"10000\"/>"
115 " <ids-range name=\"SecondCol\" size=\"100\" />"
116 "</resource>"
117 ;
118
119 // afaict there's no elegant way to load xrc direct from a string
120 // So save it as a file, from which it can be loaded
121 wxStringInputStream sis(xrcText);
122 wxFFileOutputStream fos(TEST_XRC_FILE);
123 CPPUNIT_ASSERT(fos.IsOk());
124 fos.Write(sis);
125 CPPUNIT_ASSERT(fos.Close());
126 }
127
128 } // anon namespace
129
130
131 // ----------------------------------------------------------------------------
132 // test class
133 // ----------------------------------------------------------------------------
134
135 class XrcTestCase : public CppUnit::TestCase
136 {
137 public:
138 XrcTestCase() {}
139
140 virtual void setUp() { CreateXrc(); }
141 virtual void tearDown() { wxRemoveFile(TEST_XRC_FILE); }
142
143 private:
144 CPPUNIT_TEST_SUITE( XrcTestCase );
145 CPPUNIT_TEST( ObjectReferences );
146 CPPUNIT_TEST( IDRanges );
147 CPPUNIT_TEST_SUITE_END();
148
149 void ObjectReferences();
150 void IDRanges();
151
152 DECLARE_NO_COPY_CLASS(XrcTestCase)
153 };
154
155 // register in the unnamed registry so that these tests are run by default
156 CPPUNIT_TEST_SUITE_REGISTRATION( XrcTestCase );
157
158 // also include in its own registry so that these tests can be run alone
159 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XrcTestCase, "XrcTestCase" );
160
161
162
163 void XrcTestCase::ObjectReferences()
164 {
165 wxXmlResource::Get()->InitAllHandlers();
166
167 for ( int n = 0; n < 2; ++n )
168 {
169 // Load the xrc file we're just created
170 CPPUNIT_ASSERT( wxXmlResource::Get()->Load(TEST_XRC_FILE) );
171
172 // In xrc there's now a dialog containing two panels, one an object
173 // reference of the other
174 wxDialog dlg;
175 CPPUNIT_ASSERT( wxXmlResource::Get()->LoadDialog(&dlg, NULL, "dialog") );
176 // Might as well test XRCCTRL too
177 wxPanel* panel1 = XRCCTRL(dlg,"panel1",wxPanel);
178 wxPanel* panel2 = XRCCTRL(dlg,"ref_of_panel1",wxPanel);
179 // Check that the object reference panel is a different object
180 CPPUNIT_ASSERT( panel2 != panel1 );
181
182 // Unload the xrc, so it can be reloaded and the test rerun
183 CPPUNIT_ASSERT( wxXmlResource::Get()->Unload(TEST_XRC_FILE) );
184 }
185 }
186
187 void XrcTestCase::IDRanges()
188 {
189 // Tests ID ranges
190 for ( int n = 0; n < 2; ++n )
191 {
192 // Load the xrc file we're just created
193 CPPUNIT_ASSERT( wxXmlResource::Get()->Load(TEST_XRC_FILE) );
194
195 // foo[start] should == foo[0]
196 CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[start]"), XRCID("SecondCol[0]") );
197 // foo[start] should be < foo[end]. Usually that means more negative
198 CPPUNIT_ASSERT( XRCID("SecondCol[start]") < XRCID("SecondCol[end]") );
199 // Check it works for the positive values in FirstCol too
200 CPPUNIT_ASSERT( XRCID("FirstCol[start]") < XRCID("FirstCol[end]") );
201
202 // Check that values are adjacent
203 CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[0]")+1, XRCID("SecondCol[1]") );
204 CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[1]")+1, XRCID("SecondCol[2]") );
205 // And for the positive range
206 CPPUNIT_ASSERT_EQUAL( XRCID("FirstCol[2]")+1, XRCID("FirstCol[3]") );
207
208 // Check that a large-enough range was created, despite the small
209 // 'size' parameter
210 CPPUNIT_ASSERT_EQUAL
211 (
212 4,
213 XRCID("FirstCol[end]") - XRCID("FirstCol[start]") + 1
214 );
215
216 // Check that the far-too-large size range worked off the scale too
217 CPPUNIT_ASSERT( XRCID("SecondCol[start]") < XRCID("SecondCol[90]") );
218 CPPUNIT_ASSERT( XRCID("SecondCol[90]") < XRCID("SecondCol[end]") );
219 CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[90]")+1, XRCID("SecondCol[91]") );
220
221 // Check that the positive range-start parameter worked, even after a
222 // reload
223 CPPUNIT_ASSERT_EQUAL( XRCID("FirstCol[start]"), 10000 );
224
225 // Unload the xrc, so it can be reloaded and the tests rerun
226 CPPUNIT_ASSERT( wxXmlResource::Get()->Unload(TEST_XRC_FILE) );
227 }
228 }