merged tag ooo/DEV300_m102
[LibreOffice.git] / toolkit / workben / layout / test.cxx
blob6a09518ebc4f95449facade4cc07c18afcc00db5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include <vcl/svapp.hxx>
30 // This works and was used before for standalone test, not sure why
31 // we'd want it.
32 #define LAYOUT_WEAK 1
33 #include "uno.hxx"
35 #include <cstdio>
36 #include <cstdlib>
37 #include <sys/stat.h>
39 #include <com/sun/star/xml/sax/SAXException.hpp>
40 #include <comphelper/processfactory.hxx>
41 #include <cppuhelper/bootstrap.hxx>
42 #include <rtl/ustring.hxx>
43 #include <ucbhelper/configurationkeys.hxx>
44 #include <ucbhelper/contentbroker.hxx>
46 #define SORT_DLG 1 /* requires sfx2, svx to be compiled */
47 #if SORT_DLG
48 #include "scitems.hxx"
49 #include "uiitems.hxx"
50 #endif /* SORT_DLG */
52 #include "editor.hxx"
54 #include "plugin.hxx"
55 #undef _LAYOUT_POST_HXX
57 #include "recover.hxx"
58 #undef _LAYOUT_POST_HXX
60 #if SORT_DLG
61 #include "sortdlg.hxx"
62 #undef _LAYOUT_POST_HXX
63 #endif /* SORT_DLG */
65 #include "wordcountdialog.hxx"
66 #undef _LAYOUT_POST_HXX
68 #include "zoom.hxx"
69 #undef _LAYOUT_POST_HXX
71 #include <layout/layout-pre.hxx>
73 using namespace ::rtl;
74 using namespace ::cppu;
75 using namespace ::com::sun::star;
76 using namespace ::com::sun::star::uno;
78 class LayoutTest : public Application
80 Reference< XComponentContext > mxContext;
81 Reference< lang::XMultiServiceFactory > mxMSF;
82 OUString mInstallDir;
83 OUString mTestDialog;
84 bool mEditMode;
85 std::list< OUString > mFiles;
87 public:
88 LayoutTest( char const* installDir );
90 void RunEditor();
91 void RunFiles();
92 void ExceptionalMain();
93 void Init();
94 void InitUCB();
95 void LoadFile( OUString const &aName );
96 void Main();
97 void ParseCommandLine();
100 static void usage()
102 fprintf (stderr, "usage: test [--inst OOO_INSTALL_PREFIX] [DIALOG.XML]... | --test [DIALOG.XML]\n" );
103 exit( 2 );
106 static uno::Reference< lang::XSingleServiceFactory > get_factory( char const *service )
108 uno::Reference< lang::XSingleServiceFactory > xFactory(
109 comphelper::createProcessComponent(
110 rtl::OUString::createFromAscii( service ) ), uno::UNO_QUERY );
112 if ( !xFactory.is() )
113 fprintf( stderr, "error loading: %s\n", service );
114 return xFactory;
117 #define GET_FACTORY(x) get_factory( #x )
119 void LayoutTest::LoadFile( const OUString &aTestFile )
121 fprintf( stderr, "TEST: layout instance\n" );
123 uno::Reference< lang::XSingleServiceFactory > xFactory
124 = GET_FACTORY( com.sun.star.awt.Layout );
125 if ( !xFactory.is() )
127 fprintf( stderr, "Layout engine not installed\n" );
128 throw uno::RuntimeException(
129 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Layout engine not installed" ) ),
130 uno::Reference< uno::XInterface >() );
132 fprintf( stderr, "TEST: initing root\n" );
134 uno::Sequence< uno::Any > aParams( 1 );
135 aParams[0] <<= aTestFile;
137 uno::Reference< awt::XLayoutRoot > xRoot (
138 xFactory->createInstanceWithArguments( aParams ),
139 uno::UNO_QUERY );
141 fprintf( stderr, "TEST: file loaded\n" );
144 void LayoutTest::InitUCB()
146 OUString aEmpty;
147 Sequence< Any > aArgs( 6 );
148 aArgs[0]
149 <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL );
150 aArgs[1]
151 <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE );
152 aArgs[2] <<= OUString::createFromAscii( "PIPE" );
153 aArgs[3] <<= aEmpty;
154 aArgs[4] <<= OUString::createFromAscii( "PORTAL" );
155 aArgs[5] <<= aEmpty;
157 if ( !::ucbhelper::ContentBroker::initialize( mxMSF, aArgs ) )
159 fprintf( stderr, "Failed to init content broker\n" );
160 fprintf( stderr, "arg[0]: %s\n", UCB_CONFIGURATION_KEY1_LOCAL );
161 fprintf( stderr, "arg[1]: %s\n", UCB_CONFIGURATION_KEY2_OFFICE );
165 static void support_upstream_brand_prefix ()
167 if ( char const* inst = getenv( "OOO_INSTALL_PREFIX" ) )
169 char const *brand_prefix = "/openoffice.org3";
170 OUString brand_dir = OUString::createFromAscii( inst )
171 + OUString::createFromAscii( brand_prefix );
172 struct stat stat_info;
173 if ( !stat ( OUSTRING_CSTR( brand_dir ), &stat_info ) )
175 OSL_TRACE( "Appending %s to OOO_INSTALL_PREFIX", brand_prefix );
176 setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( brand_dir ), 1 );
181 void LayoutTest::Init()
183 ParseCommandLine();
184 setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( mInstallDir ), 0 );
185 support_upstream_brand_prefix ();
186 OSL_TRACE( "OOO_INSTALL_PREFIX=%s", getenv( "OOO_INSTALL_PREFIX" ) );
188 mxContext = defaultBootstrap_InitialComponentContext();
189 mxMSF = new UnoBootstrapLayout( Reference< lang::XMultiServiceFactory >( mxContext->getServiceManager(), UNO_QUERY ) );
190 ::comphelper::setProcessServiceFactory( mxMSF );
191 InitUCB();
194 void LayoutTest::ParseCommandLine()
196 printf ("%s\n", __PRETTY_FUNCTION__);
197 for ( sal_uInt16 i = 0; i < GetCommandLineParamCount(); i++ )
199 OUString aParam = OUString( GetCommandLineParam( i ) );
200 if ( aParam.equalsAscii( "-h" ) || aParam.equalsAscii( "--help" ) )
201 usage();
202 if ( aParam.equalsAscii( "--inst" ) )
204 if ( i >= GetCommandLineParamCount() - 1)
205 usage();
206 mInstallDir = GetCommandLineParam( ++i );
207 setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( mInstallDir ), 1 );
209 else if ( aParam.equalsAscii( "--test" ) )
211 mTestDialog = OUString::createFromAscii( "zoom" );
212 if (i + 1 < GetCommandLineParamCount())
213 mTestDialog = GetCommandLineParam( ++i );
215 else if ( aParam.equalsAscii( "--editor" ) )
216 mEditMode = true;
217 else
218 mFiles.push_back( aParam );
221 if ( mFiles.size() <= 0 )
222 mFiles.push_back( OUString::createFromAscii( "layout.xml" ) );
225 void LayoutTest::RunEditor()
227 OUString aFile;
228 if ( !mFiles.empty()
229 && mFiles.front().compareToAscii( "layout.xml" ) != 0 )
230 aFile = mFiles.front();
231 Editor editor( mxMSF, aFile );
232 editor.Show();
233 editor.Execute();
236 short RunDialog( Dialog& dialog )
238 dialog.Show();
239 short result = dialog.Execute();
240 fprintf( stderr, "Done: dialog execute exited:%d\n", result);
241 return result;
244 #undef Dialog
245 short RunDialog( ::Dialog& dialog )
247 dialog.Show();
248 short result = dialog.Execute();
249 fprintf( stderr, "Done: dialog execute exited:%d\n", result);
250 return result;
253 #if SORT_DLG
254 static void LoadSC()
256 get_factory( "com.sun.star.comp.sfx2.DocumentTemplates" );
257 get_factory( "com.sun.star.comp.Calc.SpreadsheetDocument" );
258 GET_FACTORY( com.sun.star.i18n.Transliteration.l10n );
260 #endif /* SORT_DLG */
262 void TestDialog( OUString const& name )
264 if ( 0 )
266 else if ( name.equalsAscii( "plugin" ) )
268 PluginDialog plugin ( 0 );
269 RunDialog( plugin );
271 else if ( name.equalsAscii( "query" ) )
273 QueryBox query ( 0, "Do you want to do?", "do");
274 RunDialog( query );
276 else if ( name.equalsAscii( "query-compat" ) )
278 QueryBox query ( 0,
279 WinBits( WB_YES_NO | WB_DEF_YES ),
280 // WinBits( WB_ABORT_RETRY_IGNORE ),
281 OUString::createFromAscii ("Do you want to do?"));
282 RunDialog( query );
284 else if ( name.equalsAscii( "recover" ) )
286 SvxRecoverDialog recover ( 0 );
287 RunDialog( recover );
289 #if SORT_DLG
290 else if ( name.equalsAscii( "sort" ) )
292 LoadSC();
293 ScSortDlg sort (0, 0);
294 RunDialog( sort );
296 #endif /* SORT_DLG */
297 else if ( name.equalsAscii( "wordcount" ) )
299 SwWordCountDialog words ( 0 );
300 RunDialog( words );
302 else if ( name.equalsAscii( "zoom" ) )
304 SvxZoomDialog zoom( 0 );
305 RunDialog( zoom );
309 void LayoutTest::RunFiles()
311 fprintf( stderr, "TEST: loading files\n" );
312 for ( std::list< OUString >::iterator i = mFiles.begin(); i != mFiles.end(); i++ )
313 LoadFile( *i );
314 fprintf( stderr, "TEST: executing\n" );
315 Execute();
316 fprintf( stderr, "TEST: done executing\n" );
319 void LayoutTest::ExceptionalMain()
321 if ( mTestDialog.getLength() )
322 TestDialog( mTestDialog );
323 else if ( mEditMode )
324 RunEditor();
325 else
326 RunFiles();
329 void LayoutTest::Main()
333 ExceptionalMain();
335 catch (xml::sax::SAXException & rExc)
337 OString aStr( OUStringToOString( rExc.Message,
338 RTL_TEXTENCODING_ASCII_US ) );
339 uno::Exception exc;
340 if (rExc.WrappedException >>= exc)
342 aStr += OString( " >>> " );
343 aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
345 fprintf (stderr, "Parsing error: '%s'\n", aStr.getStr());
346 OSL_ENSURE( 0, aStr.getStr() );
348 catch ( uno::Exception & rExc )
350 OString aStr( OUStringToOString( rExc.Message,
351 RTL_TEXTENCODING_ASCII_US ) );
352 fprintf (stderr, "UNO error: '%s'\n", aStr.getStr());
353 OSL_ENSURE( 0, aStr.getStr() );
356 Reference< lang::XComponent > xComp( mxContext, UNO_QUERY );
357 if ( xComp.is() )
358 xComp->dispose();
361 LayoutTest::LayoutTest( char const* installDir )
362 : mInstallDir( OUString::createFromAscii ( installDir ) )
366 LayoutTest layout_test( "/usr/local/lib/ooo" );