File indexing completed on 2024-05-12 05:46:33
0001 /* 0002 * Copyright 2011 Romain Perier <bambi@kubuntu.org> 0003 * 0004 * This program is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU General Public License as 0006 * published by the Free Software Foundation; either version 2 of 0007 * the License, or (at your option) any later version. 0008 * 0009 * This program is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0012 * GNU General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU General Public License 0015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0016 */ 0017 0018 #include "kcharsetstest.h" 0019 0020 #include <kcharsets.h> 0021 #include <QString> 0022 #include <QDebug> 0023 #include <QtTest> 0024 0025 static bool encodingNameHasADescription(const QString &encodingName, const QStringList &descriptions) 0026 { 0027 for (const QString &description : descriptions) 0028 if (description.contains(encodingName)) { 0029 return true; 0030 } 0031 return false; 0032 } 0033 0034 void KCharsetsTest::testSingleton() 0035 { 0036 QVERIFY(KCharsets::charsets() != nullptr); 0037 QCOMPARE(KCharsets::charsets(), KCharsets::charsets()); 0038 } 0039 0040 void KCharsetsTest::testCodecForName_data() 0041 { 0042 QTest::addColumn<QString>("codec"); 0043 QTest::addColumn<QString>("expectedCodecFromKDE"); 0044 QTest::addColumn<QString>("expectedCodecFromQt"); 0045 0046 QTest::newRow("utf8") << "utf8" << "UTF-8" << "UTF-8"; 0047 QTest::newRow("utf-8") << "utf-8" << "UTF-8" << "UTF-8"; 0048 QTest::newRow("UTF8") << "UTF8" << "UTF-8" << "UTF-8"; 0049 QTest::newRow("UTF-8") << "UTF-8" << "UTF-8" << "UTF-8"; 0050 0051 QTest::newRow("Big5") << "Big5" << "Big5" << "Big5"; 0052 QTest::newRow("Big5-HKSCS") << "Big5-HKSCS" << "Big5-HKSCS" << "Big5-HKSCS"; 0053 QTest::newRow("EUC-JP") << "EUC-JP" << "EUC-JP" << "EUC-JP"; 0054 QTest::newRow("EUC-KR") << "EUC-KR" << "EUC-KR" << "EUC-KR"; 0055 QTest::newRow("CP 949") << "CP 949" << "CP 949" << "CP 949"; 0056 QTest::newRow("GB18030") << "GB18030" << "GB18030" << "GB18030"; 0057 QTest::newRow("GB2312") << "GB2312" << "GB2312" << "GB2312"; 0058 QTest::newRow("GBK") << "GBK" << "GBK" << "GBK"; 0059 QTest::newRow("IBM850") << "IBM850" << "IBM850" << "IBM850"; 0060 QTest::newRow("IBM866") << "IBM866" << "IBM866" << "IBM866"; 0061 QTest::newRow("IBM874") << "IBM874" << "IBM874" << "IBM874"; 0062 QTest::newRow("ISO 10646-UCS-2") << "ISO 10646-UCS-2" << "ISO 10646-UCS-2" << "ISO 10646-UCS-2"; 0063 QTest::newRow("ISO 8859-1") << "ISO 8859-1" << "ISO 8859-1" << "ISO 8859-1"; 0064 QTest::newRow("ISO 8859-11") << "ISO 8859-11" << "ISO 8859-11" << "ISO 8859-11"; 0065 QTest::newRow("ISO 8859-13") << "ISO 8859-13" << "ISO 8859-13" << "ISO 8859-13"; 0066 QTest::newRow("ISO 8859-14") << "ISO 8859-14" << "ISO 8859-14" << "ISO 8859-14"; 0067 QTest::newRow("ISO 8859-15") << "ISO 8859-15" << "ISO 8859-15" << "ISO 8859-15"; 0068 QTest::newRow("ISO 8859-16") << "ISO 8859-16" << "ISO 8859-16" << "ISO 8859-16"; 0069 QTest::newRow("ISO 8859-2") << "ISO 8859-2" << "ISO 8859-2" << "ISO 8859-2"; 0070 QTest::newRow("ISO 8859-3") << "ISO 8859-3" << "ISO 8859-3" << "ISO 8859-3"; 0071 QTest::newRow("ISO 8859-4") << "ISO 8859-4" << "ISO 8859-4" << "ISO 8859-4"; 0072 QTest::newRow("ISO 8859-5") << "ISO 8859-5" << "ISO 8859-5" << "ISO 8859-5"; 0073 QTest::newRow("ISO 8859-6") << "ISO 8859-6" << "ISO 8859-6" << "ISO 8859-6"; 0074 QTest::newRow("ISO 8859-7") << "ISO 8859-7" << "ISO 8859-7" << "ISO 8859-7"; 0075 QTest::newRow("ISO 8859-8") << "ISO 8859-8" << "ISO 8859-8" << "ISO 8859-8"; 0076 QTest::newRow("ISO 8859-8-I") << "ISO 8859-8-I" << "ISO 8859-8-I" << "ISO 8859-8-I"; 0077 QTest::newRow("ISO 8859-9") << "ISO 8859-9" << "ISO 8859-9" << "ISO 8859-9"; 0078 QTest::newRow("KOI8-R") << "KOI8-R" << "KOI8-R" << "KOI8-R"; 0079 QTest::newRow("KOI8-U") << "KOI8-U" << "KOI8-U" << "KOI8-U"; 0080 QTest::newRow("TIS620") << "TIS620" << "TIS620" << "TIS620"; 0081 QTest::newRow("TSCII") << "TSCII" << "TSCII" << "TSCII"; 0082 QTest::newRow("UTF-16") << "UTF-16" << "UTF-16" << "UTF-16"; 0083 QTest::newRow("UTF-8") << "UTF-8" << "UTF-8" << "UTF-8"; 0084 QTest::newRow("cp 1250") << "cp 1250" << "cp 1250" << "cp 1250"; 0085 QTest::newRow("cp 1251") << "cp 1251" << "cp 1251" << "cp 1251"; 0086 QTest::newRow("cp 1252") << "cp 1252" << "cp 1252" << "cp 1252"; 0087 QTest::newRow("cp 1253") << "cp 1253" << "cp 1253" << "cp 1253"; 0088 QTest::newRow("cp 1254") << "cp 1254" << "cp 1254" << "cp 1254"; 0089 QTest::newRow("cp 1255") << "cp 1255" << "cp 1255" << "cp 1255"; 0090 QTest::newRow("cp 1256") << "cp 1256" << "cp 1256" << "cp 1256"; 0091 QTest::newRow("cp 1257") << "cp 1257" << "cp 1257" << "cp 1257"; 0092 QTest::newRow("jis7") << "jis7" << "jis7" << "jis7"; 0093 QTest::newRow("sjis") << "sjis" << "sjis" << "sjis"; 0094 QTest::newRow("ucs2") << "ucs2" << "ucs2" << "ucs2"; 0095 QTest::newRow("utf7") << "utf7" << "utf7" << "utf7"; 0096 QTest::newRow("windows-1258") << "windows-1258" << "windows-1258" << "windows-1258"; 0097 QTest::newRow("winsami2") << "winsami2" << "winsami2" << "winsami2"; 0098 } 0099 0100 void KCharsetsTest::testCodecForName() 0101 { 0102 KCharsets *singleton = KCharsets::charsets(); 0103 0104 QFETCH(QString, codec); 0105 QFETCH(QString, expectedCodecFromKDE); 0106 QFETCH(QString, expectedCodecFromQt); 0107 0108 if (QTextCodec::codecForName(codec.toLocal8Bit()) == nullptr) { 0109 qWarning() << "codec " << codec << "is not supported by QTextCodec !"; 0110 return; 0111 } 0112 0113 QVERIFY(QTextCodec::codecForName(expectedCodecFromKDE.toLocal8Bit()) != nullptr); 0114 QCOMPARE(singleton->codecForName(codec)->name(), 0115 QTextCodec::codecForName(expectedCodecFromKDE.toLocal8Bit())->name()); 0116 0117 QVERIFY(QTextCodec::codecForName(expectedCodecFromQt.toLocal8Bit()) != nullptr); 0118 QCOMPARE(QTextCodec::codecForName(codec.toLocal8Bit())->name(), 0119 QTextCodec::codecForName(expectedCodecFromQt.toLocal8Bit())->name()); 0120 } 0121 0122 void KCharsetsTest::testFromEntity() 0123 { 0124 KCharsets *singleton = KCharsets::charsets(); 0125 0126 QCOMPARE(singleton->fromEntity(QString::fromLatin1("Ӓ")), QChar(1234)); 0127 QCOMPARE(singleton->fromEntity(QString::fromLatin1("ሴ")), QChar(0x1234)); 0128 QCOMPARE(singleton->fromEntity(QString::fromLatin1("lt")), QChar::fromLatin1('<')); 0129 QCOMPARE(singleton->fromEntity(QString::fromLatin1("gt")), QChar::fromLatin1('>')); 0130 QCOMPARE(singleton->fromEntity(QString::fromLatin1("quot")), QChar::fromLatin1('"')); 0131 QCOMPARE(singleton->fromEntity(QString::fromLatin1("amp")), QChar::fromLatin1('&')); 0132 QCOMPARE(singleton->fromEntity(QString::fromLatin1("apos")), QChar::fromLatin1('\'')); 0133 } 0134 0135 void KCharsetsTest::testToEntity() 0136 { 0137 QSKIP("KCharsets::toEntity test not implemented."); 0138 } 0139 0140 void KCharsetsTest::testResolveEntities() 0141 { 0142 KCharsets *singleton = KCharsets::charsets(); 0143 0144 QCOMPARE(singleton->resolveEntities(QString::fromLatin1(""'<Hello &World>'"")), 0145 QString::fromLatin1("\"\'<Hello &World>\'\"")); 0146 } 0147 0148 void KCharsetsTest::testEncodingNames() 0149 { 0150 KCharsets *singleton = KCharsets::charsets(); 0151 0152 QCOMPARE(singleton->availableEncodingNames().count(), singleton->descriptiveEncodingNames().count()); 0153 0154 for (const QString &encodingName : singleton->availableEncodingNames()) { 0155 bool ok = false; 0156 0157 if (encodingName == QLatin1String("ucs2") || encodingName == QLatin1String("ISO 10646-UCS-2")) { 0158 singleton->codecForName(QStringLiteral("UTF-16"), ok); 0159 } else if (encodingName == QLatin1String("utf7")) { 0160 continue; 0161 } else { 0162 singleton->codecForName(encodingName, ok); 0163 } 0164 // The availability of some of the charsets below depends on whether Qt was built with ICU... 0165 if (!ok) { 0166 if (encodingName == QLatin1String("jis7")) { 0167 QEXPECT_FAIL("", "jis7 is missing in Qt", Continue); 0168 } 0169 if (encodingName == QLatin1String("winsami2")) { 0170 QEXPECT_FAIL("", "winsami2 is missing in Qt", Continue); 0171 } 0172 if (encodingName == QLatin1String("ISO 8859-16")) { // ICU bug? 0173 QEXPECT_FAIL("", "ISO 8859-16 is missing in Qt", Continue); 0174 } 0175 } 0176 0177 0178 if (!ok) { 0179 qDebug() << "Error:" << encodingName << "not found"; 0180 QVERIFY(false); 0181 } 0182 QVERIFY(encodingNameHasADescription(encodingName, singleton->descriptiveEncodingNames())); 0183 QVERIFY(!singleton->descriptionForEncoding(encodingName).isEmpty()); 0184 QCOMPARE(singleton->encodingForName(singleton->descriptionForEncoding(encodingName)), encodingName); 0185 } 0186 } 0187 0188 QTEST_MAIN(KCharsetsTest)