1 ///////////////////////////////////////////////////////////////////////////////
2 // Name:        tests/controls/datepickerctrltest.cpp
3 // Purpose:     wxDatePickerCtrl unit test
4 // Author:      Vadim Zeitlin
5 // Created:     2011-06-18
6 // Copyright:   (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
7 ///////////////////////////////////////////////////////////////////////////////
8 
9 #include "testprec.h"
10 
11 #if wxUSE_DATEPICKCTRL
12 
13 
14 #ifndef WX_PRECOMP
15     #include "wx/app.h"
16     #include "wx/button.h"
17 #endif // WX_PRECOMP
18 
19 #include "wx/datectrl.h"
20 #include "wx/uiaction.h"
21 
22 #include "testableframe.h"
23 #include "testdate.h"
24 
25 class DatePickerCtrlTestCase : public CppUnit::TestCase
26 {
27 public:
DatePickerCtrlTestCase()28     DatePickerCtrlTestCase() { }
29 
30     void setUp() wxOVERRIDE;
31     void tearDown() wxOVERRIDE;
32 
33 private:
34     CPPUNIT_TEST_SUITE( DatePickerCtrlTestCase );
35         CPPUNIT_TEST( Value );
36         CPPUNIT_TEST( Range );
37         WXUISIM_TEST( Focus );
38     CPPUNIT_TEST_SUITE_END();
39 
40     void Value();
41     void Range();
42     void Focus();
43 
44     wxDatePickerCtrl* m_datepicker;
45     wxButton* m_button;
46 
47     wxDECLARE_NO_COPY_CLASS(DatePickerCtrlTestCase);
48 };
49 
50 // register in the unnamed registry so that these tests are run by default
51 CPPUNIT_TEST_SUITE_REGISTRATION( DatePickerCtrlTestCase );
52 
53 // also include in its own registry so that these tests can be run alone
54 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( DatePickerCtrlTestCase, "DatePickerCtrlTestCase" );
55 
setUp()56 void DatePickerCtrlTestCase::setUp()
57 {
58     m_datepicker = new wxDatePickerCtrl(wxTheApp->GetTopWindow(), wxID_ANY);
59     m_button = NULL;
60 }
61 
tearDown()62 void DatePickerCtrlTestCase::tearDown()
63 {
64     delete m_button;
65     delete m_datepicker;
66 }
67 
Value()68 void DatePickerCtrlTestCase::Value()
69 {
70     const wxDateTime dt(18, wxDateTime::Jul, 2011);
71     m_datepicker->SetValue(dt);
72 
73     CPPUNIT_ASSERT_EQUAL( dt, m_datepicker->GetValue() );
74 
75     // We don't use wxDP_ALLOWNONE currently, hence a value is required.
76     WX_ASSERT_FAILS_WITH_ASSERT( m_datepicker->SetValue(wxDateTime()) );
77 }
78 
Range()79 void DatePickerCtrlTestCase::Range()
80 {
81     // Initially we have no valid range but MSW version still has (built in)
82     // minimum as it doesn't support dates before 1601-01-01, hence don't rely
83     // on GetRange() returning false.
84     wxDateTime dtRangeStart, dtRangeEnd;
85 
86     // Default end date for QT is 31/12/7999 which is considered valid,
87     // therefore we should omit this assertion for QT
88 #ifndef __WXQT__
89     m_datepicker->GetRange(&dtRangeStart, &dtRangeEnd);
90     CPPUNIT_ASSERT( !dtRangeEnd.IsValid() );
91 #endif
92 
93     // After we set it we should be able to get it back.
94     const wxDateTime
95         dtStart(15, wxDateTime::Feb, 1923),
96         dtEnd(18, wxDateTime::Jun, 2011);
97 
98     m_datepicker->SetRange(dtStart, dtEnd);
99     CPPUNIT_ASSERT( m_datepicker->GetRange(&dtRangeStart, &dtRangeEnd) );
100     CPPUNIT_ASSERT_EQUAL( dtStart, dtRangeStart );
101     CPPUNIT_ASSERT_EQUAL( dtEnd, dtRangeEnd );
102 
103     // Setting dates inside the range should work, including the range end
104     // points.
105     m_datepicker->SetValue(dtStart);
106     CPPUNIT_ASSERT_EQUAL( dtStart, m_datepicker->GetValue() );
107 
108     m_datepicker->SetValue(dtEnd);
109     CPPUNIT_ASSERT_EQUAL( dtEnd, m_datepicker->GetValue() );
110 
111 
112     // Setting dates outside the range should not work.
113     m_datepicker->SetValue(dtEnd + wxTimeSpan::Day());
114     CPPUNIT_ASSERT_EQUAL( dtEnd, m_datepicker->GetValue() );
115 
116     m_datepicker->SetValue(dtStart - wxTimeSpan::Day());
117     CPPUNIT_ASSERT_EQUAL( dtEnd, m_datepicker->GetValue() );
118 
119 
120     // Changing the range should clamp the current value to it if necessary.
121     const wxDateTime
122         dtBeforeEnd = dtEnd - wxDateSpan::Day();
123     m_datepicker->SetRange(dtStart, dtBeforeEnd);
124     CPPUNIT_ASSERT_EQUAL( dtBeforeEnd, m_datepicker->GetValue() );
125 }
126 
127 #if wxUSE_UIACTIONSIMULATOR
128 
GetRectCenter(const wxRect & r)129 static wxPoint GetRectCenter(const wxRect& r)
130 {
131     return (r.GetTopRight() + r.GetBottomLeft()) / 2;
132 }
133 
Focus()134 void DatePickerCtrlTestCase::Focus()
135 {
136     // Create another control just to give focus to it initially.
137     m_button = new wxButton(wxTheApp->GetTopWindow(), wxID_OK);
138     m_button->Move(0, m_datepicker->GetSize().y * 3);
139     m_button->SetFocus();
140     wxYield();
141 
142     CHECK( !m_datepicker->HasFocus() );
143 
144     EventCounter setFocus(m_datepicker, wxEVT_SET_FOCUS);
145     EventCounter killFocus(m_datepicker, wxEVT_KILL_FOCUS);
146 
147     wxUIActionSimulator sim;
148 
149     sim.MouseMove(GetRectCenter(m_datepicker->GetScreenRect()));
150     sim.MouseClick();
151     wxYield();
152 
153     REQUIRE( m_datepicker->HasFocus() );
154     CHECK( setFocus.GetCount() == 1 );
155     CHECK( killFocus.GetCount() == 0 );
156 
157     sim.MouseMove(GetRectCenter(m_button->GetScreenRect()));
158     sim.MouseClick();
159     wxYield();
160 
161     CHECK( !m_datepicker->HasFocus() );
162     CHECK( setFocus.GetCount() == 1 );
163     CHECK( killFocus.GetCount() == 1 );
164 }
165 
166 #endif // wxUSE_UIACTIONSIMULATOR
167 
168 #endif // wxUSE_DATEPICKCTRL
169