xref: /aoo42x/main/toolkit/workben/layout/test.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 
28 #include <vcl/svapp.hxx>
29 
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"
34 
35 #include <cstdio>
36 #include <cstdlib>
37 #include <sys/stat.h>
38 
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>
45 
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 */
51 
52 #include "editor.hxx"
53 
54 #include "plugin.hxx"
55 #undef _LAYOUT_POST_HXX
56 
57 #include "recover.hxx"
58 #undef _LAYOUT_POST_HXX
59 
60 #if SORT_DLG
61 #include "sortdlg.hxx"
62 #undef _LAYOUT_POST_HXX
63 #endif /* SORT_DLG */
64 
65 #include "wordcountdialog.hxx"
66 #undef _LAYOUT_POST_HXX
67 
68 #include "zoom.hxx"
69 #undef _LAYOUT_POST_HXX
70 
71 #include <layout/layout-pre.hxx>
72 
73 using namespace ::rtl;
74 using namespace ::cppu;
75 using namespace ::com::sun::star;
76 using namespace ::com::sun::star::uno;
77 
78 class LayoutTest : public Application
79 {
80     Reference< XComponentContext > mxContext;
81     Reference< lang::XMultiServiceFactory > mxMSF;
82     OUString mInstallDir;
83     OUString mTestDialog;
84     bool mEditMode;
85     std::list< OUString > mFiles;
86 
87 public:
88     LayoutTest( char const* installDir );
89 
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();
98 };
99 
100 static void usage()
101 {
102     fprintf (stderr, "usage: test [--inst OOO_INSTALL_PREFIX] [DIALOG.XML]... | --test [DIALOG.XML]\n" );
103     exit( 2 );
104 }
105 
106 static uno::Reference< lang::XSingleServiceFactory > get_factory( char const *service )
107 {
108     uno::Reference< lang::XSingleServiceFactory > xFactory(
109         comphelper::createProcessComponent(
110             rtl::OUString::createFromAscii( service ) ), uno::UNO_QUERY );
111 
112     if ( !xFactory.is() )
113         fprintf( stderr, "error loading: %s\n", service );
114     return xFactory;
115 }
116 
117 #define GET_FACTORY(x) get_factory( #x )
118 
119 void LayoutTest::LoadFile( const OUString &aTestFile )
120 {
121     fprintf( stderr, "TEST: layout instance\n" );
122 
123     uno::Reference< lang::XSingleServiceFactory > xFactory
124         = GET_FACTORY( com.sun.star.awt.Layout );
125     if ( !xFactory.is() )
126     {
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 >() );
131     }
132     fprintf( stderr, "TEST: initing root\n" );
133 
134     uno::Sequence< uno::Any > aParams( 1 );
135     aParams[0] <<= aTestFile;
136 
137     uno::Reference< awt::XLayoutRoot > xRoot (
138         xFactory->createInstanceWithArguments( aParams ),
139         uno::UNO_QUERY );
140 
141     fprintf( stderr, "TEST: file loaded\n" );
142 }
143 
144 void LayoutTest::InitUCB()
145 {
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;
156 
157     if ( !::ucbhelper::ContentBroker::initialize( mxMSF, aArgs ) )
158     {
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 );
162     }
163 }
164 
165 static void support_upstream_brand_prefix ()
166 {
167     if ( char const* inst = getenv( "OOO_INSTALL_PREFIX" ) )
168     {
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 ) )
174         {
175             OSL_TRACE( "Appending %s to OOO_INSTALL_PREFIX", brand_prefix );
176             setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( brand_dir ), 1 );
177         }
178     }
179 }
180 
181 void LayoutTest::Init()
182 {
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" ) );
187 
188     mxContext = defaultBootstrap_InitialComponentContext();
189     mxMSF = new UnoBootstrapLayout( Reference< lang::XMultiServiceFactory >( mxContext->getServiceManager(), UNO_QUERY ) );
190     ::comphelper::setProcessServiceFactory( mxMSF );
191     InitUCB();
192 }
193 
194 void LayoutTest::ParseCommandLine()
195 {
196     printf ("%s\n", __PRETTY_FUNCTION__);
197     for ( sal_uInt16 i = 0; i < GetCommandLineParamCount(); i++ )
198     {
199         OUString aParam = OUString( GetCommandLineParam( i ) );
200         if ( aParam.equalsAscii( "-h" ) || aParam.equalsAscii( "--help" ) )
201             usage();
202         if ( aParam.equalsAscii( "--inst" ) )
203         {
204             if ( i >= GetCommandLineParamCount() - 1)
205                 usage();
206             mInstallDir = GetCommandLineParam( ++i );
207             setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( mInstallDir ), 1 );
208         }
209         else if ( aParam.equalsAscii( "--test" ) )
210         {
211             mTestDialog = OUString::createFromAscii( "zoom" );
212             if (i + 1 < GetCommandLineParamCount())
213                 mTestDialog = GetCommandLineParam( ++i );
214         }
215         else if ( aParam.equalsAscii( "--editor" ) )
216             mEditMode = true;
217         else
218             mFiles.push_back( aParam );
219     }
220 
221     if ( mFiles.size() <= 0 )
222         mFiles.push_back( OUString::createFromAscii( "layout.xml" ) );
223 }
224 
225 void LayoutTest::RunEditor()
226 {
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();
234 }
235 
236 short RunDialog( Dialog& dialog )
237 {
238     dialog.Show();
239     short result = dialog.Execute();
240     fprintf( stderr, "Done: dialog execute exited:%d\n", result);
241     return result;
242 }
243 
244 #undef Dialog
245 short RunDialog( ::Dialog& dialog )
246 {
247     dialog.Show();
248     short result = dialog.Execute();
249     fprintf( stderr, "Done: dialog execute exited:%d\n", result);
250     return result;
251 }
252 
253 #if SORT_DLG
254 static void LoadSC()
255 {
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 );
259 }
260 #endif /* SORT_DLG */
261 
262 void TestDialog( OUString const& name )
263 {
264     if ( 0 )
265         ;
266     else if ( name.equalsAscii( "plugin" ) )
267     {
268         PluginDialog plugin ( 0 );
269         RunDialog( plugin );
270     }
271     else if ( name.equalsAscii( "query" ) )
272     {
273         QueryBox query ( 0, "Do you want to do?", "do");
274         RunDialog( query );
275     }
276     else if ( name.equalsAscii( "query-compat" ) )
277     {
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 );
283     }
284     else if ( name.equalsAscii( "recover" ) )
285     {
286         SvxRecoverDialog recover ( 0 );
287         RunDialog( recover );
288     }
289 #if SORT_DLG
290     else if ( name.equalsAscii( "sort" ) )
291     {
292         LoadSC();
293         ScSortDlg sort (0, 0);
294         RunDialog( sort );
295     }
296 #endif /* SORT_DLG */
297     else if ( name.equalsAscii( "wordcount" ) )
298     {
299         SwWordCountDialog words ( 0 );
300         RunDialog( words );
301     }
302     else if ( name.equalsAscii( "zoom" ) )
303     {
304         SvxZoomDialog zoom( 0 );
305         RunDialog( zoom );
306     }
307 }
308 
309 void LayoutTest::RunFiles()
310 {
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" );
317 }
318 
319 void LayoutTest::ExceptionalMain()
320 {
321     if ( mTestDialog.getLength() )
322         TestDialog( mTestDialog );
323     else if ( mEditMode )
324         RunEditor();
325     else
326         RunFiles();
327 }
328 
329 void LayoutTest::Main()
330 {
331     try
332     {
333         ExceptionalMain();
334     }
335     catch (xml::sax::SAXException & rExc)
336     {
337         OString aStr( OUStringToOString( rExc.Message,
338                                          RTL_TEXTENCODING_ASCII_US ) );
339         uno::Exception exc;
340         if (rExc.WrappedException >>= exc)
341         {
342             aStr += OString( " >>> " );
343             aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
344         }
345         fprintf (stderr, "Parsing error: '%s'\n", aStr.getStr());
346         OSL_ENSURE( 0, aStr.getStr() );
347     }
348     catch ( uno::Exception & rExc )
349     {
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() );
354     }
355 
356     Reference< lang::XComponent > xComp( mxContext, UNO_QUERY );
357     if ( xComp.is() )
358         xComp->dispose();
359 }
360 
361 LayoutTest::LayoutTest( char const* installDir )
362     : mInstallDir( OUString::createFromAscii ( installDir ) )
363 {
364 }
365 
366 LayoutTest layout_test( "/usr/local/lib/ooo" );
367