Update git submodules
[LibreOffice.git] / smoketest / smoketest.cxx
blob3a4f2b1afc86b2f452883b1b0135272d18c83284
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/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/implbase.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 #include <utility>
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 &) override {}
79 virtual void SAL_CALL dispatchFinished(
80 css::frame::DispatchResultEvent const & Result) override;
82 Result * result_;
85 void Listener::dispatchFinished(css::frame::DispatchResultEvent const & Result)
87 result_->success =
88 (Result.State == css::frame::DispatchResultState::SUCCESS) &&
89 (Result.Result >>= result_->result);
90 result_->condition.set();
93 class Callback: public cppu::WeakImplHelper< css::awt::XCallback > {
94 public:
95 Callback(
96 css::uno::Reference< css::frame::XNotifyingDispatch > const & dispatch,
97 css::util::URL url,
98 css::uno::Sequence< css::beans::PropertyValue > const & arguments,
99 css::uno::Reference< css::frame::XDispatchResultListener > listener):
100 dispatch_(dispatch), url_(std::move(url)), arguments_(arguments),
101 listener_(std::move(listener))
102 { OSL_ASSERT(dispatch.is()); }
104 private:
105 virtual void SAL_CALL notify(css::uno::Any const &) override
106 { dispatch_->dispatchWithNotification(url_, arguments_, listener_); }
108 css::uno::Reference< css::frame::XNotifyingDispatch > dispatch_;
109 css::util::URL url_;
110 css::uno::Sequence< css::beans::PropertyValue > arguments_;
111 css::uno::Reference< css::frame::XDispatchResultListener > listener_;
114 class Test: public CppUnit::TestFixture {
115 public:
116 virtual void setUp() override;
118 virtual void tearDown() override;
120 private:
121 CPPUNIT_TEST_SUITE(Test);
122 CPPUNIT_TEST(test);
123 CPPUNIT_TEST_SUITE_END();
125 void test();
127 test::OfficeConnection connection_;
130 void Test::setUp() {
131 connection_.setUp();
134 void Test::tearDown() {
135 connection_.tearDown();
138 void Test::test() {
139 OUString doc;
140 CPPUNIT_ASSERT(
141 test::getTestArgument(
142 u"smoketest.doc", &doc));
143 css::uno::Sequence< css::beans::PropertyValue > args{
144 { /* Name */ "MacroExecutionMode",
145 /* Handle */ -1,
146 /* Value */ css::uno::Any(css::document::MacroExecMode::ALWAYS_EXECUTE_NO_WARN),
147 /* State */ css::beans::PropertyState_DIRECT_VALUE },
148 { /* Name */ "ReadOnly",
149 /* Handle */ -1,
150 /* Value */ css::uno::Any(true),
151 /* State */ css::beans::PropertyState_DIRECT_VALUE }
153 css::util::URL url;
154 url.Complete = "vnd.sun.star.script:Standard.Global.StartTestWithDefaultOptions?"
155 "language=Basic&location=document";
157 css::uno::Reference< css::frame::XDesktop2 > xDesktop = css::frame::Desktop::create(connection_.getComponentContext());
159 css::uno::Reference< css::frame::XNotifyingDispatch > disp(
160 css::uno::Reference< css::frame::XDispatchProvider >(
161 css::uno::Reference< css::frame::XController >(
162 css::uno::Reference< css::frame::XModel >(
163 xDesktop->loadComponentFromURL(
164 test::toAbsoluteFileUrl(doc),
165 "_default",
166 0, args),
167 css::uno::UNO_QUERY_THROW)->getCurrentController(),
168 css::uno::UNO_SET_THROW)->getFrame(),
169 css::uno::UNO_QUERY_THROW)->queryDispatch(
170 url, "_self", 0),
171 css::uno::UNO_QUERY_THROW);
172 Result result;
173 // Shifted to main thread to work around potential deadlocks (i112867):
174 css::awt::AsyncCallback::create(
175 connection_.getComponentContext())->addCallback(
176 new Callback(
177 disp, url, css::uno::Sequence< css::beans::PropertyValue >(),
178 new Listener(&result)),
179 css::uno::Any());
180 // Wait for result.condition or connection_ going stale:
181 for (;;) {
182 osl::Condition::Result res = result.condition.wait(std::chrono::seconds(1)); // 1 sec delay
183 if (res == osl::Condition::result_ok) {
184 break;
186 CPPUNIT_ASSERT_EQUAL(osl::Condition::result_timeout, res);
187 CPPUNIT_ASSERT(connection_.isStillAlive());
189 CPPUNIT_ASSERT(result.success);
190 CPPUNIT_ASSERT_EQUAL(OUString(), result.result);
193 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
197 CPPUNIT_PLUGIN_IMPLEMENT();
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */