diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 16:51:28 +0000 |
commit | 940b4d1848e8c70ab7642901a68594e8016caffc (patch) | |
tree | eb72f344ee6c3d9b80a7ecc079ea79e9fba8676d /sfx2/qa/cppunit | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream.tar.xz libreoffice-upstream.zip |
Adding upstream version 1:7.0.4.upstream/1%7.0.4upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | sfx2/qa/cppunit/misc/hello.odt | bin | 0 -> 8159 bytes | |||
-rw-r--r-- | sfx2/qa/cppunit/test_classification.cxx | 144 | ||||
-rw-r--r-- | sfx2/qa/cppunit/test_controlleritem.cxx | 58 | ||||
-rw-r--r-- | sfx2/qa/cppunit/test_metadatable.cxx | 249 | ||||
-rw-r--r-- | sfx2/qa/cppunit/test_misc.cxx | 198 |
5 files changed, 649 insertions, 0 deletions
diff --git a/sfx2/qa/cppunit/misc/hello.odt b/sfx2/qa/cppunit/misc/hello.odt Binary files differnew file mode 100644 index 000000000..23ce6a4db --- /dev/null +++ b/sfx2/qa/cppunit/misc/hello.odt diff --git a/sfx2/qa/cppunit/test_classification.cxx b/sfx2/qa/cppunit/test_classification.cxx new file mode 100644 index 000000000..b824ca219 --- /dev/null +++ b/sfx2/qa/cppunit/test_classification.cxx @@ -0,0 +1,144 @@ +/* -*- 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 <test/bootstrapfixture.hxx> +#include <unotest/macros_test.hxx> + +#include <com/sun/star/document/XDocumentPropertiesSupplier.hpp> +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/frame/DispatchHelper.hpp> +#include <com/sun/star/beans/XPropertySet.hpp> + +#include <comphelper/processfactory.hxx> +#include <comphelper/propertysequence.hxx> + +using namespace ::com::sun::star; + +namespace +{ + +/// Tests the handling of the .uno:ClassificationApply command in various applications. +class ClassificationTest : public test::BootstrapFixture, public unotest::MacrosTest +{ + uno::Reference<lang::XComponent> mxComponent; + void dispatchCommand(const uno::Reference<lang::XComponent>& xComponent, const OUString& rCommand, const uno::Sequence<beans::PropertyValue>& rPropertyValues); + void testClassification(); + +public: + virtual void setUp() override; + virtual void tearDown() override; + void testWriter(); + void testCalc(); + void testImpress(); + + CPPUNIT_TEST_SUITE(ClassificationTest); + CPPUNIT_TEST(testWriter); + CPPUNIT_TEST(testCalc); + CPPUNIT_TEST(testImpress); + CPPUNIT_TEST_SUITE_END(); +}; + +void ClassificationTest::setUp() +{ + test::BootstrapFixture::setUp(); + + mxDesktop.set(frame::Desktop::create(mxComponentContext)); +} + +void ClassificationTest::tearDown() +{ + if (mxComponent.is()) + mxComponent->dispose(); + + test::BootstrapFixture::tearDown(); +} + +void ClassificationTest::dispatchCommand(const uno::Reference<lang::XComponent>& xComponent, const OUString& rCommand, const uno::Sequence<beans::PropertyValue>& rPropertyValues) +{ + uno::Reference<frame::XController> xController = uno::Reference<frame::XModel>(xComponent, uno::UNO_QUERY_THROW)->getCurrentController(); + CPPUNIT_ASSERT(xController.is()); + uno::Reference<frame::XDispatchProvider> xFrame(xController->getFrame(), uno::UNO_QUERY); + CPPUNIT_ASSERT(xFrame.is()); + + uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext(); + uno::Reference<frame::XDispatchHelper> xDispatchHelper(frame::DispatchHelper::create(xContext)); + CPPUNIT_ASSERT(xDispatchHelper.is()); + + xDispatchHelper->executeDispatch(xFrame, rCommand, OUString(), 0, rPropertyValues); +} + +void ClassificationTest::testClassification() +{ + uno::Sequence<beans::PropertyValue> aPropertyValues(comphelper::InitPropertySequence( + { + {"Name", uno::makeAny(OUString("Non-Business"))}, + {"Type", uno::makeAny(OUString("urn:bails:ExportControl:"))}, + })); + dispatchCommand(mxComponent, ".uno:ClassificationApply", aPropertyValues); + + uno::Reference<document::XDocumentPropertiesSupplier> xDocumentPropertiesSupplier(mxComponent, uno::UNO_QUERY); + CPPUNIT_ASSERT(xDocumentPropertiesSupplier.is()); + uno::Reference<document::XDocumentProperties> xDocumentProperties = xDocumentPropertiesSupplier->getDocumentProperties(); + uno::Reference<beans::XPropertySet> xPropertySet(xDocumentProperties->getUserDefinedProperties(), uno::UNO_QUERY); + uno::Any aAny = xPropertySet->getPropertyValue("urn:bails:ExportControl:BusinessAuthorizationCategory:Identifier"); + CPPUNIT_ASSERT_EQUAL(OUString("urn:example:tscp:1:non-business"), aAny.get<OUString>()); + + aPropertyValues = comphelper::InitPropertySequence( + { + {"Name", uno::makeAny(OUString("Confidential"))}, + {"Type", uno::makeAny(OUString("urn:bails:NationalSecurity:"))}, + }); + dispatchCommand(mxComponent, ".uno:ClassificationApply", aPropertyValues); + aAny = xPropertySet->getPropertyValue("urn:bails:NationalSecurity:BusinessAuthorizationCategory:Identifier"); + CPPUNIT_ASSERT_EQUAL(OUString("urn:example:tscp:1:confidential"), aAny.get<OUString>()); + + aPropertyValues = comphelper::InitPropertySequence( + { + {"Name", uno::makeAny(OUString("Internal Only"))}, + {"Type", uno::makeAny(OUString("urn:bails:IntellectualProperty:"))}, + }); + dispatchCommand(mxComponent, ".uno:ClassificationApply", aPropertyValues); + aAny = xPropertySet->getPropertyValue("urn:bails:IntellectualProperty:BusinessAuthorizationCategory:Identifier"); + CPPUNIT_ASSERT_EQUAL(OUString("urn:example:tscp:1:internal-only"), aAny.get<OUString>()); +} + +void ClassificationTest::testWriter() +{ + // Test SID_CLASSIFICATION_APPLY handling in SwDocShell::Execute(). + mxComponent = loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument"); + CPPUNIT_ASSERT(mxComponent.is()); + // This resulted in a beans::UnknownPropertyException when the request wasn't handled. + testClassification(); +} + +void ClassificationTest::testCalc() +{ + // Test SID_CLASSIFICATION_APPLY handling in ScFormatShell::ExecuteStyle(). + mxComponent = loadFromDesktop("private:factory/scalc", "com.sun.star.sheet.SpreadsheetDocument"); + CPPUNIT_ASSERT(mxComponent.is()); + // This resulted in a beans::UnknownPropertyException when the request wasn't handled. + testClassification(); +} + +void ClassificationTest::testImpress() +{ + // Test SID_CLASSIFICATION_APPLY handling in sd::DrawViewShell::FuTemporary(). + mxComponent = loadFromDesktop("private:factory/simpress", "com.sun.star.presentation.PresentationDocument"); + CPPUNIT_ASSERT(mxComponent.is()); + // This resulted in a beans::UnknownPropertyException when the request wasn't handled. + testClassification(); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(ClassificationTest); + +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/qa/cppunit/test_controlleritem.cxx b/sfx2/qa/cppunit/test_controlleritem.cxx new file mode 100644 index 000000000..9576e7b19 --- /dev/null +++ b/sfx2/qa/cppunit/test_controlleritem.cxx @@ -0,0 +1,58 @@ +/* -*- 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 <sal/types.h> +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> + +#include <sfx2/ctrlitem.hxx> + +namespace { + +class ControllerItemTest + : public ::CppUnit::TestFixture +{ +public: + void test(); + + CPPUNIT_TEST_SUITE(ControllerItemTest); + CPPUNIT_TEST(test); + CPPUNIT_TEST_SUITE_END(); + +private: +}; + +static bool bDeleted = false; + +class FooController : public SfxControllerItem { +public: + FooController() : SfxControllerItem() {} + virtual ~FooController() override { bDeleted = true; } +}; + +void ControllerItemTest::test() +{ + FooController *pController(new FooController()); + + // TESTME: binding, un-binding, re-binding, IsBound, SetId etc. + + pController->dispose(); + delete pController; + CPPUNIT_ASSERT( bDeleted ); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(ControllerItemTest); + +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/qa/cppunit/test_metadatable.cxx b/sfx2/qa/cppunit/test_metadatable.cxx new file mode 100644 index 000000000..06b18edfe --- /dev/null +++ b/sfx2/qa/cppunit/test_metadatable.cxx @@ -0,0 +1,249 @@ +/* -*- 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <sal/types.h> +#include <cppunit/TestAssert.h> +#include <cppunit/TestFixture.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> + +#include <com/sun/star/lang/IllegalArgumentException.hpp> + +#include <sfx2/Metadatable.hxx> +#include <sfx2/XmlIdRegistry.hxx> + +#include <memory> + + +using namespace ::com::sun::star; + + +namespace { + +class MetadatableTest + : public ::CppUnit::TestFixture +{ +public: + void test(); + + CPPUNIT_TEST_SUITE(MetadatableTest); + CPPUNIT_TEST(test); + CPPUNIT_TEST_SUITE_END(); + +private: +}; + +class MockMetadatable + : public ::sfx2::Metadatable +{ +private: + ::sfx2::IXmlIdRegistry & m_rRegistry; + +public: + MockMetadatable(::sfx2::IXmlIdRegistry & i_rReg, + bool const i_isInClip = false) + : m_rRegistry(i_rReg) + , m_bInClipboard(i_isInClip), m_bInUndo(false), m_bInContent(true) {} + bool m_bInClipboard; + bool m_bInUndo; + bool m_bInContent; + virtual bool IsInClipboard() const override { return m_bInClipboard; } + virtual bool IsInUndo() const override { return m_bInUndo; } + virtual bool IsInContent() const override { return m_bInContent; } + virtual ::sfx2::IXmlIdRegistry& GetRegistry() override { return m_rRegistry; } + virtual css::uno::Reference< css::rdf::XMetadatable > MakeUnoObject() override { return nullptr; } +}; + +void MetadatableTest::test() +{ + std::unique_ptr< ::sfx2::IXmlIdRegistry > const pReg( + ::sfx2::createXmlIdRegistry(false) ); + std::unique_ptr< ::sfx2::IXmlIdRegistry > const pRegClip( + ::sfx2::createXmlIdRegistry(true) ); + + MockMetadatable m1(*pReg); + MockMetadatable m2(*pReg); + MockMetadatable m3(*pReg); + MockMetadatable m4(*pReg); + MockMetadatable m5(*pReg); + OUString empty; + OUString content( "content.xml" ); + OUString const styles( "styles.xml" ); + OUString const sid1( "id1" ); + OUString const sid2( "id2" ); + OUString sid3( "id3" ); + OUString sid4( "id4" ); + beans::StringPair id1(content, sid1); + beans::StringPair id2(content, sid2); + beans::StringPair id3(content, sid3); + beans::StringPair id4(styles, sid4); + beans::StringPair id3e(empty, sid3); + beans::StringPair id4e(empty, sid4); + m1.SetMetadataReference(id1); + CPPUNIT_ASSERT_MESSAGE("set failed", bool(m1.GetMetadataReference() == id1)); + try { + m2.SetMetadataReference(id1); + CPPUNIT_ASSERT_MESSAGE("set duplicate succeeded", false); + } catch (const lang::IllegalArgumentException &) { } + m1.SetMetadataReference(id1); + CPPUNIT_ASSERT_MESSAGE("set failed (existing)", + bool(m1.GetMetadataReference() == id1)); + m1.EnsureMetadataReference(); + CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)", + bool(m1.GetMetadataReference() == id1)); + + m2.EnsureMetadataReference(); + beans::StringPair m2id(m2.GetMetadataReference()); + CPPUNIT_ASSERT_MESSAGE("ensure failed", !m2id.Second.isEmpty()); + m2.EnsureMetadataReference(); + CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)", + bool(m2.GetMetadataReference() == m2id)); + + m1.m_bInUndo = true; + CPPUNIT_ASSERT_MESSAGE("move to undo failed", + m1.GetMetadataReference().Second.isEmpty()); + + m1.m_bInUndo = false; + CPPUNIT_ASSERT_MESSAGE("move from undo failed", + bool(m1.GetMetadataReference() == id1)); + + m1.m_bInUndo = true; + try { + m2.SetMetadataReference(id1); // steal! + } catch (lang::IllegalArgumentException &) { + CPPUNIT_FAIL("set duplicate to undo failed"); + } + m1.m_bInUndo = false; + CPPUNIT_ASSERT_MESSAGE("move from undo: duplicate", + m1.GetMetadataReference().Second.isEmpty()); + + m3.RegisterAsCopyOf(m2); + CPPUNIT_ASSERT_MESSAGE("copy: source", bool(m2.GetMetadataReference() == id1)); + CPPUNIT_ASSERT_MESSAGE("copy: duplicate", + m3.GetMetadataReference().Second.isEmpty()); + m4.RegisterAsCopyOf(m3); + CPPUNIT_ASSERT_MESSAGE("copy: source", bool(m2.GetMetadataReference() == id1)); + CPPUNIT_ASSERT_MESSAGE("copy: duplicate", + m3.GetMetadataReference().Second.isEmpty()); + CPPUNIT_ASSERT_MESSAGE("copy: duplicate", + m4.GetMetadataReference().Second.isEmpty()); + m2.m_bInUndo = true; + CPPUNIT_ASSERT_MESSAGE("duplicate to undo", + bool(m3.GetMetadataReference() == id1)); + CPPUNIT_ASSERT_MESSAGE("duplicate to undo", + m2.GetMetadataReference().Second.isEmpty()); + m2.m_bInUndo = false; + CPPUNIT_ASSERT_MESSAGE("duplicate from undo", + bool(m2.GetMetadataReference() == id1)); + CPPUNIT_ASSERT_MESSAGE("duplicate from undo", + m3.GetMetadataReference().Second.isEmpty()); + + m4.EnsureMetadataReference(); // new! + beans::StringPair m4id(m4.GetMetadataReference()); + CPPUNIT_ASSERT_MESSAGE("ensure on duplicate", + !m4id.Second.isEmpty() && !(m4id == id1)); + + MockMetadatable mc1(*pRegClip, true); // in clipboard + MockMetadatable mc2(*pRegClip, true); + MockMetadatable mc3(*pRegClip, true); + MockMetadatable mc4(*pRegClip, true); + MockMetadatable m2p(*pReg); + MockMetadatable m3p(*pReg); + + mc1.SetMetadataReference(id2); + CPPUNIT_ASSERT_MESSAGE("set failed", bool(mc1.GetMetadataReference() == id2)); + try { + mc2.SetMetadataReference(id2); + CPPUNIT_FAIL("set duplicate succeeded"); + } catch (const lang::IllegalArgumentException &) { } + mc1.SetMetadataReference(id2); + CPPUNIT_ASSERT_MESSAGE("set failed (existing)", + bool(mc1.GetMetadataReference() == id2)); + mc1.EnsureMetadataReference(); + CPPUNIT_ASSERT_MESSAGE("ensure failed (existing)", + bool(mc1.GetMetadataReference() == id2)); + mc2.EnsureMetadataReference(); + beans::StringPair mc2id(mc2.GetMetadataReference()); + CPPUNIT_ASSERT_MESSAGE("ensure failed", !mc2id.Second.isEmpty()); + mc2.EnsureMetadataReference(); + CPPUNIT_ASSERT_MESSAGE("ensure failed (idempotent)", + bool(mc2.GetMetadataReference() == mc2id)); + mc2.RemoveMetadataReference(); + CPPUNIT_ASSERT_MESSAGE("remove failed", + mc2.GetMetadataReference().Second.isEmpty()); + + // set up mc2 as copy of m2 and mc3 as copy of m3 + mc3.RegisterAsCopyOf(m3); + CPPUNIT_ASSERT_MESSAGE("copy to clipboard (latent)", + mc3.GetMetadataReference().Second.isEmpty() ); + mc2.RegisterAsCopyOf(m2); + CPPUNIT_ASSERT_MESSAGE("copy to clipboard (non-latent)", + bool(mc2.GetMetadataReference() == id1)); + // paste mc2 to m2p and mc3 to m3p + m2p.RegisterAsCopyOf(mc2); + CPPUNIT_ASSERT_MESSAGE("paste from clipboard (non-latent)", + m2p.GetMetadataReference().Second.isEmpty() ); + m3p.RegisterAsCopyOf(mc3); + CPPUNIT_ASSERT_MESSAGE("paste from clipboard (latent)", + m3p.GetMetadataReference().Second.isEmpty() ); + // delete m2, m2p, m3 + m2.RemoveMetadataReference(); + CPPUNIT_ASSERT_MESSAGE("remove failed", + m2.GetMetadataReference().Second.isEmpty()); + CPPUNIT_ASSERT_MESSAGE("paste-remove (non-latent)", + bool(m2p.GetMetadataReference() == id1)); + m2p.RemoveMetadataReference(); + CPPUNIT_ASSERT_MESSAGE("remove failed", + m2p.GetMetadataReference().Second.isEmpty()); + CPPUNIT_ASSERT_MESSAGE("paste-remove2 (non-latent)", + bool(m3.GetMetadataReference() == id1)); + m3.RemoveMetadataReference(); + CPPUNIT_ASSERT_MESSAGE("remove failed", + m3.GetMetadataReference().Second.isEmpty()); + CPPUNIT_ASSERT_MESSAGE("paste-remove (latent)", + bool(m3p.GetMetadataReference() == id1)); + // delete mc2 + mc2.SetMetadataReference(beans::StringPair()); + CPPUNIT_ASSERT_MESSAGE("in clipboard becomes non-latent", + mc3.GetMetadataReference().Second.isEmpty() ); + // paste mc2 + m2p.RegisterAsCopyOf(mc2); + CPPUNIT_ASSERT_MESSAGE("remove-paste", + m2p.GetMetadataReference().Second.isEmpty()); + CPPUNIT_ASSERT_MESSAGE("remove-paste (stolen)", + bool(m3p.GetMetadataReference() == id1)); + + // auto-detect stream + m5.SetMetadataReference(id3e); + CPPUNIT_ASSERT_MESSAGE("auto-detect (content)", + bool(m5.GetMetadataReference() == id3)); + m5.m_bInContent = false; + m5.SetMetadataReference(id4e); + CPPUNIT_ASSERT_MESSAGE("auto-detect (styles)", + bool(m5.GetMetadataReference() == id4)); +} + + +CPPUNIT_TEST_SUITE_REGISTRATION(MetadatableTest); + +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sfx2/qa/cppunit/test_misc.cxx b/sfx2/qa/cppunit/test_misc.cxx new file mode 100644 index 000000000..4ed4ba1fb --- /dev/null +++ b/sfx2/qa/cppunit/test_misc.cxx @@ -0,0 +1,198 @@ +/* -*- 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 <sal/types.h> + +#ifndef _WIN32 +#include <sys/stat.h> +#include <unistd.h> +#endif +#include <memory> + +#include <cppunit/TestAssert.h> +#include <cppunit/extensions/HelperMacros.h> +#include <cppunit/plugin/TestPlugIn.h> + +#include <com/sun/star/beans/PropertyState.hpp> +#include <com/sun/star/beans/PropertyValue.hpp> +#include <com/sun/star/document/DocumentProperties.hpp> +#include <com/sun/star/packages/zip/ZipFileAccess.hpp> +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/frame/XStorable.hpp> + +#include <test/bootstrapfixture.hxx> +#include <test/xmltesttools.hxx> +#include <unotest/macros_test.hxx> + +#include <unotools/ucbstreamhelper.hxx> +#include <comphelper/propertysequence.hxx> +#include <comphelper/processfactory.hxx> +#include <sfx2/app.hxx> +#include <osl/file.hxx> + + +using namespace ::com::sun::star; + + +namespace { + +class MiscTest + : public test::BootstrapFixture + , public unotest::MacrosTest + , public XmlTestTools +{ +public: + virtual void setUp() override; + + virtual void registerNamespaces(xmlXPathContextPtr& pXmlXpathCtx) override + { + // ODF + xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("office"), BAD_CAST("urn:oasis:names:tc:opendocument:xmlns:office:1.0")); + xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("meta"), BAD_CAST("urn:oasis:names:tc:opendocument:xmlns:meta:1.0")); + xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("dc"), BAD_CAST("http://purl.org/dc/elements/1.1/")); + // used in testCustomMetadata + xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("foo"), BAD_CAST("http://foo.net")); + xmlXPathRegisterNs(pXmlXpathCtx, BAD_CAST("baz"), BAD_CAST("http://baz.net")); + } +}; + +void MiscTest::setUp() +{ + m_xContext = comphelper::getProcessComponentContext(); + mxDesktop.set(frame::Desktop::create(m_xContext)); + SfxApplication::GetOrCreate(); +} + +CPPUNIT_TEST_FIXTURE(MiscTest, testODFCustomMetadata) +{ + uno::Reference<document::XDocumentProperties> const xProps( + ::com::sun::star::document::DocumentProperties::create(m_xContext)); + + OUString const url(m_directories.getURLFromSrc("/sfx2/qa/complex/sfx2/testdocuments/CUSTOM.odt")); + xProps->loadFromMedium(url, uno::Sequence<beans::PropertyValue>()); + CPPUNIT_ASSERT_EQUAL(OUString(""), xProps->getAuthor()); + uno::Sequence<beans::PropertyValue> mimeArgs({ + beans::PropertyValue("MediaType", -1, uno::Any(OUString("application/vnd.oasis.opendocument.text")), beans::PropertyState_DIRECT_VALUE) + }); + utl::TempFile aTempFile; + xProps->storeToMedium(aTempFile.GetURL(), mimeArgs); + + // check that custom metadata is preserved + uno::Reference<packages::zip::XZipFileAccess2> const xZip( + packages::zip::ZipFileAccess::createWithURL(m_xContext, aTempFile.GetURL())); + uno::Reference<io::XInputStream> const xInputStream(xZip->getByName("meta.xml"), uno::UNO_QUERY); + std::unique_ptr<SvStream> const pStream(utl::UcbStreamHelper::CreateStream(xInputStream, true)); + xmlDocUniquePtr pXmlDoc = parseXmlStream(pStream.get()); + assertXPathContent(pXmlDoc, "/office:document-meta/office:meta/bork", "bork"); + assertXPath(pXmlDoc, "/office:document-meta/office:meta/foo:bar", 1); + assertXPath(pXmlDoc, "/office:document-meta/office:meta/foo:bar/baz:foo", 1); + assertXPath(pXmlDoc, "/office:document-meta/office:meta/foo:bar/baz:foo[@baz:bar='foo']"); + assertXPathContent(pXmlDoc, "/office:document-meta/office:meta/foo:bar/foo:baz", "bar"); + + aTempFile.EnableKillingFile(); +} + +CPPUNIT_TEST_FIXTURE(MiscTest, testNoThumbnail) +{ + // Load a document. + const OUString aURL(m_directories.getURLFromSrc("/sfx2/qa/cppunit/misc/hello.odt")); + uno::Reference<lang::XComponent> xComponent + = loadFromDesktop(aURL, "com.sun.star.text.TextDocument"); + CPPUNIT_ASSERT(xComponent.is()); + + // Save it with the NoThumbnail option and assert that it has no thumbnail. +#ifndef _WIN32 + mode_t nMask = umask(022); +#endif + uno::Reference<frame::XStorable> xStorable(xComponent, uno::UNO_QUERY); + CPPUNIT_ASSERT(xStorable.is()); + utl::TempFile aTempFile; + uno::Sequence<beans::PropertyValue> aProperties( + comphelper::InitPropertySequence({ { "NoThumbnail", uno::makeAny(true) } })); + osl::File::remove(aTempFile.GetURL()); + xStorable->storeToURL(aTempFile.GetURL(), aProperties); + uno::Reference<packages::zip::XZipFileAccess2> xZipFile + = packages::zip::ZipFileAccess::createWithURL(m_xContext, aTempFile.GetURL()); + CPPUNIT_ASSERT(!xZipFile->hasByName("Thumbnails/thumbnail.png")); + +#ifndef _WIN32 + // Check permissions of the URL after store. + osl::DirectoryItem aItem; + CPPUNIT_ASSERT_EQUAL(osl::DirectoryItem::E_None, + osl::DirectoryItem::get(aTempFile.GetURL(), aItem)); + + osl::FileStatus aStatus(osl_FileStatus_Mask_Attributes); + CPPUNIT_ASSERT_EQUAL(osl::DirectoryItem::E_None, aItem.getFileStatus(aStatus)); + + // The following checks used to fail in the past, osl_File_Attribute_GrpRead was not set even if + // umask requested so: + CPPUNIT_ASSERT(aStatus.getAttributes() & osl_File_Attribute_GrpRead); + CPPUNIT_ASSERT(aStatus.getAttributes() & osl_File_Attribute_OthRead); + + // Now "save as" again to trigger the "overwrite" case. + xStorable->storeToURL(aTempFile.GetURL(), {}); + CPPUNIT_ASSERT_EQUAL(osl::DirectoryItem::E_None, aItem.getFileStatus(aStatus)); + // The following check used to fail in the past, result had temp file + // permissions. + CPPUNIT_ASSERT(aStatus.getAttributes() & osl_File_Attribute_GrpRead); + + umask(nMask); +#endif + + xComponent->dispose(); +} + +CPPUNIT_TEST_FIXTURE(MiscTest, testHardLinks) +{ +#ifndef _WIN32 + OUString aSourceDir = m_directories.getURLFromSrc("/sfx2/qa/cppunit/misc/"); + OUString aTargetDir = m_directories.getURLFromWorkdir("/CppunitTest/sfx2_misc.test.user/"); + const OUString aURL(aTargetDir + "hello.odt"); + osl::File::copy(aSourceDir + "hello.odt", aURL); + OUString aTargetPath; + osl::FileBase::getSystemPathFromFileURL(aURL, aTargetPath); + OString aOld = aTargetPath.toUtf8(); + aTargetPath += ".2"; + OString aNew = aTargetPath.toUtf8(); + int nRet = link(aOld.getStr(), aNew.getStr()); + CPPUNIT_ASSERT_EQUAL(0, nRet); + + uno::Reference<lang::XComponent> xComponent = loadFromDesktop(aURL, "com.sun.star.text.TextDocument"); + CPPUNIT_ASSERT(xComponent.is()); + + uno::Reference<frame::XStorable> xStorable(xComponent, uno::UNO_QUERY); + xStorable->store(); + + struct stat buf; + // coverity[fs_check_call] - this is legitimate in the context of this test + nRet = stat(aOld.getStr(), &buf); + CPPUNIT_ASSERT_EQUAL(0, nRet); + // This failed: hard link count was 1, the hard link broke on store. + CPPUNIT_ASSERT(buf.st_nlink > 1); + + // Test that symlinks are preserved as well. + nRet = remove(aNew.getStr()); + CPPUNIT_ASSERT_EQUAL(0, nRet); + nRet = symlink(aOld.getStr(), aNew.getStr()); + CPPUNIT_ASSERT_EQUAL(0, nRet); + xStorable->storeToURL(aURL + ".2", {}); + nRet = lstat(aNew.getStr(), &buf); + CPPUNIT_ASSERT_EQUAL(0, nRet); + // This failed, the hello.odt.2 symlink was replaced with a real file. + CPPUNIT_ASSERT(bool(S_ISLNK(buf.st_mode))); + + xComponent->dispose(); +#endif +} + +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |