1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice 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 <test/bootstrapfixture.hxx>
11 #include <unotest/macros_test.hxx>
12 
13 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
14 #include <com/sun/star/frame/Desktop.hpp>
15 #include <com/sun/star/frame/DispatchHelper.hpp>
16 #include <com/sun/star/beans/XPropertySet.hpp>
17 
18 #include <comphelper/processfactory.hxx>
19 #include <comphelper/propertysequence.hxx>
20 
21 using namespace ::com::sun::star;
22 
23 namespace
24 {
25 
26 /// Tests the handling of the .uno:ClassificationApply command in various applications.
27 class ClassificationTest : public test::BootstrapFixture, public unotest::MacrosTest
28 {
29     uno::Reference<lang::XComponent> mxComponent;
30     void dispatchCommand(const uno::Reference<lang::XComponent>& xComponent, const OUString& rCommand, const uno::Sequence<beans::PropertyValue>& rPropertyValues);
31     void testClassification();
32 
33 public:
34     virtual void setUp() override;
35     virtual void tearDown() override;
36     void testWriter();
37     void testCalc();
38     void testImpress();
39 
40     CPPUNIT_TEST_SUITE(ClassificationTest);
41     CPPUNIT_TEST(testWriter);
42     CPPUNIT_TEST(testCalc);
43     CPPUNIT_TEST(testImpress);
44     CPPUNIT_TEST_SUITE_END();
45 };
46 
setUp()47 void ClassificationTest::setUp()
48 {
49     test::BootstrapFixture::setUp();
50 
51     mxDesktop.set(frame::Desktop::create(mxComponentContext));
52 }
53 
tearDown()54 void ClassificationTest::tearDown()
55 {
56     if (mxComponent.is())
57         mxComponent->dispose();
58 
59     test::BootstrapFixture::tearDown();
60 }
61 
dispatchCommand(const uno::Reference<lang::XComponent> & xComponent,const OUString & rCommand,const uno::Sequence<beans::PropertyValue> & rPropertyValues)62 void ClassificationTest::dispatchCommand(const uno::Reference<lang::XComponent>& xComponent, const OUString& rCommand, const uno::Sequence<beans::PropertyValue>& rPropertyValues)
63 {
64     uno::Reference<frame::XController> xController = uno::Reference<frame::XModel>(xComponent, uno::UNO_QUERY_THROW)->getCurrentController();
65     CPPUNIT_ASSERT(xController.is());
66     uno::Reference<frame::XDispatchProvider> xFrame(xController->getFrame(), uno::UNO_QUERY);
67     CPPUNIT_ASSERT(xFrame.is());
68 
69     uno::Reference<uno::XComponentContext> xContext = ::comphelper::getProcessComponentContext();
70     uno::Reference<frame::XDispatchHelper> xDispatchHelper(frame::DispatchHelper::create(xContext));
71     CPPUNIT_ASSERT(xDispatchHelper.is());
72 
73     xDispatchHelper->executeDispatch(xFrame, rCommand, OUString(), 0, rPropertyValues);
74 }
75 
testClassification()76 void ClassificationTest::testClassification()
77 {
78     uno::Sequence<beans::PropertyValue> aPropertyValues(comphelper::InitPropertySequence(
79     {
80         {"Name", uno::makeAny(OUString("Non-Business"))},
81         {"Type", uno::makeAny(OUString("urn:bails:ExportControl:"))},
82     }));
83     dispatchCommand(mxComponent, ".uno:ClassificationApply", aPropertyValues);
84 
85     uno::Reference<document::XDocumentPropertiesSupplier> xDocumentPropertiesSupplier(mxComponent, uno::UNO_QUERY);
86     CPPUNIT_ASSERT(xDocumentPropertiesSupplier.is());
87     uno::Reference<document::XDocumentProperties> xDocumentProperties = xDocumentPropertiesSupplier->getDocumentProperties();
88     uno::Reference<beans::XPropertySet> xPropertySet(xDocumentProperties->getUserDefinedProperties(), uno::UNO_QUERY);
89     uno::Any aAny = xPropertySet->getPropertyValue("urn:bails:ExportControl:BusinessAuthorizationCategory:Identifier");
90     CPPUNIT_ASSERT_EQUAL(OUString("urn:example:tscp:1:non-business"), aAny.get<OUString>());
91 
92     aPropertyValues = comphelper::InitPropertySequence(
93     {
94         {"Name", uno::makeAny(OUString("Confidential"))},
95         {"Type", uno::makeAny(OUString("urn:bails:NationalSecurity:"))},
96     });
97     dispatchCommand(mxComponent, ".uno:ClassificationApply", aPropertyValues);
98     aAny = xPropertySet->getPropertyValue("urn:bails:NationalSecurity:BusinessAuthorizationCategory:Identifier");
99     CPPUNIT_ASSERT_EQUAL(OUString("urn:example:tscp:1:confidential"), aAny.get<OUString>());
100 
101     aPropertyValues = comphelper::InitPropertySequence(
102     {
103         {"Name", uno::makeAny(OUString("Internal Only"))},
104         {"Type", uno::makeAny(OUString("urn:bails:IntellectualProperty:"))},
105     });
106     dispatchCommand(mxComponent, ".uno:ClassificationApply", aPropertyValues);
107     aAny = xPropertySet->getPropertyValue("urn:bails:IntellectualProperty:BusinessAuthorizationCategory:Identifier");
108     CPPUNIT_ASSERT_EQUAL(OUString("urn:example:tscp:1:internal-only"), aAny.get<OUString>());
109 }
110 
testWriter()111 void ClassificationTest::testWriter()
112 {
113     // Test SID_CLASSIFICATION_APPLY handling in SwDocShell::Execute().
114     mxComponent = loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument");
115     CPPUNIT_ASSERT(mxComponent.is());
116     // This resulted in a beans::UnknownPropertyException when the request wasn't handled.
117     testClassification();
118 }
119 
testCalc()120 void ClassificationTest::testCalc()
121 {
122     // Test SID_CLASSIFICATION_APPLY handling in ScFormatShell::ExecuteStyle().
123     mxComponent = loadFromDesktop("private:factory/scalc", "com.sun.star.sheet.SpreadsheetDocument");
124     CPPUNIT_ASSERT(mxComponent.is());
125     // This resulted in a beans::UnknownPropertyException when the request wasn't handled.
126     testClassification();
127 }
128 
testImpress()129 void ClassificationTest::testImpress()
130 {
131     // Test SID_CLASSIFICATION_APPLY handling in sd::DrawViewShell::FuTemporary().
132     mxComponent = loadFromDesktop("private:factory/simpress", "com.sun.star.presentation.PresentationDocument");
133     CPPUNIT_ASSERT(mxComponent.is());
134     // This resulted in a beans::UnknownPropertyException when the request wasn't handled.
135     testClassification();
136 }
137 
138 CPPUNIT_TEST_SUITE_REGISTRATION(ClassificationTest);
139 
140 }
141 
142 CPPUNIT_PLUGIN_IMPLEMENT();
143 
144 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
145