1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski #include "sal/config.h"
25*b1cdbd2cSJim Jagielski
26*b1cdbd2cSJim Jagielski #include "boost/noncopyable.hpp"
27*b1cdbd2cSJim Jagielski #include "com/sun/star/awt/AsyncCallback.hpp"
28*b1cdbd2cSJim Jagielski #include "com/sun/star/awt/XCallback.hpp"
29*b1cdbd2cSJim Jagielski #include "com/sun/star/beans/PropertyState.hpp"
30*b1cdbd2cSJim Jagielski #include "com/sun/star/beans/PropertyValue.hpp"
31*b1cdbd2cSJim Jagielski #include "com/sun/star/document/MacroExecMode.hpp"
32*b1cdbd2cSJim Jagielski #include "com/sun/star/frame/DispatchResultEvent.hpp"
33*b1cdbd2cSJim Jagielski #include "com/sun/star/frame/DispatchResultState.hpp"
34*b1cdbd2cSJim Jagielski #include "com/sun/star/frame/XComponentLoader.hpp"
35*b1cdbd2cSJim Jagielski #include "com/sun/star/frame/XController.hpp"
36*b1cdbd2cSJim Jagielski #include "com/sun/star/frame/XDispatchProvider.hpp"
37*b1cdbd2cSJim Jagielski #include "com/sun/star/frame/XDispatchResultListener.hpp"
38*b1cdbd2cSJim Jagielski #include "com/sun/star/frame/XModel.hpp"
39*b1cdbd2cSJim Jagielski #include "com/sun/star/frame/XNotifyingDispatch.hpp"
40*b1cdbd2cSJim Jagielski #include "com/sun/star/lang/EventObject.hpp"
41*b1cdbd2cSJim Jagielski #include "com/sun/star/uno/Any.hxx"
42*b1cdbd2cSJim Jagielski #include "com/sun/star/uno/Reference.hxx"
43*b1cdbd2cSJim Jagielski #include "com/sun/star/uno/RuntimeException.hpp"
44*b1cdbd2cSJim Jagielski #include "com/sun/star/uno/Sequence.hxx"
45*b1cdbd2cSJim Jagielski #include "com/sun/star/util/URL.hpp"
46*b1cdbd2cSJim Jagielski #include <preextstl.h>
47*b1cdbd2cSJim Jagielski #include "cppuhelper/implbase1.hxx"
48*b1cdbd2cSJim Jagielski #include "cppunit/TestAssert.h"
49*b1cdbd2cSJim Jagielski #include "cppunit/TestFixture.h"
50*b1cdbd2cSJim Jagielski #include "cppunit/extensions/HelperMacros.h"
51*b1cdbd2cSJim Jagielski #include "cppunit/plugin/TestPlugIn.h"
52*b1cdbd2cSJim Jagielski #include <postextstl.h>
53*b1cdbd2cSJim Jagielski #include "osl/conditn.hxx"
54*b1cdbd2cSJim Jagielski #include "osl/diagnose.h"
55*b1cdbd2cSJim Jagielski #include "rtl/ustring.h"
56*b1cdbd2cSJim Jagielski #include "rtl/ustring.hxx"
57*b1cdbd2cSJim Jagielski #include "test/gettestargument.hxx"
58*b1cdbd2cSJim Jagielski #include "test/officeconnection.hxx"
59*b1cdbd2cSJim Jagielski #include "test/oustringostreaminserter.hxx"
60*b1cdbd2cSJim Jagielski #include "test/toabsolutefileurl.hxx"
61*b1cdbd2cSJim Jagielski
62*b1cdbd2cSJim Jagielski namespace {
63*b1cdbd2cSJim Jagielski
64*b1cdbd2cSJim Jagielski namespace css = com::sun::star;
65*b1cdbd2cSJim Jagielski
66*b1cdbd2cSJim Jagielski struct Result: private boost::noncopyable {
67*b1cdbd2cSJim Jagielski osl::Condition condition;
68*b1cdbd2cSJim Jagielski bool success;
69*b1cdbd2cSJim Jagielski rtl::OUString result;
70*b1cdbd2cSJim Jagielski };
71*b1cdbd2cSJim Jagielski
72*b1cdbd2cSJim Jagielski class Listener:
73*b1cdbd2cSJim Jagielski public cppu::WeakImplHelper1< css::frame::XDispatchResultListener >
74*b1cdbd2cSJim Jagielski {
75*b1cdbd2cSJim Jagielski public:
Listener(Result * result)76*b1cdbd2cSJim Jagielski Listener(Result * result): result_(result) { OSL_ASSERT(result != 0); }
77*b1cdbd2cSJim Jagielski
78*b1cdbd2cSJim Jagielski private:
disposing(css::lang::EventObject const &)79*b1cdbd2cSJim Jagielski virtual void SAL_CALL disposing(css::lang::EventObject const &)
80*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException) {}
81*b1cdbd2cSJim Jagielski
82*b1cdbd2cSJim Jagielski virtual void SAL_CALL dispatchFinished(
83*b1cdbd2cSJim Jagielski css::frame::DispatchResultEvent const & Result)
84*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException);
85*b1cdbd2cSJim Jagielski
86*b1cdbd2cSJim Jagielski Result * result_;
87*b1cdbd2cSJim Jagielski };
88*b1cdbd2cSJim Jagielski
dispatchFinished(css::frame::DispatchResultEvent const & Result)89*b1cdbd2cSJim Jagielski void Listener::dispatchFinished(css::frame::DispatchResultEvent const & Result)
90*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException)
91*b1cdbd2cSJim Jagielski {
92*b1cdbd2cSJim Jagielski result_->success =
93*b1cdbd2cSJim Jagielski (Result.State == css::frame::DispatchResultState::SUCCESS) &&
94*b1cdbd2cSJim Jagielski (Result.Result >>= result_->result);
95*b1cdbd2cSJim Jagielski result_->condition.set();
96*b1cdbd2cSJim Jagielski }
97*b1cdbd2cSJim Jagielski
98*b1cdbd2cSJim Jagielski class Callback: public cppu::WeakImplHelper1< css::awt::XCallback > {
99*b1cdbd2cSJim Jagielski public:
Callback(css::uno::Reference<css::frame::XNotifyingDispatch> const & dispatch,css::util::URL const & url,css::uno::Sequence<css::beans::PropertyValue> const & arguments,css::uno::Reference<css::frame::XDispatchResultListener> const & listener)100*b1cdbd2cSJim Jagielski Callback(
101*b1cdbd2cSJim Jagielski css::uno::Reference< css::frame::XNotifyingDispatch > const & dispatch,
102*b1cdbd2cSJim Jagielski css::util::URL const & url,
103*b1cdbd2cSJim Jagielski css::uno::Sequence< css::beans::PropertyValue > const & arguments,
104*b1cdbd2cSJim Jagielski css::uno::Reference< css::frame::XDispatchResultListener > const &
105*b1cdbd2cSJim Jagielski listener):
106*b1cdbd2cSJim Jagielski dispatch_(dispatch), url_(url), arguments_(arguments),
107*b1cdbd2cSJim Jagielski listener_(listener)
108*b1cdbd2cSJim Jagielski { OSL_ASSERT(dispatch.is()); }
109*b1cdbd2cSJim Jagielski
110*b1cdbd2cSJim Jagielski private:
notify(css::uno::Any const &)111*b1cdbd2cSJim Jagielski virtual void SAL_CALL notify(css::uno::Any const &)
112*b1cdbd2cSJim Jagielski throw (css::uno::RuntimeException)
113*b1cdbd2cSJim Jagielski { dispatch_->dispatchWithNotification(url_, arguments_, listener_); }
114*b1cdbd2cSJim Jagielski
115*b1cdbd2cSJim Jagielski css::uno::Reference< css::frame::XNotifyingDispatch > dispatch_;
116*b1cdbd2cSJim Jagielski css::util::URL url_;
117*b1cdbd2cSJim Jagielski css::uno::Sequence< css::beans::PropertyValue > arguments_;
118*b1cdbd2cSJim Jagielski css::uno::Reference< css::frame::XDispatchResultListener > listener_;
119*b1cdbd2cSJim Jagielski };
120*b1cdbd2cSJim Jagielski
121*b1cdbd2cSJim Jagielski class Test: public CppUnit::TestFixture {
122*b1cdbd2cSJim Jagielski public:
123*b1cdbd2cSJim Jagielski virtual void setUp();
124*b1cdbd2cSJim Jagielski
125*b1cdbd2cSJim Jagielski virtual void tearDown();
126*b1cdbd2cSJim Jagielski
127*b1cdbd2cSJim Jagielski private:
128*b1cdbd2cSJim Jagielski CPPUNIT_TEST_SUITE(Test);
129*b1cdbd2cSJim Jagielski CPPUNIT_TEST(test);
130*b1cdbd2cSJim Jagielski CPPUNIT_TEST_SUITE_END();
131*b1cdbd2cSJim Jagielski
132*b1cdbd2cSJim Jagielski void test();
133*b1cdbd2cSJim Jagielski
134*b1cdbd2cSJim Jagielski test::OfficeConnection connection_;
135*b1cdbd2cSJim Jagielski };
136*b1cdbd2cSJim Jagielski
setUp()137*b1cdbd2cSJim Jagielski void Test::setUp() {
138*b1cdbd2cSJim Jagielski connection_.setUp();
139*b1cdbd2cSJim Jagielski }
140*b1cdbd2cSJim Jagielski
tearDown()141*b1cdbd2cSJim Jagielski void Test::tearDown() {
142*b1cdbd2cSJim Jagielski connection_.tearDown();
143*b1cdbd2cSJim Jagielski }
144*b1cdbd2cSJim Jagielski
test()145*b1cdbd2cSJim Jagielski void Test::test() {
146*b1cdbd2cSJim Jagielski rtl::OUString doc;
147*b1cdbd2cSJim Jagielski CPPUNIT_ASSERT(
148*b1cdbd2cSJim Jagielski test::getTestArgument(
149*b1cdbd2cSJim Jagielski rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("smoketest.doc")), &doc));
150*b1cdbd2cSJim Jagielski css::uno::Sequence< css::beans::PropertyValue > args(2);
151*b1cdbd2cSJim Jagielski args[0].Name = rtl::OUString(
152*b1cdbd2cSJim Jagielski RTL_CONSTASCII_USTRINGPARAM("MacroExecutionMode"));
153*b1cdbd2cSJim Jagielski args[0].Handle = -1;
154*b1cdbd2cSJim Jagielski args[0].Value <<=
155*b1cdbd2cSJim Jagielski com::sun::star::document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
156*b1cdbd2cSJim Jagielski args[0].State = css::beans::PropertyState_DIRECT_VALUE;
157*b1cdbd2cSJim Jagielski args[1].Name = rtl::OUString(
158*b1cdbd2cSJim Jagielski RTL_CONSTASCII_USTRINGPARAM("ReadOnly"));
159*b1cdbd2cSJim Jagielski args[1].Handle = -1;
160*b1cdbd2cSJim Jagielski args[1].Value <<= sal_True;
161*b1cdbd2cSJim Jagielski args[1].State = css::beans::PropertyState_DIRECT_VALUE;
162*b1cdbd2cSJim Jagielski css::util::URL url;
163*b1cdbd2cSJim Jagielski url.Complete = rtl::OUString(
164*b1cdbd2cSJim Jagielski RTL_CONSTASCII_USTRINGPARAM(
165*b1cdbd2cSJim Jagielski "vnd.sun.star.script:Standard.Global.StartTestWithDefaultOptions?"
166*b1cdbd2cSJim Jagielski "language=Basic&location=document"));
167*b1cdbd2cSJim Jagielski css::uno::Reference< css::frame::XNotifyingDispatch > disp(
168*b1cdbd2cSJim Jagielski css::uno::Reference< css::frame::XDispatchProvider >(
169*b1cdbd2cSJim Jagielski css::uno::Reference< css::frame::XController >(
170*b1cdbd2cSJim Jagielski css::uno::Reference< css::frame::XModel >(
171*b1cdbd2cSJim Jagielski css::uno::Reference< css::frame::XComponentLoader >(
172*b1cdbd2cSJim Jagielski (connection_.getComponentContext()->
173*b1cdbd2cSJim Jagielski getServiceManager()->createInstanceWithContext(
174*b1cdbd2cSJim Jagielski rtl::OUString(
175*b1cdbd2cSJim Jagielski RTL_CONSTASCII_USTRINGPARAM(
176*b1cdbd2cSJim Jagielski "com.sun.star.frame.Desktop")),
177*b1cdbd2cSJim Jagielski connection_.getComponentContext())),
178*b1cdbd2cSJim Jagielski css::uno::UNO_QUERY_THROW)->loadComponentFromURL(
179*b1cdbd2cSJim Jagielski test::toAbsoluteFileUrl(doc),
180*b1cdbd2cSJim Jagielski rtl::OUString(
181*b1cdbd2cSJim Jagielski RTL_CONSTASCII_USTRINGPARAM("_default")),
182*b1cdbd2cSJim Jagielski 0, args),
183*b1cdbd2cSJim Jagielski css::uno::UNO_QUERY_THROW)->getCurrentController(),
184*b1cdbd2cSJim Jagielski css::uno::UNO_SET_THROW)->getFrame(),
185*b1cdbd2cSJim Jagielski css::uno::UNO_QUERY_THROW)->queryDispatch(
186*b1cdbd2cSJim Jagielski url, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_self")), 0),
187*b1cdbd2cSJim Jagielski css::uno::UNO_QUERY_THROW);
188*b1cdbd2cSJim Jagielski Result result;
189*b1cdbd2cSJim Jagielski // Shifted to main thread to work around potential deadlocks (i112867):
190*b1cdbd2cSJim Jagielski com::sun::star::awt::AsyncCallback::create(
191*b1cdbd2cSJim Jagielski connection_.getComponentContext())->addCallback(
192*b1cdbd2cSJim Jagielski new Callback(
193*b1cdbd2cSJim Jagielski disp, url, css::uno::Sequence< css::beans::PropertyValue >(),
194*b1cdbd2cSJim Jagielski new Listener(&result)),
195*b1cdbd2cSJim Jagielski css::uno::Any());
196*b1cdbd2cSJim Jagielski result.condition.wait();
197*b1cdbd2cSJim Jagielski CPPUNIT_ASSERT(result.success);
198*b1cdbd2cSJim Jagielski CPPUNIT_ASSERT_EQUAL(rtl::OUString(), result.result);
199*b1cdbd2cSJim Jagielski }
200*b1cdbd2cSJim Jagielski
201*b1cdbd2cSJim Jagielski CPPUNIT_TEST_SUITE_REGISTRATION(Test);
202*b1cdbd2cSJim Jagielski
203*b1cdbd2cSJim Jagielski }
204*b1cdbd2cSJim Jagielski
205*b1cdbd2cSJim Jagielski CPPUNIT_PLUGIN_IMPLEMENT();
206