File indexing completed on 2024-03-24 03:56:24

0001 /*
0002     SPDX-FileCopyrightText: 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
0003     SPDX-FileCopyrightText: 2017 Harald Sitter <sitter@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 // test object
0009 #include <kaboutdata.h>
0010 // Qt
0011 #include <QFile>
0012 #include <QLatin1String>
0013 #include <QObject>
0014 #include <QTest>
0015 #include <QTextStream>
0016 #ifndef Q_OS_WIN
0017 void initLocale()
0018 {
0019     qputenv("LC_ALL", "en_US.utf-8");
0020 }
0021 
0022 Q_CONSTRUCTOR_FUNCTION(initLocale)
0023 #endif
0024 
0025 class KAboutDataTest : public QObject
0026 {
0027     Q_OBJECT
0028 
0029 private Q_SLOTS:
0030     void testLongFormConstructorWithDefaults();
0031     void testLongFormConstructor();
0032     void testShortFormConstructor();
0033     void testSetAddLicense();
0034     void testSetDesktopFileName();
0035     void testCopying();
0036 
0037     void testKAboutDataOrganizationDomain();
0038 
0039     void testLicenseSPDXID();
0040     void testLicenseOrLater();
0041 
0042     void testProductName();
0043 };
0044 
0045 static const char AppName[] = "app";
0046 static const char ProgramName[] = "ProgramName";
0047 static const char Version[] = "Version";
0048 static const char ShortDescription[] = "ShortDescription";
0049 static const char CopyrightStatement[] = "CopyrightStatement";
0050 static const char Text[] = "Text";
0051 static const char HomePageAddress[] = "http://test.no.where/";
0052 static const char HomePageSecure[] = "https://test.no.where/";
0053 static const char OrganizationDomain[] = "no.where";
0054 static const char BugsEmailAddress[] = "bugs@no.else";
0055 static const char LicenseText[] = "free to write, reading forbidden";
0056 static const char LicenseFileName[] = "testlicensefile";
0057 static const char LicenseFileText[] = "free to write, reading forbidden, in the file";
0058 
0059 void KAboutDataTest::testLongFormConstructorWithDefaults()
0060 {
0061     KAboutData aboutData(QString::fromLatin1(AppName),
0062                          QLatin1String(ProgramName),
0063                          QString::fromLatin1(Version),
0064                          QLatin1String(ShortDescription),
0065                          KAboutLicense::Unknown);
0066 
0067     QCOMPARE(aboutData.componentName(), QString::fromLatin1(AppName));
0068     QCOMPARE(aboutData.productName(), QString::fromLatin1(AppName));
0069     QCOMPARE(aboutData.displayName(), QLatin1String(ProgramName));
0070     QCOMPARE(aboutData.programLogo(), QVariant());
0071     QCOMPARE(aboutData.organizationDomain(), QString::fromLatin1("kde.org"));
0072     QCOMPARE(aboutData.version(), QString::fromLatin1(Version));
0073     QCOMPARE(aboutData.homepage(), QString());
0074     QCOMPARE(aboutData.bugAddress(), QString::fromLatin1("submit@bugs.kde.org"));
0075     QVERIFY(aboutData.authors().isEmpty());
0076     QVERIFY(aboutData.credits().isEmpty());
0077     QVERIFY(aboutData.translators().isEmpty());
0078     QCOMPARE(aboutData.otherText(), QString());
0079     QCOMPARE(aboutData.licenses().count(), 1);
0080     // We don't know the default text, do we?
0081     //     QCOMPARE( aboutData.licenses().at(0).name(KAboutLicense::ShortName), QString(WarningText) );
0082     QVERIFY(!aboutData.licenses().at(0).name(KAboutLicense::ShortName).isEmpty());
0083     //     QCOMPARE( aboutData.licenses().at(0).name(KAboutLicense::FullName), QString(WarningText) );
0084     QVERIFY(!aboutData.licenses().at(0).name(KAboutLicense::FullName).isEmpty());
0085     //     QCOMPARE( aboutData.licenses().at(0).text(), QString(WarningText) );
0086     QVERIFY(!aboutData.licenses().at(0).text().isEmpty());
0087     QCOMPARE(aboutData.copyrightStatement(), QString());
0088     QCOMPARE(aboutData.shortDescription(), (QLatin1String(ShortDescription)));
0089     QCOMPARE(aboutData.customAuthorPlainText(), QString());
0090     QCOMPARE(aboutData.customAuthorRichText(), QString());
0091     QVERIFY(!aboutData.customAuthorTextEnabled());
0092     QCOMPARE(aboutData.desktopFileName(), QStringLiteral("org.kde.app"));
0093 
0094     QCOMPARE(aboutData.internalVersion(), Version);
0095     QCOMPARE(aboutData.internalProgramName(), ProgramName);
0096     QCOMPARE(aboutData.internalBugAddress(), "submit@bugs.kde.org");
0097     QCOMPARE(aboutData.internalProductName(), nullptr);
0098 }
0099 
0100 void KAboutDataTest::testLongFormConstructor()
0101 {
0102     KAboutData aboutData(QString::fromLatin1(AppName),
0103                          QLatin1String(ProgramName),
0104                          QString::fromLatin1(Version),
0105                          QLatin1String(ShortDescription),
0106                          KAboutLicense::Unknown,
0107                          QLatin1String(CopyrightStatement),
0108                          QLatin1String(Text),
0109                          QString::fromLatin1(HomePageAddress),
0110                          QString::fromLatin1(BugsEmailAddress));
0111 
0112     QCOMPARE(aboutData.componentName(), QLatin1String(AppName));
0113     QCOMPARE(aboutData.productName(), QLatin1String(AppName));
0114     QCOMPARE(aboutData.displayName(), QLatin1String(ProgramName));
0115     QCOMPARE(aboutData.programLogo(), QVariant());
0116     QCOMPARE(aboutData.organizationDomain(), QString::fromLatin1(OrganizationDomain));
0117     QCOMPARE(aboutData.version(), QString::fromLatin1(Version));
0118     QCOMPARE(aboutData.homepage(), QString::fromLatin1(HomePageAddress));
0119     QCOMPARE(aboutData.bugAddress(), QString::fromLatin1(BugsEmailAddress));
0120     QVERIFY(aboutData.authors().isEmpty());
0121     QVERIFY(aboutData.credits().isEmpty());
0122     QVERIFY(aboutData.translators().isEmpty());
0123     QCOMPARE(aboutData.otherText(), QLatin1String(Text));
0124     QCOMPARE(aboutData.licenses().count(), 1);
0125     // We don't know the default text, do we?
0126     //     QCOMPARE( aboutData.licenses().at(0).name(KAboutLicense::ShortName), QString(WarningText) );
0127     QVERIFY(!aboutData.licenses().at(0).name(KAboutLicense::ShortName).isEmpty());
0128     //     QCOMPARE( aboutData.licenses().at(0).name(KAboutLicense::FullName), QString(WarningText) );
0129     QVERIFY(!aboutData.licenses().at(0).name(KAboutLicense::FullName).isEmpty());
0130     //     QCOMPARE( aboutData.licenses().at(0).text(), QString(WarningText) );
0131     QVERIFY(!aboutData.licenses().at(0).text().isEmpty());
0132     QCOMPARE(aboutData.copyrightStatement(), QLatin1String(CopyrightStatement));
0133     QCOMPARE(aboutData.shortDescription(), QLatin1String(ShortDescription));
0134     QCOMPARE(aboutData.customAuthorPlainText(), QString());
0135     QCOMPARE(aboutData.customAuthorRichText(), QString());
0136     QVERIFY(!aboutData.customAuthorTextEnabled());
0137     QCOMPARE(aboutData.desktopFileName(), QStringLiteral("where.no.app"));
0138 
0139     QCOMPARE(aboutData.internalVersion(), Version);
0140     QCOMPARE(aboutData.internalProgramName(), ProgramName);
0141     QCOMPARE(aboutData.internalBugAddress(), BugsEmailAddress);
0142     QCOMPARE(aboutData.internalProductName(), nullptr);
0143 
0144     aboutData.addAuthor(QStringLiteral("John"),
0145                         QStringLiteral("Stuff"),
0146                         QStringLiteral("john@john.com"),
0147                         QStringLiteral("john.com"),
0148                         QStringLiteral("ocsjohn"));
0149     QCOMPARE(aboutData.authors().length(), 1);
0150     QCOMPARE(aboutData.credits().length(), 0);
0151     QCOMPARE(aboutData.authors().at(0).avatarUrl(), QUrl(QStringLiteral("https://store.kde.org/avatar/ocsjohn")));
0152 
0153     // We support http and https protocols on the homepage address, ensure they
0154     // give the same org. domain and desktop file name.
0155     KAboutData aboutDataSecure(QString::fromLatin1(AppName),
0156                                QLatin1String(ProgramName),
0157                                QString::fromLatin1(Version),
0158                                QLatin1String(ShortDescription),
0159                                KAboutLicense::Unknown,
0160                                QLatin1String(CopyrightStatement),
0161                                QLatin1String(Text),
0162                                QString::fromLatin1(HomePageSecure),
0163                                QString::fromLatin1(BugsEmailAddress));
0164     QCOMPARE(aboutDataSecure.componentName(), QLatin1String(AppName));
0165     QCOMPARE(aboutDataSecure.productName(), QLatin1String(AppName));
0166     QCOMPARE(aboutDataSecure.organizationDomain(), QString::fromLatin1(OrganizationDomain));
0167     QCOMPARE(aboutDataSecure.desktopFileName(), QStringLiteral("where.no.app"));
0168 }
0169 
0170 void KAboutDataTest::testShortFormConstructor()
0171 {
0172     KAboutData aboutData(QString::fromLatin1(AppName), QLatin1String(ProgramName), QString::fromLatin1(Version));
0173 
0174     QCOMPARE(aboutData.componentName(), QString::fromLatin1(AppName));
0175     QCOMPARE(aboutData.productName(), QString::fromLatin1(AppName));
0176     QCOMPARE(aboutData.displayName(), QLatin1String(ProgramName));
0177     QCOMPARE(aboutData.programLogo(), QVariant());
0178     QCOMPARE(aboutData.organizationDomain(), QString::fromLatin1("kde.org"));
0179     QCOMPARE(aboutData.version(), QString::fromLatin1(Version));
0180     QCOMPARE(aboutData.homepage(), QString());
0181     QCOMPARE(aboutData.bugAddress(), QString::fromLatin1("submit@bugs.kde.org"));
0182     QVERIFY(aboutData.authors().isEmpty());
0183     QVERIFY(aboutData.credits().isEmpty());
0184     QVERIFY(aboutData.translators().isEmpty());
0185     QCOMPARE(aboutData.otherText(), QString());
0186     QCOMPARE(aboutData.licenses().count(), 1);
0187     // We don't know the default text, do we?
0188     //     QCOMPARE( aboutData.licenses().at(0).name(KAboutLicense::ShortName), QString(WarningText) );
0189     QVERIFY(!aboutData.licenses().at(0).name(KAboutLicense::ShortName).isEmpty());
0190     //     QCOMPARE( aboutData.licenses().at(0).name(KAboutLicense::FullName), QString(WarningText) );
0191     QVERIFY(!aboutData.licenses().at(0).name(KAboutLicense::FullName).isEmpty());
0192     //     QCOMPARE( aboutData.licenses().at(0).text(), QString(WarningText) );
0193     QVERIFY(!aboutData.licenses().at(0).text().isEmpty());
0194     QCOMPARE(aboutData.copyrightStatement(), QString());
0195     QCOMPARE(aboutData.shortDescription(), QString());
0196     QCOMPARE(aboutData.customAuthorPlainText(), QString());
0197     QCOMPARE(aboutData.customAuthorRichText(), QString());
0198     QVERIFY(!aboutData.customAuthorTextEnabled());
0199     QCOMPARE(aboutData.desktopFileName(), QStringLiteral("org.kde.app"));
0200 
0201     QCOMPARE(aboutData.internalVersion(), Version);
0202     QCOMPARE(aboutData.internalProgramName(), ProgramName);
0203     QCOMPARE(aboutData.internalBugAddress(), "submit@bugs.kde.org");
0204     QCOMPARE(aboutData.internalProductName(), nullptr);
0205 }
0206 
0207 void KAboutDataTest::testKAboutDataOrganizationDomain()
0208 {
0209     KAboutData data(QString::fromLatin1("app"),
0210                     QLatin1String("program"),
0211                     QString::fromLatin1("version"),
0212                     QLatin1String("description"),
0213                     KAboutLicense::LGPL,
0214                     QLatin1String("copyright"),
0215                     QLatin1String("hello world"),
0216                     QStringLiteral("http://www.koffice.org"));
0217     QCOMPARE(data.organizationDomain(), QString::fromLatin1("koffice.org"));
0218     QCOMPARE(data.desktopFileName(), QStringLiteral("org.koffice.app"));
0219 
0220     KAboutData data2(QString::fromLatin1("app"),
0221                      QLatin1String("program"),
0222                      QString::fromLatin1("version"),
0223                      QLatin1String("description"),
0224                      KAboutLicense::LGPL,
0225                      QString::fromLatin1("copyright"),
0226                      QLatin1String("hello world"),
0227                      QStringLiteral("app"));
0228     QCOMPARE(data2.organizationDomain(), QString::fromLatin1("kde.org"));
0229     QCOMPARE(data2.desktopFileName(), QStringLiteral("org.kde.app"));
0230 }
0231 
0232 void KAboutDataTest::testSetAddLicense()
0233 {
0234     // prepare a file with a license text
0235     QFile licenseFile(QString::fromLatin1(LicenseFileName));
0236     licenseFile.open(QIODevice::WriteOnly);
0237     QTextStream licenseFileStream(&licenseFile);
0238     licenseFileStream << LicenseFileText;
0239     licenseFile.close();
0240 
0241     const QString copyrightStatement = QLatin1String(CopyrightStatement);
0242     const QString lineFeed = QString::fromLatin1("\n\n");
0243 
0244     KAboutData aboutData(QString::fromLatin1(AppName),
0245                          QLatin1String(ProgramName),
0246                          QString::fromLatin1(Version),
0247                          QLatin1String(ShortDescription),
0248                          KAboutLicense::Unknown,
0249                          QLatin1String(CopyrightStatement),
0250                          QLatin1String(Text),
0251                          QString::fromLatin1(HomePageAddress),
0252                          QString::fromLatin1(BugsEmailAddress));
0253 
0254     // set to GPL2
0255     aboutData.setLicense(KAboutLicense::GPL_V2);
0256 
0257     QCOMPARE(aboutData.licenses().count(), 1);
0258     QCOMPARE(aboutData.licenses().at(0).name(KAboutLicense::ShortName), QString::fromLatin1("GPL v2"));
0259     QCOMPARE(aboutData.licenses().at(0).name(KAboutLicense::FullName), QString::fromLatin1("GNU General Public License Version 2"));
0260     //     QCOMPARE( aboutData.licenses().at(0).text(), QString(GPL2Text) );
0261     QVERIFY(!aboutData.licenses().at(0).text().isEmpty());
0262 
0263     // set to Unknown again
0264     aboutData.setLicense(KAboutLicense::Unknown);
0265 
0266     QCOMPARE(aboutData.licenses().count(), 1);
0267     // We don't know the default text, do we?
0268     //     QCOMPARE( aboutData.licenses().at(0).name(KAboutLicense::ShortName), QString(WarningText) );
0269     QVERIFY(!aboutData.licenses().at(0).name(KAboutLicense::ShortName).isEmpty());
0270     //     QCOMPARE( aboutData.licenses().at(0).name(KAboutLicense::FullName), QString(WarningText) );
0271     QVERIFY(!aboutData.licenses().at(0).name(KAboutLicense::FullName).isEmpty());
0272     //     QCOMPARE( aboutData.licenses().at(0).text(), QString(WarningText) );
0273     QVERIFY(!aboutData.licenses().at(0).text().isEmpty());
0274 
0275     // add GPL3
0276     aboutData.addLicense(KAboutLicense::GPL_V3);
0277 
0278     QCOMPARE(aboutData.licenses().count(), 1);
0279     QCOMPARE(aboutData.licenses().at(0).name(KAboutLicense::ShortName), QString::fromLatin1("GPL v3"));
0280     QCOMPARE(aboutData.licenses().at(0).name(KAboutLicense::FullName), QString::fromLatin1("GNU General Public License Version 3"));
0281     //     QCOMPARE( aboutData.licenses().at(0).text(), QString(GPL3Text) );
0282     QVERIFY(!aboutData.licenses().at(0).text().isEmpty());
0283 
0284     // add GPL2, Custom and File
0285     aboutData.addLicense(KAboutLicense::GPL_V2);
0286     aboutData.addLicenseText(QLatin1String(LicenseText));
0287     aboutData.addLicenseTextFile(QLatin1String(LicenseFileName));
0288 
0289     QCOMPARE(aboutData.licenses().count(), 4);
0290     QCOMPARE(aboutData.licenses().at(0).name(KAboutLicense::ShortName), QString::fromLatin1("GPL v3"));
0291     QCOMPARE(aboutData.licenses().at(0).name(KAboutLicense::FullName), QString::fromLatin1("GNU General Public License Version 3"));
0292     //     QCOMPARE( aboutData.licenses().at(0).text(), QString(GPL3Text) );
0293     QVERIFY(!aboutData.licenses().at(0).text().isEmpty());
0294     QCOMPARE(aboutData.licenses().at(1).name(KAboutLicense::ShortName), QString::fromLatin1("GPL v2"));
0295     QCOMPARE(aboutData.licenses().at(1).name(KAboutLicense::FullName), QString::fromLatin1("GNU General Public License Version 2"));
0296     //     QCOMPARE( aboutData.licenses().at(1).text(), QString(GPL2Text) );
0297     QVERIFY(!aboutData.licenses().at(1).text().isEmpty());
0298     QCOMPARE(aboutData.licenses().at(2).name(KAboutLicense::ShortName), QString::fromLatin1("Custom"));
0299     QCOMPARE(aboutData.licenses().at(2).name(KAboutLicense::FullName), QString::fromLatin1("Custom"));
0300     QCOMPARE(aboutData.licenses().at(2).text(), QLatin1String(LicenseText));
0301     QCOMPARE(aboutData.licenses().at(3).name(KAboutLicense::ShortName), QString::fromLatin1("Custom"));
0302     QCOMPARE(aboutData.licenses().at(3).name(KAboutLicense::FullName), QString::fromLatin1("Custom"));
0303     QCOMPARE(aboutData.licenses().at(3).text(), QString(copyrightStatement + lineFeed + QLatin1String(LicenseFileText)));
0304 }
0305 
0306 void KAboutDataTest::testCopying()
0307 {
0308     KAboutData aboutData(QString::fromLatin1(AppName),
0309                          QLatin1String(ProgramName),
0310                          QString::fromLatin1(Version),
0311                          QLatin1String(ShortDescription),
0312                          KAboutLicense::GPL_V2);
0313 
0314     {
0315         KAboutData aboutData2(QString::fromLatin1(AppName),
0316                               QLatin1String(ProgramName),
0317                               QString::fromLatin1(Version),
0318                               QLatin1String(ShortDescription),
0319                               KAboutLicense::GPL_V3);
0320         aboutData2.addLicense(KAboutLicense::GPL_V2, KAboutLicense::OrLaterVersions);
0321         aboutData = aboutData2;
0322     }
0323     QList<KAboutLicense> licenses = aboutData.licenses();
0324     QCOMPARE(licenses.count(), 2);
0325     QCOMPARE(licenses.at(0).key(), KAboutLicense::GPL_V3);
0326     QCOMPARE(aboutData.licenses().at(0).spdx(), QStringLiteral("GPL-3.0"));
0327     // check it doesn't crash
0328     QVERIFY(!licenses.at(0).text().isEmpty());
0329     QCOMPARE(licenses.at(1).key(), KAboutLicense::GPL_V2);
0330     QCOMPARE(aboutData.licenses().at(1).spdx(), QStringLiteral("GPL-2.0+"));
0331     // check it doesn't crash
0332     QVERIFY(!licenses.at(1).text().isEmpty());
0333 }
0334 
0335 void KAboutDataTest::testSetDesktopFileName()
0336 {
0337     KAboutData aboutData(QString::fromLatin1(AppName),
0338                          QLatin1String(ProgramName),
0339                          QString::fromLatin1(Version),
0340                          QLatin1String(ShortDescription),
0341                          KAboutLicense::Unknown);
0342     QCOMPARE(aboutData.desktopFileName(), QStringLiteral("org.kde.app"));
0343 
0344     // set different desktopFileName
0345     aboutData.setDesktopFileName(QStringLiteral("foo.bar.application"));
0346     QCOMPARE(aboutData.desktopFileName(), QStringLiteral("foo.bar.application"));
0347 }
0348 
0349 void KAboutDataTest::testLicenseSPDXID()
0350 {
0351     // Input with + should output with +.
0352     auto license = KAboutLicense::byKeyword(QStringLiteral("GPLv2+"));
0353     QCOMPARE(license.spdx(), QStringLiteral("GPL-2.0+"));
0354     // Input without should output without.
0355     license = KAboutLicense::byKeyword(QStringLiteral("GPLv2"));
0356     QCOMPARE(license.spdx(), QStringLiteral("GPL-2.0"));
0357     // Input of spdx with or-later should also work
0358     license = KAboutLicense::byKeyword(QStringLiteral("GPL-2.0-or-later"));
0359     QCOMPARE(license.spdx(), QStringLiteral("GPL-2.0+")); // TODO: should not return the deprecatd version
0360 
0361     // we should be able to match by spdx too
0362     // create a KAboutLicense from enum, then make sure going to spdx and back gives the same enum
0363     for (int i = 1; i <= KAboutLicense::LGPL_V2_1; ++i) { /*current highest enum value*/
0364         KAboutData aboutData(QString::fromLatin1(AppName),
0365                              QLatin1String(ProgramName),
0366                              QString::fromLatin1(Version),
0367                              QLatin1String(ShortDescription),
0368                              KAboutLicense::GPL_V2);
0369         aboutData.setLicense(KAboutLicense::LicenseKey(i));
0370         QVERIFY(aboutData.licenses().count() == 1);
0371         const auto license = aboutData.licenses().constFirst();
0372         auto licenseFromKeyword = KAboutLicense::byKeyword(license.spdx());
0373 
0374         QCOMPARE(license.key(), licenseFromKeyword.key());
0375     }
0376 }
0377 
0378 void KAboutDataTest::testLicenseOrLater()
0379 {
0380     // For kaboutdata we can replace the license with an orLater version. Or add a second one.
0381     KAboutData aboutData(QString::fromLatin1(AppName),
0382                          QLatin1String(ProgramName),
0383                          QString::fromLatin1(Version),
0384                          QLatin1String(ShortDescription),
0385                          KAboutLicense::GPL_V2);
0386     QCOMPARE(aboutData.licenses().at(0).spdx(), QStringLiteral("GPL-2.0"));
0387     aboutData.setLicense(KAboutLicense::GPL_V2, KAboutLicense::OrLaterVersions);
0388     QCOMPARE(aboutData.licenses().at(0).spdx(), QStringLiteral("GPL-2.0+"));
0389     aboutData.addLicense(KAboutLicense::LGPL_V3, KAboutLicense::OrLaterVersions);
0390     bool foundLGPL = false;
0391     const QList<KAboutLicense> licenses = aboutData.licenses();
0392     for (const auto &license : licenses) {
0393         if (license.key() == KAboutLicense::LGPL_V3) {
0394             QCOMPARE(license.spdx(), QStringLiteral("LGPL-3.0+"));
0395             foundLGPL = true;
0396             break;
0397         }
0398     }
0399     QCOMPARE(foundLGPL, true);
0400 }
0401 
0402 void KAboutDataTest::testProductName()
0403 {
0404     KAboutData aboutData(QString::fromLatin1(AppName), QString::fromLatin1(ProgramName));
0405     QCOMPARE(aboutData.productName(), QString::fromLatin1(AppName));
0406     QCOMPARE(aboutData.internalProductName(), nullptr);
0407     aboutData.setProductName("frameworks-kcoreaddons/aboutdata");
0408     QCOMPARE(aboutData.productName(), QString::fromLatin1("frameworks-kcoreaddons/aboutdata"));
0409     QCOMPARE(aboutData.internalProductName(), "frameworks-kcoreaddons/aboutdata");
0410 }
0411 
0412 QTEST_MAIN(KAboutDataTest)
0413 
0414 #include "kaboutdatatest.moc"