label.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/controls/label.cpp
  3. // Purpose: wxControl and wxStaticText label tests
  4. // Author: Francesco Montorsi
  5. // Created: 2010-3-21
  6. // Copyright: (c) 2010 Francesco Montorsi
  7. ///////////////////////////////////////////////////////////////////////////////
  8. // ----------------------------------------------------------------------------
  9. // headers
  10. // ----------------------------------------------------------------------------
  11. #include "testprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #ifndef WX_PRECOMP
  16. #include "wx/app.h"
  17. #endif // WX_PRECOMP
  18. #include "wx/control.h"
  19. #include "wx/stattext.h"
  20. #include "wx/checkbox.h"
  21. // ----------------------------------------------------------------------------
  22. // test class
  23. // ----------------------------------------------------------------------------
  24. class LabelTestCase : public CppUnit::TestCase
  25. {
  26. public:
  27. LabelTestCase() { }
  28. virtual void setUp();
  29. virtual void tearDown();
  30. private:
  31. CPPUNIT_TEST_SUITE( LabelTestCase );
  32. CPPUNIT_TEST( GetLabel );
  33. CPPUNIT_TEST( GetLabelText );
  34. CPPUNIT_TEST( Statics );
  35. CPPUNIT_TEST_SUITE_END();
  36. void GetLabel();
  37. void GetLabelText();
  38. void Statics();
  39. wxStaticText *m_st;
  40. // we cannot test wxControl directly (it's abstract) so we rather test wxCheckBox
  41. wxCheckBox *m_cb;
  42. DECLARE_NO_COPY_CLASS(LabelTestCase)
  43. };
  44. // register in the unnamed registry so that these tests are run by default
  45. CPPUNIT_TEST_SUITE_REGISTRATION( LabelTestCase );
  46. // also include in its own registry so that these tests can be run alone
  47. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LabelTestCase, "LabelTestCase" );
  48. // ----------------------------------------------------------------------------
  49. // test initialization
  50. // ----------------------------------------------------------------------------
  51. #define ORIGINAL_LABEL "original label"
  52. void LabelTestCase::setUp()
  53. {
  54. m_st = new wxStaticText(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL);
  55. m_cb = new wxCheckBox(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL);
  56. CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_st->GetLabel() );
  57. CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_cb->GetLabel() );
  58. }
  59. void LabelTestCase::tearDown()
  60. {
  61. wxDELETE(m_st);
  62. wxDELETE(m_cb);
  63. }
  64. // ----------------------------------------------------------------------------
  65. // the tests themselves
  66. // ----------------------------------------------------------------------------
  67. #define SET_LABEL(str) \
  68. m_st->SetLabel(str); \
  69. m_cb->SetLabel(str);
  70. #define SET_LABEL_TEXT(str) \
  71. m_st->SetLabelText(str); \
  72. m_cb->SetLabelText(str);
  73. void LabelTestCase::GetLabel()
  74. {
  75. const wxString testLabelArray[] = {
  76. "label without mnemonics and markup",
  77. "label with &mnemonic",
  78. "label with <span foreground='blue'>some</span> <b>markup</b>",
  79. "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",
  80. };
  81. // test calls to SetLabel() and then to GetLabel()
  82. for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )
  83. {
  84. SET_LABEL(testLabelArray[s]);
  85. // GetLabel() should always return the string passed to SetLabel()
  86. CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_st->GetLabel() );
  87. CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_cb->GetLabel() );
  88. }
  89. // test calls to SetLabelText() and then to GetLabel()
  90. const wxString& testLabel = "label without mnemonics and markup";
  91. SET_LABEL_TEXT(testLabel);
  92. CPPUNIT_ASSERT_EQUAL( testLabel, m_st->GetLabel() );
  93. CPPUNIT_ASSERT_EQUAL( testLabel, m_cb->GetLabel() );
  94. const wxString& testLabel2 = "label with &mnemonic";
  95. const wxString& testLabelText2 = "label with &&mnemonic";
  96. SET_LABEL_TEXT(testLabel2);
  97. CPPUNIT_ASSERT_EQUAL( testLabelText2, m_st->GetLabel() );
  98. CPPUNIT_ASSERT_EQUAL( testLabelText2, m_cb->GetLabel() );
  99. const wxString& testLabel3 = "label with <span foreground='blue'>some</span> <b>markup</b>";
  100. SET_LABEL_TEXT(testLabel3);
  101. CPPUNIT_ASSERT_EQUAL( testLabel3, m_st->GetLabel() );
  102. CPPUNIT_ASSERT_EQUAL( testLabel3, m_cb->GetLabel() );
  103. const wxString& testLabel4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic";
  104. const wxString& testLabelText4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &&mnemonic";
  105. SET_LABEL_TEXT(testLabel4);
  106. CPPUNIT_ASSERT_EQUAL( testLabelText4, m_st->GetLabel() );
  107. CPPUNIT_ASSERT_EQUAL( testLabelText4, m_cb->GetLabel() );
  108. }
  109. void LabelTestCase::GetLabelText()
  110. {
  111. // test calls to SetLabel() and then to GetLabelText()
  112. const wxString& testLabel = "label without mnemonics and markup";
  113. SET_LABEL(testLabel);
  114. CPPUNIT_ASSERT_EQUAL( testLabel, m_st->GetLabelText() );
  115. CPPUNIT_ASSERT_EQUAL( testLabel, m_cb->GetLabelText() );
  116. const wxString& testLabel2 = "label with &mnemonic";
  117. const wxString& testLabelText2 = "label with mnemonic";
  118. SET_LABEL(testLabel2);
  119. CPPUNIT_ASSERT_EQUAL( testLabelText2, m_st->GetLabelText() );
  120. CPPUNIT_ASSERT_EQUAL( testLabelText2, m_cb->GetLabelText() );
  121. const wxString& testLabel3 = "label with <span foreground='blue'>some</span> <b>markup</b>";
  122. SET_LABEL(testLabel3);
  123. CPPUNIT_ASSERT_EQUAL( testLabel3, m_st->GetLabelText() );
  124. CPPUNIT_ASSERT_EQUAL( testLabel3, m_cb->GetLabelText() );
  125. const wxString& testLabel4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic";
  126. const wxString& testLabelText4 = "label with <span foreground='blue'>some</span> <b>markup</b> and mnemonic";
  127. SET_LABEL(testLabel4);
  128. CPPUNIT_ASSERT_EQUAL( testLabelText4, m_st->GetLabelText() );
  129. CPPUNIT_ASSERT_EQUAL( testLabelText4, m_cb->GetLabelText() );
  130. const wxString testLabelArray[] = {
  131. "label without mnemonics and markup",
  132. "label with &mnemonic",
  133. "label with <span foreground='blue'>some</span> <b>markup</b>",
  134. "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",
  135. };
  136. // test calls to SetLabelText() and then to GetLabelText()
  137. for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )
  138. {
  139. SET_LABEL_TEXT(testLabelArray[s]);
  140. // GetLabelText() should always return the string passed to SetLabelText()
  141. CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_st->GetLabelText() );
  142. CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_cb->GetLabelText() );
  143. }
  144. }
  145. void LabelTestCase::Statics()
  146. {
  147. CPPUNIT_ASSERT_EQUAL( "mnemonic", wxControl::RemoveMnemonics("&mnemonic") );
  148. CPPUNIT_ASSERT_EQUAL( "&mnemonic", wxControl::RemoveMnemonics("&&mnemonic") );
  149. CPPUNIT_ASSERT_EQUAL( "&mnemonic", wxControl::RemoveMnemonics("&&&mnemonic") );
  150. }