1*be9e621aSdamjan /**************************************************************
2*be9e621aSdamjan  *
3*be9e621aSdamjan  * Licensed to the Apache Software Foundation (ASF) under one
4*be9e621aSdamjan  * or more contributor license agreements.  See the NOTICE file
5*be9e621aSdamjan  * distributed with this work for additional information
6*be9e621aSdamjan  * regarding copyright ownership.  The ASF licenses this file
7*be9e621aSdamjan  * to you under the Apache License, Version 2.0 (the
8*be9e621aSdamjan  * "License"); you may not use this file except in compliance
9*be9e621aSdamjan  * with the License.  You may obtain a copy of the License at
10*be9e621aSdamjan  *
11*be9e621aSdamjan  *   http://www.apache.org/licenses/LICENSE-2.0
12*be9e621aSdamjan  *
13*be9e621aSdamjan  * Unless required by applicable law or agreed to in writing,
14*be9e621aSdamjan  * software distributed under the License is distributed on an
15*be9e621aSdamjan  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*be9e621aSdamjan  * KIND, either express or implied.  See the License for the
17*be9e621aSdamjan  * specific language governing permissions and limitations
18*be9e621aSdamjan  * under the License.
19*be9e621aSdamjan  *
20*be9e621aSdamjan  *************************************************************/
21*be9e621aSdamjan 
22*be9e621aSdamjan 
23*be9e621aSdamjan 
24*be9e621aSdamjan #include "sal/config.h"
25*be9e621aSdamjan 
26*be9e621aSdamjan #include "com/sun/star/bridge/UnoUrlResolver.hpp"
27*be9e621aSdamjan #include "com/sun/star/bridge/XUnoUrlResolver.hpp"
28*be9e621aSdamjan #include "com/sun/star/connection/NoConnectException.hpp"
29*be9e621aSdamjan #include "com/sun/star/frame/XDesktop.hpp"
30*be9e621aSdamjan #include "com/sun/star/lang/DisposedException.hpp"
31*be9e621aSdamjan #include "com/sun/star/uno/Reference.hxx"
32*be9e621aSdamjan #include "com/sun/star/uno/XComponentContext.hpp"
33*be9e621aSdamjan #include "cppuhelper/bootstrap.hxx"
34*be9e621aSdamjan #include <preextstl.h>
35*be9e621aSdamjan #include "cppunit/TestAssert.h"
36*be9e621aSdamjan #include <postextstl.h>
37*be9e621aSdamjan #include "osl/process.h"
38*be9e621aSdamjan #include "osl/time.h"
39*be9e621aSdamjan #include "sal/types.h"
40*be9e621aSdamjan #include "test/officeconnection.hxx"
41*be9e621aSdamjan #include "test/toabsolutefileurl.hxx"
42*be9e621aSdamjan #include "test/uniquepipename.hxx"
43*be9e621aSdamjan 
44*be9e621aSdamjan #include "getargument.hxx"
45*be9e621aSdamjan 
46*be9e621aSdamjan namespace {
47*be9e621aSdamjan 
48*be9e621aSdamjan namespace css = com::sun::star;
49*be9e621aSdamjan 
50*be9e621aSdamjan }
51*be9e621aSdamjan 
52*be9e621aSdamjan namespace test {
53*be9e621aSdamjan 
OfficeConnection()54*be9e621aSdamjan OfficeConnection::OfficeConnection(): process_(0) {}
55*be9e621aSdamjan 
~OfficeConnection()56*be9e621aSdamjan OfficeConnection::~OfficeConnection() {}
57*be9e621aSdamjan 
setUp()58*be9e621aSdamjan void OfficeConnection::setUp() {
59*be9e621aSdamjan     rtl::OUString desc;
60*be9e621aSdamjan     rtl::OUString argSoffice;
61*be9e621aSdamjan     CPPUNIT_ASSERT(
62*be9e621aSdamjan         detail::getArgument(
63*be9e621aSdamjan             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("soffice")),
64*be9e621aSdamjan             &argSoffice));
65*be9e621aSdamjan     if (argSoffice.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("path:"))) {
66*be9e621aSdamjan         desc = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("pipe,name=")) +
67*be9e621aSdamjan             uniquePipeName(
68*be9e621aSdamjan                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("oootest")));
69*be9e621aSdamjan         rtl::OUString noquickArg(
70*be9e621aSdamjan             RTL_CONSTASCII_USTRINGPARAM("-quickstart=no"));
71*be9e621aSdamjan         rtl::OUString nofirstArg(
72*be9e621aSdamjan             RTL_CONSTASCII_USTRINGPARAM("-nofirststartwizard"));
73*be9e621aSdamjan         rtl::OUString norestoreArg(RTL_CONSTASCII_USTRINGPARAM("-norestore"));
74*be9e621aSdamjan         rtl::OUString acceptArg(
75*be9e621aSdamjan             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-accept=")) + desc +
76*be9e621aSdamjan             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(";urp")));
77*be9e621aSdamjan         rtl::OUString argUser;
78*be9e621aSdamjan         CPPUNIT_ASSERT(
79*be9e621aSdamjan             detail::getArgument(
80*be9e621aSdamjan                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("user")), &argUser));
81*be9e621aSdamjan         rtl::OUString userArg(
82*be9e621aSdamjan             rtl::OUString(
83*be9e621aSdamjan                 RTL_CONSTASCII_USTRINGPARAM("-env:UserInstallation=")) +
84*be9e621aSdamjan             toAbsoluteFileUrl(argUser));
85*be9e621aSdamjan         rtl::OUString jreArg(
86*be9e621aSdamjan             RTL_CONSTASCII_USTRINGPARAM("-env:UNO_JAVA_JFW_ENV_JREHOME=true"));
87*be9e621aSdamjan         rtl_uString * args[] = {
88*be9e621aSdamjan             noquickArg.pData, nofirstArg.pData, norestoreArg.pData,
89*be9e621aSdamjan             acceptArg.pData, userArg.pData, jreArg.pData };
90*be9e621aSdamjan         rtl_uString ** envs = 0;
91*be9e621aSdamjan         rtl::OUString argEnv;
92*be9e621aSdamjan         if (detail::getArgument(
93*be9e621aSdamjan                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("env")), &argEnv))
94*be9e621aSdamjan         {
95*be9e621aSdamjan             envs = &argEnv.pData;
96*be9e621aSdamjan         }
97*be9e621aSdamjan         CPPUNIT_ASSERT_EQUAL(
98*be9e621aSdamjan             osl_Process_E_None,
99*be9e621aSdamjan             osl_executeProcess(
100*be9e621aSdamjan                 toAbsoluteFileUrl(
101*be9e621aSdamjan                     argSoffice.copy(RTL_CONSTASCII_LENGTH("path:"))).pData,
102*be9e621aSdamjan                 args, sizeof args / sizeof args[0], 0, 0, 0, envs,
103*be9e621aSdamjan                 envs == 0 ? 0 : 1, &process_));
104*be9e621aSdamjan     } else if (argSoffice.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("connect:"))) {
105*be9e621aSdamjan         desc = argSoffice.copy(RTL_CONSTASCII_LENGTH("connect:"));
106*be9e621aSdamjan     } else {
107*be9e621aSdamjan         CPPUNIT_FAIL(
108*be9e621aSdamjan             "\"soffice\" argument starts with neither \"path:\" nor"
109*be9e621aSdamjan             " \"connect:\"");
110*be9e621aSdamjan     }
111*be9e621aSdamjan     css::uno::Reference< css::bridge::XUnoUrlResolver > resolver(
112*be9e621aSdamjan         css::bridge::UnoUrlResolver::create(
113*be9e621aSdamjan             cppu::defaultBootstrap_InitialComponentContext()));
114*be9e621aSdamjan     for (;;) {
115*be9e621aSdamjan         try {
116*be9e621aSdamjan             context_ =
117*be9e621aSdamjan                 css::uno::Reference< css::uno::XComponentContext >(
118*be9e621aSdamjan                     resolver->resolve(
119*be9e621aSdamjan                         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("uno:")) +
120*be9e621aSdamjan                         desc +
121*be9e621aSdamjan                         rtl::OUString(
122*be9e621aSdamjan                             RTL_CONSTASCII_USTRINGPARAM(
123*be9e621aSdamjan                                 ";urp;StarOffice.ComponentContext"))),
124*be9e621aSdamjan                     css::uno::UNO_QUERY_THROW);
125*be9e621aSdamjan             break;
126*be9e621aSdamjan         } catch (css::connection::NoConnectException &) {}
127*be9e621aSdamjan         if (process_ != 0) {
128*be9e621aSdamjan             TimeValue delay = { 1, 0 }; // 1 sec
129*be9e621aSdamjan             CPPUNIT_ASSERT_EQUAL(
130*be9e621aSdamjan                 osl_Process_E_TimedOut,
131*be9e621aSdamjan                 osl_joinProcessWithTimeout(process_, &delay));
132*be9e621aSdamjan         }
133*be9e621aSdamjan     }
134*be9e621aSdamjan }
135*be9e621aSdamjan 
tearDown()136*be9e621aSdamjan void OfficeConnection::tearDown() {
137*be9e621aSdamjan     if (process_ != 0) {
138*be9e621aSdamjan         if (context_.is()) {
139*be9e621aSdamjan             css::uno::Reference< css::frame::XDesktop > desktop(
140*be9e621aSdamjan                 context_->getServiceManager()->createInstanceWithContext(
141*be9e621aSdamjan                     rtl::OUString(
142*be9e621aSdamjan                         RTL_CONSTASCII_USTRINGPARAM(
143*be9e621aSdamjan                             "com.sun.star.frame.Desktop")),
144*be9e621aSdamjan                     context_),
145*be9e621aSdamjan                 css::uno::UNO_QUERY_THROW);
146*be9e621aSdamjan             context_.clear();
147*be9e621aSdamjan             try {
148*be9e621aSdamjan                 CPPUNIT_ASSERT(desktop->terminate());
149*be9e621aSdamjan                 desktop.clear();
150*be9e621aSdamjan             } catch (css::lang::DisposedException &) {}
151*be9e621aSdamjan                 // it appears that DisposedExceptions can already happen while
152*be9e621aSdamjan                 // receiving the response of the terminate call
153*be9e621aSdamjan         }
154*be9e621aSdamjan         CPPUNIT_ASSERT_EQUAL(osl_Process_E_None, osl_joinProcess(process_));
155*be9e621aSdamjan         oslProcessInfo info;
156*be9e621aSdamjan         info.Size = sizeof info;
157*be9e621aSdamjan         CPPUNIT_ASSERT_EQUAL(
158*be9e621aSdamjan             osl_Process_E_None,
159*be9e621aSdamjan             osl_getProcessInfo(process_, osl_Process_EXITCODE, &info));
160*be9e621aSdamjan         CPPUNIT_ASSERT_EQUAL(oslProcessExitCode(0), info.Code);
161*be9e621aSdamjan         osl_freeProcessHandle(process_);
162*be9e621aSdamjan     }
163*be9e621aSdamjan }
164*be9e621aSdamjan 
165*be9e621aSdamjan css::uno::Reference< css::uno::XComponentContext >
getComponentContext() const166*be9e621aSdamjan OfficeConnection::getComponentContext() const {
167*be9e621aSdamjan     return context_;
168*be9e621aSdamjan }
169*be9e621aSdamjan 
170*be9e621aSdamjan }
171