From c0d9b217a7dcec3b322390efcbc5c98615cd3f16 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 26 Sep 2007 00:28:31 +0000 Subject: [PATCH] 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 --- tests/Makefile.in | 6 +- tests/controls/textctrltest.cpp | 148 ++++++++++++++++++++++++++++++++ tests/makefile.bcc | 6 +- tests/makefile.gcc | 6 +- tests/makefile.vc | 6 +- tests/makefile.wat | 6 +- tests/test.bkl | 1 + tests/test.cpp | 40 +++++++-- tests/test_test_gui.dsp | 4 + 9 files changed, 210 insertions(+), 13 deletions(-) create mode 100644 tests/controls/textctrltest.cpp diff --git a/tests/Makefile.in b/tests/Makefile.in index 3fbd386c9b..c74478c27f 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -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 diff --git a/tests/controls/textctrltest.cpp b/tests/controls/textctrltest.cpp new file mode 100644 index 0000000000..4c4234be44 --- /dev/null +++ b/tests/controls/textctrltest.cpp @@ -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 +/////////////////////////////////////////////////////////////////////////////// + +// ---------------------------------------------------------------------------- +// 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() ); +} + diff --git a/tests/makefile.bcc b/tests/makefile.bcc index 005bb1f4e5..d1ccea5631 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -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 $** diff --git a/tests/makefile.gcc b/tests/makefile.gcc index ef3d1d67c2..843ad17f4f 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -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) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index dd9c4c897a..45af219d84 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -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 $** diff --git a/tests/makefile.wat b/tests/makefile.wat index 295d7b66df..5e877a9193 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -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) $< diff --git a/tests/test.bkl b/tests/test.bkl index 81a0f71e8e..9ed82fbe3e 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -87,6 +87,7 @@ geometry/size.cpp geometry/point.cpp config/config.cpp + controls/textctrltest.cpp core base diff --git a/tests/test.cpp b/tests/test.cpp index 0da0b1de6b..b2fd91868c 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -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 diff --git a/tests/test_test_gui.dsp b/tests/test_test_gui.dsp index f3ec4c778e..4230a58a01 100644 --- a/tests/test_test_gui.dsp +++ b/tests/test_test_gui.dsp @@ -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 -- 2.45.2