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