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