update dev300-m58
[ooovba.git] / toolkit / workben / layout / test.cxx
blob0d54e2a6ff31e413f13fc82a44507b0129d75f87
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: test.cxx,v $
11 * $Revision: 1.3 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include <vcl/svapp.hxx>
34 // This works and was used before for standalone test, not sure why
35 // we'd want it.
36 #define LAYOUT_WEAK 1
37 #include "uno.hxx"
39 #include <cstdio>
40 #include <cstdlib>
41 #include <sys/stat.h>
43 #include <com/sun/star/xml/sax/SAXException.hpp>
44 #include <comphelper/processfactory.hxx>
45 #include <cppuhelper/bootstrap.hxx>
46 #include <rtl/ustring.hxx>
47 #include <ucbhelper/configurationkeys.hxx>
48 #include <ucbhelper/contentbroker.hxx>
50 #define SORT_DLG 1 /* requires sfx2, svx to be compiled */
51 #if SORT_DLG
52 #include "scitems.hxx"
53 #include "uiitems.hxx"
54 #endif /* SORT_DLG */
56 #include "editor.hxx"
58 #include "plugin.hxx"
59 #undef _LAYOUT_POST_HXX
61 #include "recover.hxx"
62 #undef _LAYOUT_POST_HXX
64 #if SORT_DLG
65 #include "sortdlg.hxx"
66 #undef _LAYOUT_POST_HXX
67 #endif /* SORT_DLG */
69 #include "wordcountdialog.hxx"
70 #undef _LAYOUT_POST_HXX
72 #include "zoom.hxx"
73 #undef _LAYOUT_POST_HXX
75 #include <layout/layout-pre.hxx>
77 using namespace ::rtl;
78 using namespace ::cppu;
79 using namespace ::com::sun::star;
80 using namespace ::com::sun::star::uno;
82 class LayoutTest : public Application
84 Reference< XComponentContext > mxContext;
85 Reference< lang::XMultiServiceFactory > mxMSF;
86 OUString mInstallDir;
87 OUString mTestDialog;
88 bool mEditMode;
89 std::list< OUString > mFiles;
91 public:
92 LayoutTest( char const* installDir );
94 void RunEditor();
95 void RunFiles();
96 void ExceptionalMain();
97 void Init();
98 void InitUCB();
99 void LoadFile( OUString const &aName );
100 void Main();
101 void ParseCommandLine();
104 static void usage()
106 fprintf (stderr, "usage: test [--inst OOO_INSTALL_PREFIX] [DIALOG.XML]... | --test [DIALOG.XML]\n" );
107 exit( 2 );
110 static uno::Reference< lang::XSingleServiceFactory > get_factory( char const *service )
112 uno::Reference< lang::XSingleServiceFactory > xFactory(
113 comphelper::createProcessComponent(
114 rtl::OUString::createFromAscii( service ) ), uno::UNO_QUERY );
116 if ( !xFactory.is() )
117 fprintf( stderr, "error loading: %s\n", service );
118 return xFactory;
121 #define GET_FACTORY(x) get_factory( #x )
123 void LayoutTest::LoadFile( const OUString &aTestFile )
125 fprintf( stderr, "TEST: layout instance\n" );
127 uno::Reference< lang::XSingleServiceFactory > xFactory
128 = GET_FACTORY( com.sun.star.awt.Layout );
129 if ( !xFactory.is() )
131 fprintf( stderr, "Layout engine not installed\n" );
132 throw uno::RuntimeException(
133 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Layout engine not installed" ) ),
134 uno::Reference< uno::XInterface >() );
136 fprintf( stderr, "TEST: initing root\n" );
138 uno::Sequence< uno::Any > aParams( 1 );
139 aParams[0] <<= aTestFile;
141 uno::Reference< awt::XLayoutRoot > xRoot (
142 xFactory->createInstanceWithArguments( aParams ),
143 uno::UNO_QUERY );
145 fprintf( stderr, "TEST: file loaded\n" );
148 void LayoutTest::InitUCB()
150 OUString aEmpty;
151 Sequence< Any > aArgs( 6 );
152 aArgs[0]
153 <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL );
154 aArgs[1]
155 <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE );
156 aArgs[2] <<= OUString::createFromAscii( "PIPE" );
157 aArgs[3] <<= aEmpty;
158 aArgs[4] <<= OUString::createFromAscii( "PORTAL" );
159 aArgs[5] <<= aEmpty;
161 if ( !::ucbhelper::ContentBroker::initialize( mxMSF, aArgs ) )
163 fprintf( stderr, "Failed to init content broker\n" );
164 fprintf( stderr, "arg[0]: %s\n", UCB_CONFIGURATION_KEY1_LOCAL );
165 fprintf( stderr, "arg[1]: %s\n", UCB_CONFIGURATION_KEY2_OFFICE );
169 static void support_upstream_brand_prefix ()
171 if ( char const* inst = getenv( "OOO_INSTALL_PREFIX" ) )
173 char const *brand_prefix = "/openoffice.org3";
174 OUString brand_dir = OUString::createFromAscii( inst )
175 + OUString::createFromAscii( brand_prefix );
176 struct stat stat_info;
177 if ( !stat ( OUSTRING_CSTR( brand_dir ), &stat_info ) )
179 OSL_TRACE( "Appending %s to OOO_INSTALL_PREFIX", brand_prefix );
180 setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( brand_dir ), 1 );
185 void LayoutTest::Init()
187 ParseCommandLine();
188 setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( mInstallDir ), 0 );
189 support_upstream_brand_prefix ();
190 OSL_TRACE( "OOO_INSTALL_PREFIX=%s", getenv( "OOO_INSTALL_PREFIX" ) );
192 mxContext = defaultBootstrap_InitialComponentContext();
193 mxMSF = new UnoBootstrapLayout( Reference< lang::XMultiServiceFactory >( mxContext->getServiceManager(), UNO_QUERY ) );
194 ::comphelper::setProcessServiceFactory( mxMSF );
195 InitUCB();
198 void LayoutTest::ParseCommandLine()
200 printf ("%s\n", __PRETTY_FUNCTION__);
201 for ( sal_uInt16 i = 0; i < GetCommandLineParamCount(); i++ )
203 OUString aParam = OUString( GetCommandLineParam( i ) );
204 if ( aParam.equalsAscii( "-h" ) || aParam.equalsAscii( "--help" ) )
205 usage();
206 if ( aParam.equalsAscii( "--oustring-cstr"))
207 exit (printf ("%s: The strings: %s and %s.\n",
208 __PRETTY_FUNCTION__,
209 OUSTRING_CSTR (OUString::createFromAscii ("foo")),
210 OUSTRING_CSTR (OUString::createFromAscii ("bar"))));
211 if ( aParam.equalsAscii( "--inst" ) )
213 if ( i >= GetCommandLineParamCount() - 1)
214 usage();
215 mInstallDir = GetCommandLineParam( ++i );
216 setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( mInstallDir ), 1 );
218 else if ( aParam.equalsAscii( "--test" ) )
220 mTestDialog = OUString::createFromAscii( "zoom" );
221 if (i + 1 < GetCommandLineParamCount())
222 mTestDialog = GetCommandLineParam( ++i );
224 else if ( aParam.equalsAscii( "--editor" ) )
225 mEditMode = true;
226 else
227 mFiles.push_back( aParam );
230 if ( mFiles.size() <= 0 )
231 mFiles.push_back( OUString::createFromAscii( "layout.xml" ) );
234 void LayoutTest::RunEditor()
236 OUString aFile;
237 if ( !mFiles.empty()
238 && mFiles.front().compareToAscii( "layout.xml" ) != 0 )
239 aFile = mFiles.front();
240 Editor editor( mxMSF, aFile );
241 editor.Show();
242 editor.Execute();
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 #undef Dialog
254 short RunDialog( ::Dialog& dialog )
256 dialog.Show();
257 short result = dialog.Execute();
258 fprintf( stderr, "Done: dialog execute exited:%d\n", result);
259 return result;
262 #if SORT_DLG
263 static void LoadSC()
265 get_factory( "com.sun.star.comp.sfx2.DocumentTemplates" );
266 get_factory( "com.sun.star.comp.Calc.SpreadsheetDocument" );
267 GET_FACTORY( com.sun.star.i18n.Transliteration.l10n );
269 #endif /* SORT_DLG */
271 void TestDialog( OUString const& name )
273 if ( 0 )
275 else if ( name.equalsAscii( "plugin" ) )
277 PluginDialog plugin ( 0 );
278 RunDialog( plugin );
280 else if ( name.equalsAscii( "query" ) )
282 QueryBox query ( 0, "Do you want to do?", "do");
283 RunDialog( query );
285 else if ( name.equalsAscii( "query-compat" ) )
287 QueryBox query ( 0,
288 WinBits( WB_YES_NO | WB_DEF_YES ),
289 // WinBits( WB_ABORT_RETRY_IGNORE ),
290 OUString::createFromAscii ("Do you want to do?"));
291 RunDialog( query );
293 else if ( name.equalsAscii( "recover" ) )
295 SvxRecoverDialog recover ( 0 );
296 RunDialog( recover );
298 #if SORT_DLG
299 else if ( name.equalsAscii( "sort" ) )
301 LoadSC();
302 ScSortDlg sort (0, 0);
303 RunDialog( sort );
305 #endif /* SORT_DLG */
306 else if ( name.equalsAscii( "wordcount" ) )
308 SwWordCountDialog words ( 0 );
309 RunDialog( words );
311 else if ( name.equalsAscii( "zoom" ) )
313 SvxZoomDialog zoom( 0 );
314 RunDialog( zoom );
318 void LayoutTest::RunFiles()
320 fprintf( stderr, "TEST: loading files\n" );
321 for ( std::list< OUString >::iterator i = mFiles.begin(); i != mFiles.end(); i++ )
322 LoadFile( *i );
323 fprintf( stderr, "TEST: executing\n" );
324 Execute();
325 fprintf( stderr, "TEST: done executing\n" );
328 void LayoutTest::ExceptionalMain()
330 if ( mTestDialog.getLength() )
331 TestDialog( mTestDialog );
332 else if ( mEditMode )
333 RunEditor();
334 else
335 RunFiles();
338 void LayoutTest::Main()
342 ExceptionalMain();
344 catch (xml::sax::SAXException & rExc)
346 OString aStr( OUStringToOString( rExc.Message,
347 RTL_TEXTENCODING_ASCII_US ) );
348 uno::Exception exc;
349 if (rExc.WrappedException >>= exc)
351 aStr += OString( " >>> " );
352 aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
354 fprintf (stderr, "Parsing error: '%s'\n", aStr.getStr());
355 OSL_ENSURE( 0, aStr.getStr() );
357 catch ( uno::Exception & rExc )
359 OString aStr( OUStringToOString( rExc.Message,
360 RTL_TEXTENCODING_ASCII_US ) );
361 fprintf (stderr, "UNO error: '%s'\n", aStr.getStr());
362 OSL_ENSURE( 0, aStr.getStr() );
365 Reference< lang::XComponent > xComp( mxContext, UNO_QUERY );
366 if ( xComp.is() )
367 xComp->dispose();
370 LayoutTest::LayoutTest( char const* installDir )
371 : mInstallDir( OUString::createFromAscii ( installDir ) )
375 LayoutTest layout_test( "/usr/local/lib/ooo" );