Updates for const->constexpr
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-internal / dali-toolkit-test-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 DALI_ADDONS_PATH
24 #define DALI_ADDONS_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(DALI_ADDONS_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   memset( lineBuf, 0, 256 );
43   size_t n = 256;
44   while( getline( &lineBuf, &n, fin ) > 0 )
45   {
46     char* c = lineBuf;
47     while( *c )
48     {
49       if( *c == '\n' || *c == '\r' )
50       {
51         *c = 0;
52         break;
53       }
54       ++c;
55     }
56     tet_printf("Adding %s\n", lineBuf);
57     addons.emplace_back( lineBuf );
58     memset( lineBuf, 0, 256 );
59   }
60   fclose(fin);
61   delete [] lineBuf;
62   std::vector<std::string> retval{};
63   // Open addons
64   for( auto& name : addons )
65   {
66     std::string path(DALI_ADDONS_PATH);
67     path += "/";
68     path += name;
69
70     mAddOnCache.emplace_back();
71     mAddOnCache.back().handle = dlopen( path.c_str(), RTLD_DEEPBIND|RTLD_LAZY );
72     if( !mAddOnCache.back().handle )
73     {
74       mAddOnCache.back().valid = false;
75       tet_printf( "Can't open addon lib: %s, error: '%s'\n", path.c_str(), dlerror());
76       continue;
77     }
78     // Here addon must self register
79     if( !mAddOnCache.back().valid )
80     {
81       puts("Addon invalid!");
82     }
83     else
84     {
85       tet_printf( "Valid AddOn: %s\n", mAddOnCache.back().name.c_str() );
86       retval.emplace_back( mAddOnCache.back().name );
87     }
88   }
89
90   return retval;
91
92   /**
93    * Check for self-registering addons
94    */
95 }
96
97 void AddOnManager::RegisterAddOnDispatchTable( const AddOnDispatchTable* dispatchTable )
98 {
99   // Register the dispatch table
100   auto& entry = mAddOnCache.back();
101   entry.name = dispatchTable->name;
102   tet_printf( "Registering AddOn: %s\n", entry.name.c_str());
103   entry.GetGlobalProc = dispatchTable->GetGlobalProc;
104   entry.GetInstanceProc = dispatchTable->GetInstanceProc;
105   entry.GetAddOnInfo = dispatchTable->GetAddOnInfo;
106   entry.OnStart = dispatchTable->OnStart;
107   entry.OnStop = dispatchTable->OnStop;
108   entry.OnPause = dispatchTable->OnPause;
109   entry.OnResume = dispatchTable->OnResume;
110   entry.valid = true;
111 }
112
113 bool AddOnManager::GetAddOnInfo(const std::string& name, AddOnInfo& info )
114 {
115   auto retval = false;
116   std::find_if( mAddOnCache.begin(), mAddOnCache.end(),
117     [&retval, name, &info]( AddOnCacheEntry& entry )
118   {
119     if(entry.name == name)
120     {
121       entry.GetAddOnInfo( info );
122       retval = true;
123       return true;
124     }
125     return false;
126   });
127   return retval;
128 }
129
130 std::vector<AddOnLibrary> AddOnManager::LoadAddOns( const std::vector<std::string>& addonNames )
131 {
132   if(mAddOnCache.empty())
133   {
134     EnumerateAddOns();
135   }
136
137   std::vector<AddOnLibrary> retval{};
138   for( auto& name : addonNames)
139   {
140     size_t index = 0;
141     auto iter = std::find_if( mAddOnCache.begin(), mAddOnCache.end(),
142       [&retval, name, &index]( AddOnCacheEntry& entry )
143       {
144         index++;
145         if(entry.name == name)
146         {
147           return true;
148         }
149         return false;
150       });
151     if( iter != mAddOnCache.end() )
152     {
153       retval.emplace_back( *reinterpret_cast<void**>( &index ) );
154     }
155     else
156     {
157       retval.emplace_back( nullptr );
158     }
159   }
160
161   return retval;
162 }
163
164 void* AddOnManager::GetGlobalProc( const Dali::AddOnLibrary& addOnLibrary, const char* procName )
165 {
166   auto index = *reinterpret_cast<const size_t*>( &addOnLibrary );
167   return mAddOnCache[index-1].GetGlobalProc( procName );
168 }
169
170 void* AddOnManager::GetInstanceProc( const Dali::AddOnLibrary& addOnLibrary, const char* procName )
171 {
172   auto index = *reinterpret_cast<const size_t*>( &addOnLibrary );
173   return mAddOnCache[index-1].GetInstanceProc( procName );
174 }
175
176 void AddOnManager::Start()
177 {
178   for( auto& entry : mAddOnCache )
179   {
180     if(entry.OnStart)
181     {
182       entry.OnStart();
183     }
184   }
185 }
186
187 void AddOnManager::Resume()
188 {
189   for( auto& entry : mAddOnCache )
190   {
191     if(entry.OnResume)
192     {
193       entry.OnResume();
194     }
195   }
196 }
197
198 void AddOnManager::Stop()
199 {
200   for( auto& entry : mAddOnCache )
201   {
202     if(entry.OnStop)
203     {
204       entry.OnStop();
205     }
206   }
207 }
208
209 void AddOnManager::Pause()
210 {
211   for( auto& entry : mAddOnCache )
212   {
213     if(entry.OnPause)
214     {
215       entry.OnPause();
216     }
217   }
218 }
219
220 }
221 }
222