radiobuttontest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/radiobuttontest.cpp
  3. // Purpose: wxRadioButton unit test
  4. // Author: Steven Lamerton
  5. // Created: 2010-07-30
  6. // Copyright: (c) 2010 Steven Lamerton
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #include "testprec.h"
  9. #if wxUSE_RADIOBTN
  10. #ifdef __BORLANDC__
  11. #pragma hdrstop
  12. #endif
  13. #ifndef WX_PRECOMP
  14. #include "wx/app.h"
  15. #include "wx/radiobut.h"
  16. #endif // WX_PRECOMP
  17. #include "wx/uiaction.h"
  18. #include "testableframe.h"
  19. class RadioButtonTestCase : public CppUnit::TestCase
  20. {
  21. public:
  22. RadioButtonTestCase() { }
  23. void setUp();
  24. void tearDown();
  25. private:
  26. CPPUNIT_TEST_SUITE( RadioButtonTestCase );
  27. WXUISIM_TEST( Click );
  28. CPPUNIT_TEST( Value );
  29. CPPUNIT_TEST( Group );
  30. CPPUNIT_TEST_SUITE_END();
  31. void Click();
  32. void Value();
  33. void Group();
  34. wxRadioButton* m_radio;
  35. DECLARE_NO_COPY_CLASS(RadioButtonTestCase)
  36. };
  37. // register in the unnamed registry so that these tests are run by default
  38. CPPUNIT_TEST_SUITE_REGISTRATION( RadioButtonTestCase );
  39. // also include in its own registry so that these tests can be run alone
  40. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( RadioButtonTestCase,
  41. "RadioButtonTestCase" );
  42. void RadioButtonTestCase::setUp()
  43. {
  44. m_radio = new wxRadioButton(wxTheApp->GetTopWindow(), wxID_ANY,
  45. "wxRadioButton");
  46. m_radio->Update();
  47. m_radio->Refresh();
  48. }
  49. void RadioButtonTestCase::tearDown()
  50. {
  51. wxDELETE(m_radio);
  52. }
  53. void RadioButtonTestCase::Click()
  54. {
  55. // GTK and OS X do not support selecting a single radio button
  56. #if wxUSE_UIACTIONSIMULATOR && !defined(__WXGTK__) && !defined(__WXOSX__)
  57. EventCounter selected(m_radio, wxEVT_RADIOBUTTON);
  58. wxUIActionSimulator sim;
  59. sim.MouseMove(m_radio->GetScreenPosition() + wxPoint(10, 10));
  60. sim.MouseClick();
  61. wxYield();
  62. CPPUNIT_ASSERT_EQUAL( 1, selected.GetCount() );
  63. #endif
  64. }
  65. void RadioButtonTestCase::Value()
  66. {
  67. #ifndef __WXGTK__
  68. EventCounter selected(m_radio, wxEVT_RADIOBUTTON);
  69. m_radio->SetValue(true);
  70. CPPUNIT_ASSERT(m_radio->GetValue());
  71. m_radio->SetValue(false);
  72. CPPUNIT_ASSERT(!m_radio->GetValue());
  73. CPPUNIT_ASSERT_EQUAL(0, selected.GetCount());
  74. #endif
  75. }
  76. void RadioButtonTestCase::Group()
  77. {
  78. //Add another button to the first group and create another of two buttons
  79. wxRadioButton* g1radio0 = new wxRadioButton(wxTheApp->GetTopWindow(),
  80. wxID_ANY, "wxRadioButton",
  81. wxDefaultPosition,
  82. wxDefaultSize, wxRB_GROUP);
  83. wxRadioButton* g1radio1 = new wxRadioButton(wxTheApp->GetTopWindow(),
  84. wxID_ANY, "wxRadioButton");
  85. wxRadioButton* g2radio0 = new wxRadioButton(wxTheApp->GetTopWindow(),
  86. wxID_ANY, "wxRadioButton",
  87. wxDefaultPosition,
  88. wxDefaultSize, wxRB_GROUP);
  89. wxRadioButton* g2radio1 = new wxRadioButton(wxTheApp->GetTopWindow(),
  90. wxID_ANY, "wxRadioButton");
  91. g1radio0->SetValue(true);
  92. g2radio0->SetValue(true);
  93. CPPUNIT_ASSERT(g1radio0->GetValue());
  94. CPPUNIT_ASSERT(!g1radio1->GetValue());
  95. CPPUNIT_ASSERT(g2radio0->GetValue());
  96. CPPUNIT_ASSERT(!g2radio1->GetValue());
  97. g1radio1->SetValue(true);
  98. g2radio1->SetValue(true);
  99. CPPUNIT_ASSERT(!g1radio0->GetValue());
  100. CPPUNIT_ASSERT(g1radio1->GetValue());
  101. CPPUNIT_ASSERT(!g2radio0->GetValue());
  102. CPPUNIT_ASSERT(g2radio1->GetValue());
  103. g1radio0->SetValue(true);
  104. g2radio0->SetValue(true);
  105. CPPUNIT_ASSERT(g1radio0->GetValue());
  106. CPPUNIT_ASSERT(!g1radio1->GetValue());
  107. CPPUNIT_ASSERT(g2radio0->GetValue());
  108. CPPUNIT_ASSERT(!g2radio1->GetValue());
  109. wxDELETE(g1radio0);
  110. wxDELETE(g1radio1);
  111. wxDELETE(g2radio0);
  112. wxDELETE(g2radio1);
  113. }
  114. #endif //wxUSE_RADIOBTN