Debian Codegraph @

autotests/kcollapsiblegroupbox_test.cpp

raw symbols SCIP
1 /*
2 This file is part of the KDE frameworks
3 SPDX-FileCopyrightText: 2016 Ragnar Thomsen <[email protected]>
4 SPDX-FileCopyrightText: 2025 Kai Uwe Broulik <[email protected]>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7 */
8
9 #include "kcollapsiblegroupbox_test.h"
10
11 #include <KCollapsibleGroupBox>
12
13 #include <QCheckBox>
14 #include <QDialog>
15 #include <QLabel>
16 #include <QLineEdit>
17 #include <QStyle>
18 #include <QTest>
19 #include <QVBoxLayout>
20
21 QTEST_MAIN(KCollapsibleGroupBoxTest)
22
23 void KCollapsibleGroupBoxTest::testDestructorCrash()
24 {
25 // The unittest does not crash the first time,
26 // so we run it twice.
27 int i = 0;
28 while (i != 2) {
29 QDialog *dlg = new QDialog(nullptr);
30
31 QVBoxLayout *mainvlayout = new QVBoxLayout(dlg);
32 KCollapsibleGroupBox *collapsible = new KCollapsibleGroupBox(dlg);
33 collapsible->setTitle(QStringLiteral("A test KCollapsibleGroupBox"));
34
35 QVBoxLayout *collapsiblevlayout = new QVBoxLayout(collapsible);
36
37 QLabel *lbl = new QLabel(QStringLiteral("A test label."), collapsible);
38 collapsiblevlayout->addWidget(lbl);
39
40 collapsible->expand();
41
42 mainvlayout->addWidget(collapsible);
43
44 connect(dlg, &QDialog::finished, dlg, &QObject::deleteLater);
45
46 dlg->open();
47
48 QVERIFY(collapsible);
49
50 // If waiting for less than ~500ms the test crashes every time.
51 // Not waiting or waiting for more than ~500ms is ok.
52 // Using qSleep(400) does NOT provoke the crash.
53 QTest::qWait(400);
54
55 dlg->accept();
56
57 i++;
58 }
59 }
60
61 void KCollapsibleGroupBoxTest::testOverrideFocus()
62 {
63 KCollapsibleGroupBox collapsible;
64 QVBoxLayout layout(&collapsible);
65
66 QCheckBox checkBox;
67 QCOMPARE(checkBox.focusPolicy(), Qt::StrongFocus);
68
69 layout.addWidget(&checkBox);
70 // The ChildAdded event is processed asynchronously.
71 qApp->processEvents();
72 // Make sure focus policy has been overridden.
73 collapsible.show();
74 QVERIFY(checkBox.isVisible());
75 QCOMPARE(checkBox.focusPolicy(), Qt::NoFocus);
76
77 // Make sure focus policy is restored on expand event.
78 collapsible.expand();
79 QVERIFY(checkBox.isVisible());
80 QCOMPARE(checkBox.focusPolicy(), Qt::StrongFocus);
81
82 // Make sure focus policy is overridden again on next collapse.
83 collapsible.collapse();
84 QVERIFY(checkBox.isVisible());
85 QCOMPARE(checkBox.focusPolicy(), Qt::NoFocus);
86 }
87
88 void KCollapsibleGroupBoxTest::childShouldGetFocus()
89 {
90 KCollapsibleGroupBox collapsible;
91 auto spinBox = new QLineEdit(&collapsible);
92 // The ChildAdded event is processed asynchronously.
93 // This also "simulates" user who manually expands.
94 qApp->processEvents();
95 collapsible.expand();
96 QCOMPARE(spinBox->focusPolicy(), Qt::StrongFocus);
97 }
98
99 void KCollapsibleGroupBoxTest::testExpandAnimation()
100 {
101 KCollapsibleGroupBox collapsible;
102 QVBoxLayout layout(&collapsible);
103
104 QLabel label;
105 label.setMinimumSize(QSize(500, 500));
106 layout.addWidget(&label);
107 // The ChildAdded event is processed asynchronously.
108 qApp->processEvents();
109
110 collapsible.show();
111
112 const int collapsedHeight = collapsible.sizeHint().height();
113 QCOMPARE(collapsible.height(), collapsedHeight);
114
115 collapsible.setExpanded(true);
116 QVERIFY(collapsible.isExpanded());
117 QCOMPARE(collapsible.height(), collapsedHeight);
118
119 const int animationDuration = qMax(1, collapsible.style()->styleHint(QStyle::SH_Widget_Animation_Duration));
120 // QTimeLine default update interval is 40ms plus some slack.
121 QTRY_COMPARE_WITH_TIMEOUT(collapsible.height(), collapsible.sizeHint().height(), animationDuration + 40 + 10);
122
123 collapsible.setExpanded(false);
124 QVERIFY(!collapsible.isExpanded());
125
126 QTRY_COMPARE_WITH_TIMEOUT(collapsible.height(), collapsedHeight, animationDuration + 40 + 10);
127 }
128
129 void KCollapsibleGroupBoxTest::testNoAnimationWhenHidden()
130 {
131 KCollapsibleGroupBox collapsible;
132 QVBoxLayout layout(&collapsible);
133
134 QLabel label;
135 label.setMinimumSize(QSize(500, 500));
136 layout.addWidget(&label);
137 // The ChildAdded event is processed asynchronously.
138 qApp->processEvents();
139
140 collapsible.setExpanded(true);
141 collapsible.show();
142
143 // Size should be correct immediately with no animation.
144 QCOMPARE(collapsible.height(), collapsible.sizeHint().height());
145 }
146
147 #include "moc_kcollapsiblegroupbox_test.cpp"