307e0a0c504c337d2f3ac3351a022ef25d4cc6d8
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / dali-test-suite-utils / test-addon-manager.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "test-addon-manager.h"
19 #include <dali-test-suite-utils.h>
20 #include <cstring>
21 #include <dlfcn.h>
22
23 #ifndef ADDON_LIBS_PATH
24 #define ADDON_LIBS_PATH ""
25 #endif
26
27 namespace Dali
28 {
29 namespace Test
30 {
31
32 std::vector<std::string> AddOnManager::EnumerateAddOns()
33 {
34   std::string listFileName(ADDON_LIBS_PATH);
35   listFileName += "/addons.txt";
36
37   // Read list of available test addons
38   tet_printf("Enumerating addons, file: %s\n", listFileName.c_str());
39   std::vector<std::string> addons{};
40   auto* fin = fopen( listFileName.c_str(), "r" );
41   char* lineBuf = new char[256];
42   size_t n = 256;
43   while( getline( &lineBuf, &n, fin ) > 0 )
44   {
45     tet_printf("Adding %s\n", lineBuf);
46     addons.emplace_back( lineBuf );
47   }
48   fclose(fin);
49   delete [] lineBuf;
50   std::vector<std::string> retval{};
51   // Open addons
52   for( auto& name : addons )
53   {
54     std::string path(ADDON_LIBS_PATH);
55     path += "/";
56     path += name;
57
58     mAddOnCache.emplace_back();
59     mAddOnCache.back().handle = dlopen( path.c_str(), RTLD_DEEPBIND|RTLD_LAZY );
60     if( !mAddOnCache.back().handle )
61     {
62       mAddOnCache.back().valid = false;
63       tet_printf( "Can't open addon lib: %s\n", path.c_str());
64       continue;
65     }
66     // Here addon must self register
67     if( !mAddOnCache.back().valid )
68     {
69       puts("Addon invalid!");
70     }
71     else
72     {
73       tet_printf( "Valid AddOn: %s\n", mAddOnCache.back().name.c_str() );
74       retval.emplace_back( mAddOnCache.back().name );
75     }
76   }
77
78   return retval;
79
80   /**
81    * Check for self-registering addons
82    */
83 }
84
85 void AddOnManager::RegisterAddOnDispatchTable( const AddOnDispatchTable* dispatchTable )
86 {
87   // Register the dispatch table
88   auto& entry = mAddOnCache.back();
89   entry.name = dispatchTable->name;
90   tet_printf( "Registering AddOn: %s\n", entry.name.c_str());
91   entry.GetGlobalProc = dispatchTable->GetGlobalProc;
92   entry.GetInstanceProc = dispatchTable->GetInstanceProc;
93   entry.GetAddOnInfo = dispatchTable->GetAddOnInfo;
94   entry.OnStart = dispatchTable->OnStart;
95   entry.OnStop = dispatchTable->OnStop;
96   entry.OnPause = dispatchTable->OnPause;
97   entry.OnResume = dispatchTable->OnResume;
98   entry.valid = true;
99 }
100
101 bool AddOnManager::GetAddOnInfo(const std::string& name, AddOnInfo& info )
102 {
103   auto retval = false;
104   std::find_if( mAddOnCache.begin(), mAddOnCache.end(),
105     [&retval, name, &info]( AddOnCacheEntry& entry )
106   {
107     if(entry.name == name)
108     {
109       entry.GetAddOnInfo( info );
110       retval = true;
111       return true;
112     }
113     return false;
114   });
115   return retval;
116 }
117
118 std::vector<AddOnLibrary> AddOnManager::LoadAddOns( const std::vector<std::string>& addonNames )
119 {
120   if(mAddOnCache.empty())
121   {
122     EnumerateAddOns();
123   }
124
125   std::vector<AddOnLibrary> retval{};
126   for( auto& name : addonNames)
127   {
128     size_t index = 0;
129     auto iter = std::find_if( mAddOnCache.begin(), mAddOnCache.end(),
130       [&retval, name, &index]( AddOnCacheEntry& entry )
131       {
132         index++;
133         if(entry.name == name)
134         {
135           return true;
136         }
137         return false;
138       });
139     if( iter != mAddOnCache.end() )
140     {
141       retval.emplace_back( *reinterpret_cast<void**>( &index ) );
142     }
143     else
144     {
145       retval.emplace_back( nullptr );
146     }
147   }
148
149   return retval;
150 }
151
152 void* AddOnManager::GetGlobalProc( const Dali::AddOnLibrary& addOnLibrary, const char* procName )
153 {
154   auto index = *reinterpret_cast<const size_t*>( &addOnLibrary );
155   return mAddOnCache[index-1].GetGlobalProc( procName );
156 }
157
158 void* AddOnManager::GetInstanceProc( const Dali::AddOnLibrary& addOnLibrary, const char* procName )
159 {
160   auto index = *reinterpret_cast<const size_t*>( &addOnLibrary );
161   return mAddOnCache[index-1].GetInstanceProc( procName );
162 }
163
164 void AddOnManager::Start()
165 {
166   for( auto& entry : mAddOnCache )
167   {
168     if(entry.OnStart)
169     {
170       entry.OnStart();
171     }
172   }
173 }
174
175 void AddOnManager::Resume()
176 {
177   for( auto& entry : mAddOnCache )
178   {
179     if(entry.OnResume)
180     {
181       entry.OnResume();
182     }
183   }
184 }
185
186 void AddOnManager::Stop()
187 {
188   for( auto& entry : mAddOnCache )
189   {
190     if(entry.OnStop)
191     {
192       entry.OnStop();
193     }
194   }
195 }
196
197 void AddOnManager::Pause()
198 {
199   for( auto& entry : mAddOnCache )
200   {
201     if(entry.OnPause)
202     {
203       entry.OnPause();
204     }
205   }
206 }
207
208 }
209 }
210