]> git.saurik.com Git - wxWidgets.git/blobdiff - tests/controls/label.cpp
reorder GetLabel(), GetLabelText(), SetLabel() and SetLabelText() function declaratio...
[wxWidgets.git] / tests / controls / label.cpp
diff --git a/tests/controls/label.cpp b/tests/controls/label.cpp
new file mode 100644 (file)
index 0000000..d9d2eeb
--- /dev/null
@@ -0,0 +1,213 @@
+///////////////////////////////////////////////////////////////////////////////\r
+// Name:        tests/controls/label.cpp\r
+// Purpose:     wxControl and wxStaticText label tests\r
+// Author:      Francesco Montorsi\r
+// Created:     2010-3-21\r
+// RCS-ID:      $Id$\r
+// Copyright:   (c) 2010 Francesco Montorsi\r
+///////////////////////////////////////////////////////////////////////////////\r
+\r
+// ----------------------------------------------------------------------------\r
+// headers\r
+// ----------------------------------------------------------------------------\r
+\r
+#include "testprec.h"\r
+\r
+#ifdef __BORLANDC__\r
+    #pragma hdrstop\r
+#endif\r
+\r
+#ifndef WX_PRECOMP\r
+    #include "wx/app.h"\r
+#endif // WX_PRECOMP\r
+\r
+#include "wx/control.h"\r
+#include "wx/stattext.h"\r
+#include "wx/checkbox.h"\r
+\r
+// ----------------------------------------------------------------------------\r
+// test class\r
+// ----------------------------------------------------------------------------\r
+\r
+class LabelTestCase : public CppUnit::TestCase\r
+{\r
+public:\r
+    LabelTestCase() { }\r
+\r
+    virtual void setUp();\r
+    virtual void tearDown();\r
+\r
+private:\r
+    CPPUNIT_TEST_SUITE( LabelTestCase );\r
+        CPPUNIT_TEST( GetLabel );\r
+        CPPUNIT_TEST( GetLabelText );\r
+        CPPUNIT_TEST( Statics );\r
+    CPPUNIT_TEST_SUITE_END();\r
+\r
+    void GetLabel();\r
+    void GetLabelText();\r
+    void Statics();\r
+\r
+    wxStaticText *m_st, *m_stWithMarkup;\r
+\r
+    // we cannot test wxControl directly (it's abstract) so we rather test wxCheckBox\r
+    wxCheckBox *m_cb;\r
+\r
+    DECLARE_NO_COPY_CLASS(LabelTestCase)\r
+};\r
+\r
+// register in the unnamed registry so that these tests are run by default\r
+CPPUNIT_TEST_SUITE_REGISTRATION( LabelTestCase );\r
+\r
+// also include in it's own registry so that these tests can be run alone\r
+CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LabelTestCase, "LabelTestCase" );\r
+\r
+// ----------------------------------------------------------------------------\r
+// test initialization\r
+// ----------------------------------------------------------------------------\r
+\r
+#define ORIGINAL_LABEL      "original label"\r
+\r
+void LabelTestCase::setUp()\r
+{\r
+    m_st = new wxStaticText(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL);\r
+    m_stWithMarkup = new wxStaticText(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL,\r
+                                      wxDefaultPosition, wxDefaultSize, wxST_MARKUP);\r
+\r
+    m_cb = new wxCheckBox(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL);\r
+\r
+    CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_st->GetLabel() );\r
+    CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_stWithMarkup->GetLabel() );\r
+    CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_cb->GetLabel() );\r
+}\r
+\r
+void LabelTestCase::tearDown()\r
+{\r
+    wxDELETE(m_st);\r
+    wxDELETE(m_stWithMarkup);\r
+    wxDELETE(m_cb);\r
+}\r
+\r
+// ----------------------------------------------------------------------------\r
+// the tests themselves\r
+// ----------------------------------------------------------------------------\r
+\r
+#define SET_LABEL(str)                              \\r
+        m_st->SetLabel(str);                        \\r
+        m_stWithMarkup->SetLabel(str);              \\r
+        m_cb->SetLabel(str);\r
+\r
+#define SET_LABEL_TEXT(str)                         \\r
+        m_st->SetLabelText(str);                    \\r
+        m_stWithMarkup->SetLabelText(str);          \\r
+        m_cb->SetLabelText(str);\r
+\r
+void LabelTestCase::GetLabel()\r
+{\r
+    const wxString testLabelArray[] = {\r
+        "label without mnemonics and markup",\r
+        "label with &mnemonic",\r
+        "label with <span foreground='blue'>some</span> <b>markup</b>",\r
+        "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",\r
+    };\r
+\r
+    // test calls to SetLabel() and then to GetLabel()\r
+\r
+    for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )\r
+    {\r
+        SET_LABEL(testLabelArray[s]);\r
+\r
+        // GetLabel() should always return the string passed to SetLabel()\r
+        CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_st->GetLabel() );\r
+        CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_stWithMarkup->GetLabel() );\r
+        CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_cb->GetLabel() );\r
+    }\r
+\r
+\r
+    // test calls to SetLabelText() and then to GetLabel()\r
+\r
+    const wxString& testLabel = "label without mnemonics and markup";\r
+    SET_LABEL_TEXT(testLabel);\r
+    CPPUNIT_ASSERT_EQUAL( testLabel, m_st->GetLabel() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabel, m_stWithMarkup->GetLabel() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabel, m_cb->GetLabel() );\r
+\r
+    const wxString& testLabel2 = "label with &mnemonic";\r
+    const wxString& testLabelText2 = "label with &&mnemonic";\r
+    SET_LABEL_TEXT(testLabel2);\r
+    CPPUNIT_ASSERT_EQUAL( testLabelText2, m_st->GetLabel() );\r
+    CPPUNIT_ASSERT_EQUAL( "label with &amp;mnemonic", m_stWithMarkup->GetLabel() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabelText2, m_cb->GetLabel() );\r
+\r
+    const wxString& testLabel3 = "label with <span foreground='blue'>some</span> <b>markup</b>";\r
+    SET_LABEL_TEXT(testLabel3);\r
+    CPPUNIT_ASSERT_EQUAL( testLabel3, m_st->GetLabel() );\r
+    CPPUNIT_ASSERT_EQUAL( "label with &lt;span foreground=&apos;blue&apos;&gt;some&lt;/span&gt; &lt;b&gt;markup&lt;/b&gt;", m_stWithMarkup->GetLabel() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabel3, m_cb->GetLabel() );\r
+\r
+    const wxString& testLabel4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic";\r
+    const wxString& testLabelText4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &&mnemonic";\r
+    SET_LABEL_TEXT(testLabel4);\r
+    CPPUNIT_ASSERT_EQUAL( testLabelText4, m_st->GetLabel() );\r
+    CPPUNIT_ASSERT_EQUAL( "label with &lt;span foreground=&apos;blue&apos;&gt;some&lt;/span&gt; &lt;b&gt;markup&lt;/b&gt; and &amp;mnemonic", m_stWithMarkup->GetLabel() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabelText4, m_cb->GetLabel() );\r
+}\r
+\r
+void LabelTestCase::GetLabelText()\r
+{\r
+    // test calls to SetLabel() and then to GetLabelText()\r
+\r
+    const wxString& testLabel = "label without mnemonics and markup";\r
+    SET_LABEL(testLabel);\r
+    CPPUNIT_ASSERT_EQUAL( testLabel, m_st->GetLabelText() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabel, m_stWithMarkup->GetLabelText() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabel, m_cb->GetLabelText() );\r
+\r
+    const wxString& testLabel2 = "label with &mnemonic";\r
+    const wxString& testLabelText2 = "label with mnemonic";\r
+    SET_LABEL(testLabel2);\r
+    CPPUNIT_ASSERT_EQUAL( testLabelText2, m_st->GetLabelText() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabelText2, m_stWithMarkup->GetLabelText() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabelText2, m_cb->GetLabelText() );\r
+\r
+    const wxString& testLabel3 = "label with <span foreground='blue'>some</span> <b>markup</b>";\r
+    SET_LABEL(testLabel3);\r
+    CPPUNIT_ASSERT_EQUAL( testLabel3, m_st->GetLabelText() );\r
+    CPPUNIT_ASSERT_EQUAL( "label with some markup", m_stWithMarkup->GetLabelText() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabel3, m_cb->GetLabelText() );\r
+\r
+    const wxString& testLabel4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic";\r
+    const wxString& testLabelText4 = "label with <span foreground='blue'>some</span> <b>markup</b> and mnemonic";\r
+    SET_LABEL(testLabel4);\r
+    CPPUNIT_ASSERT_EQUAL( testLabelText4, m_st->GetLabelText() );\r
+    CPPUNIT_ASSERT_EQUAL( "label with some markup and mnemonic", m_stWithMarkup->GetLabelText() );\r
+    CPPUNIT_ASSERT_EQUAL( testLabelText4, m_cb->GetLabelText() );\r
+\r
+\r
+    const wxString testLabelArray[] = {\r
+        "label without mnemonics and markup",\r
+        "label with &mnemonic",\r
+        "label with <span foreground='blue'>some</span> <b>markup</b>",\r
+        "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",\r
+    };\r
+\r
+    // test calls to SetLabelText() and then to GetLabelText()\r
+\r
+    for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )\r
+    {\r
+        SET_LABEL_TEXT(testLabelArray[s]);\r
+\r
+        // GetLabelText() should always return the string passed to SetLabelText()\r
+        CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_st->GetLabelText() );\r
+        CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_stWithMarkup->GetLabelText() );\r
+        CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_cb->GetLabelText() );\r
+    }\r
+}\r
+\r
+void LabelTestCase::Statics()\r
+{\r
+    CPPUNIT_ASSERT_EQUAL( "mnemonic", wxControl::RemoveMnemonics("&mnemonic") );\r
+    CPPUNIT_ASSERT_EQUAL( "&mnemonic", wxControl::RemoveMnemonics("&&mnemonic") );\r
+    CPPUNIT_ASSERT_EQUAL( "&mnemonic", wxControl::RemoveMnemonics("&&&mnemonic") );\r
+    CPPUNIT_ASSERT_EQUAL( "", wxStaticText::RemoveMarkup("<b></b>") );\r
+}\r