Debian Codegraph @

framework/qa/cppunit/dispatchtest.cxx

raw
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 <cppuhelper/implbase.hxx>
11 #include <test/unoapi_test.hxx>
12
13 #include <com/sun/star/frame/XDispatchProviderInterceptor.hpp>
14 #include <com/sun/star/frame/XInterceptorInfo.hpp>
15 #include <com/sun/star/util/URLTransformer.hpp>
16
17 #include <rtl/ref.hxx>
18 #include <mutex>
19
20 using namespace ::com::sun::star;
21
22 namespace
23 {
24 /// Sample interception implementation that asserts getInterceptedURLs() and queryDispatch() is in sync.
25 class MyInterceptor
26 : public cppu::WeakImplHelper<frame::XDispatchProviderInterceptor, frame::XInterceptorInfo>
27 {
28 std::mutex maMutex;
29 uno::Reference<frame::XDispatchProvider> m_xMaster;
30 uno::Reference<frame::XDispatchProvider> m_xSlave;
31 uno::Sequence<OUString> m_aDisabledCommands;
32 int m_nExpected;
33 int m_nUnexpected;
34
35 public:
36 MyInterceptor();
37
38 /// Number of queryDispatch() calls that operate on a command advertised by getInterceptedURLs().
39 int getExpected();
40 /// Number of queryDispatch() calls that operate on a command not advertised by getInterceptedURLs().
41 int getUnexpected();
42
43 // frame::XInterceptorInfo
44 virtual uno::Sequence<OUString> SAL_CALL getInterceptedURLs() override;
45
46 // frame::XDispatchProviderInterceptor
47 virtual void SAL_CALL setMasterDispatchProvider(
48 const uno::Reference<frame::XDispatchProvider>& xNewSupplier) override;
49 virtual uno::Reference<frame::XDispatchProvider> SAL_CALL getMasterDispatchProvider() override;
50 virtual void SAL_CALL
51 setSlaveDispatchProvider(const uno::Reference<frame::XDispatchProvider>& xNewSupplier) override;
52 virtual uno::Reference<frame::XDispatchProvider> SAL_CALL getSlaveDispatchProvider() override;
53
54 // frame::XDispatchProvider
55 virtual uno::Sequence<uno::Reference<frame::XDispatch>> SAL_CALL
56 queryDispatches(const uno::Sequence<frame::DispatchDescriptor>& rRequests) override;
57 virtual uno::Reference<frame::XDispatch>
58 SAL_CALL queryDispatch(const util::URL& rURL, const OUString& rTargetFrameName,
59 sal_Int32 SearchFlags) override;
60 };
61
62 MyInterceptor::MyInterceptor()
63 : m_aDisabledCommands{ u".uno:Bold"_ustr }
64 , m_nExpected(0)
65 , m_nUnexpected(0)
66 {
67 }
68
69 int MyInterceptor::getExpected()
70 {
71 std::unique_lock aGuard(maMutex);
72 int nRet = m_nExpected;
73 m_nExpected = 0;
74 return nRet;
75 }
76
77 int MyInterceptor::getUnexpected()
78 {
79 std::unique_lock aGuard(maMutex);
80 int nRet = m_nUnexpected;
81 m_nUnexpected = 0;
82 return nRet;
83 }
84
85 uno::Sequence<OUString> MyInterceptor::getInterceptedURLs() { return m_aDisabledCommands; }
86
87 void MyInterceptor::setMasterDispatchProvider(
88 const uno::Reference<frame::XDispatchProvider>& xNewSupplier)
89 {
90 std::unique_lock aGuard(maMutex);
91 m_xMaster = xNewSupplier;
92 }
93
94 uno::Reference<frame::XDispatchProvider> MyInterceptor::getMasterDispatchProvider()
95 {
96 std::unique_lock aGuard(maMutex);
97 return m_xMaster;
98 }
99
100 void MyInterceptor::setSlaveDispatchProvider(
101 const uno::Reference<frame::XDispatchProvider>& xNewSupplier)
102 {
103 std::unique_lock aGuard(maMutex);
104 m_xSlave = xNewSupplier;
105 }
106
107 uno::Reference<frame::XDispatchProvider> MyInterceptor::getSlaveDispatchProvider()
108 {
109 std::unique_lock aGuard(maMutex);
110 return m_xSlave;
111 }
112
113 uno::Sequence<uno::Reference<frame::XDispatch>>
114 MyInterceptor::queryDispatches(const uno::Sequence<frame::DispatchDescriptor>& rRequests)
115 {
116 uno::Sequence<uno::Reference<frame::XDispatch>> aResult(rRequests.getLength());
117 auto aResultRange = asNonConstRange(aResult);
118
119 for (sal_Int32 i = 0; i < rRequests.getLength(); ++i)
120 {
121 aResultRange[i] = queryDispatch(rRequests[i].FeatureURL, rRequests[i].FrameName,
122 rRequests[i].SearchFlags);
123 }
124
125 return aResult;
126 }
127
128 uno::Reference<frame::XDispatch> MyInterceptor::queryDispatch(const util::URL& rURL,
129 const OUString& /*rTargetFrameName*/,
130 sal_Int32 /*SearchFlags*/)
131 {
132 std::unique_lock aGuard(maMutex);
133 if (std::find(std::cbegin(m_aDisabledCommands), std::cend(m_aDisabledCommands), rURL.Complete)
134 != std::cend(m_aDisabledCommands))
135 ++m_nExpected;
136 else
137 ++m_nUnexpected;
138
139 return uno::Reference<frame::XDispatch>();
140 }
141
142 /// Tests how InterceptionHelper invokes a registered interceptor.
143 class DispatchTest : public UnoApiTest
144 {
145 public:
146 DispatchTest()
147 : UnoApiTest(u"/framework/qa/cppunit/data/"_ustr)
148 {
149 }
150 };
151
152 CPPUNIT_TEST_FIXTURE(DispatchTest, testInterception)
153 {
154 loadFromURL(u"private:factory/swriter"_ustr);
155 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
156 CPPUNIT_ASSERT(xModel.is());
157
158 uno::Reference<frame::XDispatchProviderInterception> xRegistration(
159 xModel->getCurrentController()->getFrame(), uno::UNO_QUERY);
160 CPPUNIT_ASSERT(xRegistration.is());
161
162 rtl::Reference<MyInterceptor> pInterceptor(new MyInterceptor());
163 xRegistration->registerDispatchProviderInterceptor(pInterceptor);
164
165 dispatchCommand(mxComponent, u".uno:Bold"_ustr, {});
166 CPPUNIT_ASSERT_GREATER(0, pInterceptor->getExpected());
167 CPPUNIT_ASSERT_EQUAL(0, pInterceptor->getUnexpected());
168 dispatchCommand(mxComponent, u".uno:Italic"_ustr, {});
169 // This was 1: MyInterceptor::queryDispatch() was called for .uno:Italic.
170 CPPUNIT_ASSERT_EQUAL(0, pInterceptor->getUnexpected());
171 }
172
173 CPPUNIT_TEST_FIXTURE(DispatchTest, testSfxOfficeDispatchDispose)
174 {
175 // this test doesn't work with a new document because of aURL.Main check in SfxBaseController::dispatch()
176 loadFromFile(u"empty.fodp");
177 uno::Reference<frame::XModel> xModel(mxComponent, uno::UNO_QUERY);
178 CPPUNIT_ASSERT(xModel.is());
179 uno::Reference<frame::XController> xController(xModel->getCurrentController());
180 CPPUNIT_ASSERT(xController.is());
181 uno::Reference<frame::XDispatchProvider> xFrame(xController->getFrame(), uno::UNO_QUERY);
182 CPPUNIT_ASSERT(xFrame.is());
183
184 uno::Reference<util::XURLTransformer> xParser(util::URLTransformer::create(m_xContext));
185 util::URL url;
186 url.Complete = xModel->getURL() + "#dummy";
187 xParser->parseStrict(url);
188
189 uno::Reference<frame::XDispatch> xDisp(xFrame->queryDispatch(url, u""_ustr, 0));
190 CPPUNIT_ASSERT(xDisp.is());
191
192 mxComponent->dispose();
193
194 util::URL urlSlot;
195 urlSlot.Complete = "slot:5598";
196 xParser->parseStrict(urlSlot);
197 // crashed with UAF
198 xDisp->dispatch(urlSlot, {});
199 }
200 }
201
202 CPPUNIT_PLUGIN_IMPLEMENT();
203
204 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */