File indexing completed on 2024-12-01 04:35:22

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "customsoundsmanagertest.h"
0008 #include "customsound/customsoundsmanager.h"
0009 #include "ruqola_autotest_helper.h"
0010 #include <QSignalSpy>
0011 
0012 QTEST_GUILESS_MAIN(CustomSoundsManagerTest)
0013 
0014 CustomSoundsManagerTest::CustomSoundsManagerTest(QObject *parent)
0015     : QObject{parent}
0016 {
0017 }
0018 
0019 void CustomSoundsManagerTest::shouldHaveDefaultValues()
0020 {
0021     CustomSoundsManager w;
0022     QVERIFY(w.customSoundsInfo().isEmpty());
0023 }
0024 
0025 void CustomSoundsManagerTest::shouldParseCustomSounds_data()
0026 {
0027     QTest::addColumn<QString>("name");
0028     QTest::addColumn<int>("numberOfSounds");
0029     {
0030         QTest::addRow("customSounds1") << QStringLiteral("customSounds1") << 5;
0031     }
0032 }
0033 
0034 void CustomSoundsManagerTest::shouldDeleteCustomSounds()
0035 {
0036     QFETCH(QString, name);
0037     QFETCH(QString, deleteFileName);
0038     QFETCH(int, initialNumberOfSounds);
0039     QFETCH(int, afterDeletingNumberOfSounds);
0040     QFETCH(int, signalsEmittingCount);
0041     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/customsounds/") + name + QLatin1String(".json");
0042     const QJsonArray customSoundsArray = AutoTestHelper::loadJsonArrayObject(originalJsonFile);
0043     CustomSoundsManager w;
0044     QSignalSpy spyDeleteSignal(&w, &CustomSoundsManager::customSoundRemoved);
0045     w.parseCustomSounds(customSoundsArray);
0046 
0047     //    CustomEmoji originalEmoji;
0048     //    originalEmoji.parseEmoji(obj);
0049     //    const bool emojiIsEqual = (originalEmoji == expectedEmoji);
0050     //    if (!emojiIsEqual) {
0051     //        qDebug() << "originalEmoji " << originalEmoji;
0052     //        qDebug() << "ExpectedEmoji " << expectedEmoji;
0053     //    }
0054     QCOMPARE(w.customSoundsInfo().count(), initialNumberOfSounds);
0055 
0056     QString deleteJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/customsounds/") + deleteFileName + QLatin1String(".json");
0057     const QJsonArray deleteCustomSoundsArray = AutoTestHelper::loadJsonArrayObject(deleteJsonFile);
0058     w.deleteCustomSounds(deleteCustomSoundsArray);
0059     QCOMPARE(w.customSoundsInfo().count(), afterDeletingNumberOfSounds);
0060 
0061     QCOMPARE(spyDeleteSignal.count(), signalsEmittingCount);
0062 }
0063 
0064 void CustomSoundsManagerTest::shouldDeleteCustomSounds_data()
0065 {
0066     QTest::addColumn<QString>("name");
0067     QTest::addColumn<QString>("deleteFileName");
0068     QTest::addColumn<int>("initialNumberOfSounds");
0069     QTest::addColumn<int>("afterDeletingNumberOfSounds");
0070     QTest::addColumn<int>("signalsEmittingCount");
0071     QTest::addRow("customSounds1") << QStringLiteral("customSounds1") << QStringLiteral("deleteCustomSounds1") << 5 << 4 << 1;
0072     QTest::addRow("customSounds1") << QStringLiteral("customSounds1") << QStringLiteral("deleteCustomSounds2") << 5 << 5 << 0;
0073 }
0074 
0075 void CustomSoundsManagerTest::shouldAddCustomSounds()
0076 {
0077     QFETCH(QString, name);
0078     QFETCH(QString, addFileName);
0079     QFETCH(int, initialNumberOfSounds);
0080     QFETCH(int, afterDeletingNumberOfSounds);
0081     QFETCH(int, signalsEmittingCount);
0082     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/customsounds/") + name + QLatin1String(".json");
0083     const QJsonArray customSoundsArray = AutoTestHelper::loadJsonArrayObject(originalJsonFile);
0084     CustomSoundsManager w;
0085     QSignalSpy spyAddSignal(&w, &CustomSoundsManager::customSoundAdded);
0086     w.parseCustomSounds(customSoundsArray);
0087 
0088     //    CustomEmoji originalEmoji;
0089     //    originalEmoji.parseEmoji(obj);
0090     //    const bool emojiIsEqual = (originalEmoji == expectedEmoji);
0091     //    if (!emojiIsEqual) {
0092     //        qDebug() << "originalEmoji " << originalEmoji;
0093     //        qDebug() << "ExpectedEmoji " << expectedEmoji;
0094     //    }
0095     QCOMPARE(w.customSoundsInfo().count(), initialNumberOfSounds);
0096 
0097     QString addJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/customsounds/") + addFileName + QLatin1String(".json");
0098     const QJsonArray addCustomSoundsArray = AutoTestHelper::loadJsonArrayObject(addJsonFile);
0099     w.updateCustomSounds(addCustomSoundsArray);
0100     QCOMPARE(w.customSoundsInfo().count(), afterDeletingNumberOfSounds);
0101 
0102     QCOMPARE(spyAddSignal.count(), signalsEmittingCount);
0103 }
0104 
0105 void CustomSoundsManagerTest::shouldAddCustomSounds_data()
0106 {
0107     QTest::addColumn<QString>("name");
0108     QTest::addColumn<QString>("addFileName");
0109     QTest::addColumn<int>("initialNumberOfSounds");
0110     QTest::addColumn<int>("afterDeletingNumberOfSounds");
0111     QTest::addColumn<int>("signalsEmittingCount");
0112     QTest::addRow("customSounds1") << QStringLiteral("customSounds1") << QStringLiteral("addCustomSounds1") << 5 << 6 << 1;
0113 }
0114 
0115 void CustomSoundsManagerTest::shouldUpdateCustomSounds()
0116 {
0117     QFETCH(QString, name);
0118     QFETCH(QString, updateFileName);
0119     QFETCH(int, initialNumberOfSounds);
0120     QFETCH(int, afterDeletingNumberOfSounds);
0121     QFETCH(int, signalsEmittingCount);
0122     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/customsounds/") + name + QLatin1String(".json");
0123     const QJsonArray customSoundsArray = AutoTestHelper::loadJsonArrayObject(originalJsonFile);
0124     CustomSoundsManager w;
0125     QSignalSpy spyUpdateSignal(&w, &CustomSoundsManager::customSoundUpdated);
0126     w.parseCustomSounds(customSoundsArray);
0127 
0128     //    CustomEmoji originalEmoji;
0129     //    originalEmoji.parseEmoji(obj);
0130     //    const bool emojiIsEqual = (originalEmoji == expectedEmoji);
0131     //    if (!emojiIsEqual) {
0132     //        qDebug() << "originalEmoji " << originalEmoji;
0133     //        qDebug() << "ExpectedEmoji " << expectedEmoji;
0134     //    }
0135     QCOMPARE(w.customSoundsInfo().count(), initialNumberOfSounds);
0136 
0137     QString updateJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/customsounds/") + updateFileName + QLatin1String(".json");
0138     const QJsonArray addCustomSoundsArray = AutoTestHelper::loadJsonArrayObject(updateJsonFile);
0139     w.updateCustomSounds(addCustomSoundsArray);
0140     QCOMPARE(w.customSoundsInfo().count(), afterDeletingNumberOfSounds);
0141 
0142     QCOMPARE(spyUpdateSignal.count(), signalsEmittingCount);
0143 }
0144 
0145 void CustomSoundsManagerTest::shouldUpdateCustomSounds_data()
0146 {
0147     QTest::addColumn<QString>("name");
0148     QTest::addColumn<QString>("updateFileName");
0149     QTest::addColumn<int>("initialNumberOfSounds");
0150     QTest::addColumn<int>("afterDeletingNumberOfSounds");
0151     QTest::addColumn<int>("signalsEmittingCount");
0152     QTest::addRow("customSounds1") << QStringLiteral("customSounds1") << QStringLiteral("updateCustomSounds1") << 5 << 5 << 1;
0153 }
0154 
0155 void CustomSoundsManagerTest::shouldParseCustomSounds()
0156 {
0157     QFETCH(QString, name);
0158     QFETCH(int, numberOfSounds);
0159     const QString originalJsonFile = QLatin1String(RUQOLA_DATA_DIR) + QLatin1String("/customsounds/") + name + QLatin1String(".json");
0160     const QJsonArray customSoundsArray = AutoTestHelper::loadJsonArrayObject(originalJsonFile);
0161     CustomSoundsManager w;
0162     w.parseCustomSounds(customSoundsArray);
0163     //    CustomEmoji originalEmoji;
0164     //    originalEmoji.parseEmoji(obj);
0165     //    const bool emojiIsEqual = (originalEmoji == expectedEmoji);
0166     //    if (!emojiIsEqual) {
0167     //        qDebug() << "originalEmoji " << originalEmoji;
0168     //        qDebug() << "ExpectedEmoji " << expectedEmoji;
0169     //    }
0170     QCOMPARE(w.customSoundsInfo().count(), numberOfSounds);
0171 }
0172 
0173 #include "moc_customsoundsmanagertest.cpp"