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