windowtest.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/windowtest.cpp
  3. // Purpose: wxWindow unit test
  4. // Author: Steven Lamerton
  5. // Created: 2010-07-10
  6. // Copyright: (c) 2010 Steven Lamerton
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #ifdef __BORLANDC__
  10. #pragma hdrstop
  11. #endif
  12. #ifndef WX_PRECOMP
  13. #include "wx/app.h"
  14. #include "wx/window.h"
  15. #include "wx/button.h"
  16. #endif // WX_PRECOMP
  17. #include "asserthelper.h"
  18. #include "testableframe.h"
  19. #include "wx/uiaction.h"
  20. #include "wx/caret.h"
  21. #include "wx/cshelp.h"
  22. #include "wx/tooltip.h"
  23. class WindowTestCase : public CppUnit::TestCase
  24. {
  25. public:
  26. WindowTestCase() { }
  27. void setUp();
  28. void tearDown();
  29. private:
  30. CPPUNIT_TEST_SUITE( WindowTestCase );
  31. CPPUNIT_TEST( ShowHideEvent );
  32. WXUISIM_TEST( KeyEvent );
  33. CPPUNIT_TEST( FocusEvent );
  34. CPPUNIT_TEST( Mouse );
  35. CPPUNIT_TEST( Properties );
  36. #if wxUSE_TOOLTIPS
  37. CPPUNIT_TEST( ToolTip );
  38. #endif // wxUSE_TOOLTIPS
  39. CPPUNIT_TEST( Help );
  40. CPPUNIT_TEST( Parent );
  41. CPPUNIT_TEST( Siblings );
  42. CPPUNIT_TEST( Children );
  43. CPPUNIT_TEST( Focus );
  44. CPPUNIT_TEST( Positioning );
  45. CPPUNIT_TEST( Show );
  46. CPPUNIT_TEST( Enable );
  47. CPPUNIT_TEST( FindWindowBy );
  48. CPPUNIT_TEST_SUITE_END();
  49. void ShowHideEvent();
  50. void KeyEvent();
  51. void FocusEvent();
  52. void Mouse();
  53. void Properties();
  54. #if wxUSE_TOOLTIPS
  55. void ToolTip();
  56. #endif // wxUSE_TOOLTIPS
  57. void Help();
  58. void Parent();
  59. void Siblings();
  60. void Children();
  61. void Focus();
  62. void Positioning();
  63. void Show();
  64. void Enable();
  65. void FindWindowBy();
  66. wxWindow *m_window;
  67. DECLARE_NO_COPY_CLASS(WindowTestCase)
  68. };
  69. // register in the unnamed registry so that these tests are run by default
  70. CPPUNIT_TEST_SUITE_REGISTRATION( WindowTestCase );
  71. // also include in its own registry so that these tests can be run alone
  72. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( WindowTestCase, "WindowTestCase" );
  73. void WindowTestCase::setUp()
  74. {
  75. m_window = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY);
  76. }
  77. void WindowTestCase::tearDown()
  78. {
  79. wxTheApp->GetTopWindow()->DestroyChildren();
  80. }
  81. void WindowTestCase::ShowHideEvent()
  82. {
  83. #if defined(__WXMSW__) || defined (__WXPM__)
  84. EventCounter show(m_window, wxEVT_SHOW);
  85. CPPUNIT_ASSERT(m_window->IsShown());
  86. m_window->Show(false);
  87. CPPUNIT_ASSERT(!m_window->IsShown());
  88. m_window->Show();
  89. CPPUNIT_ASSERT(m_window->IsShown());
  90. CPPUNIT_ASSERT_EQUAL(2, show.GetCount());
  91. #endif
  92. }
  93. void WindowTestCase::KeyEvent()
  94. {
  95. #if wxUSE_UIACTIONSIMULATOR
  96. EventCounter keydown(m_window, wxEVT_KEY_DOWN);
  97. EventCounter keyup(m_window, wxEVT_KEY_UP);
  98. EventCounter keychar(m_window, wxEVT_CHAR);
  99. wxUIActionSimulator sim;
  100. m_window->SetFocus();
  101. sim.Text("text");
  102. sim.Char(WXK_SHIFT);
  103. wxYield();
  104. CPPUNIT_ASSERT_EQUAL(5, keydown.GetCount());
  105. CPPUNIT_ASSERT_EQUAL(5, keyup.GetCount());
  106. CPPUNIT_ASSERT_EQUAL(4, keychar.GetCount());
  107. #endif
  108. }
  109. void WindowTestCase::FocusEvent()
  110. {
  111. #ifndef __WXOSX__
  112. EventCounter setfocus(m_window, wxEVT_SET_FOCUS);
  113. EventCounter killfocus(m_window, wxEVT_KILL_FOCUS);
  114. m_window->SetFocus();
  115. CPPUNIT_ASSERT_EQUAL(1, setfocus.GetCount());
  116. CPPUNIT_ASSERT(m_window->HasFocus());
  117. wxButton* button = new wxButton(wxTheApp->GetTopWindow(), wxID_ANY);
  118. button->SetFocus();
  119. CPPUNIT_ASSERT_EQUAL(1, killfocus.GetCount());
  120. CPPUNIT_ASSERT(!m_window->HasFocus());
  121. #endif
  122. }
  123. void WindowTestCase::Mouse()
  124. {
  125. wxCursor cursor(wxCURSOR_CHAR);
  126. m_window->SetCursor(cursor);
  127. CPPUNIT_ASSERT(m_window->GetCursor().IsOk());
  128. //A plain window doesn't have a caret
  129. CPPUNIT_ASSERT(!m_window->GetCaret());
  130. wxCaret* caret = new wxCaret(m_window, 16, 16);
  131. m_window->SetCaret(caret);
  132. CPPUNIT_ASSERT(m_window->GetCaret()->IsOk());
  133. m_window->CaptureMouse();
  134. CPPUNIT_ASSERT(m_window->HasCapture());
  135. m_window->ReleaseMouse();
  136. CPPUNIT_ASSERT(!m_window->HasCapture());
  137. }
  138. void WindowTestCase::Properties()
  139. {
  140. m_window->SetLabel("label");
  141. CPPUNIT_ASSERT_EQUAL("label", m_window->GetLabel());
  142. m_window->SetName("name");
  143. CPPUNIT_ASSERT_EQUAL("name", m_window->GetName());
  144. //As we used wxID_ANY we should have a negative id
  145. CPPUNIT_ASSERT(m_window->GetId() < 0);
  146. m_window->SetId(wxID_HIGHEST + 10);
  147. CPPUNIT_ASSERT_EQUAL(wxID_HIGHEST + 10, m_window->GetId());
  148. }
  149. #if wxUSE_TOOLTIPS
  150. void WindowTestCase::ToolTip()
  151. {
  152. CPPUNIT_ASSERT(!m_window->GetToolTip());
  153. CPPUNIT_ASSERT_EQUAL("", m_window->GetToolTipText());
  154. m_window->SetToolTip("text tip");
  155. CPPUNIT_ASSERT_EQUAL("text tip", m_window->GetToolTipText());
  156. m_window->UnsetToolTip();
  157. CPPUNIT_ASSERT(!m_window->GetToolTip());
  158. CPPUNIT_ASSERT_EQUAL("", m_window->GetToolTipText());
  159. wxToolTip* tip = new wxToolTip("other tip");
  160. m_window->SetToolTip(tip);
  161. CPPUNIT_ASSERT_EQUAL(tip, m_window->GetToolTip());
  162. CPPUNIT_ASSERT_EQUAL("other tip", m_window->GetToolTipText());
  163. }
  164. #endif // wxUSE_TOOLTIPS
  165. void WindowTestCase::Help()
  166. {
  167. wxHelpProvider::Set(new wxSimpleHelpProvider());
  168. CPPUNIT_ASSERT_EQUAL("", m_window->GetHelpText());
  169. m_window->SetHelpText("helptext");
  170. CPPUNIT_ASSERT_EQUAL("helptext", m_window->GetHelpText());
  171. }
  172. void WindowTestCase::Parent()
  173. {
  174. CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetGrandParent());
  175. CPPUNIT_ASSERT_EQUAL(wxTheApp->GetTopWindow(), m_window->GetParent());
  176. }
  177. void WindowTestCase::Siblings()
  178. {
  179. CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetNextSibling());
  180. CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetPrevSibling());
  181. wxWindow* newwin = new wxWindow(wxTheApp->GetTopWindow(), wxID_ANY);
  182. CPPUNIT_ASSERT_EQUAL(newwin, m_window->GetNextSibling());
  183. CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), m_window->GetPrevSibling());
  184. CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL), newwin->GetNextSibling());
  185. CPPUNIT_ASSERT_EQUAL(m_window, newwin->GetPrevSibling());
  186. wxDELETE(newwin);
  187. }
  188. void WindowTestCase::Children()
  189. {
  190. CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount());
  191. wxWindow* child1 = new wxWindow(m_window, wxID_ANY);
  192. CPPUNIT_ASSERT_EQUAL(1, m_window->GetChildren().GetCount());
  193. m_window->RemoveChild(child1);
  194. CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount());
  195. child1->SetId(wxID_HIGHEST + 1);
  196. child1->SetName("child1");
  197. m_window->AddChild(child1);
  198. CPPUNIT_ASSERT_EQUAL(1, m_window->GetChildren().GetCount());
  199. CPPUNIT_ASSERT_EQUAL(child1, m_window->FindWindow(wxID_HIGHEST + 1));
  200. CPPUNIT_ASSERT_EQUAL(child1, m_window->FindWindow("child1"));
  201. m_window->DestroyChildren();
  202. CPPUNIT_ASSERT_EQUAL(0, m_window->GetChildren().GetCount());
  203. }
  204. void WindowTestCase::Focus()
  205. {
  206. #ifndef __WXOSX__
  207. CPPUNIT_ASSERT(!m_window->HasFocus());
  208. if ( m_window->AcceptsFocus() )
  209. {
  210. m_window->SetFocus();
  211. CPPUNIT_ASSERT(m_window->HasFocus());
  212. }
  213. //Set the focus back to the main window
  214. wxTheApp->GetTopWindow()->SetFocus();
  215. if ( m_window->AcceptsFocusFromKeyboard() )
  216. {
  217. m_window->SetFocusFromKbd();
  218. CPPUNIT_ASSERT(m_window->HasFocus());
  219. }
  220. #endif
  221. }
  222. void WindowTestCase::Positioning()
  223. {
  224. //Some basic tests for consistency
  225. int x, y;
  226. m_window->GetPosition(&x, &y);
  227. CPPUNIT_ASSERT_EQUAL(x, m_window->GetPosition().x);
  228. CPPUNIT_ASSERT_EQUAL(y, m_window->GetPosition().y);
  229. CPPUNIT_ASSERT_EQUAL(m_window->GetPosition(),
  230. m_window->GetRect().GetTopLeft());
  231. m_window->GetScreenPosition(&x, &y);
  232. CPPUNIT_ASSERT_EQUAL(x, m_window->GetScreenPosition().x);
  233. CPPUNIT_ASSERT_EQUAL(y, m_window->GetScreenPosition().y);
  234. CPPUNIT_ASSERT_EQUAL(m_window->GetScreenPosition(),
  235. m_window->GetScreenRect().GetTopLeft());
  236. }
  237. void WindowTestCase::Show()
  238. {
  239. CPPUNIT_ASSERT(m_window->IsShown());
  240. m_window->Hide();
  241. CPPUNIT_ASSERT(!m_window->IsShown());
  242. m_window->Show();
  243. CPPUNIT_ASSERT(m_window->IsShown());
  244. m_window->Show(false);
  245. CPPUNIT_ASSERT(!m_window->IsShown());
  246. m_window->ShowWithEffect(wxSHOW_EFFECT_BLEND);
  247. CPPUNIT_ASSERT(m_window->IsShown());
  248. m_window->HideWithEffect(wxSHOW_EFFECT_BLEND);
  249. CPPUNIT_ASSERT(!m_window->IsShown());
  250. }
  251. void WindowTestCase::Enable()
  252. {
  253. CPPUNIT_ASSERT(m_window->IsEnabled());
  254. m_window->Disable();
  255. CPPUNIT_ASSERT(!m_window->IsEnabled());
  256. m_window->Enable();
  257. CPPUNIT_ASSERT(m_window->IsEnabled());
  258. m_window->Enable(false);
  259. CPPUNIT_ASSERT(!m_window->IsEnabled());
  260. }
  261. void WindowTestCase::FindWindowBy()
  262. {
  263. m_window->SetId(wxID_HIGHEST + 1);
  264. m_window->SetName("name");
  265. m_window->SetLabel("label");
  266. CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowById(wxID_HIGHEST + 1));
  267. CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowByName("name"));
  268. CPPUNIT_ASSERT_EQUAL(m_window, wxWindow::FindWindowByLabel("label"));
  269. CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL),
  270. wxWindow::FindWindowById(wxID_HIGHEST + 3));
  271. CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL),
  272. wxWindow::FindWindowByName("noname"));
  273. CPPUNIT_ASSERT_EQUAL(static_cast<wxWindow*>(NULL),
  274. wxWindow::FindWindowByLabel("nolabel"));
  275. }