From: Vadim Zeitlin Date: Sun, 19 Jun 2011 22:46:45 +0000 (+0000) Subject: Fix bug when setting an out of range date in wxMSW wxDatePickerCtrl. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/5f899cbe913d58a2b43d00b83c617518376cbe56?ds=inline Fix bug when setting an out of range date in wxMSW wxDatePickerCtrl. The internally stored date value was getting out of sync with the real date in the control itself when SetValue() was called with a date out of the currently set range. This resulted in an assert failure and other unpleasantness later. Fix the bug and add a unit test checking for it (and also with some other basic wxDatePickerCtrl checks). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67990 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/src/msw/datectrl.cpp b/src/msw/datectrl.cpp index 31952163c8..2bf4df09b2 100644 --- a/src/msw/datectrl.cpp +++ b/src/msw/datectrl.cpp @@ -193,7 +193,19 @@ void wxDatePickerCtrl::SetValue(const wxDateTime& dt) dt.IsValid() ? GDT_VALID : GDT_NONE, &st) ) { - wxLogDebug(wxT("DateTime_SetSystemtime() failed")); + // Attempts to set the date outside of the valid range should fail so + // there is nothing unexpected if they do but still log a message if we + // failed for some other reason. + wxDateTime dtStart, dtEnd; + GetRange(&dtStart, &dtEnd); + if ( (!dtStart.IsValid() || dt >= dtStart) && + (!dtEnd.IsValid() || dt <= dtEnd) ) + { + wxLogDebug(wxT("DateTime_SetSystemtime() unexpectedly failed")); + } + + // In any case, skip updating m_date below. + return; } // we need to keep only the date part, times don't make sense for this diff --git a/tests/Makefile.in b/tests/Makefile.in index 9f9506631b..61c235eaee 100644 --- a/tests/Makefile.in +++ b/tests/Makefile.in @@ -166,6 +166,7 @@ TEST_GUI_OBJECTS = \ test_gui_choicebooktest.o \ test_gui_choicetest.o \ test_gui_comboboxtest.o \ + test_gui_datepickerctrltest.o \ test_gui_frametest.o \ test_gui_gaugetest.o \ test_gui_gridtest.o \ @@ -731,6 +732,9 @@ test_gui_choicetest.o: $(srcdir)/controls/choicetest.cpp $(TEST_GUI_ODEP) test_gui_comboboxtest.o: $(srcdir)/controls/comboboxtest.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/controls/comboboxtest.cpp +test_gui_datepickerctrltest.o: $(srcdir)/controls/datepickerctrltest.cpp $(TEST_GUI_ODEP) + $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/controls/datepickerctrltest.cpp + test_gui_frametest.o: $(srcdir)/controls/frametest.cpp $(TEST_GUI_ODEP) $(CXXC) -c -o $@ $(TEST_GUI_CXXFLAGS) $(srcdir)/controls/frametest.cpp diff --git a/tests/controls/datepickerctrltest.cpp b/tests/controls/datepickerctrltest.cpp new file mode 100644 index 0000000000..b28b1491e9 --- /dev/null +++ b/tests/controls/datepickerctrltest.cpp @@ -0,0 +1,112 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: tests/controls/datepickerctrltest.cpp +// Purpose: wxDatePickerCtrl unit test +// Author: Vadim Zeitlin +// Created: 2011-06-18 +// RCS-ID: $Id$ +// Copyright: (c) 2011 Vadim Zeitlin +/////////////////////////////////////////////////////////////////////////////// + +#include "testprec.h" + +#if wxUSE_DATEPICKCTRL + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/app.h" +#endif // WX_PRECOMP + +#include "wx/datectrl.h" + +#include "testableframe.h" +#include "testdate.h" + +class DatePickerCtrlTestCase : public CppUnit::TestCase +{ +public: + DatePickerCtrlTestCase() { } + + void setUp(); + void tearDown(); + +private: + CPPUNIT_TEST_SUITE( DatePickerCtrlTestCase ); + CPPUNIT_TEST( Value ); + CPPUNIT_TEST( Range ); + CPPUNIT_TEST_SUITE_END(); + + void Value(); + void Range(); + + wxDatePickerCtrl* m_datepicker; + + DECLARE_NO_COPY_CLASS(DatePickerCtrlTestCase) +}; + +// register in the unnamed registry so that these tests are run by default +CPPUNIT_TEST_SUITE_REGISTRATION( DatePickerCtrlTestCase ); + +// also include in its own registry so that these tests can be run alone +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DatePickerCtrlTestCase, "DatePickerCtrlTestCase" ); + +void DatePickerCtrlTestCase::setUp() +{ + m_datepicker = new wxDatePickerCtrl(wxTheApp->GetTopWindow(), wxID_ANY); +} + +void DatePickerCtrlTestCase::tearDown() +{ + delete m_datepicker; +} + +void DatePickerCtrlTestCase::Value() +{ + const wxDateTime dt(18, wxDateTime::Jul, 2011); + m_datepicker->SetValue(dt); + + CPPUNIT_ASSERT_EQUAL( dt, m_datepicker->GetValue() ); + + // We don't use wxDP_ALLOWNONE currently, hence a value is required. + WX_ASSERT_FAILS_WITH_ASSERT( m_datepicker->SetValue(wxDateTime()) ); +} + +void DatePickerCtrlTestCase::Range() +{ + // Initially we have no valid range but MSW version still has (built in) + // minimum as it doesn't support dates before 1601-01-01, hence don't rely + // on GetRange() returning false. + wxDateTime dtRangeStart, dtRangeEnd; + m_datepicker->GetRange(&dtRangeStart, &dtRangeEnd); + CPPUNIT_ASSERT( !dtRangeEnd.IsValid() ); + + // After we set it we should be able to get it back. + const wxDateTime + dtStart(15, wxDateTime::Feb, 1923), + dtEnd(18, wxDateTime::Jun, 2011); + + m_datepicker->SetRange(dtStart, dtEnd); + CPPUNIT_ASSERT( m_datepicker->GetRange(&dtRangeStart, &dtRangeEnd) ); + CPPUNIT_ASSERT_EQUAL( dtStart, dtRangeStart ); + CPPUNIT_ASSERT_EQUAL( dtEnd, dtRangeEnd ); + + // Setting dates inside the range should work, including the range end + // points. + m_datepicker->SetValue(dtStart); + CPPUNIT_ASSERT_EQUAL( dtStart, m_datepicker->GetValue() ); + + m_datepicker->SetValue(dtEnd); + CPPUNIT_ASSERT_EQUAL( dtEnd, m_datepicker->GetValue() ); + + + // Setting dates outside the range should not work. + m_datepicker->SetValue(dtEnd + wxTimeSpan::Day()); + CPPUNIT_ASSERT_EQUAL( dtEnd, m_datepicker->GetValue() ); + + m_datepicker->SetValue(dtStart - wxTimeSpan::Day()); + CPPUNIT_ASSERT_EQUAL( dtEnd, m_datepicker->GetValue() ); +} + +#endif // wxUSE_DATEPICKCTRL diff --git a/tests/makefile.bcc b/tests/makefile.bcc index da4658bfb1..3f816730c2 100644 --- a/tests/makefile.bcc +++ b/tests/makefile.bcc @@ -151,6 +151,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_choicebooktest.obj \ $(OBJS)\test_gui_choicetest.obj \ $(OBJS)\test_gui_comboboxtest.obj \ + $(OBJS)\test_gui_datepickerctrltest.obj \ $(OBJS)\test_gui_frametest.obj \ $(OBJS)\test_gui_gaugetest.obj \ $(OBJS)\test_gui_gridtest.obj \ @@ -779,6 +780,9 @@ $(OBJS)\test_gui_choicetest.obj: .\controls\choicetest.cpp $(OBJS)\test_gui_comboboxtest.obj: .\controls\comboboxtest.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\controls\comboboxtest.cpp +$(OBJS)\test_gui_datepickerctrltest.obj: .\controls\datepickerctrltest.cpp + $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\controls\datepickerctrltest.cpp + $(OBJS)\test_gui_frametest.obj: .\controls\frametest.cpp $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\controls\frametest.cpp diff --git a/tests/makefile.gcc b/tests/makefile.gcc index 05aa99bef4..652df038ca 100644 --- a/tests/makefile.gcc +++ b/tests/makefile.gcc @@ -144,6 +144,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_choicebooktest.o \ $(OBJS)\test_gui_choicetest.o \ $(OBJS)\test_gui_comboboxtest.o \ + $(OBJS)\test_gui_datepickerctrltest.o \ $(OBJS)\test_gui_frametest.o \ $(OBJS)\test_gui_gaugetest.o \ $(OBJS)\test_gui_gridtest.o \ @@ -760,6 +761,9 @@ $(OBJS)\test_gui_choicetest.o: ./controls/choicetest.cpp $(OBJS)\test_gui_comboboxtest.o: ./controls/comboboxtest.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< +$(OBJS)\test_gui_datepickerctrltest.o: ./controls/datepickerctrltest.cpp + $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< + $(OBJS)\test_gui_frametest.o: ./controls/frametest.cpp $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $< diff --git a/tests/makefile.vc b/tests/makefile.vc index 0de79873de..a509e659ee 100644 --- a/tests/makefile.vc +++ b/tests/makefile.vc @@ -146,6 +146,7 @@ TEST_GUI_OBJECTS = \ $(OBJS)\test_gui_choicebooktest.obj \ $(OBJS)\test_gui_choicetest.obj \ $(OBJS)\test_gui_comboboxtest.obj \ + $(OBJS)\test_gui_datepickerctrltest.obj \ $(OBJS)\test_gui_frametest.obj \ $(OBJS)\test_gui_gaugetest.obj \ $(OBJS)\test_gui_gridtest.obj \ @@ -905,6 +906,9 @@ $(OBJS)\test_gui_choicetest.obj: .\controls\choicetest.cpp $(OBJS)\test_gui_comboboxtest.obj: .\controls\comboboxtest.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\controls\comboboxtest.cpp +$(OBJS)\test_gui_datepickerctrltest.obj: .\controls\datepickerctrltest.cpp + $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\controls\datepickerctrltest.cpp + $(OBJS)\test_gui_frametest.obj: .\controls\frametest.cpp $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\controls\frametest.cpp diff --git a/tests/makefile.wat b/tests/makefile.wat index f89e93c51d..12fca8172a 100644 --- a/tests/makefile.wat +++ b/tests/makefile.wat @@ -390,6 +390,7 @@ TEST_GUI_OBJECTS = & $(OBJS)\test_gui_choicebooktest.obj & $(OBJS)\test_gui_choicetest.obj & $(OBJS)\test_gui_comboboxtest.obj & + $(OBJS)\test_gui_datepickerctrltest.obj & $(OBJS)\test_gui_frametest.obj & $(OBJS)\test_gui_gaugetest.obj & $(OBJS)\test_gui_gridtest.obj & @@ -819,6 +820,9 @@ $(OBJS)\test_gui_choicetest.obj : .AUTODEPEND .\controls\choicetest.cpp $(OBJS)\test_gui_comboboxtest.obj : .AUTODEPEND .\controls\comboboxtest.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< +$(OBJS)\test_gui_datepickerctrltest.obj : .AUTODEPEND .\controls\datepickerctrltest.cpp + $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< + $(OBJS)\test_gui_frametest.obj : .AUTODEPEND .\controls\frametest.cpp $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $< diff --git a/tests/test.bkl b/tests/test.bkl index 4087a16b67..6530babbb1 100644 --- a/tests/test.bkl +++ b/tests/test.bkl @@ -147,6 +147,7 @@ controls/choicebooktest.cpp controls/choicetest.cpp controls/comboboxtest.cpp + controls/datepickerctrltest.cpp controls/frametest.cpp controls/gaugetest.cpp controls/gridtest.cpp diff --git a/tests/test_test_gui.dsp b/tests/test_test_gui.dsp index 9fd20781c2..4ebfd62727 100644 --- a/tests/test_test_gui.dsp +++ b/tests/test_test_gui.dsp @@ -307,6 +307,10 @@ SOURCE=.\config\config.cpp # End Source File # Begin Source File +SOURCE=.\controls\datepickerctrltest.cpp +# End Source File +# Begin Source File + SOURCE=.\dummy.cpp # ADD BASE CPP /Yc"testprec.h" # ADD CPP /Yc"testprec.h" diff --git a/tests/test_vc7_test_gui.vcproj b/tests/test_vc7_test_gui.vcproj index 861d041cec..344e7b1059 100644 --- a/tests/test_vc7_test_gui.vcproj +++ b/tests/test_vc7_test_gui.vcproj @@ -620,6 +620,9 @@ RelativePath=".\config\config.cpp"> + + diff --git a/tests/test_vc8_test_gui.vcproj b/tests/test_vc8_test_gui.vcproj index 8bfa56f191..978c1aa733 100644 --- a/tests/test_vc8_test_gui.vcproj +++ b/tests/test_vc8_test_gui.vcproj @@ -900,6 +900,10 @@ > + + + +