| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 
 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#include <cppunit/TestAssert.h>
#include <cppunit/extensions/HelperMacros.h>
#include <unotest/bootstrapfixturebase.hxx>
#include <graphic/GraphicFormatDetector.hxx>
#include <graphic/DetectorTools.hxx>
#include <tools/stream.hxx>
using namespace css;
namespace
{
class GraphicFormatDetectorTest : public test::BootstrapFixtureBase
{
    OUString getFullUrl(const OUString& sFileName)
    {
        return m_directories.getURLFromSrc("/vcl/qa/cppunit/data/") + sFileName;
    }
    void testDetectMET();
    void testDetectBMP();
    void testDetectWMF();
    void testDetectPCX();
    void testDetectJPG();
    void testDetectPNG();
    void testDetectGIF();
    void testDetectPSD();
    void testDetectTGA();
    void testDetectTIF();
    void testDetectXBM();
    void testDetectXPM();
    void testDetectSVG();
    void testDetectSVGZ();
    void testDetectPDF();
    void testDetectEPS();
    void testMatchArray();
    void testCheckArrayForMatchingStrings();
    CPPUNIT_TEST_SUITE(GraphicFormatDetectorTest);
    CPPUNIT_TEST(testDetectMET);
    CPPUNIT_TEST(testDetectBMP);
    CPPUNIT_TEST(testDetectWMF);
    CPPUNIT_TEST(testDetectPCX);
    CPPUNIT_TEST(testDetectJPG);
    CPPUNIT_TEST(testDetectPNG);
    CPPUNIT_TEST(testDetectGIF);
    CPPUNIT_TEST(testDetectPSD);
    CPPUNIT_TEST(testDetectTGA);
    CPPUNIT_TEST(testDetectTIF);
    CPPUNIT_TEST(testDetectXBM);
    CPPUNIT_TEST(testDetectXPM);
    CPPUNIT_TEST(testDetectSVG);
    CPPUNIT_TEST(testDetectSVGZ);
    CPPUNIT_TEST(testDetectPDF);
    CPPUNIT_TEST(testDetectEPS);
    CPPUNIT_TEST(testMatchArray);
    CPPUNIT_TEST(testCheckArrayForMatchingStrings);
    CPPUNIT_TEST_SUITE_END();
};
void GraphicFormatDetectorTest::testDetectMET()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.met"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "MET");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkMET());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("MET"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectBMP()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.bmp"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "BMP");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkBMP());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("BMP"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectWMF()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.wmf"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "WMF");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkWMForEMF());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("WMF"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectPCX()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.pcx"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "PCX");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkPCX());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("PCX"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectJPG()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.jpg"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "JPG");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkJPG());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("JPG"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectPNG()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.png"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "PNG");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkPNG());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("PNG"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectGIF()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.gif"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "GIF");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkGIF());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("GIF"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectPSD()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.psd"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "PSD");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkPSD());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("PSD"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectTGA()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.tga"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "TGA");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkTGA());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension("TGA"); // detection is based on extension only
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("TGA"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectTIF()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.tif"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "TIF");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkTIF());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("TIF"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectXBM()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.xbm"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "XBM");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkXBM());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("XBM"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectXPM()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.xpm"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "XPM");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkXPM());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("XPM"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectSVG()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.svg"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "SVG");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkSVG());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("SVG"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectSVGZ()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.svgz"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "SVG");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkSVG());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("SVG"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectPDF()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.pdf"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "PDF");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkPDF());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("PDF"), rFormatExtension);
}
void GraphicFormatDetectorTest::testDetectEPS()
{
    SvFileStream aFileStream(getFullUrl("TypeDetectionExample.eps"), StreamMode::READ);
    vcl::GraphicFormatDetector aDetector(aFileStream, "EPS");
    CPPUNIT_ASSERT(aDetector.detect());
    CPPUNIT_ASSERT(aDetector.checkEPS());
    aFileStream.Seek(aDetector.mnStreamPosition);
    OUString rFormatExtension;
    CPPUNIT_ASSERT(ImpPeekGraphicFormat(aFileStream, rFormatExtension, false));
    CPPUNIT_ASSERT_EQUAL(OUString("EPS"), rFormatExtension);
}
void GraphicFormatDetectorTest::testMatchArray()
{
    std::string aString("<?xml version=\"1.0\" standalone=\"no\"?>\n"
                        "<svg width=\"5cm\" height=\"4cm\" version=\"1.1\"\n"
                        "xmlns=\"http://www.w3.org/2000/svg\">\n"
                        "</svg>");
    const char* pCompleteStringPointer = aString.c_str();
    const char* pMatchPointer;
    int nCheckSize = aString.size();
    // Check beginning of the input string
    pMatchPointer = vcl::matchArrayWithString(pCompleteStringPointer, nCheckSize, "<?xml");
    CPPUNIT_ASSERT(pMatchPointer != nullptr);
    CPPUNIT_ASSERT_EQUAL(0, int(pMatchPointer - pCompleteStringPointer));
    CPPUNIT_ASSERT_EQUAL(true, OString(pMatchPointer).startsWith("<?xml"));
    // Check middle of the input string
    pMatchPointer = vcl::matchArrayWithString(aString.c_str(), nCheckSize, "version");
    CPPUNIT_ASSERT(pMatchPointer != nullptr);
    CPPUNIT_ASSERT_EQUAL(6, int(pMatchPointer - pCompleteStringPointer));
    CPPUNIT_ASSERT_EQUAL(true, OString(pMatchPointer).startsWith("version"));
    pMatchPointer = vcl::matchArrayWithString(aString.c_str(), nCheckSize, "<svg");
    CPPUNIT_ASSERT(pMatchPointer != nullptr);
    CPPUNIT_ASSERT_EQUAL(38, int(pMatchPointer - pCompleteStringPointer));
    CPPUNIT_ASSERT_EQUAL(true, OString(pMatchPointer).startsWith("<svg"));
    // Check end of the input string
    pMatchPointer = vcl::matchArrayWithString(aString.c_str(), nCheckSize, "/svg>");
    CPPUNIT_ASSERT(pMatchPointer != nullptr);
    CPPUNIT_ASSERT_EQUAL(119, int(pMatchPointer - pCompleteStringPointer));
    CPPUNIT_ASSERT_EQUAL(true, OString(pMatchPointer).startsWith("/svg>"));
    // Check that non-existing search string
    pMatchPointer = vcl::matchArrayWithString(aString.c_str(), nCheckSize, "none");
    CPPUNIT_ASSERT(pMatchPointer == nullptr);
}
void GraphicFormatDetectorTest::testCheckArrayForMatchingStrings()
{
    std::string aString("<?xml version=\"1.0\" standalone=\"no\"?>\n"
                        "<svg width=\"5cm\" height=\"4cm\" version=\"1.1\"\n"
                        "xmlns=\"http://www.w3.org/2000/svg\">\n"
                        "</svg>");
    const char* pCompleteStringPointer = aString.c_str();
    int nCheckSize = aString.size();
    bool bResult;
    // check beginning string
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize, { "<?xml" });
    CPPUNIT_ASSERT_EQUAL(true, bResult);
    // check ending string
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize, { "/svg>" });
    CPPUNIT_ASSERT_EQUAL(true, bResult);
    // check middle string
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize, { "version" });
    CPPUNIT_ASSERT_EQUAL(true, bResult);
    // check beginning and then ending string
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize,
                                                { "<?xml", "/svg>" });
    CPPUNIT_ASSERT_EQUAL(true, bResult);
    // check ending and then beginning string
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize,
                                                { "/svg>", "<?xml" });
    CPPUNIT_ASSERT_EQUAL(false, bResult);
    // check middle strings
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize,
                                                { "version", "<svg" });
    CPPUNIT_ASSERT_EQUAL(true, bResult);
    // check beginning, middle and ending strings
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize,
                                                { "<?xml", "version", "<svg", "/svg>" });
    CPPUNIT_ASSERT_EQUAL(true, bResult);
    // check non-existing
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize, { "none" });
    CPPUNIT_ASSERT_EQUAL(false, bResult);
    // check non-existing on the beginning
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize,
                                                { "none", "version", "<svg", "/svg>" });
    CPPUNIT_ASSERT_EQUAL(false, bResult);
    // check non-existing on the end
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize,
                                                { "<?xml", "version", "<svg", "none" });
    CPPUNIT_ASSERT_EQUAL(false, bResult);
    // check non-existing after the end
    bResult = vcl::checkArrayForMatchingStrings(pCompleteStringPointer, nCheckSize,
                                                { "<?xml", "/svg>", "none" });
    CPPUNIT_ASSERT_EQUAL(false, bResult);
}
} // namespace
CPPUNIT_TEST_SUITE_REGISTRATION(GraphicFormatDetectorTest);
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
 |