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