xref: /aoo4110/main/toolkit/workben/layout/test.cxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <vcl/svapp.hxx>
25 
26 // This works and was used before for standalone test, not sure why
27 // we'd want it.
28 #define LAYOUT_WEAK 1
29 #include "uno.hxx"
30 
31 #include <cstdio>
32 #include <cstdlib>
33 #include <sys/stat.h>
34 
35 #include <com/sun/star/xml/sax/SAXException.hpp>
36 #include <comphelper/processfactory.hxx>
37 #include <cppuhelper/bootstrap.hxx>
38 #include <rtl/ustring.hxx>
39 #include <ucbhelper/configurationkeys.hxx>
40 #include <ucbhelper/contentbroker.hxx>
41 
42 #define SORT_DLG 1 /* requires sfx2, svx to be compiled */
43 #if SORT_DLG
44 #include "scitems.hxx"
45 #include "uiitems.hxx"
46 #endif /* SORT_DLG */
47 
48 #include "editor.hxx"
49 
50 #include "plugin.hxx"
51 #undef _LAYOUT_POST_HXX
52 
53 #include "recover.hxx"
54 #undef _LAYOUT_POST_HXX
55 
56 #if SORT_DLG
57 #include "sortdlg.hxx"
58 #undef _LAYOUT_POST_HXX
59 #endif /* SORT_DLG */
60 
61 #include "wordcountdialog.hxx"
62 #undef _LAYOUT_POST_HXX
63 
64 #include "zoom.hxx"
65 #undef _LAYOUT_POST_HXX
66 
67 #include <layout/layout-pre.hxx>
68 
69 using namespace ::rtl;
70 using namespace ::cppu;
71 using namespace ::com::sun::star;
72 using namespace ::com::sun::star::uno;
73 
74 class LayoutTest : public Application
75 {
76     Reference< XComponentContext > mxContext;
77     Reference< lang::XMultiServiceFactory > mxMSF;
78     OUString mInstallDir;
79     OUString mTestDialog;
80     bool mEditMode;
81     std::list< OUString > mFiles;
82 
83 public:
84     LayoutTest( char const* installDir );
85 
86     void RunEditor();
87     void RunFiles();
88     void ExceptionalMain();
89     void Init();
90     void InitUCB();
91     void LoadFile( OUString const &aName );
92     void Main();
93     void ParseCommandLine();
94 };
95 
usage()96 static void usage()
97 {
98     fprintf (stderr, "usage: test [--inst OOO_INSTALL_PREFIX] [DIALOG.XML]... | --test [DIALOG.XML]\n" );
99     exit( 2 );
100 }
101 
get_factory(char const * service)102 static uno::Reference< lang::XSingleServiceFactory > get_factory( char const *service )
103 {
104     uno::Reference< lang::XSingleServiceFactory > xFactory(
105         comphelper::createProcessComponent(
106             rtl::OUString::createFromAscii( service ) ), uno::UNO_QUERY );
107 
108     if ( !xFactory.is() )
109         fprintf( stderr, "error loading: %s\n", service );
110     return xFactory;
111 }
112 
113 #define GET_FACTORY(x) get_factory( #x )
114 
LoadFile(const OUString & aTestFile)115 void LayoutTest::LoadFile( const OUString &aTestFile )
116 {
117     fprintf( stderr, "TEST: layout instance\n" );
118 
119     uno::Reference< lang::XSingleServiceFactory > xFactory
120         = GET_FACTORY( com.sun.star.awt.Layout );
121     if ( !xFactory.is() )
122     {
123         fprintf( stderr, "Layout engine not installed\n" );
124         throw uno::RuntimeException(
125             rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Layout engine not installed" ) ),
126             uno::Reference< uno::XInterface >() );
127     }
128     fprintf( stderr, "TEST: initing root\n" );
129 
130     uno::Sequence< uno::Any > aParams( 1 );
131     aParams[0] <<= aTestFile;
132 
133     uno::Reference< awt::XLayoutRoot > xRoot (
134         xFactory->createInstanceWithArguments( aParams ),
135         uno::UNO_QUERY );
136 
137     fprintf( stderr, "TEST: file loaded\n" );
138 }
139 
InitUCB()140 void LayoutTest::InitUCB()
141 {
142     OUString aEmpty;
143     Sequence< Any > aArgs( 6 );
144     aArgs[0]
145         <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY1_LOCAL );
146     aArgs[1]
147         <<= OUString::createFromAscii( UCB_CONFIGURATION_KEY2_OFFICE );
148     aArgs[2] <<= OUString::createFromAscii( "PIPE" );
149     aArgs[3] <<= aEmpty;
150     aArgs[4] <<= OUString::createFromAscii( "PORTAL" );
151     aArgs[5] <<= aEmpty;
152 
153     if ( !::ucbhelper::ContentBroker::initialize( mxMSF, aArgs ) )
154     {
155         fprintf( stderr, "Failed to init content broker\n" );
156         fprintf( stderr, "arg[0]: %s\n", UCB_CONFIGURATION_KEY1_LOCAL );
157         fprintf( stderr, "arg[1]: %s\n", UCB_CONFIGURATION_KEY2_OFFICE );
158     }
159 }
160 
support_upstream_brand_prefix()161 static void support_upstream_brand_prefix ()
162 {
163     if ( char const* inst = getenv( "OOO_INSTALL_PREFIX" ) )
164     {
165         char const *brand_prefix = "/openoffice.org3";
166         OUString brand_dir = OUString::createFromAscii( inst )
167             + OUString::createFromAscii( brand_prefix );
168         struct stat stat_info;
169         if ( !stat ( OUSTRING_CSTR( brand_dir ), &stat_info ) )
170         {
171             OSL_TRACE( "Appending %s to OOO_INSTALL_PREFIX", brand_prefix );
172             setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( brand_dir ), 1 );
173         }
174     }
175 }
176 
Init()177 void LayoutTest::Init()
178 {
179     ParseCommandLine();
180     setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( mInstallDir ), 0 );
181     support_upstream_brand_prefix ();
182     OSL_TRACE( "OOO_INSTALL_PREFIX=%s", getenv( "OOO_INSTALL_PREFIX" ) );
183 
184     mxContext = defaultBootstrap_InitialComponentContext();
185     mxMSF = new UnoBootstrapLayout( Reference< lang::XMultiServiceFactory >( mxContext->getServiceManager(), UNO_QUERY ) );
186     ::comphelper::setProcessServiceFactory( mxMSF );
187     InitUCB();
188 }
189 
ParseCommandLine()190 void LayoutTest::ParseCommandLine()
191 {
192     printf ("%s\n", __PRETTY_FUNCTION__);
193     for ( sal_uInt16 i = 0; i < GetCommandLineParamCount(); i++ )
194     {
195         OUString aParam = OUString( GetCommandLineParam( i ) );
196         if ( aParam.equalsAscii( "-h" ) || aParam.equalsAscii( "--help" ) )
197             usage();
198         if ( aParam.equalsAscii( "--inst" ) )
199         {
200             if ( i >= GetCommandLineParamCount() - 1)
201                 usage();
202             mInstallDir = GetCommandLineParam( ++i );
203             setenv( "OOO_INSTALL_PREFIX", OUSTRING_CSTR( mInstallDir ), 1 );
204         }
205         else if ( aParam.equalsAscii( "--test" ) )
206         {
207             mTestDialog = OUString::createFromAscii( "zoom" );
208             if (i + 1 < GetCommandLineParamCount())
209                 mTestDialog = GetCommandLineParam( ++i );
210         }
211         else if ( aParam.equalsAscii( "--editor" ) )
212             mEditMode = true;
213         else
214             mFiles.push_back( aParam );
215     }
216 
217     if ( mFiles.size() <= 0 )
218         mFiles.push_back( OUString::createFromAscii( "layout.xml" ) );
219 }
220 
RunEditor()221 void LayoutTest::RunEditor()
222 {
223     OUString aFile;
224     if ( !mFiles.empty()
225         && mFiles.front().compareToAscii( "layout.xml" ) != 0 )
226         aFile = mFiles.front();
227     Editor editor( mxMSF, aFile );
228     editor.Show();
229     editor.Execute();
230 }
231 
RunDialog(Dialog & dialog)232 short RunDialog( Dialog& dialog )
233 {
234     dialog.Show();
235     short result = dialog.Execute();
236     fprintf( stderr, "Done: dialog execute exited:%d\n", result);
237     return result;
238 }
239 
240 #undef Dialog
RunDialog(::Dialog & dialog)241 short RunDialog( ::Dialog& dialog )
242 {
243     dialog.Show();
244     short result = dialog.Execute();
245     fprintf( stderr, "Done: dialog execute exited:%d\n", result);
246     return result;
247 }
248 
249 #if SORT_DLG
LoadSC()250 static void LoadSC()
251 {
252     get_factory( "com.sun.star.comp.sfx2.DocumentTemplates" );
253     get_factory( "com.sun.star.comp.Calc.SpreadsheetDocument" );
254     GET_FACTORY( com.sun.star.i18n.Transliteration.l10n );
255 }
256 #endif /* SORT_DLG */
257 
TestDialog(OUString const & name)258 void TestDialog( OUString const& name )
259 {
260     if ( 0 )
261         ;
262     else if ( name.equalsAscii( "plugin" ) )
263     {
264         PluginDialog plugin ( 0 );
265         RunDialog( plugin );
266     }
267     else if ( name.equalsAscii( "query" ) )
268     {
269         QueryBox query ( 0, "Do you want to do?", "do");
270         RunDialog( query );
271     }
272     else if ( name.equalsAscii( "query-compat" ) )
273     {
274         QueryBox query ( 0,
275                          WinBits( WB_YES_NO | WB_DEF_YES ),
276 //                         WinBits( WB_ABORT_RETRY_IGNORE ),
277                          OUString::createFromAscii ("Do you want to do?"));
278         RunDialog( query );
279     }
280     else if ( name.equalsAscii( "recover" ) )
281     {
282         SvxRecoverDialog recover ( 0 );
283         RunDialog( recover );
284     }
285 #if SORT_DLG
286     else if ( name.equalsAscii( "sort" ) )
287     {
288         LoadSC();
289         ScSortDlg sort (0, 0);
290         RunDialog( sort );
291     }
292 #endif /* SORT_DLG */
293     else if ( name.equalsAscii( "wordcount" ) )
294     {
295         SwWordCountDialog words ( 0 );
296         RunDialog( words );
297     }
298     else if ( name.equalsAscii( "zoom" ) )
299     {
300         SvxZoomDialog zoom( 0 );
301         RunDialog( zoom );
302     }
303 }
304 
RunFiles()305 void LayoutTest::RunFiles()
306 {
307     fprintf( stderr, "TEST: loading files\n" );
308     for ( std::list< OUString >::iterator  i = mFiles.begin(); i != mFiles.end(); i++ )
309         LoadFile( *i );
310     fprintf( stderr, "TEST: executing\n" );
311     Execute();
312     fprintf( stderr, "TEST: done executing\n" );
313 }
314 
ExceptionalMain()315 void LayoutTest::ExceptionalMain()
316 {
317     if ( mTestDialog.getLength() )
318         TestDialog( mTestDialog );
319     else if ( mEditMode )
320         RunEditor();
321     else
322         RunFiles();
323 }
324 
Main()325 void LayoutTest::Main()
326 {
327     try
328     {
329         ExceptionalMain();
330     }
331     catch (xml::sax::SAXException & rExc)
332     {
333         OString aStr( OUStringToOString( rExc.Message,
334                                          RTL_TEXTENCODING_ASCII_US ) );
335         uno::Exception exc;
336         if (rExc.WrappedException >>= exc)
337         {
338             aStr += OString( " >>> " );
339             aStr += OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US );
340         }
341         fprintf (stderr, "Parsing error: '%s'\n", aStr.getStr());
342         OSL_ENSURE( 0, aStr.getStr() );
343     }
344     catch ( uno::Exception & rExc )
345     {
346         OString aStr( OUStringToOString( rExc.Message,
347                                          RTL_TEXTENCODING_ASCII_US ) );
348         fprintf (stderr, "UNO error: '%s'\n", aStr.getStr());
349         OSL_ENSURE( 0, aStr.getStr() );
350     }
351 
352     Reference< lang::XComponent > xComp( mxContext, UNO_QUERY );
353     if ( xComp.is() )
354         xComp->dispose();
355 }
356 
LayoutTest(char const * installDir)357 LayoutTest::LayoutTest( char const* installDir )
358     : mInstallDir( OUString::createFromAscii ( installDir ) )
359 {
360 }
361 
362 LayoutTest layout_test( "/usr/local/lib/ooo" );
363