#include #include "backend/formats/TextEncoding.h" class TextEncodingTest : public QObject { Q_OBJECT private slots: void testEmpty(); void testUtf8NoBom(); void testUtf8WithBom(); void testUtf16LeWithBom(); void testUtf16BeWithBom(); void testDetectXmlEncoding(); void testDetectHtml5Charset(); void testDetectHtml4Charset(); void testDecodeWindows1251(); void testDecodeWithHint(); void testDecodeWithExplicitEncoding(); void testAutoDetectWindows1251(); }; void TextEncodingTest::testEmpty() { QCOMPARE(TextEncoding::detectEncoding(QByteArray()), QString()); QCOMPARE(TextEncoding::decode(QByteArray()), QString()); QCOMPARE(TextEncoding::decodeWithEncoding(QByteArray(), QStringLiteral("utf-8")), QString()); } void TextEncodingTest::testUtf8NoBom() { const QByteArray bytes = "Hello, world! \xF0\x9F\x93\x96"; // book emoji const QString result = TextEncoding::decode(bytes); QCOMPARE(result, QString::fromUtf8(bytes)); } void TextEncodingTest::testUtf8WithBom() { const QByteArray bytes = "\xEF\xBB\xBFHello, BOM!"; QCOMPARE(TextEncoding::detectEncoding(bytes), QStringLiteral("UTF-8")); const QString result = TextEncoding::decode(bytes); QCOMPARE(result, QStringLiteral("Hello, BOM!")); } void TextEncodingTest::testUtf16LeWithBom() { // UTF-16 LE BOM + "AB" (0x0041, 0x0042) const QByteArray bytes = QByteArray("\xFF\xFE\x41\x00\x42\x00", 6); QCOMPARE(TextEncoding::detectEncoding(bytes), QStringLiteral("UTF-16LE")); const QString result = TextEncoding::decode(bytes); QCOMPARE(result, QStringLiteral("AB")); } void TextEncodingTest::testUtf16BeWithBom() { // UTF-16 BE BOM + "AB" (0x0041, 0x0042) const QByteArray bytes = QByteArray("\xFE\xFF\x00\x41\x00\x42", 6); QCOMPARE(TextEncoding::detectEncoding(bytes), QStringLiteral("UTF-16BE")); const QString result = TextEncoding::decode(bytes); QCOMPARE(result, QStringLiteral("AB")); } void TextEncodingTest::testDetectXmlEncoding() { const QByteArray xml1 = "\n"; QCOMPARE(TextEncoding::detectEncoding(xml1), QStringLiteral("windows-1251")); const QByteArray xml2 = "\n"; QCOMPARE(TextEncoding::detectEncoding(xml2), QStringLiteral("KOI8-R")); } void TextEncodingTest::testDetectHtml5Charset() { const QByteArray html = ""; QCOMPARE(TextEncoding::detectEncoding(html), QStringLiteral("shift_jis")); } void TextEncodingTest::testDetectHtml4Charset() { const QByteArray html = ""; QCOMPARE(TextEncoding::detectEncoding(html), QStringLiteral("gbk")); } void TextEncodingTest::testDecodeWindows1251() { // "Привет" in windows-1251: CF F0 E8 E2 E5 F2 const QByteArray win1251Text("\xCF\xF0\xE8\xE2\xE5\xF2"); const QByteArray xml = "\n" + win1251Text + ""; const QString result = TextEncoding::decode(xml); QVERIFY(result.contains(QString::fromUtf8("Привет"))); } void TextEncodingTest::testDecodeWithHint() { // "Привет" in windows-1251 without header const QByteArray win1251Text("\xCF\xF0\xE8\xE2\xE5\xF2"); const QString result = TextEncoding::decode(win1251Text, QStringLiteral("windows-1251")); QCOMPARE(result, QString::fromUtf8("Привет")); } void TextEncodingTest::testDecodeWithExplicitEncoding() { const QByteArray win1251Text("\xCF\xF0\xE8\xE2\xE5\xF2"); const QString result = TextEncoding::decodeWithEncoding(win1251Text, QStringLiteral("windows-1251")); QCOMPARE(result, QString::fromUtf8("Привет")); // Invalid encoding returns empty const QString invalid = TextEncoding::decodeWithEncoding(win1251Text, QStringLiteral("invalid_encoding_xyz")); QVERIFY(invalid.isEmpty()); } void TextEncodingTest::testAutoDetectWindows1251() { // Russian text in windows-1251 without BOM or headers: "Версия для программистов" const QByteArray win1251("\xC2\xE5\xF0\xF1\xE8\xFF\x20\xE4\xEB\xFF\x20\xEF\xF0\xEE\xE3\xF0\xE0\xEC\xEC\xE8\xF1\xF2\xEE\xE2"); const QString enc = TextEncoding::detectEncoding(win1251); QCOMPARE(enc.toLower(), QStringLiteral("windows-1251")); const QString result = TextEncoding::decode(win1251); QCOMPARE(result, QString::fromUtf8("Версия для программистов")); QVERIFY(!result.contains(QChar::ReplacementCharacter)); } QTEST_MAIN(TextEncodingTest) #include "TextEncodingTest.moc"