xrctest.cpp 8.0 KB

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