AddOn manager
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor-internal / utc-Dali-AddOns.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 #include <dali/dali.h>
18 #include <dali-test-suite-utils.h>
19 #include <dali/integration-api/addon-manager.h>
20 #include <dali/internal/addons/common/addon-manager-factory.h>
21 #include <dali/devel-api/common/addon-binder.h>
22
23 std::unique_ptr<Dali::Integration::AddOnManager> CreateAddOnManager()
24 {
25   // Set env variables
26   setenv( "DALI_ADDONS_PATH", ADDON_LIBS_PATH, 1 );
27   setenv( "DALI_ADDONS_LIBS", "libSampleAddOn.so", 1);
28
29   return std::unique_ptr<Dali::Integration::AddOnManager>( Dali::Internal::AddOnManagerFactory::CreateAddOnManager() );
30 }
31
32 struct TestAddOn : public Dali::AddOn::AddOnBinder
33 {
34   TestAddOn() : AddOnBinder( "SampleAddOn", 0u )
35   {}
36
37   ADDON_BIND_FUNCTION( GetLifecycleStatus, bool() );
38 };
39
40 int UtcDaliTestAddOnInterface(void)
41 {
42   TestApplication application;
43   // Create AddOnManager using internal factory
44   auto addOnManager = CreateAddOnManager();
45
46   TestAddOn addon;
47
48   DALI_TEST_EQUALS( addon.IsValid(), true, TEST_LOCATION );
49
50   const auto& info = addon.GetAddOnInfo();
51
52   // Test returned addon version and type
53   DALI_TEST_EQUALS( info.version, Dali::DALI_ADDON_VERSION( 1, 0 , 0), TEST_LOCATION );
54   DALI_TEST_EQUALS( info.type, Dali::AddOnType::GENERIC, TEST_LOCATION );
55
56   // Test lifecycle
57   addOnManager->Pause();
58
59   auto result1 = addon.GetLifecycleStatus();
60   DALI_TEST_EQUALS( result1, true, TEST_LOCATION );
61
62   addOnManager->Resume();
63   auto result2 = addon.GetLifecycleStatus();
64   DALI_TEST_EQUALS( result2, false, TEST_LOCATION );
65
66   END_TEST;
67 }
68
69 int UtcDaliTestAddOnManager(void)
70 {
71   TestApplication application;
72
73   // Create AddOnManager using internal factory
74   auto addOnManagerUPTR = CreateAddOnManager();
75
76   // Get addon-manager
77   auto* addonManager = Dali::Integration::AddOnManager::Get();
78
79   bool result = addonManager != nullptr;
80
81   DALI_TEST_EQUALS( result, true, TEST_LOCATION );
82
83   auto availableAddons = addonManager->EnumerateAddOns();
84
85   // must be 1 addon available
86   DALI_TEST_EQUALS( availableAddons.size(), 1u, TEST_LOCATION );
87
88   Dali::AddOnInfo info{};
89   addonManager->GetAddOnInfo( availableAddons[0], info );
90
91   // Test returned addon version and type
92   DALI_TEST_EQUALS( info.version, Dali::DALI_ADDON_VERSION( 1, 0 , 0), TEST_LOCATION );
93   DALI_TEST_EQUALS( info.type, Dali::AddOnType::GENERIC, TEST_LOCATION );
94
95   // Get addon handle
96   auto testAddon = addonManager->GetAddOn( availableAddons[0] );
97   result = testAddon != 0;
98   DALI_TEST_EQUALS( result, true, TEST_LOCATION );
99
100   // Get addon global function
101   auto createInstance = addonManager->GetGlobalProc<void*()>( testAddon, "CreateInstance");
102   result = createInstance != nullptr;
103   DALI_TEST_EQUALS( result, true, TEST_LOCATION );
104
105   // Test for false positive (queried function must not be found)
106   auto dummyfunction = addonManager->GetGlobalProc<void*()>( testAddon, "ThisFunctionDoesntExist");
107   result = dummyfunction == nullptr;
108   DALI_TEST_EQUALS( result, true, TEST_LOCATION );
109
110   // Get Instance function and call it, expect answer 42
111   auto instanceFunction = addonManager->GetInstanceProc<uint32_t(void*)>( testAddon, "InstanceCall" );
112   auto* instance = createInstance();
113   auto answer = instanceFunction( instance );
114   DALI_TEST_EQUALS( answer, 42, TEST_LOCATION );
115
116   // Test lifecycle
117   auto GetLifecycleStatus = addonManager->GetGlobalProc<bool()>( testAddon, "GetLifecycleStatus");
118   addonManager->Pause();
119
120   DALI_TEST_EQUALS( GetLifecycleStatus(), true, TEST_LOCATION );
121
122   addonManager->Resume();
123   DALI_TEST_EQUALS( GetLifecycleStatus(), false, TEST_LOCATION );
124
125   END_TEST;
126 }