1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include "sal/config.h"
30 #include "boost/noncopyable.hpp"
31 #include "com/sun/star/awt/AsyncCallback.hpp"
32 #include "com/sun/star/awt/XCallback.hpp"
33 #include "com/sun/star/beans/PropertyState.hpp"
34 #include "com/sun/star/beans/PropertyValue.hpp"
35 #include "com/sun/star/document/MacroExecMode.hpp"
36 #include "com/sun/star/frame/DispatchResultEvent.hpp"
37 #include "com/sun/star/frame/DispatchResultState.hpp"
38 #include "com/sun/star/frame/XComponentLoader.hpp"
39 #include "com/sun/star/frame/XController.hpp"
40 #include "com/sun/star/frame/XDispatchProvider.hpp"
41 #include "com/sun/star/frame/XDispatchResultListener.hpp"
42 #include "com/sun/star/frame/XModel.hpp"
43 #include "com/sun/star/frame/XNotifyingDispatch.hpp"
44 #include "com/sun/star/lang/EventObject.hpp"
45 #include "com/sun/star/uno/Any.hxx"
46 #include "com/sun/star/uno/Reference.hxx"
47 #include "com/sun/star/uno/RuntimeException.hpp"
48 #include "com/sun/star/uno/Sequence.hxx"
49 #include "com/sun/star/util/URL.hpp"
50 #include <preextstl.h>
51 #include "cppuhelper/implbase1.hxx"
52 #include "cppunit/TestAssert.h"
53 #include "cppunit/TestFixture.h"
54 #include "cppunit/extensions/HelperMacros.h"
55 #include "cppunit/plugin/TestPlugIn.h"
56 #include <postextstl.h>
57 #include "osl/conditn.hxx"
58 #include "osl/diagnose.h"
59 #include "rtl/ustring.h"
60 #include "rtl/ustring.hxx"
61 #include "test/gettestargument.hxx"
62 #include "test/officeconnection.hxx"
63 #include "test/oustringostreaminserter.hxx"
64 #include "test/toabsolutefileurl.hxx"
68 namespace css
= com::sun::star
;
70 struct Result
: private boost::noncopyable
{
71 osl::Condition condition
;
77 public cppu::WeakImplHelper1
< css::frame::XDispatchResultListener
>
80 Listener(Result
* result
): result_(result
) { OSL_ASSERT(result
!= 0); }
83 virtual void SAL_CALL
disposing(css::lang::EventObject
const &)
84 throw (css::uno::RuntimeException
) {}
86 virtual void SAL_CALL
dispatchFinished(
87 css::frame::DispatchResultEvent
const & Result
)
88 throw (css::uno::RuntimeException
);
93 void Listener::dispatchFinished(css::frame::DispatchResultEvent
const & Result
)
94 throw (css::uno::RuntimeException
)
97 (Result
.State
== css::frame::DispatchResultState::SUCCESS
) &&
98 (Result
.Result
>>= result_
->result
);
99 result_
->condition
.set();
102 class Callback
: public cppu::WeakImplHelper1
< css::awt::XCallback
> {
105 css::uno::Reference
< css::frame::XNotifyingDispatch
> const & dispatch
,
106 css::util::URL
const & url
,
107 css::uno::Sequence
< css::beans::PropertyValue
> const & arguments
,
108 css::uno::Reference
< css::frame::XDispatchResultListener
> const &
110 dispatch_(dispatch
), url_(url
), arguments_(arguments
),
112 { OSL_ASSERT(dispatch
.is()); }
115 virtual void SAL_CALL
notify(css::uno::Any
const &)
116 throw (css::uno::RuntimeException
)
117 { dispatch_
->dispatchWithNotification(url_
, arguments_
, listener_
); }
119 css::uno::Reference
< css::frame::XNotifyingDispatch
> dispatch_
;
121 css::uno::Sequence
< css::beans::PropertyValue
> arguments_
;
122 css::uno::Reference
< css::frame::XDispatchResultListener
> listener_
;
125 class Test
: public CppUnit::TestFixture
{
127 virtual void setUp();
129 virtual void tearDown();
132 CPPUNIT_TEST_SUITE(Test
);
134 CPPUNIT_TEST_SUITE_END();
138 test::OfficeConnection connection_
;
145 void Test::tearDown() {
146 connection_
.tearDown();
152 test::getTestArgument(
153 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("smoketest.doc")), &doc
));
154 css::uno::Sequence
< css::beans::PropertyValue
> args(1);
155 args
[0].Name
= rtl::OUString(
156 RTL_CONSTASCII_USTRINGPARAM("MacroExecutionMode"));
159 com::sun::star::document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN
;
160 args
[0].State
= css::beans::PropertyState_DIRECT_VALUE
;
162 url
.Complete
= rtl::OUString(
163 RTL_CONSTASCII_USTRINGPARAM(
164 "vnd.sun.star.script:Standard.Global.StartTestWithDefaultOptions?"
165 "language=Basic&location=document"));
166 css::uno::Reference
< css::frame::XNotifyingDispatch
> disp(
167 css::uno::Reference
< css::frame::XDispatchProvider
>(
168 css::uno::Reference
< css::frame::XController
>(
169 css::uno::Reference
< css::frame::XModel
>(
170 css::uno::Reference
< css::frame::XComponentLoader
>(
171 (connection_
.getComponentContext()->
172 getServiceManager()->createInstanceWithContext(
174 RTL_CONSTASCII_USTRINGPARAM(
175 "com.sun.star.frame.Desktop")),
176 connection_
.getComponentContext())),
177 css::uno::UNO_QUERY_THROW
)->loadComponentFromURL(
178 test::toAbsoluteFileUrl(doc
),
180 RTL_CONSTASCII_USTRINGPARAM("_default")),
182 css::uno::UNO_QUERY_THROW
)->getCurrentController(),
183 css::uno::UNO_SET_THROW
)->getFrame(),
184 css::uno::UNO_QUERY_THROW
)->queryDispatch(
185 url
, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_self")), 0),
186 css::uno::UNO_QUERY_THROW
);
188 // Shifted to main thread to work around potential deadlocks (i112867):
189 com::sun::star::awt::AsyncCallback::create(
190 connection_
.getComponentContext())->addCallback(
192 disp
, url
, css::uno::Sequence
< css::beans::PropertyValue
>(),
193 new Listener(&result
)),
195 result
.condition
.wait();
196 CPPUNIT_ASSERT(result
.success
);
197 CPPUNIT_ASSERT_EQUAL(rtl::OUString(), result
.result
);
200 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
204 CPPUNIT_PLUGIN_IMPLEMENT();