File: label.cpp

package info (click to toggle)
wxpython4.0 4.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 211,112 kB
  • sloc: cpp: 888,355; python: 223,130; makefile: 52,087; ansic: 45,780; sh: 3,012; xml: 1,534; perl: 264
file content (195 lines) | stat: -rw-r--r-- 6,954 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/controls/label.cpp
// Purpose:     wxControl and wxStaticText label tests
// Author:      Francesco Montorsi
// Created:     2010-3-21
// Copyright:   (c) 2010 Francesco Montorsi
///////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#include "testprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#ifndef WX_PRECOMP
    #include "wx/app.h"
#endif // WX_PRECOMP

#include "wx/control.h"
#include "wx/stattext.h"
#include "wx/checkbox.h"

// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------

class LabelTestCase : public CppUnit::TestCase
{
public:
    LabelTestCase() { }

    virtual void setUp();
    virtual void tearDown();

private:
    CPPUNIT_TEST_SUITE( LabelTestCase );
        CPPUNIT_TEST( GetLabel );
        CPPUNIT_TEST( GetLabelText );
        CPPUNIT_TEST( Statics );
    CPPUNIT_TEST_SUITE_END();

    void GetLabel();
    void GetLabelText();
    void Statics();

    wxStaticText *m_st;

    // we cannot test wxControl directly (it's abstract) so we rather test wxCheckBox
    wxCheckBox *m_cb;

    DECLARE_NO_COPY_CLASS(LabelTestCase)
};

// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( LabelTestCase );

// also include in its own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( LabelTestCase, "LabelTestCase" );

// ----------------------------------------------------------------------------
// test initialization
// ----------------------------------------------------------------------------

#define ORIGINAL_LABEL      "original label"

void LabelTestCase::setUp()
{
    m_st = new wxStaticText(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL);

    m_cb = new wxCheckBox(wxTheApp->GetTopWindow(), wxID_ANY, ORIGINAL_LABEL);

    CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_st->GetLabel() );
    CPPUNIT_ASSERT_EQUAL( ORIGINAL_LABEL, m_cb->GetLabel() );
}

void LabelTestCase::tearDown()
{
    wxDELETE(m_st);
    wxDELETE(m_cb);
}

// ----------------------------------------------------------------------------
// the tests themselves
// ----------------------------------------------------------------------------

#define SET_LABEL(str)                              \
        m_st->SetLabel(str);                        \
        m_cb->SetLabel(str);

#define SET_LABEL_TEXT(str)                         \
        m_st->SetLabelText(str);                    \
        m_cb->SetLabelText(str);

void LabelTestCase::GetLabel()
{
    const wxString testLabelArray[] = {
        "label without mnemonics and markup",
        "label with &mnemonic",
        "label with <span foreground='blue'>some</span> <b>markup</b>",
        "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",
    };

    // test calls to SetLabel() and then to GetLabel()

    for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )
    {
        SET_LABEL(testLabelArray[s]);

        // GetLabel() should always return the string passed to SetLabel()
        CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_st->GetLabel() );
        CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_cb->GetLabel() );
    }


    // test calls to SetLabelText() and then to GetLabel()

    const wxString& testLabel = "label without mnemonics and markup";
    SET_LABEL_TEXT(testLabel);
    CPPUNIT_ASSERT_EQUAL( testLabel, m_st->GetLabel() );
    CPPUNIT_ASSERT_EQUAL( testLabel, m_cb->GetLabel() );

    const wxString& testLabel2 = "label with &mnemonic";
    const wxString& testLabelText2 = "label with &&mnemonic";
    SET_LABEL_TEXT(testLabel2);
    CPPUNIT_ASSERT_EQUAL( testLabelText2, m_st->GetLabel() );
    CPPUNIT_ASSERT_EQUAL( testLabelText2, m_cb->GetLabel() );

    const wxString& testLabel3 = "label with <span foreground='blue'>some</span> <b>markup</b>";
    SET_LABEL_TEXT(testLabel3);
    CPPUNIT_ASSERT_EQUAL( testLabel3, m_st->GetLabel() );
    CPPUNIT_ASSERT_EQUAL( testLabel3, m_cb->GetLabel() );

    const wxString& testLabel4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic";
    const wxString& testLabelText4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &&mnemonic";
    SET_LABEL_TEXT(testLabel4);
    CPPUNIT_ASSERT_EQUAL( testLabelText4, m_st->GetLabel() );
    CPPUNIT_ASSERT_EQUAL( testLabelText4, m_cb->GetLabel() );
}

void LabelTestCase::GetLabelText()
{
    // test calls to SetLabel() and then to GetLabelText()

    const wxString& testLabel = "label without mnemonics and markup";
    SET_LABEL(testLabel);
    CPPUNIT_ASSERT_EQUAL( testLabel, m_st->GetLabelText() );
    CPPUNIT_ASSERT_EQUAL( testLabel, m_cb->GetLabelText() );

    const wxString& testLabel2 = "label with &mnemonic";
    const wxString& testLabelText2 = "label with mnemonic";
    SET_LABEL(testLabel2);
    CPPUNIT_ASSERT_EQUAL( testLabelText2, m_st->GetLabelText() );
    CPPUNIT_ASSERT_EQUAL( testLabelText2, m_cb->GetLabelText() );

    const wxString& testLabel3 = "label with <span foreground='blue'>some</span> <b>markup</b>";
    SET_LABEL(testLabel3);
    CPPUNIT_ASSERT_EQUAL( testLabel3, m_st->GetLabelText() );
    CPPUNIT_ASSERT_EQUAL( testLabel3, m_cb->GetLabelText() );

    const wxString& testLabel4 = "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic";
    const wxString& testLabelText4 = "label with <span foreground='blue'>some</span> <b>markup</b> and mnemonic";
    SET_LABEL(testLabel4);
    CPPUNIT_ASSERT_EQUAL( testLabelText4, m_st->GetLabelText() );
    CPPUNIT_ASSERT_EQUAL( testLabelText4, m_cb->GetLabelText() );


    const wxString testLabelArray[] = {
        "label without mnemonics and markup",
        "label with &mnemonic",
        "label with <span foreground='blue'>some</span> <b>markup</b>",
        "label with <span foreground='blue'>some</span> <b>markup</b> and &mnemonic",
    };

    // test calls to SetLabelText() and then to GetLabelText()

    for ( unsigned int s = 0; s < WXSIZEOF(testLabelArray); s++ )
    {
        SET_LABEL_TEXT(testLabelArray[s]);

        // GetLabelText() should always return the string passed to SetLabelText()
        CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_st->GetLabelText() );
        CPPUNIT_ASSERT_EQUAL( testLabelArray[s], m_cb->GetLabelText() );
    }
}

void LabelTestCase::Statics()
{
    CPPUNIT_ASSERT_EQUAL( "mnemonic", wxControl::RemoveMnemonics("&mnemonic") );
    CPPUNIT_ASSERT_EQUAL( "&mnemonic", wxControl::RemoveMnemonics("&&mnemonic") );
    CPPUNIT_ASSERT_EQUAL( "&mnemonic", wxControl::RemoveMnemonics("&&&mnemonic") );
}