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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_tools.hxx"
26 
27 #include "tools/testtoolloader.hxx"
28 #include <osl/module.h>
29 #include <rtl/logfile.hxx>
30 #include <vos/process.hxx>
31 #include "tools/solar.h"
32 #include "tools/string.hxx"
33 #include "tools/debug.hxx"
34 
35 #include <comphelper/uieventslogger.hxx>
36 
37 using namespace rtl;
38 
39 namespace tools
40 {
41     typedef void ( *pfunc_CreateRemoteControl)();
42     typedef void ( *pfunc_DestroyRemoteControl)();
43 
44     typedef void ( *pfunc_CreateEventLogger)();
45     typedef void ( *pfunc_DestroyEventLogger)();
46 
47 static oslModule	aTestToolModule	= 0;
48 // are we to be automated at all?
49 static bool bAutomate = false;
50 static bool bLoggerStarted = false;
51 
52 
GetCommandLineParamCount()53 sal_uInt32 GetCommandLineParamCount()
54 {
55 	vos:: OStartupInfo  aStartInfo;
56 	return aStartInfo.getCommandArgCount();
57 }
58 
GetCommandLineParam(sal_uInt32 nParam)59 String GetCommandLineParam( sal_uInt32 nParam )
60 {
61 	vos:: OStartupInfo  aStartInfo;
62 	::rtl::OUString aParam;
63 	vos:: OStartupInfo ::TStartupError eError = aStartInfo.getCommandArg( nParam, aParam );
64 	if ( eError == vos:: OStartupInfo ::E_None )
65 		return String( aParam );
66 	else
67 	{
68 		DBG_ERROR( "Unable to get CommandLineParam" );
69 		return String();
70 	}
71 }
72 
thisModule()73 extern "C" { static void SAL_CALL thisModule() {} }
74 
LoadLib()75 void LoadLib()
76 {
77 	if ( !aTestToolModule )
78 	{
79 		aTestToolModule = osl_loadModuleRelative(
80 			&thisModule,
81 			rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(SVLIBRARY("sts"))).pData,
82 			SAL_LOADMODULE_GLOBAL );
83 	}
84 }
85 
InitTestToolLib()86 void InitTestToolLib()
87 {
88 	RTL_LOGFILE_CONTEXT( aLog, "desktop (cd100003) ::InitTestToolLib" );
89 
90     sal_uInt32 i;
91 
92     for ( i = 0 ; i < GetCommandLineParamCount() ; i++ )
93 	{
94 		if ( GetCommandLineParam( i ).EqualsIgnoreCaseAscii("/enableautomation")
95 			|| GetCommandLineParam( i ).EqualsIgnoreCaseAscii("-enableautomation"))
96         {
97             bAutomate = true;
98             break;
99         }
100     }
101 
102     if ( bAutomate )
103 	{
104 		OUString	aFuncName( RTL_CONSTASCII_USTRINGPARAM( "CreateRemoteControl" ));
105 
106 		LoadLib();
107 		if ( aTestToolModule )
108 		{
109 			oslGenericFunction pInitFunc = osl_getFunctionSymbol(
110 				aTestToolModule, aFuncName.pData );
111 			if ( pInitFunc )
112 				(reinterpret_cast< pfunc_CreateRemoteControl >(pInitFunc))();
113 			else
114 			{
115 				DBG_ERROR1( "Unable to get Symbol 'CreateRemoteControl' from library %s while loading testtool support.", SVLIBRARY( "sts" ) );
116 			}
117 		}
118 		else
119 		{
120 			DBG_ERROR1( "Unable to access library %s while loading testtool support.", SVLIBRARY( "sts" ) );
121 		}
122 	}
123 
124 	if ( ::comphelper::UiEventsLogger::isEnabled() )
125 	{
126 		OUString	aFuncName( RTL_CONSTASCII_USTRINGPARAM( "CreateEventLogger" ));
127 
128 		LoadLib();
129 		if ( aTestToolModule )
130 		{
131 			oslGenericFunction pInitFunc = osl_getFunctionSymbol(
132 				aTestToolModule, aFuncName.pData );
133 			if ( pInitFunc )
134             {
135 				(reinterpret_cast< pfunc_CreateEventLogger >(pInitFunc))();
136                 bLoggerStarted = sal_True;
137             }
138 			else
139 			{
140 				DBG_ERROR1( "Unable to get Symbol 'CreateEventLogger' from library %s while loading testtool support.", SVLIBRARY( "sts" ) );
141 			}
142 		}
143 		else
144 		{
145 			DBG_ERROR1( "Unable to access library %s while loading testtool support.", SVLIBRARY( "sts" ) );
146 		}
147 	}
148 }
149 
DeInitTestToolLib()150 void DeInitTestToolLib()
151 {
152 	if ( aTestToolModule )
153 	{
154 		if ( bAutomate )
155 		{
156 			OUString	aFuncName( RTL_CONSTASCII_USTRINGPARAM( "DestroyRemoteControl" ));
157 
158 			oslGenericFunction pDeInitFunc = osl_getFunctionSymbol(
159 				aTestToolModule, aFuncName.pData );
160 			if ( pDeInitFunc )
161 				(reinterpret_cast< pfunc_DestroyRemoteControl >(pDeInitFunc))();
162 		}
163 
164 		if ( bLoggerStarted /*::comphelper::UiEventsLogger::isEnabled()*/ )
165 		{
166 			OUString	aFuncName( RTL_CONSTASCII_USTRINGPARAM( "DestroyEventLogger" ));
167 
168 			oslGenericFunction pDeInitFunc = osl_getFunctionSymbol(
169 				aTestToolModule, aFuncName.pData );
170 			if ( pDeInitFunc )
171             {
172 				(reinterpret_cast< pfunc_DestroyEventLogger >(pDeInitFunc))();
173                 bLoggerStarted = sal_False;
174             }
175 		}
176 
177 		osl_unloadModule( aTestToolModule );
178 	}
179 }
180 
181 } // namespace tools
182