| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2011 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 |
** GNU Lesser General Public License Usage |
| 11 |
** This file may be used under the terms of the GNU Lesser General Public |
| 12 |
** License version 2.1 as published by the Free Software Foundation and |
| 13 |
** appearing in the file LICENSE.LGPL included in the packaging of this |
| 14 |
** file. Please review the following information to ensure the GNU Lesser |
| 15 |
** General Public License version 2.1 requirements will be met: |
| 16 |
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 17 |
** |
| 18 |
** In addition, as a special exception, Nokia gives you certain additional |
| 19 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
| 20 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 21 |
** |
| 22 |
** GNU General Public License Usage |
| 23 |
** Alternatively, this file may be used under the terms of the GNU General |
| 24 |
** Public License version 3.0 as published by the Free Software Foundation |
| 25 |
** and appearing in the file LICENSE.GPL included in the packaging of this |
| 26 |
** file. Please review the following information to ensure the GNU General |
| 27 |
** Public License version 3.0 requirements will be met: |
| 28 |
** http://www.gnu.org/copyleft/gpl.html. |
| 29 |
** |
| 30 |
** Other Usage |
| 31 |
** Alternatively, this file may be used in accordance with the terms and |
| 32 |
** conditions contained in a signed written agreement between you and Nokia. |
| 33 |
** |
| 34 |
** |
| 35 |
** |
| 36 |
** |
| 37 |
** |
| 38 |
** $QT_END_LICENSE$ |
| 39 |
** |
| 40 |
****************************************************************************/ |
| 41 |
|
| 42 |
|
| 43 |
#include <QtTest/QtTest> |
| 44 |
|
| 45 |
#include "q3widgetstack.h" |
| 46 |
#include <qapplication.h> |
| 47 |
#include <qboxlayout.h> |
| 48 |
#include <qlayout.h> |
| 49 |
#include <qradiobutton.h> |
| 50 |
#include <qlineedit.h> |
| 51 |
#include <qdialog.h> |
| 52 |
|
| 53 |
//TESTED_CLASS= |
| 54 |
//TESTED_FILES= |
| 55 |
|
| 56 |
class tst_Q3WidgetStack : public QObject |
| 57 |
{ |
| 58 |
Q_OBJECT |
| 59 |
|
| 60 |
public: |
| 61 |
tst_Q3WidgetStack(); |
| 62 |
virtual ~tst_Q3WidgetStack(); |
| 63 |
|
| 64 |
protected slots: |
| 65 |
void aboutToShow_helper(int); |
| 66 |
void aboutToShow_helper(QWidget *); |
| 67 |
public slots: |
| 68 |
void initTestCase(); |
| 69 |
void cleanupTestCase(); |
| 70 |
void init(); |
| 71 |
void cleanup(); |
| 72 |
private slots: |
| 73 |
void aboutToShow(); |
| 74 |
void sizeHint(); |
| 75 |
void addWidget(); |
| 76 |
|
| 77 |
private: |
| 78 |
Q3WidgetStack *testWidget; |
| 79 |
QWidget *widgetOne; |
| 80 |
QWidget *widgetTwo; |
| 81 |
|
| 82 |
// Helpers for aboutToShow() test |
| 83 |
QWidget *currentVisibleWidgetOne; |
| 84 |
int aboutToShowId; |
| 85 |
bool aboutToShowSignalOne; |
| 86 |
QWidget *currentVisibleWidgetTwo; |
| 87 |
QWidget *aboutToShowWidget; |
| 88 |
bool aboutToShowSignalTwo; |
| 89 |
|
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
const QSizePolicy ignored(QSizePolicy::Ignored, QSizePolicy::Ignored); |
| 94 |
const QSizePolicy preferred(QSizePolicy::Preferred, QSizePolicy::Preferred); |
| 95 |
|
| 96 |
tst_Q3WidgetStack::tst_Q3WidgetStack() |
| 97 |
{ |
| 98 |
testWidget = 0; |
| 99 |
} |
| 100 |
|
| 101 |
tst_Q3WidgetStack::~tst_Q3WidgetStack() |
| 102 |
{ |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
void tst_Q3WidgetStack::initTestCase() |
| 107 |
{ |
| 108 |
testWidget = new Q3WidgetStack( 0 ); |
| 109 |
qApp->setMainWidget(testWidget); |
| 110 |
testWidget->show(); |
| 111 |
widgetOne = new QWidget( testWidget ); |
| 112 |
testWidget->addWidget( widgetOne ); |
| 113 |
widgetTwo = new QWidget( testWidget ); |
| 114 |
testWidget->addWidget( widgetTwo ); |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
void tst_Q3WidgetStack::cleanupTestCase() |
| 119 |
{ |
| 120 |
delete testWidget; |
| 121 |
testWidget = 0; |
| 122 |
} |
| 123 |
|
| 124 |
void tst_Q3WidgetStack::init() |
| 125 |
{ |
| 126 |
testWidget->raiseWidget( widgetOne ); |
| 127 |
} |
| 128 |
|
| 129 |
void tst_Q3WidgetStack::cleanup() |
| 130 |
{ |
| 131 |
} |
| 132 |
|
| 133 |
void tst_Q3WidgetStack::aboutToShow_helper(int id) |
| 134 |
{ |
| 135 |
currentVisibleWidgetOne = testWidget->visibleWidget(); |
| 136 |
aboutToShowId = id; |
| 137 |
aboutToShowSignalOne = TRUE; |
| 138 |
} |
| 139 |
|
| 140 |
void tst_Q3WidgetStack::aboutToShow_helper(QWidget *widget) |
| 141 |
{ |
| 142 |
currentVisibleWidgetTwo = testWidget->visibleWidget(); |
| 143 |
aboutToShowWidget = widget; |
| 144 |
aboutToShowSignalTwo = TRUE; |
| 145 |
} |
| 146 |
|
| 147 |
void tst_Q3WidgetStack::aboutToShow() |
| 148 |
{ |
| 149 |
currentVisibleWidgetOne = 0; |
| 150 |
aboutToShowId = 1000; // The id is not likely to be 1000 |
| 151 |
aboutToShowSignalOne = FALSE; |
| 152 |
currentVisibleWidgetTwo = 0; |
| 153 |
aboutToShowWidget = 0; |
| 154 |
aboutToShowSignalTwo = FALSE; |
| 155 |
|
| 156 |
connect(testWidget, SIGNAL(aboutToShow(int)), this, SLOT(aboutToShow_helper(int))); |
| 157 |
connect(testWidget, SIGNAL(aboutToShow(QWidget *)), this, SLOT(aboutToShow_helper(QWidget *))); |
| 158 |
testWidget->raiseWidget(widgetTwo); |
| 159 |
for (int a = 0;a < 10;a++) { |
| 160 |
qApp->processEvents(); |
| 161 |
if (aboutToShowSignalOne || aboutToShowSignalTwo) |
| 162 |
break; |
| 163 |
} |
| 164 |
QVERIFY(aboutToShowSignalOne); |
| 165 |
QVERIFY(aboutToShowSignalTwo); |
| 166 |
QVERIFY(currentVisibleWidgetOne == widgetOne); |
| 167 |
QVERIFY(currentVisibleWidgetTwo == widgetOne); |
| 168 |
QCOMPARE(aboutToShowId, testWidget->id(widgetTwo)); |
| 169 |
QCOMPARE(aboutToShowWidget, widgetTwo); |
| 170 |
QCOMPARE(testWidget->visibleWidget(), widgetTwo); |
| 171 |
} |
| 172 |
|
| 173 |
void tst_Q3WidgetStack::sizeHint() |
| 174 |
{ |
| 175 |
QDialog dialog(0); |
| 176 |
|
| 177 |
QHBoxLayout *layout = new QHBoxLayout(&dialog); |
| 178 |
Q3WidgetStack *stack = new Q3WidgetStack(&dialog); |
| 179 |
layout->addWidget(stack); |
| 180 |
|
| 181 |
QWidget *pageA = new QWidget(stack); |
| 182 |
(new QVBoxLayout(pageA))->addWidget(new QLineEdit(pageA)); |
| 183 |
stack->addWidget(pageA); |
| 184 |
pageA->setSizePolicy(ignored); |
| 185 |
|
| 186 |
QWidget *pageB = new QWidget(stack); |
| 187 |
(new QVBoxLayout(pageB))->addWidget(new QRadioButton(pageB)); |
| 188 |
stack->addWidget(pageB); |
| 189 |
pageB->setSizePolicy(ignored); |
| 190 |
|
| 191 |
stack->raiseWidget(pageA); |
| 192 |
dialog.show(); |
| 193 |
|
| 194 |
QSize before(dialog.size()); |
| 195 |
pageB->setSizePolicy(preferred); |
| 196 |
stack->raiseWidget(pageB); |
| 197 |
|
| 198 |
layout->activate(); |
| 199 |
dialog.setFixedSize(dialog.minimumSizeHint()); |
| 200 |
qApp->processEvents(); |
| 201 |
QVERIFY(before != dialog.minimumSizeHint()); |
| 202 |
} |
| 203 |
|
| 204 |
void tst_Q3WidgetStack::addWidget() |
| 205 |
{ |
| 206 |
QDialog dialog(0); |
| 207 |
Q3WidgetStack *stack = new Q3WidgetStack(&dialog); |
| 208 |
|
| 209 |
// The widget should now be added to the stack |
| 210 |
QWidget *widget = new QWidget(stack); |
| 211 |
QCOMPARE(stack->id(widget), -1); |
| 212 |
|
| 213 |
// The widget should get a positive ID |
| 214 |
int id = stack->addWidget(widget); |
| 215 |
QVERIFY(id >= 0); |
| 216 |
QCOMPARE(stack->id(widget), id); |
| 217 |
QCOMPARE(stack->widget(id), widget); |
| 218 |
|
| 219 |
QWidget *widget2 = new QWidget(stack); |
| 220 |
QCOMPARE(stack->id(widget2), -1); |
| 221 |
|
| 222 |
// The widget should get a negative ID different from -1 |
| 223 |
id = stack->addWidget(widget2, -2); |
| 224 |
QVERIFY(id < -1); |
| 225 |
QCOMPARE(stack->id(widget2), id); |
| 226 |
QVERIFY(stack->id(widget2) != stack->id(widget)); |
| 227 |
QCOMPARE(stack->widget(id), widget2); |
| 228 |
|
| 229 |
// The widget should be removed when it's deleted |
| 230 |
delete widget2; |
| 231 |
QCOMPARE(stack->id(widget2), -1); |
| 232 |
QCOMPARE(stack->widget(id), (QWidget*)0); |
| 233 |
|
| 234 |
// Create a unique ID |
| 235 |
int uid = stack->id(widget) + 100; |
| 236 |
QWidget *widget3 = new QWidget(widget); |
| 237 |
|
| 238 |
// Test of reparenting |
| 239 |
id = stack->addWidget(widget3, uid); |
| 240 |
QCOMPARE(id, uid); |
| 241 |
QCOMPARE(stack->id(widget3), id); |
| 242 |
QCOMPARE(widget3->parentWidget(), (QWidget*)stack); |
| 243 |
QCOMPARE(stack->widget(id), widget3); |
| 244 |
|
| 245 |
delete widget3; |
| 246 |
QCOMPARE(stack->id(widget3), -1); |
| 247 |
QCOMPARE(stack->widget(id), (QWidget*)0); |
| 248 |
|
| 249 |
delete widget; |
| 250 |
QCOMPARE(stack->id(widget), -1); |
| 251 |
|
| 252 |
delete stack; |
| 253 |
|
| 254 |
} |
| 255 |
|
| 256 |
QTEST_MAIN(tst_Q3WidgetStack) |
| 257 |
#include "tst_q3widgetstack.moc" |