]>
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
7 // Copyright: (c) 2010 wxWidgets team
8 ///////////////////////////////////////////////////////////////////////////////
10 // ----------------------------------------------------------------------------
12 // ----------------------------------------------------------------------------
24 #include "wx/xml/xml.h"
25 #include "wx/sstream.h"
26 #include "wx/wfstream.h"
27 #include "wx/xrc/xmlres.h"
31 // ----------------------------------------------------------------------------
32 // helpers to create/save some xrc
33 // ----------------------------------------------------------------------------
38 static const char *TEST_XRC_FILE
= "test.xrc";
40 // I'm hard-wiring the xrc into this function for now
41 // If different xrcs are wanted for future tests, it'll be easy to refactor
45 "<?xml version=\"1.0\" ?>"
47 " <object class=\"wxDialog\" name=\"dialog\">"
48 " <object class=\"wxBoxSizer\">"
49 " <orient>wxVERTICAL</orient>"
50 " <object class=\"sizeritem\">"
51 " <object class=\"wxPanel\" name=\"panel1\">"
52 " <object class=\"wxBoxSizer\">"
53 " <object class=\"sizeritem\">"
54 " <object class=\"wxBoxSizer\">"
55 " <orient>wxVERTICAL</orient>"
56 " <object class=\"sizeritem\">"
57 " <object class=\"wxButton\" name=\"FirstCol[0]\">"
61 " <object class=\"sizeritem\">"
62 " <object class=\"wxButton\" name=\"FirstCol[1]\">"
66 " <object class=\"sizeritem\">"
67 " <object class=\"wxButton\" name=\"FirstCol[2]\">"
71 " <object class=\"sizeritem\">"
72 " <object class=\"wxButton\" name=\"FirstCol[3]\">"
78 " <object class=\"sizeritem\">"
79 " <object class=\"wxBoxSizer\">"
80 " <orient>wxVERTICAL</orient>"
81 " <object class=\"sizeritem\">"
82 " <object class=\"wxButton\" name=\"SecondCol[start]\">"
86 " <object class=\"sizeritem\">"
87 " <object class=\"wxButton\" name=\"SecondCol[1]\">"
91 " <object class=\"sizeritem\">"
92 " <object class=\"wxButton\" name=\"SecondCol[2]\">"
96 " <object class=\"sizeritem\">"
97 " <object class=\"wxButton\" name=\"SecondCol[end]\">"
103 " <orient>wxHORIZONTAL</orient>"
107 " <object class=\"sizeritem\">"
108 " <object class=\"wxPanel\" name=\"ref_of_panel1\">"
109 " <object_ref ref=\"panel1\"/>"
113 " <title>test</title>"
115 " <ids-range name=\"FirstCol\" size=\"2\" start=\"10000\"/>"
116 " <ids-range name=\"SecondCol\" size=\"100\" />"
120 // afaict there's no elegant way to load xrc direct from a string
121 // So save it as a file, from which it can be loaded
122 wxStringInputStream
sis(xrcText
);
123 wxFFileOutputStream
fos(TEST_XRC_FILE
);
124 CPPUNIT_ASSERT(fos
.IsOk());
126 CPPUNIT_ASSERT(fos
.Close());
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 class XrcTestCase
: public CppUnit::TestCase
141 virtual void setUp() { CreateXrc(); }
142 virtual void tearDown() { wxRemoveFile(TEST_XRC_FILE
); }
145 CPPUNIT_TEST_SUITE( XrcTestCase
);
146 CPPUNIT_TEST( ObjectReferences
);
147 CPPUNIT_TEST( IDRanges
);
148 CPPUNIT_TEST_SUITE_END();
150 void ObjectReferences();
153 DECLARE_NO_COPY_CLASS(XrcTestCase
)
156 // register in the unnamed registry so that these tests are run by default
157 CPPUNIT_TEST_SUITE_REGISTRATION( XrcTestCase
);
159 // also include in it's own registry so that these tests can be run alone
160 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( XrcTestCase
, "XrcTestCase" );
164 void XrcTestCase::ObjectReferences()
166 wxXmlResource::Get()->InitAllHandlers();
168 for ( int n
= 0; n
< 2; ++n
)
170 // Load the xrc file we're just created
171 CPPUNIT_ASSERT( wxXmlResource::Get()->Load(TEST_XRC_FILE
) );
173 // In xrc there's now a dialog containing two panels, one an object
174 // reference of the other
176 CPPUNIT_ASSERT( wxXmlResource::Get()->LoadDialog(&dlg
, NULL
, "dialog") );
177 // Might as well test XRCCTRL too
178 wxPanel
* panel1
= XRCCTRL(dlg
,"panel1",wxPanel
);
179 wxPanel
* panel2
= XRCCTRL(dlg
,"ref_of_panel1",wxPanel
);
180 // Check that the object reference panel is a different object
181 CPPUNIT_ASSERT( panel2
!= panel1
);
183 // Unload the xrc, so it can be reloaded and the test rerun
184 CPPUNIT_ASSERT( wxXmlResource::Get()->Unload(TEST_XRC_FILE
) );
188 void XrcTestCase::IDRanges()
191 for ( int n
= 0; n
< 2; ++n
)
193 // Load the xrc file we're just created
194 CPPUNIT_ASSERT( wxXmlResource::Get()->Load(TEST_XRC_FILE
) );
196 // foo[start] should == foo[0]
197 CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[start]"), XRCID("SecondCol[0]") );
198 // foo[start] should be < foo[end]. Usually that means more negative
199 CPPUNIT_ASSERT( XRCID("SecondCol[start]") < XRCID("SecondCol[end]") );
200 // Check it works for the positive values in FirstCol too
201 CPPUNIT_ASSERT( XRCID("FirstCol[start]") < XRCID("FirstCol[end]") );
203 // Check that values are adjacent
204 CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[0]")+1, XRCID("SecondCol[1]") );
205 CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[1]")+1, XRCID("SecondCol[2]") );
206 // And for the positive range
207 CPPUNIT_ASSERT_EQUAL( XRCID("FirstCol[2]")+1, XRCID("FirstCol[3]") );
209 // Check that a large-enough range was created, despite the small
214 XRCID("FirstCol[end]") - XRCID("FirstCol[start]") + 1
217 // Check that the far-too-large size range worked off the scale too
218 CPPUNIT_ASSERT( XRCID("SecondCol[start]") < XRCID("SecondCol[90]") );
219 CPPUNIT_ASSERT( XRCID("SecondCol[90]") < XRCID("SecondCol[end]") );
220 CPPUNIT_ASSERT_EQUAL( XRCID("SecondCol[90]")+1, XRCID("SecondCol[91]") );
222 // Check that the positive range-start parameter worked, even after a
224 CPPUNIT_ASSERT_EQUAL( XRCID("FirstCol[start]"), 10000 );
226 // Unload the xrc, so it can be reloaded and the tests rerun
227 CPPUNIT_ASSERT( wxXmlResource::Get()->Unload(TEST_XRC_FILE
) );