tdf#164037 Check property exists before accessing it
[LibreOffice.git] / svtools / source / config / test / test.cxx
blobc84d2ca9f808a0c925c07398f937134e817a7971
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 // switches
22 // use it to enable test scenarios
25 #define TEST_DYNAMICMENUOPTIONS
27 #include <unotools/dynamicmenuoptions.hxx>
29 #include <cppuhelper/bootstrap.hxx>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/lang/XInitialization.hpp>
32 #include <com/sun/star/registry/XSimpleRegistry.hpp>
34 #include <cppuhelper/servicefactory.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <com/sun/star/uno/Reference.h>
37 #include <com/sun/star/uno/Sequence.h>
39 #include <rtl/ustring>
40 #include <rtl/ustrbuf.hxx>
41 #include <osl/diagnose.h>
42 #include <sal/log.hxx>
44 #include <vcl/event.hxx>
45 #include <vcl/svapp.hxx>
47 using namespace ::osl ;
48 using namespace ::comphelper ;
49 using namespace ::com::sun::star::uno ;
50 using namespace ::com::sun::star::lang ;
51 using namespace ::com::sun::star::beans ;
52 using namespace ::com::sun::star::registry ;
54 class TestApplication : public Application
57 // interface
59 public:
60 void Main();
63 // test methods
65 private:
66 void impl_testDynamicMenuOptions();
69 // helper methods
71 private:
72 static Reference< XMultiServiceFactory > getUNOServiceManager();
75 // member
77 private:
79 }; // class TestApplication
82 // global variables
85 TestApplication aTestApplication ;
88 // main
91 void TestApplication::Main()
93 /**-***********************************************************************************************************
94 initialize program
95 **************************************************************************************************************/
97 // Init global servicemanager and set it for external services.
98 ::comphelper::setProcessServiceFactory( TestApplication::getUNOServiceManager() );
99 // Control success of operation.
100 OSL_ENSURE( !(::comphelper::getProcessServiceFactory()!=TestApplication::getUNOServiceManager()), "TestApplication::Main() Global servicemanager not right initialized." );
102 /**-***********************************************************************************************************
103 test area
104 **************************************************************************************************************/
106 #ifdef TEST_DYNAMICMENUOPTIONS
107 impl_testDynamicMenuOptions();
108 #endif
110 // Execute();
111 OSL_FAIL( "Test was successful!" );
115 // test configuration of dynamic menus "New" and "Wizard"
117 void TestApplication::impl_testDynamicMenuOptions()
119 SvtDynamicMenuOptions aCFG;
121 // Test:
122 // read menus
123 // if( menus == empty )
124 // {
125 // fill it with samples
126 // read it again
127 // }
128 // output content
130 Sequence< Sequence< PropertyValue > > lNewMenu = aCFG.GetMenu( EDynamicMenuType::NewMenu );
131 Sequence< Sequence< PropertyValue > > lWizardMenu = aCFG.GetMenu( EDynamicMenuType::WizardMenu );
133 if( lNewMenu.getLength() < 1 )
135 aCFG.AppendItem( EDynamicMenuType::NewMenu, "private:factory/swriter", "new writer", "icon_writer", "_blank");
136 aCFG.AppendItem( EDynamicMenuType::NewMenu, "private:factory/scalc", "new calc", "icon_calc", "_blank");
137 aCFG.AppendItem( EDynamicMenuType::NewMenu, "private:factory/sdraw", "new draw", "icon_draw", "_blank");
139 lNewMenu = aCFG.GetMenu( EDynamicMenuType::NewMenu );
142 if( lWizardMenu.getLength() < 1 )
144 aCFG.AppendItem( EDynamicMenuType::WizardMenu, "file://a", "system file", "icon_file", "_self");
145 aCFG.AppendItem( EDynamicMenuType::WizardMenu, "ftp://b", "ftp host", "icon_ftp", "_self");
146 aCFG.AppendItem( EDynamicMenuType::WizardMenu, "http://c", "www", "icon_www", "_self");
148 lWizardMenu = aCFG.GetMenu( EDynamicMenuType::WizardMenu );
151 sal_uInt32 nItemCount ;
152 sal_uInt32 nItem ;
153 sal_uInt32 nPropertyCount;
154 sal_uInt32 nProperty ;
155 OUString sPropertyValue;
156 OUStringBuffer sOut( 5000 ) ;
158 nItemCount = lNewMenu.getLength();
159 for( nItem=0; nItem<nItemCount; ++nItem )
161 nPropertyCount = lNewMenu[nItem].getLength();
162 for( nProperty=0; nProperty<nPropertyCount; ++nProperty )
164 lNewMenu[nItem][nProperty].Value >>= sPropertyValue;
166 sOut.appendAscii ( "New/" );
167 sOut.append ( (sal_Int32)nItem );
168 sOut.appendAscii ( "/" );
169 sOut.append ( lNewMenu[nItem][nProperty].Name );
170 sOut.appendAscii ( " = " );
171 sOut.append ( sPropertyValue );
172 sOut.appendAscii ( "\n" );
176 sOut.appendAscii("\n--------------------------------------\n");
178 nItemCount = lWizardMenu.getLength();
179 for( nItem=0; nItem<nItemCount; ++nItem )
181 nPropertyCount = lNewMenu[nItem].getLength();
182 for( nProperty=0; nProperty<nPropertyCount; ++nProperty )
184 lWizardMenu[nItem][nProperty].Value >>= sPropertyValue;
186 sOut.appendAscii ( "Wizard/" );
187 sOut.append ( (sal_Int32)nItem );
188 sOut.appendAscii ( "/" );
189 sOut.append ( lNewMenu[nItem][nProperty].Name );
190 sOut.appendAscii ( " = " );
191 sOut.append ( sPropertyValue );
192 sOut.appendAscii ( "\n" );
196 SAL_WARN( "svtools", sOut );
200 // create new uno servicemanager by using normal applicat.rdb and user.rdb of an office installation!
201 // Don't use this application at the same time like the office!
203 Reference< XMultiServiceFactory > TestApplication::getUNOServiceManager()
205 static Reference< XMultiServiceFactory > smgr;
206 if( ! smgr.is() )
208 Reference< XComponentContext > rCtx =
209 cppu::defaultBootstrap_InitialComponentContext();
210 smgr.set( rCtx->getServiceManager() , UNO_QUERY );
212 return smgr;
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */