]> git.saurik.com Git - wxWidgets.git/commitdiff
Fix bug when setting an out of range date in wxMSW wxDatePickerCtrl.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 19 Jun 2011 22:46:45 +0000 (22:46 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 19 Jun 2011 22:46:45 +0000 (22:46 +0000)
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

12 files changed:
src/msw/datectrl.cpp
tests/Makefile.in
tests/controls/datepickerctrltest.cpp [new file with mode: 0644]
tests/makefile.bcc
tests/makefile.gcc
tests/makefile.vc
tests/makefile.wat
tests/test.bkl
tests/test_test_gui.dsp
tests/test_vc7_test_gui.vcproj
tests/test_vc8_test_gui.vcproj
tests/test_vc9_test_gui.vcproj

index 31952163c809d16b5d8ffd4220e2aaa6a0db6f22..2bf4df09b2af463fc45a47fbd0cdb0393e1b5fc5 100644 (file)
@@ -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
index 9f9506631b4b4c03012b5781d8962694771112ab..61c235eaee5f67e078a03a6a2e354889a1c46123 100644 (file)
@@ -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 (file)
index 0000000..b28b149
--- /dev/null
@@ -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 <vadim@wxwidgets.org>
+///////////////////////////////////////////////////////////////////////////////
+
+#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
index da4658bfb1b18b007be30310a8d219afc0074888..3f816730c28f8ec542e88157ccf43477bcab31a4 100644 (file)
@@ -151,6 +151,7 @@ TEST_GUI_OBJECTS =  \
        $(OBJS)\test_gui_choicebooktest.obj \\r
        $(OBJS)\test_gui_choicetest.obj \\r
        $(OBJS)\test_gui_comboboxtest.obj \\r
+       $(OBJS)\test_gui_datepickerctrltest.obj \
        $(OBJS)\test_gui_frametest.obj \\r
        $(OBJS)\test_gui_gaugetest.obj \\r
        $(OBJS)\test_gui_gridtest.obj \\r
@@ -779,6 +780,9 @@ $(OBJS)\test_gui_choicetest.obj: .\controls\choicetest.cpp
 $(OBJS)\test_gui_comboboxtest.obj: .\controls\comboboxtest.cpp\r
        $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\controls\comboboxtest.cpp\r
 \r
+$(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\r
        $(CXX) -q -c -P -o$@ $(TEST_GUI_CXXFLAGS) .\controls\frametest.cpp\r
 \r
index 05aa99bef412a9a1b156d67c8ea0907d17972da4..652df038cad17accb674f756836cbf4e8ad81135 100644 (file)
@@ -144,6 +144,7 @@ TEST_GUI_OBJECTS =  \
        $(OBJS)\test_gui_choicebooktest.o \\r
        $(OBJS)\test_gui_choicetest.o \\r
        $(OBJS)\test_gui_comboboxtest.o \\r
+       $(OBJS)\test_gui_datepickerctrltest.o \
        $(OBJS)\test_gui_frametest.o \\r
        $(OBJS)\test_gui_gaugetest.o \\r
        $(OBJS)\test_gui_gridtest.o \\r
@@ -760,6 +761,9 @@ $(OBJS)\test_gui_choicetest.o: ./controls/choicetest.cpp
 $(OBJS)\test_gui_comboboxtest.o: ./controls/comboboxtest.cpp\r
        $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<\r
 \r
+$(OBJS)\test_gui_datepickerctrltest.o: ./controls/datepickerctrltest.cpp
+       $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<
+
 $(OBJS)\test_gui_frametest.o: ./controls/frametest.cpp\r
        $(CXX) -c -o $@ $(TEST_GUI_CXXFLAGS) $(CPPDEPS) $<\r
 \r
index 0de79873de17aa674e9b05ae8402f4682f99e099..a509e659eed4e66c1b4de9b7ef80b1cc3ad91bd6 100644 (file)
@@ -146,6 +146,7 @@ TEST_GUI_OBJECTS =  \
        $(OBJS)\test_gui_choicebooktest.obj \\r
        $(OBJS)\test_gui_choicetest.obj \\r
        $(OBJS)\test_gui_comboboxtest.obj \\r
+       $(OBJS)\test_gui_datepickerctrltest.obj \
        $(OBJS)\test_gui_frametest.obj \\r
        $(OBJS)\test_gui_gaugetest.obj \\r
        $(OBJS)\test_gui_gridtest.obj \\r
@@ -905,6 +906,9 @@ $(OBJS)\test_gui_choicetest.obj: .\controls\choicetest.cpp
 $(OBJS)\test_gui_comboboxtest.obj: .\controls\comboboxtest.cpp\r
        $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\controls\comboboxtest.cpp\r
 \r
+$(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\r
        $(CXX) /c /nologo /TP /Fo$@ $(TEST_GUI_CXXFLAGS) .\controls\frametest.cpp\r
 \r
index f89e93c51d4cae8b2d6ce76109121fbd101ed078..12fca8172ad8d4383b0b520f81cfe90981420b3b 100644 (file)
@@ -390,6 +390,7 @@ TEST_GUI_OBJECTS =  &
        $(OBJS)\test_gui_choicebooktest.obj &\r
        $(OBJS)\test_gui_choicetest.obj &\r
        $(OBJS)\test_gui_comboboxtest.obj &\r
+       $(OBJS)\test_gui_datepickerctrltest.obj &
        $(OBJS)\test_gui_frametest.obj &\r
        $(OBJS)\test_gui_gaugetest.obj &\r
        $(OBJS)\test_gui_gridtest.obj &\r
@@ -819,6 +820,9 @@ $(OBJS)\test_gui_choicetest.obj :  .AUTODEPEND .\controls\choicetest.cpp
 $(OBJS)\test_gui_comboboxtest.obj :  .AUTODEPEND .\controls\comboboxtest.cpp\r
        $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $<\r
 \r
+$(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\r
        $(CXX) -bt=nt -zq -fo=$^@ $(TEST_GUI_CXXFLAGS) $<\r
 \r
index 4087a16b67c2e1a94ae7ba10de3ba2d39bb46c14..6530babbb109c7313253b901f7675695ec9b255d 100644 (file)
             controls/choicebooktest.cpp
             controls/choicetest.cpp
             controls/comboboxtest.cpp
+            controls/datepickerctrltest.cpp
             controls/frametest.cpp
             controls/gaugetest.cpp
             controls/gridtest.cpp
index 9fd20781c21b6f707de2c95a2270fff0582dfcf6..4ebfd627270773ac05d22eebf0b1ac04dcd5a2fc 100644 (file)
@@ -307,6 +307,10 @@ SOURCE=.\config\config.cpp
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=.\controls\datepickerctrltest.cpp
+# End Source File
+# Begin Source File
+
 SOURCE=.\dummy.cpp\r
 # ADD BASE CPP /Yc"testprec.h"\r
 # ADD CPP /Yc"testprec.h"\r
index 861d041cec3cb5b78d78dd6001f89e4378f9729f..344e7b10594117254548dfc5d5ede661f44b4e5a 100644 (file)
                                RelativePath=".\config\config.cpp">\r
                        </File>\r
                        <File\r
+                               RelativePath=".\controls\datepickerctrltest.cpp">
+                       </File>
+                       <File
                                RelativePath=".\dummy.cpp">\r
                                <FileConfiguration\r
                                        Name="Debug|Win32">\r
index 8bfa56f191b48a03575faec4964eb8d9996ea3e9..978c1aa7332da76ce4316b028f3e83f1131e56f9 100644 (file)
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\controls\datepickerctrltest.cpp"
+                               >
+                       </File>
+                       <File
                                RelativePath=".\dummy.cpp"\r
                                >\r
                                <FileConfiguration\r
index 76f09246555823805742f8fc0a324b9c43084d1f..dc18d968728c5c572fad463b44ad53f1fbd8d68e 100644 (file)
                                >\r
                        </File>\r
                        <File\r
+                               RelativePath=".\controls\datepickerctrltest.cpp"
+                               >
+                       </File>
+                       <File
                                RelativePath=".\dummy.cpp"\r
                                >\r
                                <FileConfiguration\r