1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
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
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
44 #include "qprogressbar.h"
45 #include <qapplication.h>
46 #include <qstyleoption.h>
49 #include <QStyleFactory>
51 #include "../../shared/util.h"
56 class tst_QProgressBar
: public QObject
61 void minMaxSameValue();
62 void destroyIndeterminate();
65 void setValueRepaint();
67 void formatedText_data();
70 void task245201_testChangeStyleAndDelete_data();
71 void task245201_testChangeStyleAndDelete();
74 // Testing get/set functions
75 void tst_QProgressBar::getSetCheck()
78 // bool QProgressBar::invertedAppearance()
79 // void QProgressBar::setInvertedAppearance(bool)
80 obj1
.setInvertedAppearance(false);
81 QCOMPARE(false, obj1
.invertedAppearance());
82 obj1
.setInvertedAppearance(true);
83 QCOMPARE(true, obj1
.invertedAppearance());
85 // int QProgressBar::minimum()
86 // void QProgressBar::setMinimum(int)
88 QCOMPARE(0, obj1
.minimum());
89 obj1
.setMinimum(INT_MAX
);
90 QCOMPARE(INT_MAX
, obj1
.minimum());
91 obj1
.setMinimum(INT_MIN
);
92 QCOMPARE(INT_MIN
, obj1
.minimum());
94 // int QProgressBar::maximum()
95 // void QProgressBar::setMaximum(int)
97 QCOMPARE(0, obj1
.maximum());
98 obj1
.setMaximum(INT_MIN
);
99 QCOMPARE(INT_MIN
, obj1
.maximum());
100 obj1
.setMaximum(INT_MAX
);
101 QCOMPARE(INT_MAX
, obj1
.maximum());
103 // int QProgressBar::value()
104 // void QProgressBar::setValue(int)
106 QCOMPARE(0, obj1
.value());
107 obj1
.setValue(INT_MIN
);
108 QCOMPARE(INT_MIN
, obj1
.value());
109 obj1
.setValue(INT_MAX
);
110 QCOMPARE(INT_MAX
, obj1
.value());
113 void tst_QProgressBar::minMaxSameValue()
116 bar
.setRange(10, 10);
121 void tst_QProgressBar::destroyIndeterminate()
123 // This test crashes on styles that animate indeterminate / busy
124 // progressbars, and forget to remove the bars from internal logics when
126 QPointer
<QProgressBar
> bar
= new QProgressBar
;
131 QTimer::singleShot(500, bar
, SLOT(deleteLater()));
132 QTimer::singleShot(3000, &loop
, SLOT(quit()));
138 void tst_QProgressBar::text()
141 bar
.setRange(10, 10);
143 QCOMPARE(bar
.text(), QString("100%"));
145 QCOMPARE(bar
.text(), QString("100%"));
147 QCOMPARE(bar
.text(), QString("50%"));
151 QCOMPARE(bar
.text(), QString());
154 class ProgressBar
: public QProgressBar
156 void paintEvent(QPaintEvent
*event
)
159 QProgressBar::paintEvent(event
);
163 using QProgressBar::initStyleOption
;
166 void tst_QProgressBar::format()
172 QTest::qWaitForWindowShown(&bar
);
175 bar
.repainted
= false;
176 bar
.setFormat("%v of %m (%p%)");
178 QVERIFY(bar
.repainted
);
179 bar
.repainted
= false;
180 bar
.setFormat("%v of %m (%p%)");
181 qApp
->processEvents();
183 // The Mac scroll bar is animated, which means we get paint events all the time.
184 QVERIFY(!bar
.repainted
);
186 QCOMPARE(bar
.text(), QString("1 of 10 (10%)"));
189 QCOMPARE(bar
.text(), QString("5 of 0 (100%)"));
193 QCOMPARE(bar
.text(), QString());
196 void tst_QProgressBar::setValueRepaint()
201 pbar
.setFormat("%v");
203 QTest::qWaitForWindowShown(&pbar
);
205 QApplication::processEvents();
206 for (int i
= pbar
.minimum(); i
< pbar
.maximum(); ++i
) {
207 pbar
.repainted
= false;
210 QTRY_VERIFY(pbar
.repainted
);
214 void tst_QProgressBar::sizeHint()
221 //test if the sizeHint is big enough
222 QFontMetrics fm
= bar
.fontMetrics();
223 QStyleOptionProgressBarV2 opt
;
224 bar
.initStyleOption(&opt
);
225 QSize size
= QSize(9 * 7 + fm
.width(QLatin1Char('0')) * 4, fm
.height() + 8);
226 size
= bar
.style()->sizeFromContents(QStyle::CT_ProgressBar
, &opt
, size
, &bar
);
227 QSize barSize
= bar
.sizeHint();
228 QVERIFY(barSize
.width() >= size
.width());
229 QCOMPARE(barSize
.height(), size
.height());
232 void tst_QProgressBar::formatedText_data()
234 QTest::addColumn
<int>("minimum");
235 QTest::addColumn
<int>("maximum");
236 QTest::addColumn
<int>("value");
237 QTest::addColumn
<QString
>("format");
238 QTest::addColumn
<QString
>("text");
240 QTest::newRow("1") << -100 << 100 << 0 << QString::fromLatin1(" %p - %v - %m ") << QString::fromLatin1(" 50 - 0 - 200 ");
241 QTest::newRow("2") << -100 << 0 << -25 << QString::fromLatin1(" %p - %v - %m ") << QString::fromLatin1(" 75 - -25 - 100 ");
242 QTest::newRow("3") << 10 << 10 << 10 << QString::fromLatin1(" %p - %v - %m ") << QString::fromLatin1(" 100 - 10 - 0 ");
243 QTest::newRow("task152227") << INT_MIN
<< INT_MAX
<< 42 << QString::fromLatin1(" %p - %v - %m ") << QString::fromLatin1(" 50 - 42 - 4294967295 ");
246 void tst_QProgressBar::formatedText()
248 QFETCH(int, minimum
);
249 QFETCH(int, maximum
);
251 QFETCH(QString
, format
);
252 QFETCH(QString
, text
);
254 bar
.setRange(minimum
, maximum
);
256 bar
.setFormat(format
);
257 QCOMPARE(bar
.text(), text
);
260 void tst_QProgressBar::task245201_testChangeStyleAndDelete_data()
262 QTest::addColumn
<QString
>("style1_str");
263 QTest::addColumn
<QString
>("style2_str");
265 QTest::newRow("plastique-windows") << QString::fromLatin1("plastique") << QString::fromLatin1("windows");
266 QTest::newRow("mlotif-windows") << QString::fromLatin1("motif") << QString::fromLatin1("windows");
267 QTest::newRow("cleanlooks-cde") << QString::fromLatin1("cleanlooks") << QString::fromLatin1("cde");
268 QTest::newRow("gtk-plastique") << QString::fromLatin1("gtk") << QString::fromLatin1("plastique");
271 void tst_QProgressBar::task245201_testChangeStyleAndDelete()
273 QFETCH(QString
, style1_str
);
274 QFETCH(QString
, style2_str
);
276 QProgressBar
*bar
= new QProgressBar
;
278 QStyle
*style
= QStyleFactory::create(style1_str
);
279 bar
->setStyle(style
);
281 QStyle
*style2
= QStyleFactory::create(style2_str
);
282 bar
->setStyle(style2
);
286 QTest::qWait(100); //should not crash
291 QTEST_MAIN(tst_QProgressBar
)
292 #include "tst_qprogressbar.moc"