Branch data 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 "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/XDesktop.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/time.h"
31 : : #include "sal/macros.h"
32 : : #include "sal/types.h"
33 : : #include "unotest/officeconnection.hxx"
34 : : #include "unotest/toabsolutefileurl.hxx"
35 : : #include "unotest/uniquepipename.hxx"
36 : :
37 : : #include "getargument.hxx"
38 : :
39 : : namespace {
40 : :
41 : : namespace css = com::sun::star;
42 : :
43 : : }
44 : :
45 : : namespace test {
46 : :
47 : 2 : OfficeConnection::OfficeConnection(): process_(0) {}
48 : :
49 : 2 : OfficeConnection::~OfficeConnection() {}
50 : :
51 : 2 : void OfficeConnection::setUp() {
52 : : css::uno::Reference< css::bridge::XUnoUrlResolver > resolver(
53 : : css::bridge::UnoUrlResolver::create(
54 [ + - ][ + - ]: 2 : cppu::defaultBootstrap_InitialComponentContext()));
55 : 2 : rtl::OUString desc;
56 : 2 : rtl::OUString argSoffice;
57 [ + - ][ + - ]: 4 : CPPUNIT_ASSERT(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
58 : : detail::getArgument(
59 : : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("soffice")),
60 [ + - ]: 2 : &argSoffice));
61 [ + - ]: 2 : if (argSoffice.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("path:"))) {
62 : : desc = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("pipe,name=")) +
63 : : uniquePipeName(
64 [ + - ][ + - ]: 2 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("oootest")));
[ + - ]
65 : : rtl::OUString noquickArg(
66 [ + - ]: 2 : RTL_CONSTASCII_USTRINGPARAM("--quickstart=no"));
67 : : rtl::OUString nofirstArg(
68 [ + - ]: 2 : RTL_CONSTASCII_USTRINGPARAM("--nofirststartwizard"));
69 [ + - ]: 2 : rtl::OUString norestoreArg(RTL_CONSTASCII_USTRINGPARAM("--norestore"));
70 [ + - ]: 2 : rtl::OUString nologoArg(RTL_CONSTASCII_USTRINGPARAM("--nologo"));
71 : : // disable use of the unix standalone splash screen app for the
72 : : // tests (probably not needed in combination with --headless?)
73 [ + - ]: 2 : rtl::OUString headlessArg(RTL_CONSTASCII_USTRINGPARAM("--headless"));
74 : : rtl::OUString acceptArg(
75 : : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("--accept=")) + desc +
76 [ + - ][ + - ]: 2 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(";urp")));
77 : 2 : rtl::OUString argUser;
78 [ + - ][ + - ]: 4 : CPPUNIT_ASSERT(
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
79 : : detail::getArgument(
80 [ + - ]: 2 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("user")), &argUser));
81 : : rtl::OUString userArg(
82 : : rtl::OUString(
83 : : RTL_CONSTASCII_USTRINGPARAM("-env:UserInstallation=")) +
84 [ + - ][ + - ]: 2 : toAbsoluteFileUrl(argUser));
85 : : rtl::OUString jreArg(
86 [ + - ]: 2 : RTL_CONSTASCII_USTRINGPARAM("-env:UNO_JAVA_JFW_ENV_JREHOME=true"));
87 : : rtl::OUString classpathArg(
88 : : RTL_CONSTASCII_USTRINGPARAM(
89 [ + - ]: 2 : "-env:UNO_JAVA_JFW_ENV_CLASSPATH=true"));
90 : : rtl_uString * args[] = {
91 : : noquickArg.pData, nofirstArg.pData, norestoreArg.pData,
92 : : nologoArg.pData, headlessArg.pData, acceptArg.pData, userArg.pData,
93 : 2 : jreArg.pData, classpathArg.pData };
94 : 2 : rtl_uString ** envs = 0;
95 : 2 : rtl::OUString argEnv;
96 [ + - + - ]: 4 : if (detail::getArgument(
97 [ + - ]: 4 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("env")), &argEnv))
98 : : {
99 : 2 : envs = &argEnv.pData;
100 : : }
101 [ + - ][ + - ]: 4 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
[ + - ][ + - ]
102 : : osl_Process_E_None,
103 : : osl_executeProcess(
104 : : toAbsoluteFileUrl(
105 : : argSoffice.copy(RTL_CONSTASCII_LENGTH("path:"))).pData,
106 : : args, SAL_N_ELEMENTS(args), 0, 0, 0, envs, envs == 0 ? 0 : 1,
107 [ + - ]: 4 : &process_));
108 [ # # ]: 0 : } else if (argSoffice.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("connect:"))) {
109 : 0 : desc = argSoffice.copy(RTL_CONSTASCII_LENGTH("connect:"));
110 : : } else {
111 [ # # ][ # # ]: 0 : CPPUNIT_FAIL(
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
112 : : "\"soffice\" argument starts with neither \"path:\" nor"
113 [ # # ]: 0 : " \"connect:\"");
114 : : }
115 : 15 : for (;;) {
116 : : try {
117 : : context_ =
118 : : css::uno::Reference< css::uno::XComponentContext >(
119 [ + - ]: 17 : resolver->resolve(
120 : : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("uno:")) +
121 : : desc +
122 : : rtl::OUString(
123 : : RTL_CONSTASCII_USTRINGPARAM(
124 : 17 : ";urp;StarOffice.ComponentContext"))),
125 [ + - ][ + - ]: 17 : css::uno::UNO_QUERY_THROW);
[ + + ][ + - ]
[ + - ][ - + ]
126 : 2 : break;
127 [ + - ]: 15 : } catch (css::connection::NoConnectException &) {}
128 [ + - ]: 15 : if (process_ != 0) {
129 : 15 : TimeValue delay = { 1, 0 }; // 1 sec
130 [ + - ][ + - ]: 30 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
[ + - ]
131 : : osl_Process_E_TimedOut,
132 [ + - ]: 15 : osl_joinProcessWithTimeout(process_, &delay));
133 : : }
134 : 2 : }
135 : 2 : }
136 : :
137 : 2 : void OfficeConnection::tearDown() {
138 [ + - ]: 2 : if (process_ != 0) {
139 [ + - ]: 2 : if (context_.is()) {
140 : : css::uno::Reference< css::frame::XDesktop > desktop(
141 [ + - ][ + - ]: 4 : context_->getServiceManager()->createInstanceWithContext(
[ + - ]
142 : : rtl::OUString(
143 : : RTL_CONSTASCII_USTRINGPARAM(
144 : : "com.sun.star.frame.Desktop")),
145 : 2 : context_),
146 [ + - ][ + - ]: 2 : css::uno::UNO_QUERY_THROW);
[ + - ]
147 : 2 : context_.clear();
148 : : try {
149 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT(desktop->terminate());
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ # # ]
[ + - ]
150 : 2 : desktop.clear();
151 [ # # ]: 2 : } catch (css::lang::DisposedException &) {}
152 : : // it appears that DisposedExceptions can already happen while
153 : : // receiving the response of the terminate call
154 : : }
155 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT_EQUAL(osl_Process_E_None, osl_joinProcess(process_));
[ + - ][ + - ]
[ + - ][ + - ]
156 : : oslProcessInfo info;
157 : 2 : info.Size = sizeof info;
158 [ + - ][ + - ]: 4 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
[ + - ]
159 : : osl_Process_E_None,
160 [ + - ]: 2 : osl_getProcessInfo(process_, osl_Process_EXITCODE, &info));
161 [ + - ][ + - ]: 2 : CPPUNIT_ASSERT_EQUAL(oslProcessExitCode(0), info.Code);
[ + - ][ + - ]
[ + - ]
162 [ + - ]: 2 : osl_freeProcessHandle(process_);
163 : 2 : process_ = 0; // guard against subsequent calls to isStillAlive
164 : : }
165 : 2 : }
166 : :
167 : : css::uno::Reference< css::uno::XComponentContext >
168 : 6 : OfficeConnection::getComponentContext() const {
169 : 6 : return context_;
170 : : }
171 : :
172 : 166 : bool OfficeConnection::isStillAlive() const {
173 [ - + ]: 166 : if (process_ == 0) {
174 : : // In case "soffice" argument starts with "connect:" we have no direct
175 : : // control over the liveness of the soffice.bin process (would need to
176 : : // directly monitor the bridge) so can only assume the best here:
177 : 0 : return true;
178 : : }
179 : 166 : TimeValue delay = { 0, 0 }; // 0 sec
180 [ + - ]: 166 : oslProcessError e = osl_joinProcessWithTimeout(process_, &delay);
181 [ + - ][ + - ]: 166 : CPPUNIT_ASSERT(e == osl_Process_E_None || e == osl_Process_E_TimedOut);
[ + - ][ + - ]
[ + - ][ + - ]
[ - + ][ + - ]
[ + - ][ + - ]
182 : 166 : return e == osl_Process_E_TimedOut;
183 : : }
184 : :
185 [ + - ][ + - ]: 348 : }
186 : :
187 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|