]>
git.saurik.com Git - wxWidgets.git/blob - tests/xml/xrctest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/xml/xrctest.cpp
3 // Purpose: XRC classes unit test
4 // Author: wxWidgets team
6 // Copyright: (c) 2010 wxWidgets team
7 ///////////////////////////////////////////////////////////////////////////////
9 // ----------------------------------------------------------------------------
11 // ----------------------------------------------------------------------------
23 #include "wx/xml/xml.h"
24 #include "wx/sstream.h"
25 #include "wx/wfstream.h"
26 #include "wx/xrc/xmlres.h"
30 // ----------------------------------------------------------------------------
31 // helpers to create/save some xrc
32 // ----------------------------------------------------------------------------
37 static const char *TEST_XRC_FILE
= "test.xrc";
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
44 "<?xml version=\"1.0\" ?>"
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]\">"
60 " <object class=\"sizeritem\">"
61 " <object class=\"wxButton\" name=\"FirstCol[1]\">"
65 " <object class=\"sizeritem\">"
66 " <object class=\"wxButton\" name=\"FirstCol[2]\">"
70 " <object class=\"sizeritem\">"
71 " <object class=\"wxButton\" name=\"FirstCol[3]\">"
77 " <object class=\"sizeritem\">"
78 " <object class=\"wxBoxSizer\">"
79 " <orient>wxVERTICAL</orient>"
80 " <object class=\"sizeritem\">"
81 " <object class=\"wxButton\" name=\"SecondCol[start]\">"
85 " <object class=\"sizeritem\">"
86 " <object class=\"wxButton\" name=\"SecondCol[1]\">"
90 " <object class=\"sizeritem\">"
91 " <object class=\"wxButton\" name=\"SecondCol[2]\">"
95 " <object class=\"sizeritem\">"
96 " <object class=\"wxButton\" name=\"SecondCol[end]\">"
102 " <orient>wxHORIZONTAL</orient>"
106 " <object class=\"sizeritem\">"
107 " <object class=\"wxPanel\" name=\"ref_of_panel1\">"
108 " <object_ref ref=\"panel1\"/>"
112 " <title>test</title>"
114 " <ids-range name=\"FirstCol\" size=\"2\" start=\"10000\"/>"
115 " <ids-range name=\"SecondCol\" size=\"100\" />"
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());
125 CPPUNIT_ASSERT(fos
.Close());
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 class XrcTestCase
: public CppUnit::TestCase
140 virtual void setUp() { CreateXrc(); }
141 virtual void tearDown() { wxRemoveFile(TEST_XRC_FILE
); }
144 CPPUNIT_TEST_SUITE( XrcTestCase
);
145 CPPUNIT_TEST( ObjectReferences
);
146 CPPUNIT_TEST( IDRanges
);
147 CPPUNIT_TEST_SUITE_END();
149 void ObjectReferences();
152 DECLARE_NO_COPY_CLASS(XrcTestCase
)
155 // register in the unnamed registry so that these tests are run by default
156 CPPUNIT_TEST_SUITE_REGISTRATION( XrcTestCase
);
158 // also include in its own registry so that these tests can be run alone
159 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XrcTestCase
, "XrcTestCase" );
163 void XrcTestCase::ObjectReferences()
165 wxXmlResource::Get()->InitAllHandlers();
167 for ( int n
= 0; n
< 2; ++n
)
169 // Load the xrc file we're just created
170 CPPUNIT_ASSERT( wxXmlResource::Get()->Load(TEST_XRC_FILE
) );
172 // In xrc there's now a dialog containing two panels, one an object
173 // reference of the other
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
);
182 // Unload the xrc, so it can be reloaded and the test rerun
183 CPPUNIT_ASSERT( wxXmlResource::Get()->Unload(TEST_XRC_FILE
) );
187 void XrcTestCase::IDRanges()
190 for ( int n
= 0; n
< 2; ++n
)
192 // Load the xrc file we're just created
193 CPPUNIT_ASSERT( wxXmlResource::Get()->Load(TEST_XRC_FILE
) );
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]") );
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]") );
208 // Check that a large-enough range was created, despite the small
213 XRCID("FirstCol[end]") - XRCID("FirstCol[start]") + 1
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]") );
221 // Check that the positive range-start parameter worked, even after a
223 CPPUNIT_ASSERT_EQUAL( XRCID("FirstCol[start]"), 10000 );
225 // Unload the xrc, so it can be reloaded and the tests rerun
226 CPPUNIT_ASSERT( wxXmlResource::Get()->Unload(TEST_XRC_FILE
) );