1 /*************************************************************************
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3 *
4 * Copyright 2000, 2010 Oracle and/or its affiliates.
5 *
6 * OpenOffice.org - a multi-platform office productivity suite
7 *
8 * This file is part of OpenOffice.org.
9 *
10 * OpenOffice.org is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License version 3
12 * only, as published by the Free Software Foundation.
13 *
14 * OpenOffice.org is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU Lesser General Public License version 3 for more details
18 * (a copy is included in the LICENSE file that accompanied this code).
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * version 3 along with OpenOffice.org.  If not, see
22 * <http://www.openoffice.org/license.html>
23 * for a copy of the LGPLv3 License.
24 ************************************************************************/
25 
26 #include "sal/config.h"
27 
28 #include "com/sun/star/bridge/UnoUrlResolver.hpp"
29 #include "com/sun/star/bridge/XUnoUrlResolver.hpp"
30 #include "com/sun/star/connection/NoConnectException.hpp"
31 #include "com/sun/star/frame/XDesktop.hpp"
32 #include "com/sun/star/lang/DisposedException.hpp"
33 #include "com/sun/star/uno/Reference.hxx"
34 #include "com/sun/star/uno/XComponentContext.hpp"
35 #include "cppuhelper/bootstrap.hxx"
36 #include <preextstl.h>
37 #include "cppunit/TestAssert.h"
38 #include <postextstl.h>
39 #include "osl/process.h"
40 #include "osl/time.h"
41 #include "sal/types.h"
42 #include "test/officeconnection.hxx"
43 #include "test/toabsolutefileurl.hxx"
44 #include "test/uniquepipename.hxx"
45 
46 #include "getargument.hxx"
47 
48 namespace {
49 
50 namespace css = com::sun::star;
51 
52 }
53 
54 namespace test {
55 
56 OfficeConnection::OfficeConnection(): process_(0) {}
57 
58 OfficeConnection::~OfficeConnection() {}
59 
60 void OfficeConnection::setUp() {
61     rtl::OUString desc;
62     rtl::OUString argSoffice;
63     CPPUNIT_ASSERT(
64         detail::getArgument(
65             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("soffice")),
66             &argSoffice));
67     if (argSoffice.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("path:"))) {
68         desc = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("pipe,name=")) +
69             uniquePipeName(
70                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("oootest")));
71         rtl::OUString noquickArg(
72             RTL_CONSTASCII_USTRINGPARAM("-quickstart=no"));
73         rtl::OUString nofirstArg(
74             RTL_CONSTASCII_USTRINGPARAM("-nofirststartwizard"));
75         rtl::OUString norestoreArg(RTL_CONSTASCII_USTRINGPARAM("-norestore"));
76         rtl::OUString acceptArg(
77             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-accept=")) + desc +
78             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(";urp")));
79         rtl::OUString argUser;
80         CPPUNIT_ASSERT(
81             detail::getArgument(
82                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("user")), &argUser));
83         rtl::OUString userArg(
84             rtl::OUString(
85                 RTL_CONSTASCII_USTRINGPARAM("-env:UserInstallation=")) +
86             toAbsoluteFileUrl(argUser));
87         rtl::OUString jreArg(
88             RTL_CONSTASCII_USTRINGPARAM("-env:UNO_JAVA_JFW_ENV_JREHOME=true"));
89         rtl_uString * args[] = {
90             noquickArg.pData, nofirstArg.pData, norestoreArg.pData,
91             acceptArg.pData, userArg.pData, jreArg.pData };
92         rtl_uString ** envs = 0;
93         rtl::OUString argEnv;
94         if (detail::getArgument(
95                 rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("env")), &argEnv))
96         {
97             envs = &argEnv.pData;
98         }
99         CPPUNIT_ASSERT_EQUAL(
100             osl_Process_E_None,
101             osl_executeProcess(
102                 toAbsoluteFileUrl(
103                     argSoffice.copy(RTL_CONSTASCII_LENGTH("path:"))).pData,
104                 args, sizeof args / sizeof args[0], 0, 0, 0, envs,
105                 envs == 0 ? 0 : 1, &process_));
106     } else if (argSoffice.matchAsciiL(RTL_CONSTASCII_STRINGPARAM("connect:"))) {
107         desc = argSoffice.copy(RTL_CONSTASCII_LENGTH("connect:"));
108     } else {
109         CPPUNIT_FAIL(
110             "\"soffice\" argument starts with neither \"path:\" nor"
111             " \"connect:\"");
112     }
113     css::uno::Reference< css::bridge::XUnoUrlResolver > resolver(
114         css::bridge::UnoUrlResolver::create(
115             cppu::defaultBootstrap_InitialComponentContext()));
116     for (;;) {
117         try {
118             context_ =
119                 css::uno::Reference< css::uno::XComponentContext >(
120                     resolver->resolve(
121                         rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("uno:")) +
122                         desc +
123                         rtl::OUString(
124                             RTL_CONSTASCII_USTRINGPARAM(
125                                 ";urp;StarOffice.ComponentContext"))),
126                     css::uno::UNO_QUERY_THROW);
127             break;
128         } catch (css::connection::NoConnectException &) {}
129         if (process_ != 0) {
130             TimeValue delay = { 1, 0 }; // 1 sec
131             CPPUNIT_ASSERT_EQUAL(
132                 osl_Process_E_TimedOut,
133                 osl_joinProcessWithTimeout(process_, &delay));
134         }
135     }
136 }
137 
138 void OfficeConnection::tearDown() {
139     if (process_ != 0) {
140         if (context_.is()) {
141             css::uno::Reference< css::frame::XDesktop > desktop(
142                 context_->getServiceManager()->createInstanceWithContext(
143                     rtl::OUString(
144                         RTL_CONSTASCII_USTRINGPARAM(
145                             "com.sun.star.frame.Desktop")),
146                     context_),
147                 css::uno::UNO_QUERY_THROW);
148             context_.clear();
149             try {
150                 CPPUNIT_ASSERT(desktop->terminate());
151                 desktop.clear();
152             } catch (css::lang::DisposedException &) {}
153                 // it appears that DisposedExceptions can already happen while
154                 // receiving the response of the terminate call
155         }
156         CPPUNIT_ASSERT_EQUAL(osl_Process_E_None, osl_joinProcess(process_));
157         oslProcessInfo info;
158         info.Size = sizeof info;
159         CPPUNIT_ASSERT_EQUAL(
160             osl_Process_E_None,
161             osl_getProcessInfo(process_, osl_Process_EXITCODE, &info));
162         CPPUNIT_ASSERT_EQUAL(oslProcessExitCode(0), info.Code);
163         osl_freeProcessHandle(process_);
164     }
165 }
166 
167 css::uno::Reference< css::uno::XComponentContext >
168 OfficeConnection::getComponentContext() const {
169     return context_;
170 }
171 
172 }
173