LCOV - code coverage report
Current view: top level - libreoffice/unotest/source/cpp - officeconnection.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 51 76 67.1 %
Date: 2012-12-17 Functions: 6 8 75.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.10