added unit test for some of wxTextCtrl methods and its events generation

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@48943 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2007-09-26 00:28:31 +00:00
parent 232f89100e
commit c0d9b217a7
9 changed files with 210 additions and 13 deletions

View File

@@ -106,7 +106,8 @@ TEST_GUI_OBJECTS = \
test_gui_rect.o \
test_gui_size.o \
test_gui_point.o \
test_gui_config.o
test_gui_config.o \
test_gui_textctrltest.o
TEST_GUI_ODEP = $(___pch_testprec_test_gui_testprec_h_gch___depname)
PRINTFBENCH_CXXFLAGS = $(__printfbench_PCH_INC) -D__WX$(TOOLKIT)__ \
$(__WXUNIV_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) \
@@ -464,6 +465,9 @@ test_gui_point.o: $(srcdir)/geometry/point.cpp $(TEST_GUI_ODEP)
test_gui_config.o: $(srcdir)/config/config.cpp $(TEST_GUI_ODEP)
$(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/config/config.cpp
test_gui_textctrltest.o: $(srcdir)/controls/textctrltest.cpp $(TEST_GUI_ODEP)
$(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/controls/textctrltest.cpp
printfbench_printfbench.o: $(srcdir)/benchmarks/printfbench.cpp $(PRINTFBENCH_ODEP)
$(CXXC) -c -o $@ $(PRINTFBENCH_CXXFLAGS) $(srcdir)/benchmarks/printfbench.cpp

View File

@@ -0,0 +1,148 @@
///////////////////////////////////////////////////////////////////////////////
// Name: tests/textctrl/textctrltest.cpp
// Purpose: wxTextCtrl unit test
// Author: Vadim Zeitlin
// Created: 2007-09-25
// RCS-ID: $Id$
// Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwidgets.org>
///////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#include "testprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/textctrl.h"
#endif // WX_PRECOMP
// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------
class TextCtrlTestCase : public CppUnit::TestCase
{
public:
TextCtrlTestCase() { }
virtual void setUp();
virtual void tearDown();
private:
CPPUNIT_TEST_SUITE( TextCtrlTestCase );
CPPUNIT_TEST( SetValue );
CPPUNIT_TEST( TextChangeEvents );
CPPUNIT_TEST_SUITE_END();
void SetValue();
void TextChangeEvents();
wxTextCtrl *m_text;
wxFrame *m_frame;
DECLARE_NO_COPY_CLASS(TextCtrlTestCase)
};
// register in the unnamed registry so that these tests are run by default
CPPUNIT_TEST_SUITE_REGISTRATION( TextCtrlTestCase );
// also include in it's own registry so that these tests can be run alone
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( TextCtrlTestCase, "TextCtrlTestCase" );
// ----------------------------------------------------------------------------
// test initialization
// ----------------------------------------------------------------------------
void TextCtrlTestCase::setUp()
{
m_text = new wxTextCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
}
void TextCtrlTestCase::tearDown()
{
delete m_text;
m_text = NULL;
}
// ----------------------------------------------------------------------------
// tests themselves
// ----------------------------------------------------------------------------
void TextCtrlTestCase::SetValue()
{
CPPUNIT_ASSERT( m_text->IsEmpty() );
m_text->SetValue("foo");
WX_ASSERT_STR_EQUAL( "foo", m_text->GetValue() );
m_text->SetValue("");
CPPUNIT_ASSERT( m_text->IsEmpty() );
m_text->SetValue("hi");
WX_ASSERT_STR_EQUAL( "hi", m_text->GetValue() );
m_text->SetValue("bye");
WX_ASSERT_STR_EQUAL( "bye", m_text->GetValue() );
}
void TextCtrlTestCase::TextChangeEvents()
{
class TextTestEventHandler : public wxEvtHandler
{
public:
TextTestEventHandler() { m_events = 0; }
// calling this automatically resets the events counter
int GetEvents()
{
const int events = m_events;
m_events = 0;
return events;
}
void OnText(wxCommandEvent& WXUNUSED(event)) { m_events++; }
private:
int m_events;
} handler;
m_text->Connect(wxEVT_COMMAND_TEXT_UPDATED,
wxCommandEventHandler(TextTestEventHandler::OnText),
NULL,
&handler);
// notice that SetValue() generates an event even if the text didn't change
m_text->SetValue("");
CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
m_text->SetValue("foo");
CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
m_text->SetValue("foo");
CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
m_text->ChangeValue("bar");
CPPUNIT_ASSERT_EQUAL( 0, handler.GetEvents() );
m_text->AppendText("bar");
CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
m_text->Replace(3, 6, "baz");
CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
m_text->Remove(0, 3);
CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
m_text->WriteText("foo");
CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
m_text->Clear();
CPPUNIT_ASSERT_EQUAL( 1, handler.GetEvents() );
}

View File

@@ -95,7 +95,8 @@ TEST_GUI_OBJECTS = \
$(OBJS)\test_gui_rect.obj \
$(OBJS)\test_gui_size.obj \
$(OBJS)\test_gui_point.obj \
$(OBJS)\test_gui_config.obj
$(OBJS)\test_gui_config.obj \
$(OBJS)\test_gui_textctrltest.obj
PRINTFBENCH_CXXFLAGS = $(__RUNTIME_LIBS) -I$(BCCDIR)\include $(__DEBUGINFO) \
$(__OPTIMIZEFLAG) $(__THREADSFLAG_1) -D__WXMSW__ $(__WXUNIV_DEFINE_p) \
$(__DEBUG_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) \
@@ -495,6 +496,9 @@ $(OBJS)\test_gui_point.obj: .\geometry\point.cpp
$(OBJS)\test_gui_config.obj: .\config\config.cpp
$(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) $**
$(OBJS)\test_gui_textctrltest.obj: .\controls\textctrltest.cpp
$(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) $**
$(OBJS)\printfbench_dummy.obj: .\dummy.cpp
$(CXX) -q -c -P -o$@ $(PRINTFBENCH_CXXFLAGS) -H $**

View File

@@ -88,7 +88,8 @@ TEST_GUI_OBJECTS = \
$(OBJS)\test_gui_rect.o \
$(OBJS)\test_gui_size.o \
$(OBJS)\test_gui_point.o \
$(OBJS)\test_gui_config.o
$(OBJS)\test_gui_config.o \
$(OBJS)\test_gui_textctrltest.o
PRINTFBENCH_CXXFLAGS = $(__DEBUGINFO) $(__OPTIMIZEFLAG) $(__THREADSFLAG) \
$(GCCFLAGS) -DHAVE_W32API_H -D__WXMSW__ $(__WXUNIV_DEFINE_p) \
$(__DEBUG_DEFINE_p) $(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) \
@@ -473,6 +474,9 @@ $(OBJS)\test_gui_point.o: ./geometry/point.cpp
$(OBJS)\test_gui_config.o: ./config/config.cpp
$(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<
$(OBJS)\test_gui_textctrltest.o: ./controls/textctrltest.cpp
$(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<
$(OBJS)\printfbench_dummy.o: ./dummy.cpp
$(CXX) -c -o $@ $(PRINTFBENCH_CXXFLAGS) $(CPPDEPS) $<

View File

@@ -91,7 +91,8 @@ TEST_GUI_OBJECTS = \
$(OBJS)\test_gui_rect.obj \
$(OBJS)\test_gui_size.obj \
$(OBJS)\test_gui_point.obj \
$(OBJS)\test_gui_config.obj
$(OBJS)\test_gui_config.obj \
$(OBJS)\test_gui_textctrltest.obj
PRINTFBENCH_CXXFLAGS = /M$(__RUNTIME_LIBS_38)$(__DEBUGRUNTIME) /DWIN32 \
$(__DEBUGINFO) /Fd$(OBJS)\printfbench.pdb $(____DEBUGRUNTIME) \
$(__OPTIMIZEFLAG) $(__NO_VC_CRTDBG_p) /D__WXMSW__ $(__WXUNIV_DEFINE_p) \
@@ -580,6 +581,9 @@ $(OBJS)\test_gui_point.obj: .\geometry\point.cpp
$(OBJS)\test_gui_config.obj: .\config\config.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) $**
$(OBJS)\test_gui_textctrltest.obj: .\controls\textctrltest.cpp
$(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) $**
$(OBJS)\printfbench_dummy.obj: .\dummy.cpp
$(CXX) /c /nologo /TP /Fo$@ $(PRINTFBENCH_CXXFLAGS) /Yctestprec.h $**

View File

@@ -292,7 +292,8 @@ TEST_GUI_OBJECTS = &
$(OBJS)\test_gui_rect.obj &
$(OBJS)\test_gui_size.obj &
$(OBJS)\test_gui_point.obj &
$(OBJS)\test_gui_config.obj
$(OBJS)\test_gui_config.obj &
$(OBJS)\test_gui_textctrltest.obj
PRINTFBENCH_CXXFLAGS = $(__DEBUGINFO) $(__OPTIMIZEFLAG) $(__THREADSFLAG) &
$(__RUNTIME_LIBS) -d__WXMSW__ $(__WXUNIV_DEFINE_p) $(__DEBUG_DEFINE_p) &
$(__EXCEPTIONS_DEFINE_p) $(__RTTI_DEFINE_p) $(__THREAD_DEFINE_p) &
@@ -528,6 +529,9 @@ $(OBJS)\test_gui_point.obj : .AUTODEPEND .\geometry\point.cpp
$(OBJS)\test_gui_config.obj : .AUTODEPEND .\config\config.cpp
$(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $<
$(OBJS)\test_gui_textctrltest.obj : .AUTODEPEND .\controls\textctrltest.cpp
$(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $<
$(OBJS)\printfbench_dummy.obj : .AUTODEPEND .\dummy.cpp
$(CXX) -bt=nt -zq -fo=$^@ $(PRINTFBENCH_CXXFLAGS) $<

View File

@@ -87,6 +87,7 @@
geometry/size.cpp
geometry/point.cpp
config/config.cpp
controls/textctrltest.cpp
</sources>
<wx-lib>core</wx-lib>
<wx-lib>base</wx-lib>

View File

@@ -34,18 +34,25 @@ using std::vector;
using std::auto_ptr;
using std::cout;
#if wxUSE_GUI
typedef wxApp TestAppBase;
#else
typedef wxAppConsole TestAppBase;
#endif
// The application class
//
class TestApp : public wxAppConsole
class TestApp : public TestAppBase
{
public:
TestApp();
// standard overrides
void OnInitCmdLine(wxCmdLineParser& parser);
bool OnCmdLineParsed(wxCmdLineParser& parser);
bool OnInit();
int OnRun();
virtual void OnInitCmdLine(wxCmdLineParser& parser);
virtual bool OnCmdLineParsed(wxCmdLineParser& parser);
virtual bool OnInit();
virtual int OnRun();
virtual int OnExit();
private:
void List(Test *test, const string& parent = "") const;
@@ -68,6 +75,9 @@ TestApp::TestApp()
//
bool TestApp::OnInit()
{
if ( !TestAppBase::OnInit() )
return false;
cout << "Test program for wxWidgets\n"
<< "build: " << WX_BUILD_OPTIONS_SIGNATURE << std::endl;
@@ -78,14 +88,19 @@ bool TestApp::OnInit()
cout << "\n";
#endif
return wxAppConsole::OnInit();
#if wxUSE_GUI
// create a hidden parent window to be used as parent for the GUI controls
new wxFrame(NULL, wxID_ANY, "Hidden wx test frame");
#endif // wxUSE_GUI
return true;
};
// The table of command line options
//
void TestApp::OnInitCmdLine(wxCmdLineParser& parser)
{
wxAppConsole::OnInitCmdLine(parser);
TestAppBase::OnInitCmdLine(parser);
static const wxCmdLineEntryDesc cmdLineDesc[] = {
{ wxCMD_LINE_SWITCH, "l", "list",
@@ -115,7 +130,7 @@ bool TestApp::OnCmdLineParsed(wxCmdLineParser& parser)
m_longlist = parser.Found(_T("longlist"));
m_list = m_longlist || parser.Found(_T("list"));
return wxAppConsole::OnCmdLineParsed(parser);
return TestAppBase::OnCmdLineParsed(parser);
}
// Run
@@ -155,6 +170,15 @@ int TestApp::OnRun()
: EXIT_FAILURE;
}
int TestApp::OnExit()
{
#if wxUSE_GUI
delete GetTopWindow();
#endif // wxUSE_GUI
return 0;
}
// List the tests
//
void TestApp::List(Test *test, const string& parent /*=""*/) const

View File

@@ -463,6 +463,10 @@ SOURCE=.\geometry\size.cpp
SOURCE=.\test.cpp
# End Source File
# Begin Source File
SOURCE=.\controls\textctrltest.cpp
# End Source File
# End Group
# End Target
# End Project