| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | * This file is part of the libzmf 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 | |
| 10 | #include <string> |
| 11 | |
| 12 | #include <cppunit/TestFixture.h> |
| 13 | #include <cppunit/extensions/HelperMacros.h> |
| 14 | |
| 15 | #include <librevenge-stream/librevenge-stream.h> |
| 16 | |
| 17 | #include <libzmf/libzmf.h> |
| 18 | |
| 19 | #if !defined DETECTION_TEST_DIR |
| 20 | #error DETECTION_TEST_DIR not defined, cannot test |
| 21 | #endif |
| 22 | |
| 23 | namespace test |
| 24 | { |
| 25 | |
| 26 | using libzmf::ZMFDocument; |
| 27 | |
| 28 | using std::string; |
| 29 | |
| 30 | namespace |
| 31 | { |
| 32 | |
| 33 | void assertDetection(const string &name, const bool expectedSupported, const ZMFDocument::Type *const expectedType = 0, const ZMFDocument::Kind *const expectedKind = 0) |
| 34 | { |
| 35 | librevenge::RVNGFileStream input((string(DETECTION_TEST_DIR) + "/" + name).c_str()); |
| 36 | ZMFDocument::Kind kind = ZMFDocument::KIND_UNKNOWN; |
| 37 | ZMFDocument::Type type = ZMFDocument::TYPE_UNKNOWN; |
| 38 | const bool supported = ZMFDocument::isSupported(&input, &type, &kind); |
| 39 | CPPUNIT_ASSERT_EQUAL_MESSAGE(name + ": supported", expectedSupported, supported); |
| 40 | if (expectedType) |
| 41 | CPPUNIT_ASSERT_EQUAL_MESSAGE(name + ": type", *expectedType, type); |
| 42 | if (expectedKind) |
| 43 | CPPUNIT_ASSERT_EQUAL_MESSAGE(name + ": kind", *expectedKind, kind); |
| 44 | } |
| 45 | |
| 46 | void assertSupported(const string &name, const ZMFDocument::Type type, const ZMFDocument::Kind kind) |
| 47 | { |
| 48 | assertDetection(name, true, &type, &kind); |
| 49 | } |
| 50 | |
| 51 | void assertUnsupported(const string &name) |
| 52 | { |
| 53 | assertDetection(name, false); |
| 54 | } |
| 55 | |
| 56 | } |
| 57 | |
| 58 | class ZMFDocumentTest : public CPPUNIT_NS::TestFixture |
| 59 | { |
| 60 | public: |
| 61 | virtual void setUp() override; |
| 62 | virtual void tearDown() override; |
| 63 | |
| 64 | private: |
| 65 | CPPUNIT_TEST_SUITE(ZMFDocumentTest); |
| 66 | CPPUNIT_TEST(testDetectDraw); |
| 67 | CPPUNIT_TEST(testDetectZebra); |
| 68 | CPPUNIT_TEST(testDetectBitmap); |
| 69 | CPPUNIT_TEST(testUnsupported); |
| 70 | CPPUNIT_TEST_SUITE_END(); |
| 71 | |
| 72 | private: |
| 73 | void testDetectDraw(); |
| 74 | void testDetectZebra(); |
| 75 | void testDetectBitmap(); |
| 76 | void testUnsupported(); |
| 77 | }; |
| 78 | |
| 79 | void ZMFDocumentTest::setUp() |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | void ZMFDocumentTest::tearDown() |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | void ZMFDocumentTest::testDetectZebra() |
| 88 | { |
| 89 | assertSupported("zebra.zbr", ZMFDocument::TYPE_ZEBRA, ZMFDocument::KIND_DRAW); |
| 90 | } |
| 91 | |
| 92 | void ZMFDocumentTest::testDetectDraw() |
| 93 | { |
| 94 | const ZMFDocument::Type type(ZMFDocument::TYPE_DRAW); |
| 95 | const ZMFDocument::Kind kind(ZMFDocument::KIND_DRAW); |
| 96 | |
| 97 | // version 2-3 |
| 98 | // assertSupported("draw2.zmf", type, kind); |
| 99 | // assertSupported("draw3.zmf", type, kind); |
| 100 | |
| 101 | // version 4-5 |
| 102 | assertSupported("draw4.zmf", type, kind); |
| 103 | assertSupported("draw5.zmf", type, kind); |
| 104 | assertSupported("draw5-uncompressed.zmf", type, kind); |
| 105 | } |
| 106 | |
| 107 | void ZMFDocumentTest::testDetectBitmap() |
| 108 | { |
| 109 | const ZMFDocument::Type type(ZMFDocument::TYPE_BITMAP); |
| 110 | const ZMFDocument::Kind kind(ZMFDocument::KIND_PAINT); |
| 111 | |
| 112 | assertSupported("bitmap.bmi", type, kind); |
| 113 | } |
| 114 | |
| 115 | void ZMFDocumentTest::testUnsupported() |
| 116 | { |
| 117 | // ensure the detection is not too broad |
| 118 | assertUnsupported("unsupported.zip"); |
| 119 | } |
| 120 | |
| 121 | CPPUNIT_TEST_SUITE_REGISTRATION(ZMFDocumentTest); |
| 122 | |
| 123 | } |
| 124 | |
| 125 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |