File indexing completed on 2024-04-28 11:37:24

0001 /* This file is part of the KDE libraries
0002     Copyright (c) 2009 Germain Garand <germain@ebooksfrance.org>
0003 
0004     This library is free software; you can redistribute it and/or
0005     modify it under the terms of the GNU Library General Public
0006     License version 2 as published by the Free Software Foundation.
0007 
0008     This library is distributed in the hope that it will be useful,
0009     but WITHOUT ANY WARRANTY; without even the implied warranty of
0010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011     Library General Public License for more details.
0012 
0013     You should have received a copy of the GNU Library General Public License
0014     along with this library; see the file COPYING.LIB.  If not, write to
0015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0016     Boston, MA 02110-1301, USA.
0017 */
0018 
0019 #include "kencodingdetectortest.h"
0020 
0021 #include <QTest>
0022 
0023 #include <kencodingdetector.h>
0024 
0025 static const char data1[] = "this should decode correctly";
0026 static const char data2[] = "this is an invalid utf-8 byte: \xBF and another one: \xBE";
0027 
0028 static KEncodingDetector *ed = nullptr;
0029 
0030 void KEncodingDetectorTest::initTestCase()
0031 {
0032     ed = new KEncodingDetector();
0033 }
0034 
0035 void KEncodingDetectorTest::testSetEncoding()
0036 {
0037     QCOMPARE(ed->setEncoding("iso8859-1", KEncodingDetector::UserChosenEncoding), true);
0038     QCOMPARE(ed->setEncoding("utf-8", KEncodingDetector::UserChosenEncoding), true);
0039 }
0040 
0041 void KEncodingDetectorTest::testDecode()
0042 {
0043     QString s = ed->decode(data1, sizeof(data1) - 1);
0044     QCOMPARE(ed->decodedInvalidCharacters(), false);
0045     QString s2 = ed->decode(data2, sizeof(data2) - 1);
0046     QCOMPARE(ed->decodedInvalidCharacters(), true);
0047     QCOMPARE(s, QString::fromLatin1(data1));
0048 
0049     ed->resetDecoder();
0050     QVERIFY(!ed->decodedInvalidCharacters());
0051 
0052     // set to automatic detection
0053     ed->setEncoding("", KEncodingDetector::DefaultEncoding);
0054 
0055     // decodeWithBuffering should just accumulate the buffer here,
0056     // waiting for some HTML/XML encoding tags
0057     s = ed->decodeWithBuffering(data2, sizeof data2 - 1);
0058 
0059     // shouldn't even decode anything yet, so:
0060     QCOMPARE(s.isEmpty(), true);
0061     QCOMPARE(ed->decodedInvalidCharacters(), false);
0062 
0063     // force encoding, as the high bytes must have switched the encoding
0064     // to anything *but* utf-8
0065     QCOMPARE(QString::fromLatin1("utf-8").startsWith(QString::fromLatin1(ed->encoding()), Qt::CaseInsensitive), false);
0066     ed->setEncoding("utf-8", KEncodingDetector::UserChosenEncoding);
0067     QCOMPARE(QString::fromLatin1("utf-8").startsWith(QString::fromLatin1(ed->encoding()), Qt::CaseInsensitive), true);
0068 
0069     // force decoding now
0070     s = ed->flush();
0071     QCOMPARE(s.isEmpty(), false);
0072     QCOMPARE(ed->decodedInvalidCharacters(), true);
0073 
0074     // now check that resetDecoder() empties the buffer
0075     s2 = ed->decodeWithBuffering(data1, sizeof data1 - 1);
0076     ed->resetDecoder();
0077     s2 = ed->flush();
0078     QCOMPARE(s2.isEmpty(), true);
0079 
0080     // check that buffered decoding with non-overridable specified codec decodes right away
0081     ed->setEncoding("utf-8", KEncodingDetector::EncodingFromHTTPHeader);
0082     s = ed->decodeWithBuffering(data2, sizeof data2 - 1);
0083 
0084     QCOMPARE(s.isEmpty(), false);
0085     QCOMPARE(ed->decodedInvalidCharacters(), true);
0086 }
0087 
0088 QTEST_MAIN(KEncodingDetectorTest)
0089 
0090 #include "moc_kencodingdetectortest.cpp"