| 1 |
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the libqxp project. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
*/ |
| 9 |
#include <algorithm> |
| 10 |
#include <string> |
| 11 |
#include <map> |
| 12 |
#include <memory> |
| 13 |
|
| 14 |
#include <cppunit/TestFixture.h> |
| 15 |
#include <cppunit/extensions/HelperMacros.h> |
| 16 |
|
| 17 |
#include <librevenge-stream/librevenge-stream.h> |
| 18 |
|
| 19 |
#include "libqxp_utils.h" |
| 20 |
#include "QXPDetector.h" |
| 21 |
#include "QXPHeader.h" |
| 22 |
#include "QXPBlockParser.h" |
| 23 |
|
| 24 |
#if !defined TEST_DATA_DIR |
| 25 |
#error TEST_DATA_DIR not defined, cannot test |
| 26 |
#endif |
| 27 |
|
| 28 |
namespace test |
| 29 |
{ |
| 30 |
|
| 31 |
using libqxp::QXPDetector; |
| 32 |
using libqxp::QXPHeader; |
| 33 |
using libqxp::QXPBlockParser; |
| 34 |
using libqxp::readString; |
| 35 |
using libqxp::getRemainingLength; |
| 36 |
|
| 37 |
using librevenge::RVNGInputStream; |
| 38 |
using std::string; |
| 39 |
using std::map; |
| 40 |
using std::make_shared; |
| 41 |
using std::shared_ptr; |
| 42 |
|
| 43 |
namespace |
| 44 |
{ |
| 45 |
|
| 46 |
shared_ptr<QXPBlockParser> createParser(const string &name) |
| 47 |
{ |
| 48 |
const shared_ptr<RVNGInputStream> input(new librevenge::RVNGFileStream((string(TEST_DATA_DIR) + "/" + name).c_str())); |
| 49 |
QXPDetector detector; |
| 50 |
detector.detect(input); |
| 51 |
CPPUNIT_ASSERT(detector.header()); |
| 52 |
CPPUNIT_ASSERT(detector.input()); |
| 53 |
CPPUNIT_ASSERT(detector.header()->load(detector.input())); |
| 54 |
return make_shared<QXPBlockParser>(detector.input(), detector.header()); |
| 55 |
} |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
class QXPBlockParserTest : public CPPUNIT_NS::TestFixture |
| 60 |
{ |
| 61 |
public: |
| 62 |
virtual void setUp() override; |
| 63 |
virtual void tearDown() override; |
| 64 |
|
| 65 |
private: |
| 66 |
CPPUNIT_TEST_SUITE(QXPBlockParserTest); |
| 67 |
CPPUNIT_TEST(testGetTextBlock); |
| 68 |
CPPUNIT_TEST(testGetDocChain); |
| 69 |
CPPUNIT_TEST_SUITE_END(); |
| 70 |
|
| 71 |
private: |
| 72 |
void testGetTextBlock(); |
| 73 |
void testGetDocChain(); |
| 74 |
void testUnsupported(); |
| 75 |
}; |
| 76 |
|
| 77 |
void QXPBlockParserTest::setUp() |
| 78 |
{ |
| 79 |
} |
| 80 |
|
| 81 |
void QXPBlockParserTest::tearDown() |
| 82 |
{ |
| 83 |
} |
| 84 |
|
| 85 |
void QXPBlockParserTest::testGetTextBlock() |
| 86 |
{ |
| 87 |
string expectedBlock1 = "123" + string(252, 't') + "4"; |
| 88 |
string expectedBlock2 = "56"; |
| 89 |
map<string, unsigned> fileBlockMap = |
| 90 |
{ |
| 91 |
{ "qxp33mac_text", 0xC }, |
| 92 |
{ "qxp33win_text.qxd", 0x11 }, |
| 93 |
{ "qxp4mac_text", 0x40 }, |
| 94 |
{ "qxp4win_text.qxd", 0x45 }, |
| 95 |
}; |
| 96 |
|
| 97 |
for (const auto &item : fileBlockMap) |
| 98 |
{ |
| 99 |
const auto name = item.first; |
| 100 |
const auto index = item.second; |
| 101 |
|
| 102 |
auto parser = createParser(name); |
| 103 |
auto stream = parser->getBlock(index); |
| 104 |
CPPUNIT_ASSERT_EQUAL_MESSAGE(name + " block 1", expectedBlock1, readString(stream, 256)); |
| 105 |
stream = parser->getBlock(index + 1); |
| 106 |
CPPUNIT_ASSERT_EQUAL_MESSAGE(name + " block 2", expectedBlock2, readString(stream, 2)); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
void QXPBlockParserTest::testGetDocChain() |
| 111 |
{ |
| 112 |
map<string, unsigned long> fileDocSizeMap = |
| 113 |
{ |
| 114 |
{ "qxp33mac_text", 3786 }, |
| 115 |
{ "qxp33win_text.qxd", 5582 }, |
| 116 |
{ "qxp4mac_text", 17780 }, |
| 117 |
{ "qxp4win_text.qxd", 18800 }, |
| 118 |
}; |
| 119 |
|
| 120 |
for (const auto &item : fileDocSizeMap) |
| 121 |
{ |
| 122 |
const auto name = item.first; |
| 123 |
const auto expectedSize = item.second; |
| 124 |
|
| 125 |
auto parser = createParser(name); |
| 126 |
auto stream = parser->getChain(3); |
| 127 |
CPPUNIT_ASSERT_EQUAL_MESSAGE(name, expectedSize, getRemainingLength(stream)); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
CPPUNIT_TEST_SUITE_REGISTRATION(QXPBlockParserTest); |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
/* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |