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