1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #include <cppuhelper/implbase.hxx>
11 #include <test/bootstrapfixture.hxx>
12 #include <unotest/macros_test.hxx>
14 #include <com/sun/star/frame/Desktop.hpp>
15 #include <com/sun/star/frame/DispatchHelper.hpp>
16 #include <com/sun/star/frame/XDispatchProviderInterceptor.hpp>
17 #include <com/sun/star/frame/XInterceptorInfo.hpp>
19 #include <comphelper/processfactory.hxx>
20 #include <rtl/ref.hxx>
22 using namespace ::com::sun::star
;
27 /// Sample interception implementation that asserts getInterceptedURLs() and queryDispatch() is in sync.
28 class MyInterceptor
: public cppu::WeakImplHelper
30 frame::XDispatchProviderInterceptor
,
31 frame::XInterceptorInfo
34 uno::Reference
<frame::XDispatchProvider
> m_xMaster
;
35 uno::Reference
<frame::XDispatchProvider
> m_xSlave
;
36 uno::Sequence
<OUString
> m_aDisabledCommands
;
43 /// Number of queryDispatch() calls that operate on a command advertised by getInterceptedURLs().
45 /// Number of queryDispatch() calls that operate on a command not advertised by getInterceptedURLs().
48 // frame::XInterceptorInfo
49 virtual uno::Sequence
<OUString
> SAL_CALL
getInterceptedURLs() override
;
51 // frame::XDispatchProviderInterceptor
52 virtual void SAL_CALL
setMasterDispatchProvider(const uno::Reference
<frame::XDispatchProvider
>& xNewSupplier
) override
;
53 virtual uno::Reference
<frame::XDispatchProvider
> SAL_CALL
getMasterDispatchProvider() override
;
54 virtual void SAL_CALL
setSlaveDispatchProvider(const uno::Reference
<frame::XDispatchProvider
>& xNewSupplier
) override
;
55 virtual uno::Reference
<frame::XDispatchProvider
> SAL_CALL
getSlaveDispatchProvider() override
;
57 // frame::XDispatchProvider
58 virtual uno::Sequence
<uno::Reference
<frame::XDispatch
>> SAL_CALL
queryDispatches(const uno::Sequence
<frame::DispatchDescriptor
>& rRequests
) override
;
59 virtual uno::Reference
<frame::XDispatch
> SAL_CALL
queryDispatch(const util::URL
& rURL
, const OUString
& rTargetFrameName
, sal_Int32 SearchFlags
) override
;
62 MyInterceptor::MyInterceptor()
63 : m_aDisabledCommands
{".uno:Bold"},
69 int MyInterceptor::getExpected()
71 int nRet
= m_nExpected
;
76 int MyInterceptor::getUnexpected()
78 int nRet
= m_nUnexpected
;
83 uno::Sequence
<OUString
> MyInterceptor::getInterceptedURLs()
85 return m_aDisabledCommands
;
88 void MyInterceptor::setMasterDispatchProvider(const uno::Reference
<frame::XDispatchProvider
>& xNewSupplier
)
90 m_xMaster
= xNewSupplier
;
93 uno::Reference
<frame::XDispatchProvider
> MyInterceptor::getMasterDispatchProvider()
98 void MyInterceptor::setSlaveDispatchProvider(const uno::Reference
<frame::XDispatchProvider
>& xNewSupplier
)
100 m_xSlave
= xNewSupplier
;
103 uno::Reference
<frame::XDispatchProvider
> MyInterceptor::getSlaveDispatchProvider()
108 uno::Sequence
<uno::Reference
<frame::XDispatch
>> MyInterceptor::queryDispatches(const uno::Sequence
<frame::DispatchDescriptor
>& rRequests
)
110 uno::Sequence
<uno::Reference
<frame::XDispatch
>> aResult(rRequests
.getLength());
112 for (sal_Int32 i
= 0; i
< rRequests
.getLength(); ++i
)
114 aResult
[i
] = queryDispatch(rRequests
[i
].FeatureURL
, rRequests
[i
].FrameName
, rRequests
[i
].SearchFlags
);
120 uno::Reference
<frame::XDispatch
> MyInterceptor::queryDispatch(const util::URL
& rURL
, const OUString
& /*rTargetFrameName*/, sal_Int32
/*SearchFlags*/)
122 if (std::find(m_aDisabledCommands
.begin(), m_aDisabledCommands
.end(), rURL
.Complete
) != m_aDisabledCommands
.end())
127 return uno::Reference
<frame::XDispatch
>();
130 /// Tests how InterceptionHelper invokes a registered interceptor.
131 class DispatchTest
: public test::BootstrapFixture
, public unotest::MacrosTest
134 uno::Reference
<uno::XComponentContext
> mxComponentContext
;
135 uno::Reference
<lang::XComponent
> mxComponent
;
136 void dispatchCommand(const uno::Reference
<lang::XComponent
>& xComponent
, const OUString
& rCommand
, const uno::Sequence
<beans::PropertyValue
>& rPropertyValues
);
139 virtual void setUp() override
;
140 virtual void tearDown() override
;
143 void DispatchTest::setUp()
145 test::BootstrapFixture::setUp();
147 mxComponentContext
.set(comphelper::getComponentContext(getMultiServiceFactory()));
148 mxDesktop
.set(frame::Desktop::create(mxComponentContext
));
151 void DispatchTest::tearDown()
153 if (mxComponent
.is())
154 mxComponent
->dispose();
156 test::BootstrapFixture::tearDown();
159 void DispatchTest::dispatchCommand(const uno::Reference
<lang::XComponent
>& xComponent
, const OUString
& rCommand
, const uno::Sequence
<beans::PropertyValue
>& rPropertyValues
)
161 uno::Reference
<frame::XController
> xController
= uno::Reference
<frame::XModel
>(xComponent
, uno::UNO_QUERY_THROW
)->getCurrentController();
162 CPPUNIT_ASSERT(xController
.is());
163 uno::Reference
<frame::XDispatchProvider
> xFrame(xController
->getFrame(), uno::UNO_QUERY
);
164 CPPUNIT_ASSERT(xFrame
.is());
166 uno::Reference
<uno::XComponentContext
> xContext
= ::comphelper::getProcessComponentContext();
167 uno::Reference
<frame::XDispatchHelper
> xDispatchHelper(frame::DispatchHelper::create(xContext
));
168 CPPUNIT_ASSERT(xDispatchHelper
.is());
170 xDispatchHelper
->executeDispatch(xFrame
, rCommand
, OUString(), 0, rPropertyValues
);
173 CPPUNIT_TEST_FIXTURE(DispatchTest
, testInterception
)
175 mxComponent
= loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument");
176 CPPUNIT_ASSERT(mxComponent
.is());
177 uno::Reference
<frame::XModel
> xModel(mxComponent
, uno::UNO_QUERY
);
178 CPPUNIT_ASSERT(xModel
.is());
180 uno::Reference
<frame::XDispatchProviderInterception
> xRegistration(xModel
->getCurrentController()->getFrame(), uno::UNO_QUERY
);
181 CPPUNIT_ASSERT(xRegistration
.is());
183 rtl::Reference
<MyInterceptor
> pInterceptor(new MyInterceptor());
184 xRegistration
->registerDispatchProviderInterceptor(uno::Reference
<frame::XDispatchProviderInterceptor
>(pInterceptor
.get()));
186 dispatchCommand(mxComponent
, ".uno:Bold", {});
187 CPPUNIT_ASSERT_EQUAL(1, pInterceptor
->getExpected());
188 CPPUNIT_ASSERT_EQUAL(0, pInterceptor
->getUnexpected());
189 dispatchCommand(mxComponent
, ".uno:Italic", {});
190 CPPUNIT_ASSERT_EQUAL(1, pInterceptor
->getExpected());
191 // This was 1: MyInterceptor::queryDispatch() was called for .uno:Italic.
192 CPPUNIT_ASSERT_EQUAL(0, pInterceptor
->getUnexpected());
197 CPPUNIT_PLUGIN_IMPLEMENT();
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */