|
| 1 | +#include "../src/ui/helpers/ColorComboHelper.h" |
| 2 | + |
| 3 | +#include <QApplication> |
| 4 | +#include <QComboBox> |
| 5 | +#include <QDebug> |
| 6 | +#include <QImage> |
| 7 | +#include <QPalette> |
| 8 | + |
| 9 | +int main(int argc, char** argv) |
| 10 | +{ |
| 11 | + QApplication app(argc, argv); |
| 12 | + QComboBox combo; |
| 13 | + for (const QColor background : {QColor(Qt::white), QColor(Qt::black)}) { |
| 14 | + QPalette palette = combo.palette(); |
| 15 | + palette.setColor(QPalette::Base, background); |
| 16 | + combo.setPalette(palette); |
| 17 | + combo.clear(); |
| 18 | + for (const QString hex : {QString("FFFFFF"), QString("#000000"), QString(" 12aBcD ")}) { |
| 19 | + const int index = ColorComboHelper::addColorItem(&combo, hex, 42, hex, true); |
| 20 | + QString normalized = hex.trimmed(); |
| 21 | + if (!normalized.startsWith('#')) normalized.prepend('#'); |
| 22 | + const QColor expected(normalized); |
| 23 | + if (combo.itemData(index).toInt() != 42 || combo.itemText(index) != hex) return 1; |
| 24 | + if (combo.itemData(index, Qt::ForegroundRole).isValid()) return 6; |
| 25 | + for (const auto mode : {QIcon::Normal, QIcon::Disabled, QIcon::Active, QIcon::Selected}) { |
| 26 | + const QImage image = combo.itemIcon(index).pixmap(16, 16, mode).toImage(); |
| 27 | + if (image.isNull() || image.pixelColor(8, 8) != expected) return 2; |
| 28 | + } |
| 29 | + } |
| 30 | + for (const QString hex : {QString(), QString("not-hex"), QString("#GGGGGG")}) { |
| 31 | + const int index = ColorComboHelper::addColorItem(&combo, "Unknown", 99, hex, true); |
| 32 | + combo.setCurrentIndex(index); |
| 33 | + if (!combo.itemIcon(index).isNull() || combo.currentData().toInt() != 99 |
| 34 | + || combo.currentText() != "Unknown") return 3; |
| 35 | + } |
| 36 | + const int plain = ColorComboHelper::addColorItem(&combo, "Other dialog", 7, "FF0000"); |
| 37 | + if (!combo.itemIcon(plain).isNull() || combo.itemData(plain).toInt() != 7) return 4; |
| 38 | + // A palette change must not recolor an already populated swatch. |
| 39 | + palette.setColor(QPalette::Base, background == QColor(Qt::white) ? Qt::black : Qt::white); |
| 40 | + palette.setColor(QPalette::Text, background == QColor(Qt::white) ? Qt::white : Qt::black); |
| 41 | + palette.setColor(QPalette::HighlightedText, background == QColor(Qt::white) ? Qt::black : Qt::white); |
| 42 | + combo.setPalette(palette); |
| 43 | + if (combo.itemIcon(0).pixmap(16, 16).toImage().pixelColor(8, 8) != QColor(Qt::white)) return 5; |
| 44 | + if (combo.itemData(0, Qt::ForegroundRole).isValid() |
| 45 | + || combo.palette().color(QPalette::Text) != palette.color(QPalette::Text) |
| 46 | + || combo.palette().color(QPalette::HighlightedText) != palette.color(QPalette::HighlightedText)) return 7; |
| 47 | + } |
| 48 | + qInfo() << "Color combo swatch validation passed."; |
| 49 | + return 0; |
| 50 | +} |
0 commit comments