Debian Codegraph @

tests/buttonoverlay.cpp

raw symbols SCIP
1 #include "../widgets/clearcombobox.h"
2 #include "../widgets/clearlineedit.h"
3 #include "../widgets/clearplaintextedit.h"
4 #include "../widgets/clearspinbox.h"
5 #include "../widgets/iconbutton.h"
6
7 #include "../misc/disablewarningsmoc.h"
8
9 #include <QHBoxLayout>
10 #include <QPushButton>
11 #include <QtTest/QtTest>
12
13 using namespace QtUtilities;
14
15 class ButtonOverlayTests : public QObject {
16 Q_OBJECT
17
18 private:
19 void assertGeneralDefaults(ButtonOverlay &buttonOverlay);
20 void changeBasicConfiguration(ButtonOverlay &buttonOverlay);
21
22 private Q_SLOTS:
23 void testClearLineEdit();
24 void testClearComboBox();
25 void testClearSpinBox();
26 void testClearPlainTextEdit();
27 };
28
29 void ButtonOverlayTests::assertGeneralDefaults(ButtonOverlay &buttonOverlay)
30 {
31 // assert defaults
32 QVERIFY2(buttonOverlay.isClearButtonEnabled(), "clear button enabled by default");
33 QVERIFY2(!buttonOverlay.isInfoButtonEnabled(), "info button disabled by default");
34 QVERIFY2(buttonOverlay.isCleared(), "widget considered cleared by default");
35 QVERIFY2(!buttonOverlay.isUsingCustomLayout(), "not using custom layout by default");
36 }
37
38 void ButtonOverlayTests::changeBasicConfiguration(ButtonOverlay &buttonOverlay)
39 {
40 buttonOverlay.setClearButtonEnabled(false);
41 QVERIFY2(!buttonOverlay.isClearButtonEnabled(), "clear button disabled");
42 buttonOverlay.enableInfoButton(
43 QIcon::fromTheme(QStringLiteral("data-information")).pixmap(IconButton::defaultPixmapSize), QStringLiteral("Some info"));
44 QVERIFY2(buttonOverlay.isInfoButtonEnabled(), "info button enabled");
45 }
46
47 void ButtonOverlayTests::testClearLineEdit()
48 {
49 auto clearWidget = ClearLineEdit();
50
51 // assert defaults
52 assertGeneralDefaults(clearWidget);
53 QVERIFY2(static_cast<QLineEdit &>(clearWidget).isClearButtonEnabled(), "clear button enabled via QLineEdit");
54
55 // change configuration
56 changeBasicConfiguration(clearWidget);
57 clearWidget.setText(QStringLiteral("not cleared anymore"));
58 QVERIFY2(!clearWidget.isCleared(), "widget not considered cleared anymore");
59 auto customAction = QAction(QIcon::fromTheme(QStringLiteral("edit-copy")).pixmap(IconButton::defaultPixmapSize), QStringLiteral("Copy"));
60 clearWidget.addCustomAction(&customAction);
61 QVERIFY2(!clearWidget.isUsingCustomLayout(), "not resorted to using custom layout so far");
62 QCOMPARE(clearWidget.actions().size(), 2); // more an implementation detail but the QLineEdit should have 2 actions right now
63
64 // test fallback to custom layout
65 static_cast<ButtonOverlay &>(clearWidget).setClearButtonEnabled(true);
66 auto pushButton = QPushButton(QStringLiteral("Custom widget"));
67 clearWidget.addCustomButton(&pushButton);
68 QVERIFY2(clearWidget.isUsingCustomLayout(), "resorted to using custom layout due to addCustomButton()");
69 QVERIFY2(static_cast<ButtonOverlay &>(clearWidget).isClearButtonEnabled(), "clear button still enabled");
70 QVERIFY2(!static_cast<QLineEdit &>(clearWidget).isClearButtonEnabled(), "clear button not enabled via QLineEdit");
71 QCOMPARE(clearWidget.actions().size(), 0); // more an implementation detail but the QLineEdit should have been converted to icon buttons
72 QCOMPARE(clearWidget.buttonLayout()->count(), 4);
73
74 // change custom action; its fallback icon button should reflect the change
75 const auto *const iconButton = qobject_cast<const IconButton *>(clearWidget.buttonLayout()->itemAt(2)->widget());
76 QVERIFY2(iconButton, "icon button present");
77 QCOMPARE(iconButton->toolTip(), QStringLiteral("Copy"));
78 customAction.setText(QStringLiteral("Paste"));
79 QCOMPARE(iconButton->toolTip(), QStringLiteral("Paste"));
80
81 // remove buttons again
82 static_cast<ButtonOverlay &>(clearWidget).setClearButtonEnabled(false);
83 clearWidget.disableInfoButton();
84 clearWidget.removeCustomAction(&customAction);
85 clearWidget.removeCustomButton(&pushButton);
86 QCOMPARE(clearWidget.buttonLayout()->count(), 0);
87 }
88
89 void ButtonOverlayTests::testClearComboBox()
90 {
91 auto clearWidget = ClearComboBox();
92
93 // assert defaults
94 assertGeneralDefaults(clearWidget);
95
96 // change configuration
97 changeBasicConfiguration(clearWidget);
98 clearWidget.setClearButtonEnabled(true);
99 clearWidget.setCurrentText(QStringLiteral("not cleared anymore"));
100 QVERIFY2(!clearWidget.isCleared(), "widget not considered cleared anymore");
101 auto customAction = QAction(QIcon::fromTheme(QStringLiteral("edit-copy")).pixmap(IconButton::defaultPixmapSize), QStringLiteral("Copy"));
102 clearWidget.addCustomAction(&customAction);
103 QVERIFY2(!clearWidget.isUsingCustomLayout(), "not resorted to using custom layout so far");
104
105 // trigger fallback
106 QCOMPARE(clearWidget.buttonLayout()->count(), 3);
107 }
108
109 void ButtonOverlayTests::testClearSpinBox()
110 {
111 auto clearWidget = ClearSpinBox();
112
113 // assert defaults
114 assertGeneralDefaults(clearWidget);
115
116 // change configuration
117 changeBasicConfiguration(clearWidget);
118 clearWidget.setClearButtonEnabled(true);
119 clearWidget.setValue(1);
120 QVERIFY2(!clearWidget.isCleared(), "widget not considered cleared anymore");
121 auto customAction = QAction(QIcon::fromTheme(QStringLiteral("edit-copy")).pixmap(IconButton::defaultPixmapSize), QStringLiteral("Copy"));
122 clearWidget.addCustomAction(&customAction);
123 QVERIFY2(!clearWidget.isUsingCustomLayout(), "not resorted to using custom layout so far");
124
125 // trigger fallback
126 QCOMPARE(clearWidget.buttonLayout()->count(), 3);
127 }
128
129 void ButtonOverlayTests::testClearPlainTextEdit()
130 {
131 auto clearWidget = ClearPlainTextEdit();
132
133 // assert defaults
134 QVERIFY2(clearWidget.isClearButtonEnabled(), "clear button enabled by default");
135 QVERIFY2(!clearWidget.isInfoButtonEnabled(), "info button disabled by default");
136 QVERIFY2(clearWidget.isCleared(), "widget considered cleared by default");
137 QVERIFY2(clearWidget.isUsingCustomLayout(), "using custom layout by default");
138 QCOMPARE(clearWidget.buttonLayout()->count(), 1);
139
140 // change configuration
141 clearWidget.document()->setPlainText(QStringLiteral("not cleared anymore"));
142 QVERIFY2(!clearWidget.isCleared(), "widget not considered cleared anymore");
143 }
144
145 QT_UTILITIES_DISABLE_WARNINGS_FOR_MOC_INCLUDE
146 QTEST_MAIN(ButtonOverlayTests)
147 #include "buttonoverlay.moc"