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