Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / smoketest / smoketest.cxx
blob5845c332997ca6a4d28ce8849dd1063070c5b73d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/types.h>
21 #include <chrono>
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/Desktop.hpp"
28 #include "com/sun/star/frame/DispatchResultEvent.hpp"
29 #include "com/sun/star/frame/DispatchResultState.hpp"
30 #include "com/sun/star/frame/XComponentLoader.hpp"
31 #include "com/sun/star/frame/XController.hpp"
32 #include "com/sun/star/frame/XDispatchProvider.hpp"
33 #include "com/sun/star/frame/XDispatchResultListener.hpp"
34 #include "com/sun/star/frame/XModel.hpp"
35 #include "com/sun/star/frame/XNotifyingDispatch.hpp"
36 #include "com/sun/star/lang/EventObject.hpp"
37 #include "com/sun/star/uno/Any.hxx"
38 #include "com/sun/star/uno/Reference.hxx"
39 #include "com/sun/star/uno/RuntimeException.hpp"
40 #include "com/sun/star/uno/Sequence.hxx"
41 #include "com/sun/star/util/URL.hpp"
42 #include <cppuhelper/implbase.hxx>
43 #include "cppunit/TestAssert.h"
44 #include "cppunit/TestFixture.h"
45 #include "cppunit/extensions/HelperMacros.h"
46 #include "cppunit/plugin/TestPlugIn.h"
47 #include "osl/conditn.hxx"
48 #include "osl/diagnose.h"
49 #include "osl/time.h"
50 #include "rtl/ustring.h"
51 #include "rtl/ustring.hxx"
52 #include "unotest/gettestargument.hxx"
53 #include "unotest/officeconnection.hxx"
54 #include "unotest/toabsolutefileurl.hxx"
56 namespace {
58 struct Result {
59 osl::Condition condition;
60 bool success;
61 OUString result;
62 Result()
63 : success(false)
66 Result(const Result&) = delete;
67 Result& operator=(const Result&) = delete;
70 class Listener:
71 public cppu::WeakImplHelper< css::frame::XDispatchResultListener >
73 public:
74 explicit Listener(Result * result): result_(result) { OSL_ASSERT(result != nullptr); }
76 private:
77 virtual void SAL_CALL disposing(css::lang::EventObject const &)
78 throw (css::uno::RuntimeException, std::exception) override {}
80 virtual void SAL_CALL dispatchFinished(
81 css::frame::DispatchResultEvent const & Result)
82 throw (css::uno::RuntimeException, std::exception) override;
84 Result * result_;
87 void Listener::dispatchFinished(css::frame::DispatchResultEvent const & Result)
88 throw (css::uno::RuntimeException, std::exception)
90 result_->success =
91 (Result.State == css::frame::DispatchResultState::SUCCESS) &&
92 (Result.Result >>= result_->result);
93 result_->condition.set();
96 class Callback: public cppu::WeakImplHelper< css::awt::XCallback > {
97 public:
98 Callback(
99 css::uno::Reference< css::frame::XNotifyingDispatch > const & dispatch,
100 css::util::URL const & url,
101 css::uno::Sequence< css::beans::PropertyValue > const & arguments,
102 css::uno::Reference< css::frame::XDispatchResultListener > const &
103 listener):
104 dispatch_(dispatch), url_(url), arguments_(arguments),
105 listener_(listener)
106 { OSL_ASSERT(dispatch.is()); }
108 private:
109 virtual void SAL_CALL notify(css::uno::Any const &)
110 throw (css::uno::RuntimeException, std::exception) override
111 { dispatch_->dispatchWithNotification(url_, arguments_, listener_); }
113 css::uno::Reference< css::frame::XNotifyingDispatch > dispatch_;
114 css::util::URL url_;
115 css::uno::Sequence< css::beans::PropertyValue > arguments_;
116 css::uno::Reference< css::frame::XDispatchResultListener > listener_;
119 class Test: public CppUnit::TestFixture {
120 public:
121 virtual void setUp() override;
123 virtual void tearDown() override;
125 private:
126 CPPUNIT_TEST_SUITE(Test);
127 CPPUNIT_TEST(test);
128 CPPUNIT_TEST_SUITE_END();
130 void test();
132 test::OfficeConnection connection_;
135 void Test::setUp() {
136 connection_.setUp();
139 void Test::tearDown() {
140 connection_.tearDown();
143 void Test::test() {
144 OUString doc;
145 CPPUNIT_ASSERT(
146 test::getTestArgument(
147 "smoketest.doc", &doc));
148 css::uno::Sequence< css::beans::PropertyValue > args(2);
149 args[0].Name = "MacroExecutionMode";
150 args[0].Handle = -1;
151 args[0].Value <<= css::document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN;
152 args[0].State = css::beans::PropertyState_DIRECT_VALUE;
153 args[1].Name = "ReadOnly";
154 args[1].Handle = -1;
155 args[1].Value <<= true;
156 args[1].State = css::beans::PropertyState_DIRECT_VALUE;
157 css::util::URL url;
158 url.Complete = "vnd.sun.star.script:Standard.Global.StartTestWithDefaultOptions?"
159 "language=Basic&location=document";
161 css::uno::Reference< css::frame::XDesktop2 > xDesktop = css::frame::Desktop::create(connection_.getComponentContext());
163 css::uno::Reference< css::frame::XNotifyingDispatch > disp(
164 css::uno::Reference< css::frame::XDispatchProvider >(
165 css::uno::Reference< css::frame::XController >(
166 css::uno::Reference< css::frame::XModel >(
167 xDesktop->loadComponentFromURL(
168 test::toAbsoluteFileUrl(doc),
169 "_default",
170 0, args),
171 css::uno::UNO_QUERY_THROW)->getCurrentController(),
172 css::uno::UNO_SET_THROW)->getFrame(),
173 css::uno::UNO_QUERY_THROW)->queryDispatch(
174 url, "_self", 0),
175 css::uno::UNO_QUERY_THROW);
176 Result result;
177 // Shifted to main thread to work around potential deadlocks (i112867):
178 css::awt::AsyncCallback::create(
179 connection_.getComponentContext())->addCallback(
180 new Callback(
181 disp, url, css::uno::Sequence< css::beans::PropertyValue >(),
182 new Listener(&result)),
183 css::uno::Any());
184 // Wait for result.condition or connection_ going stale:
185 for (;;) {
186 osl::Condition::Result res = result.condition.wait(std::chrono::seconds(1)); // 1 sec delay
187 if (res == osl::Condition::result_ok) {
188 break;
190 CPPUNIT_ASSERT_EQUAL(osl::Condition::result_timeout, res);
191 CPPUNIT_ASSERT(connection_.isStillAlive());
193 CPPUNIT_ASSERT(result.success);
194 CPPUNIT_ASSERT_EQUAL(OUString(), result.result);
197 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
201 CPPUNIT_PLUGIN_IMPLEMENT();
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */