File indexing completed on 2024-04-14 14:29:54

0001 #include "kactioncategorytest.h"
0002 
0003 #include <QtTestWidgets>
0004 
0005 #include "kactioncategory.h"
0006 #include "kactioncollection.h"
0007 #include <KSelectAction>
0008 #include <KStandardAction>
0009 
0010 void tst_KActionCategory::tstCreation()
0011 {
0012     KActionCollection collection((QObject *)nullptr);
0013     KActionCategory category1(QStringLiteral("category1"), &collection);
0014     KActionCategory category2(QStringLiteral("category2"), &collection);
0015 
0016     // Check that the name is correct
0017     QCOMPARE(category1.text(), QStringLiteral("category1"));
0018     QCOMPARE(category2.text(), QStringLiteral("category2"));
0019 
0020     // Check that the parent is correct
0021     QCOMPARE(category1.collection(), &collection);
0022     QCOMPARE(category2.collection(), &collection);
0023 
0024     // Check that the category is available as a child of the collection
0025     QList<KActionCategory *> categories = collection.findChildren<KActionCategory *>();
0026     QCOMPARE(categories.size(), 2);
0027     QCOMPARE(categories.count(&category1), 1);
0028     QCOMPARE(categories.count(&category2), 1);
0029 
0030     // Change the text
0031     category1.setText(QStringLiteral("Other Text"));
0032     QCOMPARE(category1.text(), QStringLiteral("Other Text"));
0033 }
0034 
0035 void tst_KActionCategory::tstSynchronization()
0036 {
0037     KActionCollection collection((QObject *)nullptr);
0038     KActionCategory category1(QStringLiteral("category1"), &collection);
0039     KActionCategory category2(QStringLiteral("category2"), &collection);
0040 
0041     // The collection is empty
0042     QCOMPARE(collection.count(), 0);
0043 
0044     // Now add a action to category1
0045     QAction *action1 = category1.addAction(QStringLiteral("action1"));
0046     // Check it was added to the category.
0047     QCOMPARE(category1.actions().count(), 1);
0048     QCOMPARE(category1.actions().count(action1), 1);
0049     // Check it was added to the collection
0050     QCOMPARE(collection.actions().count(), 1);
0051     QCOMPARE(collection.actions().count(action1), 1);
0052 
0053     // Short intermezzo. Add the action a second time
0054     category1.addAction(QStringLiteral("action1_new"), action1);
0055     QCOMPARE(category1.actions().count(), 1);
0056     QCOMPARE(category1.actions().count(action1), 1);
0057     QCOMPARE(collection.actions().count(), 1);
0058     QCOMPARE(collection.actions().count(action1), 1);
0059 
0060     // Now add a action to category2
0061     QAction *action2 = category2.addAction(QStringLiteral("action2"));
0062     // Check it was added to the category.
0063     QCOMPARE(category2.actions().count(), 1);
0064     QCOMPARE(category2.actions().count(action2), 1);
0065     // Check it was added to the collection
0066     QCOMPARE(collection.actions().count(), 2);
0067     QCOMPARE(collection.actions().count(action2), 1);
0068 
0069     // Delete action1
0070     delete action1;
0071     // Check it was removed from the collection
0072     QCOMPARE(collection.actions().count(), 1);
0073     QCOMPARE(collection.actions().count(action1), 0);
0074     // Check it was removed from the category.
0075     QCOMPARE(category1.actions().count(), 0);
0076     QCOMPARE(category1.actions().count(action1), 0);
0077 
0078     // Remove action2 from the collection
0079     collection.removeAction(action2);
0080     // Check it was removed from the collection
0081     QCOMPARE(collection.actions().count(), 0);
0082     QCOMPARE(collection.actions().count(action2), 0);
0083     // Check it was removed from the category.
0084     QCOMPARE(category2.actions().count(), 0);
0085     QCOMPARE(category2.actions().count(action2), 0);
0086 
0087     // Create another category, add a action, delete the category and check
0088     // if the action is still part of the collection.
0089     KActionCategory *category3 = new KActionCategory(QStringLiteral("category3"), &collection);
0090     QAction *action3 = category3->addAction(QStringLiteral("action3"));
0091     // Check it was added to the collection
0092     QCOMPARE(collection.actions().count(action3), 1);
0093     // delete the category
0094     delete category3;
0095     // Make sure the action is still there.
0096     QCOMPARE(collection.actions().count(action3), 1);
0097 }
0098 
0099 void tst_KActionCategory::tstActionCreation()
0100 {
0101     KActionCollection collection((QObject *)nullptr);
0102     KActionCategory category(QStringLiteral("category"), &collection);
0103 
0104     // QAction * addAction(const QString &name, QAction *action);
0105     QAction *action1 = new QAction(nullptr);
0106     category.addAction(QStringLiteral("action1"), action1);
0107     QCOMPARE(category.actions().count(action1), 1);
0108     QCOMPARE(collection.actions().count(action1), 1);
0109 
0110     // QAction * addAction(const QString &name, QAction *action);
0111     QAction *action2 = new QAction(nullptr);
0112     category.addAction(QStringLiteral("action2"), action2);
0113     QCOMPARE(category.actions().count(action2), 1);
0114     QCOMPARE(collection.actions().count(action2), 1);
0115 
0116     // QAction * addAction(
0117     //         KStandardAction::StandardAction actionType,
0118     //         const QObject *receiver = NULL,
0119     //         const char *member = NULL);
0120     QAction *action3 = category.addAction(KStandardAction::Revert);
0121     QCOMPARE(category.actions().count(action3), 1);
0122     QCOMPARE(collection.actions().count(action3), 1);
0123 
0124     // QAction * addAction(
0125     //         KStandardAction::StandardAction actionType,
0126     //         const QString &name,
0127     //         const QObject *receiver = NULL,
0128     //         const char *member = NULL);
0129     QAction *action4 = category.addAction(KStandardAction::Quit, QStringLiteral("myownname"));
0130     QCOMPARE(action4->objectName(), QStringLiteral("myownname"));
0131     QCOMPARE(category.actions().count(action4), 1);
0132     QCOMPARE(collection.actions().count(action4), 1);
0133 
0134     // QAction *addAction(
0135     //         const QString &name,
0136     //         const QObject *receiver = NULL,
0137     //         const char *member = NULL);
0138     QAction *action5 = category.addAction(QStringLiteral("action5"));
0139     QCOMPARE(category.actions().count(action5), 1);
0140     QCOMPARE(collection.actions().count(action5), 1);
0141 
0142     // template<class ActionType>
0143     // ActionType *add(
0144     //         const QString &name,
0145     //         const QObject *receiver = NULL,
0146     //         const char *member = NULL)
0147     KSelectAction *action6 = category.add<KSelectAction>(QStringLiteral("action6"));
0148     QCOMPARE(category.actions().count(action6), 1);
0149     QCOMPARE(collection.actions().count(action6), 1);
0150 
0151     // There should be 6 actions inside the collection and category
0152     QCOMPARE(category.actions().count(), 6);
0153     QCOMPARE(collection.actions().count(), 6);
0154     delete action1;
0155     delete action2;
0156 }
0157 
0158 QTEST_MAIN(tst_KActionCategory)
0159 
0160 #include "moc_kactioncategorytest.cpp"