tests/auto/qcalendarwidget/tst_qcalendarwidget.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #include <qcalendarwidget.h>
       
    46 #include <qtoolbutton.h>
       
    47 #include <qspinbox.h>
       
    48 #include <qmenu.h>
       
    49 #include <qdebug.h>
       
    50 #include <qdatetime.h>
       
    51 #include <qtextformat.h>
       
    52 
       
    53 
       
    54 //TESTED_CLASS=
       
    55 //TESTED_FILES=
       
    56 
       
    57 class tst_QCalendarWidget : public QObject
       
    58 {
       
    59     Q_OBJECT
       
    60 
       
    61 public:
       
    62     tst_QCalendarWidget();
       
    63     virtual ~tst_QCalendarWidget();
       
    64 public slots:
       
    65     void initTestCase();
       
    66     void cleanupTestCase();
       
    67     void init();
       
    68     void cleanup();
       
    69 
       
    70 private slots:
       
    71     void getSetCheck();
       
    72     void buttonClickCheck();
       
    73 
       
    74     void setTextFormat();
       
    75     void resetTextFormat();
       
    76 
       
    77     void setWeekdayFormat();
       
    78 };
       
    79 
       
    80 // Testing get/set functions
       
    81 void tst_QCalendarWidget::getSetCheck()
       
    82 {
       
    83     QCalendarWidget object;
       
    84 
       
    85     //horizontal header formats
       
    86     object.setHorizontalHeaderFormat(QCalendarWidget::NoHorizontalHeader);
       
    87     QCOMPARE(QCalendarWidget::NoHorizontalHeader, object.horizontalHeaderFormat());
       
    88     object.setHorizontalHeaderFormat(QCalendarWidget::SingleLetterDayNames);
       
    89     QCOMPARE(QCalendarWidget::SingleLetterDayNames, object.horizontalHeaderFormat());
       
    90     object.setHorizontalHeaderFormat(QCalendarWidget::ShortDayNames);
       
    91     QCOMPARE(QCalendarWidget::ShortDayNames, object.horizontalHeaderFormat());
       
    92     object.setHorizontalHeaderFormat(QCalendarWidget::LongDayNames);
       
    93     QCOMPARE(QCalendarWidget::LongDayNames, object.horizontalHeaderFormat());
       
    94     //vertical header formats
       
    95     object.setVerticalHeaderFormat(QCalendarWidget::ISOWeekNumbers);
       
    96     QCOMPARE(QCalendarWidget::ISOWeekNumbers, object.verticalHeaderFormat());
       
    97     object.setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
       
    98     QCOMPARE(QCalendarWidget::NoVerticalHeader, object.verticalHeaderFormat());
       
    99     //maximum Date
       
   100     QDate maxDate(2006, 7, 3);
       
   101     object.setMaximumDate(maxDate);
       
   102     QCOMPARE(maxDate, object.maximumDate());
       
   103     //minimum date
       
   104     QDate minDate(2004, 7, 3);
       
   105     object.setMinimumDate(minDate);
       
   106     QCOMPARE(minDate, object.minimumDate());
       
   107     //day of week
       
   108     object.setFirstDayOfWeek(Qt::Thursday);
       
   109     QCOMPARE(Qt::Thursday, object.firstDayOfWeek());
       
   110     //grid visible
       
   111     object.setGridVisible(true);
       
   112     QVERIFY(object.isGridVisible());
       
   113     object.setGridVisible(false);
       
   114     QVERIFY(!object.isGridVisible());
       
   115     //header visible
       
   116     object.setHeaderVisible(true);
       
   117     QVERIFY(object.isHeaderVisible());
       
   118     object.setHeaderVisible(false);
       
   119     QVERIFY(!object.isHeaderVisible());
       
   120     //selection mode
       
   121     QCOMPARE(QCalendarWidget::SingleSelection, object.selectionMode());
       
   122     object.setSelectionMode(QCalendarWidget::NoSelection);
       
   123     QCOMPARE(QCalendarWidget::NoSelection, object.selectionMode());
       
   124     object.setSelectionMode(QCalendarWidget::SingleSelection);
       
   125     QCOMPARE(QCalendarWidget::SingleSelection, object.selectionMode());
       
   126    //selected date
       
   127     QDate selectedDate(2005, 7, 3);
       
   128     QSignalSpy spy(&object, SIGNAL(selectionChanged()));
       
   129     object.setSelectedDate(selectedDate);
       
   130     QCOMPARE(spy.count(), 1);
       
   131     QCOMPARE(selectedDate, object.selectedDate());
       
   132     //month and year
       
   133     object.setCurrentPage(2004, 1);
       
   134     QCOMPARE(1, object.monthShown());
       
   135     QCOMPARE(2004, object.yearShown());
       
   136     object.showNextMonth();
       
   137     QCOMPARE(2, object.monthShown());
       
   138     object.showPreviousMonth();
       
   139     QCOMPARE(1, object.monthShown());
       
   140     object.showNextYear();
       
   141     QCOMPARE(2005, object.yearShown());
       
   142     object.showPreviousYear();
       
   143     QCOMPARE(2004, object.yearShown());
       
   144     //date range
       
   145     minDate = QDate(2006,1,1);
       
   146     maxDate = QDate(2010,12,31);
       
   147     object.setDateRange(minDate, maxDate);
       
   148     QCOMPARE(maxDate, object.maximumDate());
       
   149     QCOMPARE(minDate, object.minimumDate());
       
   150 
       
   151     //date should not go beyond the minimum.
       
   152     selectedDate = minDate.addDays(-10);
       
   153     object.setSelectedDate(selectedDate);
       
   154     QCOMPARE(minDate, object.selectedDate());
       
   155     QVERIFY(selectedDate != object.selectedDate());
       
   156     //date should not go beyond the maximum.
       
   157     selectedDate = maxDate.addDays(10);
       
   158     object.setSelectedDate(selectedDate);
       
   159     QCOMPARE(maxDate, object.selectedDate());
       
   160     QVERIFY(selectedDate != object.selectedDate());
       
   161     //show today
       
   162     QDate today = QDate::currentDate();
       
   163     object.showToday();
       
   164     QCOMPARE(today.month(), object.monthShown());
       
   165     QCOMPARE(today.year(), object.yearShown());
       
   166     //slect a different date and move.
       
   167     object.setSelectedDate(minDate);
       
   168     object.showSelectedDate();
       
   169     QCOMPARE(minDate.month(), object.monthShown());
       
   170     QCOMPARE(minDate.year(), object.yearShown());
       
   171 }
       
   172 
       
   173 void tst_QCalendarWidget::buttonClickCheck()
       
   174 {
       
   175     QCalendarWidget object;
       
   176     QSize size = object.sizeHint();
       
   177     object.setGeometry(0,0,size.width(), size.height());
       
   178     object.show();
       
   179 
       
   180     QRect rect = object.geometry();
       
   181     QDate selectedDate(2005, 1, 1);
       
   182     //click on the month buttons
       
   183     int month = object.monthShown();
       
   184     QToolButton *button = qFindChild<QToolButton *>(&object, "qt_calendar_prevmonth");
       
   185     QTest::mouseClick(button, Qt::LeftButton);
       
   186 	QCOMPARE(month > 1 ? month-1 : 12, object.monthShown());
       
   187     button = qFindChild<QToolButton *>(&object, "qt_calendar_nextmonth");
       
   188     QTest::mouseClick(button, Qt::LeftButton);
       
   189     QCOMPARE(month, object.monthShown());
       
   190 
       
   191     button = qFindChild<QToolButton *>(&object, "qt_calendar_yearbutton");
       
   192     QTest::mouseClick(button, Qt::LeftButton);
       
   193     QVERIFY(!button->isVisible());
       
   194     QSpinBox *spinbox = qFindChild<QSpinBox *>(&object, "qt_calendar_yearedit");
       
   195     QTest::qWait(500);
       
   196     QTest::keyClick(spinbox, '2');
       
   197     QTest::keyClick(spinbox, '0');
       
   198     QTest::keyClick(spinbox, '0');
       
   199     QTest::keyClick(spinbox, '6');
       
   200     QTest::qWait(500);
       
   201     QWidget *widget = qFindChild<QWidget *>(&object, "qt_calendar_calendarview");
       
   202     QTest::mouseMove(widget);
       
   203     QTest::mouseClick(widget, Qt::LeftButton);
       
   204     QCOMPARE(2006, object.yearShown());
       
   205     object.setSelectedDate(selectedDate);
       
   206     object.showSelectedDate();
       
   207     QTest::keyClick(widget, Qt::Key_Down);
       
   208     QVERIFY(selectedDate != object.selectedDate());
       
   209 
       
   210     object.setDateRange(QDate(2006,1,1), QDate(2006,2,28));
       
   211     object.setSelectedDate(QDate(2006,1,1));
       
   212     object.showSelectedDate();
       
   213     button = qFindChild<QToolButton *>(&object, "qt_calendar_prevmonth");
       
   214     QTest::mouseClick(button, Qt::LeftButton);
       
   215     QCOMPARE(1, object.monthShown());
       
   216 
       
   217     button = qFindChild<QToolButton *>(&object, "qt_calendar_nextmonth");
       
   218     QTest::mouseClick(button, Qt::LeftButton);
       
   219     QCOMPARE(2, object.monthShown());
       
   220     QTest::mouseClick(button, Qt::LeftButton);
       
   221     QCOMPARE(2, object.monthShown());
       
   222 
       
   223 }
       
   224 
       
   225 void tst_QCalendarWidget::setTextFormat()
       
   226 {
       
   227     QCalendarWidget calendar;
       
   228     QTextCharFormat format;
       
   229     format.setFontItalic(true);
       
   230     format.setForeground(Qt::green);
       
   231 
       
   232     const QDate date(1984, 10, 20);
       
   233     calendar.setDateTextFormat(date, format);
       
   234     QCOMPARE(calendar.dateTextFormat(date), format);
       
   235 }
       
   236 
       
   237 void tst_QCalendarWidget::resetTextFormat()
       
   238 {
       
   239     QCalendarWidget calendar;
       
   240     QTextCharFormat format;
       
   241     format.setFontItalic(true);
       
   242     format.setForeground(Qt::green);
       
   243 
       
   244     const QDate date(1984, 10, 20);
       
   245     calendar.setDateTextFormat(date, format);
       
   246 
       
   247     calendar.setDateTextFormat(QDate(), QTextCharFormat());
       
   248     QCOMPARE(calendar.dateTextFormat(date), QTextCharFormat());
       
   249 }
       
   250 
       
   251 void tst_QCalendarWidget::setWeekdayFormat()
       
   252 {
       
   253     QCalendarWidget calendar;
       
   254 
       
   255     QTextCharFormat format;
       
   256     format.setFontItalic(true);
       
   257     format.setForeground(Qt::green);
       
   258 
       
   259     calendar.setWeekdayTextFormat(Qt::Wednesday, format);
       
   260 
       
   261     // check the format of the a given month
       
   262     for (int i = 1; i <= 31; ++i) {
       
   263         const QDate date(1984, 10, i);
       
   264         const Qt::DayOfWeek dayOfWeek = static_cast<Qt::DayOfWeek>(date.dayOfWeek());
       
   265         if (dayOfWeek == Qt::Wednesday)
       
   266             QCOMPARE(calendar.weekdayTextFormat(dayOfWeek), format);
       
   267         else
       
   268             QVERIFY(calendar.weekdayTextFormat(dayOfWeek) != format);
       
   269     }
       
   270 }
       
   271 
       
   272 tst_QCalendarWidget::tst_QCalendarWidget()
       
   273 {
       
   274 }
       
   275 
       
   276 tst_QCalendarWidget::~tst_QCalendarWidget()
       
   277 {
       
   278 }
       
   279 
       
   280 void tst_QCalendarWidget::initTestCase()
       
   281 {
       
   282 }
       
   283 
       
   284 void tst_QCalendarWidget::cleanupTestCase()
       
   285 {
       
   286 }
       
   287 
       
   288 void tst_QCalendarWidget::init()
       
   289 {
       
   290 }
       
   291 
       
   292 void tst_QCalendarWidget::cleanup()
       
   293 {
       
   294 }
       
   295 
       
   296 QTEST_MAIN(tst_QCalendarWidget)
       
   297 #include "tst_qcalendarwidget.moc"