guifuncs.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/misc/misctests.cpp
  3. // Purpose: test miscellaneous GUI functions
  4. // Author: Vadim Zeitlin
  5. // Created: 2008-09-22
  6. // Copyright: (c) 2008 Vadim Zeitlin
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #include "wx/defs.h"
  16. #ifndef WX_PRECOMP
  17. #include "wx/gdicmn.h"
  18. #include "wx/filefn.h"
  19. #endif // !PCH
  20. #include "wx/app.h"
  21. #include "wx/button.h"
  22. #include "wx/clipbrd.h"
  23. #include "wx/dataobj.h"
  24. #include "wx/panel.h"
  25. #include "asserthelper.h"
  26. // ----------------------------------------------------------------------------
  27. // test class
  28. // ----------------------------------------------------------------------------
  29. class MiscGUIFuncsTestCase : public CppUnit::TestCase
  30. {
  31. public:
  32. MiscGUIFuncsTestCase() { }
  33. private:
  34. CPPUNIT_TEST_SUITE( MiscGUIFuncsTestCase );
  35. CPPUNIT_TEST( DisplaySize );
  36. CPPUNIT_TEST( URLDataObject );
  37. CPPUNIT_TEST( ParseFileDialogFilter );
  38. CPPUNIT_TEST( ClientToScreen );
  39. CPPUNIT_TEST( FindWindowAtPoint );
  40. CPPUNIT_TEST_SUITE_END();
  41. void DisplaySize();
  42. void URLDataObject();
  43. void ParseFileDialogFilter();
  44. void ClientToScreen();
  45. void FindWindowAtPoint();
  46. DECLARE_NO_COPY_CLASS(MiscGUIFuncsTestCase)
  47. };
  48. // register in the unnamed registry so that these tests are run by default
  49. CPPUNIT_TEST_SUITE_REGISTRATION( MiscGUIFuncsTestCase );
  50. // also include in its own registry so that these tests can be run alone
  51. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscGUIFuncsTestCase, "MiscGUIFuncsTestCase" );
  52. void MiscGUIFuncsTestCase::DisplaySize()
  53. {
  54. // test that different (almost) overloads return the same results
  55. int w, h;
  56. wxDisplaySize(&w, &h);
  57. wxSize sz = wxGetDisplaySize();
  58. CPPUNIT_ASSERT_EQUAL( w, sz.x );
  59. CPPUNIT_ASSERT_EQUAL( h, sz.y );
  60. // test that passing NULL works as expected, e.g. doesn't crash
  61. wxDisplaySize(NULL, NULL);
  62. wxDisplaySize(&w, NULL);
  63. wxDisplaySize(NULL, &h);
  64. CPPUNIT_ASSERT_EQUAL( w, sz.x );
  65. CPPUNIT_ASSERT_EQUAL( h, sz.y );
  66. // test that display PPI is something reasonable
  67. sz = wxGetDisplayPPI();
  68. CPPUNIT_ASSERT( sz.x < 1000 && sz.y < 1000 );
  69. }
  70. void MiscGUIFuncsTestCase::URLDataObject()
  71. {
  72. // this tests for buffer overflow, see #11102
  73. const char * const
  74. url = "http://something.long.to.overwrite.plenty.memory.example.com";
  75. wxURLDataObject * const dobj = new wxURLDataObject(url);
  76. CPPUNIT_ASSERT_EQUAL( url, dobj->GetURL() );
  77. wxClipboardLocker lockClip;
  78. CPPUNIT_ASSERT( wxTheClipboard->SetData(dobj) );
  79. wxTheClipboard->Flush();
  80. }
  81. void MiscGUIFuncsTestCase::ParseFileDialogFilter()
  82. {
  83. wxArrayString descs,
  84. filters;
  85. CPPUNIT_ASSERT_EQUAL
  86. (
  87. 1,
  88. wxParseCommonDialogsFilter("Image files|*.jpg;*.png", descs, filters)
  89. );
  90. CPPUNIT_ASSERT_EQUAL( "Image files", descs[0] );
  91. CPPUNIT_ASSERT_EQUAL( "*.jpg;*.png", filters[0] );
  92. CPPUNIT_ASSERT_EQUAL
  93. (
  94. 2,
  95. wxParseCommonDialogsFilter
  96. (
  97. "All files (*.*)|*.*|Python source (*.py)|*.py",
  98. descs, filters
  99. )
  100. );
  101. CPPUNIT_ASSERT_EQUAL( "*.*", filters[0] );
  102. CPPUNIT_ASSERT_EQUAL( "*.py", filters[1] );
  103. // Test some invalid ones too.
  104. WX_ASSERT_FAILS_WITH_ASSERT
  105. (
  106. wxParseCommonDialogsFilter
  107. (
  108. "All files (*.*)|*.*|Python source (*.py)|*.py|",
  109. descs, filters
  110. )
  111. );
  112. }
  113. void MiscGUIFuncsTestCase::ClientToScreen()
  114. {
  115. wxWindow* const tlw = wxTheApp->GetTopWindow();
  116. CPPUNIT_ASSERT( tlw );
  117. wxPanel* const
  118. p1 = new wxPanel(tlw, wxID_ANY, wxPoint(0, 0), wxSize(100, 50));
  119. wxPanel* const
  120. p2 = new wxPanel(tlw, wxID_ANY, wxPoint(0, 50), wxSize(100, 50));
  121. wxWindow* const
  122. b = new wxWindow(p2, wxID_ANY, wxPoint(10, 10), wxSize(30, 10));
  123. // We need this to realize the windows created above under wxGTK.
  124. wxYield();
  125. const wxPoint tlwOrig = tlw->ClientToScreen(wxPoint(0, 0));
  126. CPPUNIT_ASSERT_EQUAL
  127. (
  128. tlwOrig + wxPoint(0, 50),
  129. p2->ClientToScreen(wxPoint(0, 0))
  130. );
  131. CPPUNIT_ASSERT_EQUAL
  132. (
  133. tlwOrig + wxPoint(10, 60),
  134. b->ClientToScreen(wxPoint(0, 0))
  135. );
  136. p1->Destroy();
  137. p2->Destroy();
  138. }
  139. namespace
  140. {
  141. // This class is used as a test window here. We can't use a real wxButton
  142. // because we can't create other windows as its children in wxGTK.
  143. class TestButton : public wxWindow
  144. {
  145. public:
  146. TestButton(wxWindow* parent, const wxString& label, const wxPoint& pos)
  147. : wxWindow(parent, wxID_ANY, pos, wxSize(100, 50))
  148. {
  149. SetLabel(label);
  150. }
  151. };
  152. // Helper function returning the label of the window at the given point or
  153. // "NONE" if there is no window there.
  154. wxString GetLabelOfWindowAtPoint(wxWindow* parent, int x, int y)
  155. {
  156. wxWindow* const
  157. win = wxFindWindowAtPoint(parent->ClientToScreen(wxPoint(x, y)));
  158. return win ? win->GetLabel() : wxString("NONE");
  159. }
  160. } // anonymous namespace
  161. void MiscGUIFuncsTestCase::FindWindowAtPoint()
  162. {
  163. wxWindow* const parent = wxTheApp->GetTopWindow();
  164. CPPUNIT_ASSERT( parent );
  165. // Set a label to allow distinguishing it from the other windows in the
  166. // assertion messages.
  167. parent->SetLabel("parent");
  168. wxWindow* btn1 = new TestButton(parent, "1", wxPoint(10, 10));
  169. wxWindow* btn2 = new TestButton(parent, "2", wxPoint(10, 90));
  170. wxWindow* btn3 = new TestButton(btn2, "3", wxPoint(20, 20));
  171. // We need this to realize the windows created above under wxGTK.
  172. wxYield();
  173. CPPUNIT_ASSERT_EQUAL_MESSAGE
  174. (
  175. "No window for a point outside of the window",
  176. "NONE",
  177. GetLabelOfWindowAtPoint(parent, 900, 900)
  178. );
  179. CPPUNIT_ASSERT_EQUAL_MESSAGE
  180. (
  181. "Point over a child control corresponds to it",
  182. btn1->GetLabel(),
  183. GetLabelOfWindowAtPoint(parent, 11, 11)
  184. );
  185. CPPUNIT_ASSERT_EQUAL_MESSAGE
  186. (
  187. "Point outside of any child control returns the TLW itself",
  188. parent->GetLabel(),
  189. GetLabelOfWindowAtPoint(parent, 5, 5)
  190. );
  191. btn2->Disable();
  192. CPPUNIT_ASSERT_EQUAL_MESSAGE
  193. (
  194. "Point over a disabled child control still corresponds to it",
  195. btn2->GetLabel(),
  196. GetLabelOfWindowAtPoint(parent, 11, 91)
  197. );
  198. btn2->Hide();
  199. CPPUNIT_ASSERT_EQUAL_MESSAGE
  200. (
  201. "Point over a hidden child control doesn't take it into account",
  202. parent->GetLabel(),
  203. GetLabelOfWindowAtPoint(parent, 11, 91)
  204. );
  205. btn2->Show();
  206. CPPUNIT_ASSERT_EQUAL_MESSAGE
  207. (
  208. "Point over child control corresponds to the child",
  209. btn3->GetLabel(),
  210. GetLabelOfWindowAtPoint(parent, 31, 111)
  211. );
  212. btn3->Disable();
  213. CPPUNIT_ASSERT_EQUAL_MESSAGE
  214. (
  215. "Point over disabled child controls still corresponds to this child",
  216. btn3->GetLabel(),
  217. GetLabelOfWindowAtPoint(parent, 31, 111)
  218. );
  219. btn1->Destroy();
  220. btn2->Destroy();
  221. // btn3 was already deleted when its parent was
  222. }