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