1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include "sal/config.h"
30 #include "sal/precppunit.hxx"
32 #include "boost/noncopyable.hpp"
33 #include "com/sun/star/awt/AsyncCallback.hpp"
34 #include "com/sun/star/awt/XCallback.hpp"
35 #include "com/sun/star/beans/PropertyState.hpp"
36 #include "com/sun/star/beans/PropertyValue.hpp"
37 #include "com/sun/star/document/MacroExecMode.hpp"
38 #include "com/sun/star/frame/DispatchResultEvent.hpp"
39 #include "com/sun/star/frame/DispatchResultState.hpp"
40 #include "com/sun/star/frame/XComponentLoader.hpp"
41 #include "com/sun/star/frame/XController.hpp"
42 #include "com/sun/star/frame/XDispatchProvider.hpp"
43 #include "com/sun/star/frame/XDispatchResultListener.hpp"
44 #include "com/sun/star/frame/XModel.hpp"
45 #include "com/sun/star/frame/XNotifyingDispatch.hpp"
46 #include "com/sun/star/lang/EventObject.hpp"
47 #include "com/sun/star/uno/Any.hxx"
48 #include "com/sun/star/uno/Reference.hxx"
49 #include "com/sun/star/uno/RuntimeException.hpp"
50 #include "com/sun/star/uno/Sequence.hxx"
51 #include "com/sun/star/util/URL.hpp"
52 #include "cppuhelper/implbase1.hxx"
53 #include "cppunit/TestAssert.h"
54 #include "cppunit/TestFixture.h"
55 #include "cppunit/extensions/HelperMacros.h"
56 #include "cppunit/plugin/TestPlugIn.h"
57 #include "osl/conditn.hxx"
58 #include "osl/diagnose.h"
60 #include "rtl/oustringostreaminserter.hxx"
61 #include "rtl/ustring.h"
62 #include "rtl/ustring.hxx"
63 #include "unotest/gettestargument.hxx"
64 #include "unotest/officeconnection.hxx"
65 #include "unotest/toabsolutefileurl.hxx"
69 namespace css
= com::sun::star
;
71 struct Result
: private boost::noncopyable
{
72 osl::Condition condition
;
78 public cppu::WeakImplHelper1
< css::frame::XDispatchResultListener
>
81 Listener(Result
* result
): result_(result
) { OSL_ASSERT(result
!= 0); }
84 virtual void SAL_CALL
disposing(css::lang::EventObject
const &)
85 throw (css::uno::RuntimeException
) {}
87 virtual void SAL_CALL
dispatchFinished(
88 css::frame::DispatchResultEvent
const & Result
)
89 throw (css::uno::RuntimeException
);
94 void Listener::dispatchFinished(css::frame::DispatchResultEvent
const & Result
)
95 throw (css::uno::RuntimeException
)
98 (Result
.State
== css::frame::DispatchResultState::SUCCESS
) &&
99 (Result
.Result
>>= result_
->result
);
100 result_
->condition
.set();
103 class Callback
: public cppu::WeakImplHelper1
< css::awt::XCallback
> {
106 css::uno::Reference
< css::frame::XNotifyingDispatch
> const & dispatch
,
107 css::util::URL
const & url
,
108 css::uno::Sequence
< css::beans::PropertyValue
> const & arguments
,
109 css::uno::Reference
< css::frame::XDispatchResultListener
> const &
111 dispatch_(dispatch
), url_(url
), arguments_(arguments
),
113 { OSL_ASSERT(dispatch
.is()); }
116 virtual void SAL_CALL
notify(css::uno::Any
const &)
117 throw (css::uno::RuntimeException
)
118 { dispatch_
->dispatchWithNotification(url_
, arguments_
, listener_
); }
120 css::uno::Reference
< css::frame::XNotifyingDispatch
> dispatch_
;
122 css::uno::Sequence
< css::beans::PropertyValue
> arguments_
;
123 css::uno::Reference
< css::frame::XDispatchResultListener
> listener_
;
126 class Test
: public CppUnit::TestFixture
{
128 virtual void setUp();
130 virtual void tearDown();
133 CPPUNIT_TEST_SUITE(Test
);
135 CPPUNIT_TEST_SUITE_END();
139 test::OfficeConnection connection_
;
146 void Test::tearDown() {
147 connection_
.tearDown();
153 test::getTestArgument(
154 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("smoketest.doc")), &doc
));
155 css::uno::Sequence
< css::beans::PropertyValue
> args(2);
156 args
[0].Name
= rtl::OUString(
157 RTL_CONSTASCII_USTRINGPARAM("MacroExecutionMode"));
160 com::sun::star::document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN
;
161 args
[0].State
= css::beans::PropertyState_DIRECT_VALUE
;
162 args
[1].Name
= rtl::OUString(
163 RTL_CONSTASCII_USTRINGPARAM("ReadOnly"));
165 args
[1].Value
<<= sal_True
;
166 args
[1].State
= css::beans::PropertyState_DIRECT_VALUE
;
168 url
.Complete
= rtl::OUString(
169 RTL_CONSTASCII_USTRINGPARAM(
170 "vnd.sun.star.script:Standard.Global.StartTestWithDefaultOptions?"
171 "language=Basic&location=document"));
172 css::uno::Reference
< css::frame::XNotifyingDispatch
> disp(
173 css::uno::Reference
< css::frame::XDispatchProvider
>(
174 css::uno::Reference
< css::frame::XController
>(
175 css::uno::Reference
< css::frame::XModel
>(
176 css::uno::Reference
< css::frame::XComponentLoader
>(
177 (connection_
.getComponentContext()->
178 getServiceManager()->createInstanceWithContext(
180 RTL_CONSTASCII_USTRINGPARAM(
181 "com.sun.star.frame.Desktop")),
182 connection_
.getComponentContext())),
183 css::uno::UNO_QUERY_THROW
)->loadComponentFromURL(
184 test::toAbsoluteFileUrl(doc
),
186 RTL_CONSTASCII_USTRINGPARAM("_default")),
188 css::uno::UNO_QUERY_THROW
)->getCurrentController(),
189 css::uno::UNO_SET_THROW
)->getFrame(),
190 css::uno::UNO_QUERY_THROW
)->queryDispatch(
191 url
, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("_self")), 0),
192 css::uno::UNO_QUERY_THROW
);
194 // Shifted to main thread to work around potential deadlocks (i112867):
195 com::sun::star::awt::AsyncCallback::create(
196 connection_
.getComponentContext())->addCallback(
198 disp
, url
, css::uno::Sequence
< css::beans::PropertyValue
>(),
199 new Listener(&result
)),
201 // Wait for result.condition or connection_ going stale:
203 TimeValue delay
= { 1, 0 }; // 1 sec
204 osl::Condition::Result res
= result
.condition
.wait(&delay
);
205 if (res
== osl::Condition::result_ok
) {
208 CPPUNIT_ASSERT_EQUAL(osl::Condition::result_timeout
, res
);
209 CPPUNIT_ASSERT(connection_
.isStillAlive());
211 CPPUNIT_ASSERT(result
.success
);
212 CPPUNIT_ASSERT_EQUAL(rtl::OUString(), result
.result
);
215 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
219 CPPUNIT_PLUGIN_IMPLEMENT();
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */