LeechCraft 0.6.70-19424-g027e980fd1
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
categoryselectortest.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
10#include <QAction>
11#include <QSignalSpy>
12#include <QStandardItemModel>
13#include <QTest>
14#include <QTreeView>
15#include <util/sll/qtutil.h>
16#include "categoryselector.h"
17
19
20namespace LC::Util
21{
22 namespace
23 {
24 const auto A = "a"_qs;
25 const auto B = "b"_qs;
26 const auto C = "C"_qs;
27
28 struct Fixture
29 {
30 QStandardItemModel Model_;
31 CategorySelector Selector_;
32 QSignalSpy Edited_ { &Selector_, &CategorySelector::selectionEdited };
33
34 explicit Fixture (const QStringList& names = { B, A, C })
35 {
36 for (const auto& name : names)
37 Model_.appendRow (new QStandardItem { name });
38 Selector_.SetModel (Model_);
39 }
40
41 QTreeView& View () const
42 {
43 return *Selector_.findChild<QTreeView*> ();
44 }
45
46 QStringList Listed () const
47 {
48 QStringList result;
49 const auto model = View ().model ();
50 for (int i = 0; i < model->rowCount (); ++i)
51 result << model->index (i, 0).data ().toString ();
52 return result;
53 }
54
55 bool Check (const QString& name, Qt::CheckState state) const
56 {
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);
60 }
61
62 QStringList LastReported () const
63 {
64 return Edited_.last ().first ().toStringList ();
65 }
66 };
67 }
68
70 {
71 qputenv ("QT_QPA_PLATFORM", "offscreen");
72 }
73
74 void CategorySelectorTest::testListsTheNamesSorted ()
75 {
76 Fixture f;
77 QCOMPARE (f.Listed (), (QStringList { A, B, C }));
78
79 f.Selector_.SelectAll ();
80 QCOMPARE (f.Selector_.GetSelections (), (QStringList { A, B, C }));
81 QCOMPARE (f.Edited_.size (), 0);
82 }
83
84 void CategorySelectorTest::testSelectionIsKeyedByName ()
85 {
86 Fixture f;
87 f.Selector_.SetSelections ({ A, "zzz"_qs });
88 QCOMPARE (f.Selector_.GetSelections (), QStringList { A });
89
90 f.Model_.appendRow (new QStandardItem { "zzz"_qs });
91 QCOMPARE (f.Selector_.GetSelections (), (QStringList { A, "zzz"_qs }));
92
93 f.Model_.clear ();
94 QVERIFY (f.Selector_.GetSelections ().isEmpty ());
95
96 f.Model_.appendRow (new QStandardItem { A });
97 QCOMPARE (f.Selector_.GetSelections (), QStringList { A });
98 QCOMPARE (f.Edited_.size (), 0);
99 }
100
101 void CategorySelectorTest::testSelectedIndexesAreTheModelsOwn ()
102 {
103 Fixture f;
104 f.Model_.item (1)->setData (42, Qt::UserRole);
105 f.Selector_.SetSelections ({ A });
106
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);
111 }
112
113 void CategorySelectorTest::testUserChecksAreReported ()
114 {
115 Fixture f;
116 QVERIFY (f.Check (B, Qt::Checked));
117 QCOMPARE (f.Edited_.size (), 1);
118 QCOMPARE (f.LastReported (), QStringList { B });
119
120 QVERIFY (f.Check (A, Qt::Checked));
121 QCOMPARE (f.Edited_.size (), 2);
122 QCOMPARE (f.LastReported (), (QStringList { A, B }));
123
124 QVERIFY (f.Check (B, Qt::Unchecked));
125 QCOMPARE (f.Edited_.size (), 3);
126 QCOMPARE (f.LastReported (), QStringList { A });
127
128 f.Selector_.SelectNone ();
129 f.Selector_.SetSelections ({ C });
130 QCOMPARE (f.Edited_.size (), 3);
131 QCOMPARE (f.Selector_.GetSelections (), QStringList { C });
132 }
133
134 void CategorySelectorTest::testContextActionsAreReported ()
135 {
136 Fixture f;
137 const auto& actions = f.View ().actions ();
138 QCOMPARE (actions.size (), 2);
139
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 }));
144
145 actions.last ()->trigger ();
146 QVERIFY (f.Selector_.GetSelections ().isEmpty ());
147 QCOMPARE (f.Edited_.size (), 2);
148 QVERIFY (f.LastReported ().isEmpty ());
149 }
150
151 void CategorySelectorTest::testCaptionShowsTheHeader ()
152 {
153 Fixture f;
154 QVERIFY (f.View ().isHeaderHidden ());
155
156 f.Selector_.SetCaption ("Tags"_qs);
157 QVERIFY (!f.View ().isHeaderHidden ());
158 QCOMPARE (f.View ().model ()->headerData (0, Qt::Horizontal).toString (), "Tags"_qs);
159
160 f.Selector_.SetCaption ({});
161 QVERIFY (f.View ().isHeaderHidden ());
162 }
163
164 void CategorySelectorTest::testSetModelKeepsTheSelection ()
165 {
166 Fixture f;
167 f.Selector_.SetSelections ({ A, B });
168
169 QStandardItemModel other;
170 for (const auto& name : { "x"_qs, A })
171 other.appendRow (new QStandardItem { name });
172 f.Selector_.SetModel (other);
173
174 QCOMPARE (f.Listed (), (QStringList { A, "x"_qs }));
175 QCOMPARE (f.Selector_.GetSelections (), QStringList { A });
176 QCOMPARE (f.Edited_.size (), 0);
177 }
178}
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
Definition oral.h:1079