12#include <QStandardItemModel>
24 const auto A =
"a"_qs;
25 const auto B =
"b"_qs;
26 const auto C =
"C"_qs;
30 QStandardItemModel Model_;
31 CategorySelector Selector_;
34 explicit Fixture (
const QStringList& names = { B, A, C })
36 for (
const auto& name : names)
37 Model_.appendRow (
new QStandardItem { name });
38 Selector_.SetModel (Model_);
41 QTreeView& View ()
const
43 return *Selector_.findChild<QTreeView*> ();
46 QStringList Listed ()
const
49 const auto model = View ().model ();
50 for (
int i = 0; i < model->rowCount (); ++i)
51 result << model->index (i, 0).data ().toString ();
55 bool Check (
const QString& name, Qt::CheckState state)
const
57 const auto model = View ().model ();
58 const auto& found = model->match (model->index (0, 0), Qt::DisplayRole, name, 1, Qt::MatchExactly);
59 return !found.isEmpty () && model->setData (found.front (), state, Qt::CheckStateRole);
62 QStringList LastReported ()
const
64 return Edited_.last ().first ().toStringList ();
71 qputenv (
"QT_QPA_PLATFORM",
"offscreen");
74 void CategorySelectorTest::testListsTheNamesSorted ()
77 QCOMPARE (f.Listed (), (QStringList { A, B, C }));
79 f.Selector_.SelectAll ();
80 QCOMPARE (f.Selector_.GetSelections (), (QStringList { A, B, C }));
81 QCOMPARE (f.Edited_.size (), 0);
84 void CategorySelectorTest::testSelectionIsKeyedByName ()
87 f.Selector_.SetSelections ({ A,
"zzz"_qs });
88 QCOMPARE (f.Selector_.GetSelections (), QStringList { A });
90 f.Model_.appendRow (
new QStandardItem {
"zzz"_qs });
91 QCOMPARE (
f.Selector_.GetSelections (), (QStringList { A,
"zzz"_qs }));
94 QVERIFY (
f.Selector_.GetSelections ().isEmpty ());
96 f.Model_.appendRow (
new QStandardItem { A });
97 QCOMPARE (
f.Selector_.GetSelections (), QStringList { A });
98 QCOMPARE (
f.Edited_.size (), 0);
101 void CategorySelectorTest::testSelectedIndexesAreTheModelsOwn ()
104 f.Model_.item (1)->setData (42, Qt::UserRole);
105 f.Selector_.SetSelections ({ A });
107 const auto& indexes =
f.Selector_.GetSelectedIndexes ();
108 QCOMPARE (indexes.size (), 1);
109 QCOMPARE (indexes.front ().model (), &
f.Model_);
110 QCOMPARE (indexes.front ().data (Qt::UserRole).toInt (), 42);
113 void CategorySelectorTest::testUserChecksAreReported ()
116 QVERIFY (
f.Check (B, Qt::Checked));
117 QCOMPARE (
f.Edited_.size (), 1);
118 QCOMPARE (
f.LastReported (), QStringList { B });
120 QVERIFY (
f.Check (A, Qt::Checked));
121 QCOMPARE (
f.Edited_.size (), 2);
122 QCOMPARE (
f.LastReported (), (QStringList { A, B }));
124 QVERIFY (
f.Check (B, Qt::Unchecked));
125 QCOMPARE (
f.Edited_.size (), 3);
126 QCOMPARE (
f.LastReported (), QStringList { A });
128 f.Selector_.SelectNone ();
129 f.Selector_.SetSelections ({ C });
130 QCOMPARE (
f.Edited_.size (), 3);
131 QCOMPARE (
f.Selector_.GetSelections (), QStringList { C });
134 void CategorySelectorTest::testContextActionsAreReported ()
137 const auto& actions =
f.View ().actions ();
138 QCOMPARE (actions.size (), 2);
140 actions.first ()->trigger ();
141 QCOMPARE (
f.Selector_.GetSelections (), (QStringList { A, B, C }));
142 QCOMPARE (
f.Edited_.size (), 1);
143 QCOMPARE (
f.LastReported (), (QStringList { A, B, C }));
145 actions.last ()->trigger ();
146 QVERIFY (
f.Selector_.GetSelections ().isEmpty ());
147 QCOMPARE (
f.Edited_.size (), 2);
148 QVERIFY (
f.LastReported ().isEmpty ());
151 void CategorySelectorTest::testCaptionShowsTheHeader ()
154 QVERIFY (
f.View ().isHeaderHidden ());
156 f.Selector_.SetCaption (
"Tags"_qs);
157 QVERIFY (!
f.View ().isHeaderHidden ());
158 QCOMPARE (
f.View ().model ()->headerData (0, Qt::Horizontal).toString (),
"Tags"_qs);
160 f.Selector_.SetCaption ({});
161 QVERIFY (
f.View ().isHeaderHidden ());
164 void CategorySelectorTest::testSetModelKeepsTheSelection ()
167 f.Selector_.SetSelections ({ A, B });
169 QStandardItemModel other;
170 for (
const auto& name : {
"x"_qs, A })
171 other.appendRow (
new QStandardItem { name });
172 f.Selector_.SetModel (other);
174 QCOMPARE (
f.Listed (), (QStringList { A,
"x"_qs }));
175 QCOMPARE (
f.Selector_.GetSelections (), QStringList { A });
176 QCOMPARE (
f.Edited_.size (), 0);
void selectionEdited(const QStringList &selected)
Emitted after the user has changed the selection, with GetSelections().
Task< CheckResult_t > Check(QString dbPath)
constexpr detail::ExprTree< detail::ExprType::LeafStaticPlaceholder, detail::MemberPtrs< Ptr > > f