summaryrefslogtreecommitdiff
path: root/writerperfect/qa/unit/DirectoryStreamTest.cxx
blob: fd75805f589936012106aa437aec3dcc49390715 (plain)
1
2
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
/* -*- 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 <memory>

#include <cppunit/extensions/HelperMacros.h>

#include <comphelper/processfactory.hxx>

#include <ucbhelper/content.hxx>

#include <test/bootstrapfixture.hxx>

#include <DirectoryStream.hxx>
#include <com/sun/star/ucb/XContent.hpp>

namespace ucb = com::sun::star::ucb;
namespace uno = com::sun::star::uno;

using std::unique_ptr;

using librevenge::RVNGInputStream;

using writerperfect::DirectoryStream;

namespace
{
class DirectoryStreamTest : public test::BootstrapFixture
{
public:
    DirectoryStreamTest();

public:
    CPPUNIT_TEST_SUITE(DirectoryStreamTest);
    CPPUNIT_TEST(testConstruction);
    CPPUNIT_TEST(testDetection);
    CPPUNIT_TEST(testDataOperations);
    CPPUNIT_TEST(testStructuredOperations);
    CPPUNIT_TEST_SUITE_END();

private:
    void testConstruction();
    void testDetection();
    void testDataOperations();
    void testStructuredOperations();

private:
    uno::Reference<ucb::XContent> m_xDir;
    uno::Reference<ucb::XContent> m_xFile;
    uno::Reference<ucb::XContent> m_xNonexistent;
};

constexpr OUStringLiteral g_aDirPath = u"/writerperfect/qa/unit/data/stream/test.dir";
constexpr OUStringLiteral g_aNondirPath = u"/writerperfect/qa/unit/data/stream/test.dir/mimetype";
constexpr OUStringLiteral g_aNonexistentPath = u"/writerperfect/qa/unit/data/stream/foo/bar";

DirectoryStreamTest::DirectoryStreamTest()
{
    const uno::Reference<ucb::XCommandEnvironment> xCmdEnv;
    const uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext());

    using ucbhelper::Content;

    m_xDir = Content(m_directories.getURLFromSrc(g_aDirPath), xCmdEnv, xContext).get();
    m_xFile = Content(m_directories.getURLFromSrc(g_aNondirPath), xCmdEnv, xContext).get();
    m_xNonexistent
        = Content(m_directories.getURLFromSrc(g_aNonexistentPath), xCmdEnv, xContext).get();
}

void DirectoryStreamTest::testConstruction()
{
    const unique_ptr<DirectoryStream> pDir(DirectoryStream::createForParent(m_xFile));
    CPPUNIT_ASSERT(bool(pDir));
    CPPUNIT_ASSERT(pDir->isStructured());

    // this should work for dirs too
    const unique_ptr<DirectoryStream> pDir2(DirectoryStream::createForParent(m_xDir));
    CPPUNIT_ASSERT(bool(pDir2));
    CPPUNIT_ASSERT(pDir2->isStructured());

    // for nonexistent dirs nothing is created
    const unique_ptr<DirectoryStream> pNondir(DirectoryStream::createForParent(m_xNonexistent));
    CPPUNIT_ASSERT(!pNondir);

    // even if we try harder, just an empty shell is created
    DirectoryStream aNondir2(m_xNonexistent);
    CPPUNIT_ASSERT(!aNondir2.isStructured());
}

void DirectoryStreamTest::testDetection()
{
    CPPUNIT_ASSERT(DirectoryStream::isDirectory(m_xDir));
    CPPUNIT_ASSERT(!DirectoryStream::isDirectory(m_xFile));
    CPPUNIT_ASSERT(!DirectoryStream::isDirectory(m_xNonexistent));
}

void lcl_testDataOperations(RVNGInputStream& rStream)
{
    CPPUNIT_ASSERT(rStream.isEnd());
    CPPUNIT_ASSERT_EQUAL(0L, rStream.tell());
    CPPUNIT_ASSERT_EQUAL(-1, rStream.seek(0, librevenge::RVNG_SEEK_CUR));

    unsigned long numBytesRead = 0;
    CPPUNIT_ASSERT(!rStream.read(1, numBytesRead));
    CPPUNIT_ASSERT_EQUAL(0UL, numBytesRead);
}

void DirectoryStreamTest::testDataOperations()
{
    // data operations do not make sense on a directory -> just dummy
    // impls.
    DirectoryStream aDir(m_xDir);
    lcl_testDataOperations(aDir);

    // ... and they are equally empty if we try to pass a file
    DirectoryStream aFile(m_xFile);
    lcl_testDataOperations(aFile);
}

void lcl_testStructuredOperations(RVNGInputStream& rStream)
{
    CPPUNIT_ASSERT(rStream.isStructured());
    unique_ptr<RVNGInputStream> pSubstream(rStream.getSubStreamByName("mimetype"));
    CPPUNIT_ASSERT(bool(pSubstream));

    // TODO: test for other operations when they are implemented =)
}

void DirectoryStreamTest::testStructuredOperations()
{
    DirectoryStream aDir(m_xDir);
    lcl_testStructuredOperations(aDir);

    unique_ptr<DirectoryStream> pDir(DirectoryStream::createForParent(m_xFile));
    CPPUNIT_ASSERT(bool(pDir));
    lcl_testStructuredOperations(*pDir);
}

CPPUNIT_TEST_SUITE_REGISTRATION(DirectoryStreamTest);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */