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