bump product version to 7.6.3.2-android
[LibreOffice.git] / sot / qa / cppunit / test_sot.cxx
blob7fd1b9a6c18f0cc25f620a0054ab5ce8e2f00896
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <unotest/filters-test.hxx>
11 #include <unotest/bootstrapfixturebase.hxx>
13 #include <sot/storage.hxx>
14 #include <sot/storinfo.hxx>
15 #include <sysformats.hxx>
17 using namespace ::com::sun::star;
19 namespace
21 size_t FindFormatIndex(const SotAction_Impl* pFormats, SotClipboardFormatId eFormat)
23 size_t nRet = 0;
24 SotClipboardFormatId nId = pFormats->nFormatId;
26 while (nId != static_cast<SotClipboardFormatId>(0xffff))
28 if (nId == eFormat)
29 break;
31 ++pFormats;
32 ++nRet;
33 nId = pFormats->nFormatId;
36 return nRet;
39 class SotTest
40 : public test::FiltersTest
41 , public test::BootstrapFixtureBase
43 public:
44 SotTest() {}
46 bool checkStream( const tools::SvRef<SotStorage> &xObjStor,
47 const OUString &rStreamName,
48 sal_uInt64 nSize );
49 bool checkStorage( const tools::SvRef<SotStorage> &xObjStor );
51 virtual bool load(const OUString &,
52 const OUString &rURL, const OUString &,
53 SfxFilterFlags, SotClipboardFormatId, unsigned int) override;
55 void test();
56 void testSize();
57 void testClipboard();
59 CPPUNIT_TEST_SUITE(SotTest);
60 CPPUNIT_TEST(test);
61 CPPUNIT_TEST(testSize);
62 CPPUNIT_TEST(testClipboard);
63 CPPUNIT_TEST_SUITE_END();
66 bool SotTest::checkStream( const tools::SvRef<SotStorage> &xObjStor,
67 const OUString &rStreamName,
68 sal_uInt64 nSize )
70 unsigned char *pData = static_cast<unsigned char*>(malloc( nSize ));
71 sal_uInt64 nReadableSize = 0;
72 if( !pData )
73 return true;
75 { // Read the data in one block
76 tools::SvRef<SotStorageStream> xStream( xObjStor->OpenSotStream( rStreamName ) );
77 xStream->Seek(0);
78 sal_uInt64 nRemaining = xStream->GetSize() - xStream->Tell();
80 CPPUNIT_ASSERT_EQUAL_MESSAGE( "check size", nSize, nRemaining );
81 CPPUNIT_ASSERT_EQUAL_MESSAGE( "check size #2", nSize, xStream->remainingSize());
83 // Read as much as we can, a corrupted FAT chain can cause real grief here
84 nReadableSize = xStream->ReadBytes(static_cast<void *>(pData), nSize);
86 { // Read the data backwards as well
87 tools::SvRef<SotStorageStream> xStream( xObjStor->OpenSotStream( rStreamName ) );
88 for( sal_uInt64 i = nReadableSize; i > 0; i-- )
90 CPPUNIT_ASSERT_MESSAGE( "sot reading error", !xStream->GetError() );
91 unsigned char c;
92 xStream->Seek( i - 1 );
93 CPPUNIT_ASSERT_EQUAL_MESSAGE( "sot storage reading byte",
94 static_cast<size_t>(1), xStream->ReadBytes(&c, 1));
95 CPPUNIT_ASSERT_EQUAL_MESSAGE( "mismatching data storage reading byte",
96 c, pData[i - 1] );
99 free(pData);
100 return true;
103 bool SotTest::checkStorage( const tools::SvRef<SotStorage> &xObjStor )
105 SvStorageInfoList aInfoList;
106 xObjStor->FillInfoList( &aInfoList );
108 for (auto& rInfo : aInfoList)
110 if( rInfo.IsStorage() )
112 tools::SvRef<SotStorage> xChild( xObjStor->OpenSotStorage( rInfo.GetName() ) );
113 checkStorage( xChild );
115 else if( rInfo.IsStream() )
116 checkStream( xObjStor, rInfo.GetName(), rInfo.GetSize() );
119 return true;
122 bool SotTest::load(const OUString &,
123 const OUString &rURL, const OUString &,
124 SfxFilterFlags, SotClipboardFormatId, unsigned int)
126 SvFileStream aStream(rURL, StreamMode::READ);
127 tools::SvRef<SotStorage> xObjStor = new SotStorage(aStream);
128 if (!xObjStor.is() || xObjStor->GetError())
129 return false;
131 CPPUNIT_ASSERT_MESSAGE("sot storage is not valid", xObjStor->Validate());
132 return checkStorage (xObjStor);
135 void SotTest::test()
137 testDir(OUString(),
138 m_directories.getURLFromSrc(u"/sot/qa/cppunit/data/"));
141 void SotTest::testSize()
143 OUString aURL(
144 m_directories.getURLFromSrc(u"/sot/qa/cppunit/data/pass/fdo84229-1.compound"));
145 SvFileStream aStream(aURL, StreamMode::READ);
146 tools::SvRef<SotStorage> xObjStor = new SotStorage(aStream);
147 CPPUNIT_ASSERT_MESSAGE("sot storage failed to open",
148 xObjStor.is());
149 CPPUNIT_ASSERT_MESSAGE("sot storage failed to open",
150 !xObjStor->GetError());
151 tools::SvRef<SotStorageStream> xStream = xObjStor->OpenSotStream("Book");
152 CPPUNIT_ASSERT_MESSAGE("stream failed to open",
153 xStream.is());
154 CPPUNIT_ASSERT_MESSAGE("stream failed to open",
155 !xObjStor->GetError());
156 CPPUNIT_ASSERT_MESSAGE("error in opened stream", !xStream->GetError());
157 sal_uInt64 nPos = xStream->GetSize();
158 CPPUNIT_ASSERT_EQUAL_MESSAGE("odd stream length", static_cast<sal_uInt64>(13312), nPos);
160 xStream->Seek(STREAM_SEEK_TO_END);
161 CPPUNIT_ASSERT_MESSAGE("error seeking to end", !xStream->GetError());
162 // cf. comment in Pos2Page, not extremely intuitive ...
163 CPPUNIT_ASSERT_EQUAL_MESSAGE("stream not at beginning", static_cast<sal_uInt64>(xStream->GetSize()), xStream->Tell());
164 xStream->Seek(STREAM_SEEK_TO_BEGIN);
166 CPPUNIT_ASSERT_MESSAGE("error seeking to beginning", !xStream->GetError());
167 CPPUNIT_ASSERT_EQUAL_MESSAGE("stream not at beginning", static_cast<sal_uInt64>(0), xStream->Tell());
170 void SotTest::testClipboard()
172 const SotAction_Impl* pFormats = sot::GetExchangeDestinationWriterFreeAreaCopy();
173 // tdf#52547 prefer BITMAP over HTML
174 // tdf#78801 prefer image over html over text
175 CPPUNIT_ASSERT(FindFormatIndex(pFormats, SotClipboardFormatId::BITMAP) < FindFormatIndex(pFormats, SotClipboardFormatId::HTML));
176 CPPUNIT_ASSERT(FindFormatIndex(pFormats, SotClipboardFormatId::HTML) < FindFormatIndex(pFormats, SotClipboardFormatId::STRING));
177 // tdf#81835 prefer RTF/HTML over GDI Metafile
178 CPPUNIT_ASSERT(FindFormatIndex(pFormats, SotClipboardFormatId::RTF) < FindFormatIndex(pFormats, SotClipboardFormatId::GDIMETAFILE));
179 CPPUNIT_ASSERT(FindFormatIndex(pFormats, SotClipboardFormatId::HTML) < FindFormatIndex(pFormats, SotClipboardFormatId::GDIMETAFILE));
180 #ifndef MACOSX
181 // tdf#115574 prefer RTF over BITMAP (Excel provides a BITMAP we can't
182 // read, also Excel paste result used to be an editable table)
183 CPPUNIT_ASSERT(FindFormatIndex(pFormats, SotClipboardFormatId::RTF) < FindFormatIndex(pFormats, SotClipboardFormatId::BITMAP));
184 #else
185 CPPUNIT_ASSERT(FindFormatIndex(pFormats, SotClipboardFormatId::BITMAP) < FindFormatIndex(pFormats, SotClipboardFormatId::RTF));
186 #endif
189 CPPUNIT_TEST_SUITE_REGISTRATION(SotTest);
192 CPPUNIT_PLUGIN_IMPLEMENT();
194 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */