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