Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / framework / qa / cppunit / dispatchtest.cxx
blob7903715e4113f086e660af31850baca8134eb1f9
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 <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;
24 namespace
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;
37 int m_nExpected;
38 int m_nUnexpected;
40 public:
41 MyInterceptor();
43 /// Number of queryDispatch() calls that operate on a command advertised by getInterceptedURLs().
44 int getExpected();
45 /// Number of queryDispatch() calls that operate on a command not advertised by getInterceptedURLs().
46 int getUnexpected();
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"},
64 m_nExpected(0),
65 m_nUnexpected(0)
69 int MyInterceptor::getExpected()
71 int nRet = m_nExpected;
72 m_nExpected = 0;
73 return nRet;
76 int MyInterceptor::getUnexpected()
78 int nRet = m_nUnexpected;
79 m_nUnexpected = 0;
80 return nRet;
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()
95 return m_xMaster;
98 void MyInterceptor::setSlaveDispatchProvider(const uno::Reference<frame::XDispatchProvider>& xNewSupplier)
100 m_xSlave = xNewSupplier;
103 uno::Reference<frame::XDispatchProvider> MyInterceptor::getSlaveDispatchProvider()
105 return m_xSlave;
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);
117 return aResult;
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())
123 ++m_nExpected;
124 else
125 ++m_nUnexpected;
127 return uno::Reference<frame::XDispatch>();
130 /// Tests how InterceptionHelper invokes a registered interceptor.
131 class DispatchTest : public test::BootstrapFixture, public unotest::MacrosTest
133 protected:
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);
138 public:
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: */