Changeset View
Changeset View
Standalone View
Standalone View
autotests/kactioncollectiontest.cpp
1 | #include "kactioncollectiontest.h" | 1 | #include "kactioncollectiontest.h" | ||
---|---|---|---|---|---|
2 | 2 | | |||
3 | #include <QtTestWidgets> | 3 | #include <QtTestWidgets> | ||
4 | #include <QAction> | 4 | #include <QAction> | ||
5 | #include <QPointer> | 5 | #include <QPointer> | ||
6 | #include <QSignalSpy> | ||||
6 | 7 | | |||
7 | #include <ksharedconfig.h> | 8 | #include <ksharedconfig.h> | ||
8 | #include <kstandardaction.h> | 9 | #include <kstandardaction.h> | ||
9 | 10 | | |||
10 | void tst_KActionCollection::init() | 11 | void tst_KActionCollection::init() | ||
11 | { | 12 | { | ||
12 | collection = new KActionCollection(static_cast<QObject *>(nullptr)); | 13 | collection = new KActionCollection(static_cast<QObject *>(nullptr)); | ||
13 | } | 14 | } | ||
▲ Show 20 Lines • Show All 226 Lines • ▼ Show 20 Line(s) | |||||
240 | { | 241 | { | ||
241 | KActionCollection collection(static_cast<QObject *>(nullptr)); | 242 | KActionCollection collection(static_cast<QObject *>(nullptr)); | ||
242 | QAction *cut = KStandardAction::cut(&collection); | 243 | QAction *cut = KStandardAction::cut(&collection); | ||
243 | QAction *a = collection.action(QString::fromLatin1(KStandardAction::name(KStandardAction::Cut))); | 244 | QAction *a = collection.action(QString::fromLatin1(KStandardAction::name(KStandardAction::Cut))); | ||
244 | QVERIFY(a); | 245 | QVERIFY(a); | ||
245 | QVERIFY(a == cut); | 246 | QVERIFY(a == cut); | ||
246 | } | 247 | } | ||
247 | 248 | | |||
249 | void tst_KActionCollection::shouldEmitSignals() | ||||
250 | { | ||||
251 | QAction *a = new QAction(nullptr); | ||||
252 | QAction *b = new QAction(nullptr); | ||||
253 | | ||||
254 | QSignalSpy insertedSpy(collection, &KActionCollection::inserted); | ||||
255 | QSignalSpy changedSpy(collection, &KActionCollection::changed); | ||||
256 | | ||||
257 | // Insert "first" | ||||
258 | collection->addAction(QStringLiteral("first"), a); | ||||
259 | QVERIFY(collection->actions().contains(a)); | ||||
260 | QCOMPARE(insertedSpy.count(), 1); | ||||
261 | QCOMPARE(insertedSpy.at(0).at(0).value<QAction *>(), a); | ||||
262 | QCOMPARE(changedSpy.count(), 1); | ||||
263 | insertedSpy.clear(); | ||||
264 | changedSpy.clear(); | ||||
265 | | ||||
266 | // Replace "first" | ||||
267 | collection->addAction(QStringLiteral("first"), b); | ||||
268 | QVERIFY(!collection->actions().contains(a)); | ||||
269 | QVERIFY(collection->actions().contains(b)); | ||||
270 | QCOMPARE(insertedSpy.count(), 1); | ||||
271 | QCOMPARE(insertedSpy.at(0).at(0).value<QAction *>(), b); | ||||
272 | QCOMPARE(changedSpy.count(), 2); // once for removing a, once for inserting b | ||||
273 | insertedSpy.clear(); | ||||
274 | changedSpy.clear(); | ||||
275 | | ||||
276 | // Insert "second" | ||||
277 | collection->addAction(QStringLiteral("second"), a); | ||||
278 | QCOMPARE(insertedSpy.count(), 1); | ||||
279 | QCOMPARE(insertedSpy.at(0).at(0).value<QAction *>(), a); | ||||
280 | QCOMPARE(changedSpy.count(), 1); | ||||
281 | insertedSpy.clear(); | ||||
282 | changedSpy.clear(); | ||||
283 | | ||||
284 | // Remove and delete "second" (which is a) | ||||
285 | collection->removeAction(a); | ||||
286 | QCOMPARE(changedSpy.count(), 1); | ||||
287 | changedSpy.clear(); | ||||
288 | | ||||
289 | // Delete b directly, should automatically remove it from the collection and emit changed | ||||
290 | delete b; | ||||
291 | QCOMPARE(changedSpy.count(), 1); | ||||
292 | changedSpy.clear(); | ||||
293 | } | ||||
294 | | ||||
248 | QTEST_MAIN(tst_KActionCollection) | 295 | QTEST_MAIN(tst_KActionCollection) | ||
249 | 296 | |