bump product version to 7.2.5.1
[LibreOffice.git] / unotest / source / cpp / officeconnection.cxx
blob55525736f3460f696016f31f821af094762ee059
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 <com/sun/star/bridge/UnoUrlResolver.hpp>
21 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
22 #include <com/sun/star/connection/NoConnectException.hpp>
23 #include <com/sun/star/frame/Desktop.hpp>
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <com/sun/star/uno/Reference.hxx>
26 #include <com/sun/star/uno/XComponentContext.hpp>
27 #include <cppuhelper/bootstrap.hxx>
28 #include <cppunit/TestAssert.h>
29 #include <osl/process.h>
30 #include <osl/test/uniquepipename.hxx>
31 #include <osl/time.h>
32 #include <sal/macros.h>
33 #include <unotest/officeconnection.hxx>
34 #include <unotest/toabsolutefileurl.hxx>
36 #include "getargument.hxx"
38 namespace test {
40 OfficeConnection::OfficeConnection(): process_(nullptr) {}
42 OfficeConnection::~OfficeConnection() {}
44 void OfficeConnection::setUp() {
45 css::uno::Reference< css::bridge::XUnoUrlResolver > resolver(
46 css::bridge::UnoUrlResolver::create(
47 cppu::defaultBootstrap_InitialComponentContext()));
48 OUString desc;
49 OUString argSoffice;
50 CPPUNIT_ASSERT(
51 detail::getArgument(
52 u"soffice",
53 &argSoffice));
54 if (argSoffice.match("path:")) {
55 desc = "pipe,name=" + osl::test::uniquePipeName("oootest");
56 OUString noquickArg("--quickstart=no");
57 OUString norestoreArg("--norestore");
58 OUString nologoArg("--nologo");
59 // disable use of the unix standalone splash screen app for the
60 // tests (probably not needed in combination with --headless?)
61 OUString headlessArg("--headless");
62 OUString acceptArg("--accept=" + desc + ";urp");
63 OUString argUser;
64 CPPUNIT_ASSERT(
65 detail::getArgument(u"user", &argUser));
66 OUString userArg("-env:UserInstallation=" + toAbsoluteFileUrl(argUser));
67 OUString jreArg(
68 "-env:UNO_JAVA_JFW_ENV_JREHOME=true");
69 rtl_uString * args[] = {
70 noquickArg.pData, norestoreArg.pData,
71 nologoArg.pData, headlessArg.pData, acceptArg.pData, userArg.pData,
72 jreArg.pData };
73 rtl_uString ** envs = nullptr;
74 OUString argEnv;
75 if (detail::getArgument(u"env", &argEnv))
77 envs = &argEnv.pData;
79 // coverity[callee_ptr_arith] - arith is fine
80 CPPUNIT_ASSERT_EQUAL(
81 osl_Process_E_None,
82 osl_executeProcess(
83 toAbsoluteFileUrl(
84 argSoffice.copy(RTL_CONSTASCII_LENGTH("path:"))).pData,
85 args, SAL_N_ELEMENTS(args), 0, nullptr, nullptr, envs, envs == nullptr ? 0 : 1,
86 &process_));
87 } else if (argSoffice.match("connect:")) {
88 desc = argSoffice.copy(RTL_CONSTASCII_LENGTH("connect:"));
89 } else {
90 CPPUNIT_FAIL(
91 "\"soffice\" argument starts with neither \"path:\" nor"
92 " \"connect:\"");
94 for (;;) {
95 try {
96 context_ =
97 css::uno::Reference< css::uno::XComponentContext >(
98 resolver->resolve(
99 "uno:" + desc + ";urp;StarOffice.ComponentContext"),
100 css::uno::UNO_QUERY_THROW);
101 break;
102 } catch (css::connection::NoConnectException &) {}
103 if (process_ != nullptr) {
104 TimeValue delay = { 1, 0 }; // 1 sec
105 CPPUNIT_ASSERT_EQUAL(
106 osl_Process_E_TimedOut,
107 osl_joinProcessWithTimeout(process_, &delay));
112 void OfficeConnection::tearDown() {
113 if (process_ == nullptr)
114 return;
116 if (context_.is()) {
117 css::uno::Reference< css::frame::XDesktop2 > desktop = css::frame::Desktop::create( context_ );
118 context_.clear();
119 try {
120 CPPUNIT_ASSERT(desktop->terminate());
121 desktop.clear();
122 } catch (css::lang::DisposedException &) {}
123 // it appears that DisposedExceptions can already happen while
124 // receiving the response of the terminate call
126 CPPUNIT_ASSERT_EQUAL(osl_Process_E_None, osl_joinProcess(process_));
127 oslProcessInfo info;
128 info.Size = sizeof info;
129 CPPUNIT_ASSERT_EQUAL(
130 osl_Process_E_None,
131 osl_getProcessInfo(process_, osl_Process_EXITCODE, &info));
132 CPPUNIT_ASSERT_EQUAL(oslProcessExitCode(0), info.Code);
133 osl_freeProcessHandle(process_);
134 process_ = nullptr; // guard against subsequent calls to isStillAlive
138 bool OfficeConnection::isStillAlive() const {
139 if (process_ == nullptr) {
140 // In case "soffice" argument starts with "connect:" we have no direct
141 // control over the liveness of the soffice.bin process (would need to
142 // directly monitor the bridge) so can only assume the best here:
143 return true;
145 TimeValue delay = { 0, 0 }; // 0 sec
146 oslProcessError e = osl_joinProcessWithTimeout(process_, &delay);
147 CPPUNIT_ASSERT(e == osl_Process_E_None || e == osl_Process_E_TimedOut);
148 return e == osl_Process_E_TimedOut;
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */