tizen 2.4 release
[framework/web/wrt-commons.git] / tests / dao / TestCases_FeatureDAO.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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  * @file   TestCases_FeatureDAO.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   This file contains tests for feature dao class.
21  */
22
23 #include <dpl/test/test_runner.h>
24 #include <dpl/foreach.h>
25 #include <dpl/wrt-dao-rw/feature_dao.h>
26 #include <dpl/wrt-dao-rw/plugin_dao.h>
27 #include <dpl/wrt-dao-ro/wrt_db_types.h>
28
29 using namespace WrtDB;
30
31 #define RUNNER_ASSERT_WHAT_EQUALS(in, test)                   \
32     { std::string tmp(in);                                     \
33       RUNNER_ASSERT_MSG(tmp == test, "Equals: [" + tmp + "]"); }
34
35 RUNNER_TEST_GROUP_INIT(DAO)
36
37 /*
38  * Name: feature_dao_test_register_features
39  * Description: Checks if plugin registeration performs features registration
40  *              and if registration is made properly
41  * Expected: registrartion should succeed
42  */
43 RUNNER_TEST(feature_dao_test_register_features)
44 {
45     PluginHandle plHandle;
46     {
47         std::string libraryPath("nfp1 lib_path");
48         std::string libraryName("nfp1");
49
50         PluginMetafileData pluginData;
51         pluginData.m_libraryName = libraryName;
52
53         plHandle = PluginDAO::registerPlugin(pluginData, libraryPath);
54         RUNNER_ASSERT(PluginDAO::isPluginInstalled(libraryName) == true);
55
56         FeatureHandleList old = FeatureDAOReadOnly::GetHandleList();
57         PluginMetafileData::Feature f;
58         f.m_name = std::string("new_f1");
59
60         FeatureHandle handle = FeatureDAO::RegisterFeature(f, plHandle);
61         RUNNER_ASSERT_MSG(handle != -1, "Already registered");
62         RUNNER_ASSERT_MSG(old.size() < FeatureDAOReadOnly::GetHandleList().size(),
63                 "New feature should be saved");
64
65         FeatureDAOReadOnly dao(handle);
66
67         RUNNER_ASSERT_WHAT_EQUALS(dao.GetName(), "new_f1");
68         plHandle = dao.GetPluginHandle();
69     }
70
71     {
72         FeatureHandleList old = FeatureDAOReadOnly::GetHandleList();
73
74         PluginMetafileData::Feature f;
75         f.m_name = std::string("new_f2");
76
77         FeatureHandle handle = FeatureDAO::RegisterFeature(f, plHandle);
78         RUNNER_ASSERT_MSG(handle != -1, "Already registered");
79         RUNNER_ASSERT_MSG(old.size() < FeatureDAOReadOnly::GetHandleList().size(),
80                 "New feature should be saved");
81
82         FeatureDAOReadOnly dao(handle);
83
84         RUNNER_ASSERT_MSG(plHandle == dao.GetPluginHandle(),
85                 "New plugin registered (should be old used)");
86
87         FeatureDAO::UnregisterFeature(handle);
88         PluginDAO::unregisterPlugin(plHandle);
89     }
90 }
91
92 /*
93  * Name: feature_dao_test_get_feature_handle
94  * Description: Checks GetFeatureHandleListForPlugin
95  * Expected: no errors found
96  * directly
97  */
98 RUNNER_TEST(feature_dao_test_get_feature_handle)
99 {
100     PluginHandleSetPtr plugins =
101         PluginDAO::getPluginHandleByStatus(PluginDAO::INSTALLATION_COMPLETED);
102
103     RUNNER_ASSERT(plugins->size() == 5);
104
105     FOREACH(it, *plugins) {
106         FeatureHandleListPtr featureListPtr =
107             FeatureDAOReadOnly::GetFeatureHandleListForPlugin(*it);
108         int size = featureListPtr->size();
109
110         switch(*it)
111         {
112             case 1:
113                 RUNNER_ASSERT(size == 1);
114                 break;
115             case 2:
116                 RUNNER_ASSERT(size == 0);
117                 break;
118             case 3:
119                 RUNNER_ASSERT(size == 0);
120                 break;
121             case 4:
122                 RUNNER_ASSERT(size == 3);
123                 break;
124             case 5:
125                 RUNNER_ASSERT(size == 0);
126                 break;
127             default:
128                 RUNNER_ASSERT_MSG(false, "Wrong plugin handle");
129                 break;
130         }
131     }
132 }
133
134 /*
135  * Name: feature_dao_test_get_device_capability
136  * Description: Checks GetDeviceCapability
137  * Expected: no errors found
138  * directly
139  */
140 RUNNER_TEST(feature_dao_test_get_device_capability)
141 {
142     RUNNER_ASSERT(FeatureDAOReadOnly::GetDeviceCapability(L"feature1").size() == 1);
143     RUNNER_ASSERT(FeatureDAOReadOnly::GetDeviceCapability(L"feature2").size() == 2);
144     RUNNER_ASSERT(FeatureDAOReadOnly::GetDeviceCapability(L"feature3").size() == 1);
145     RUNNER_ASSERT(FeatureDAOReadOnly::GetDeviceCapability(L"feature4").size() == 0);
146 }
147
148 /*
149  * Name: feature_dao_test_unregister
150  * Description: Checks UnregisterFeature
151  * Expected: no errors found
152  */
153 RUNNER_TEST(feature_dao_test_unregister)
154 {
155     RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled("feature5") == false);
156     unsigned int size = FeatureDAOReadOnly::GetHandleList().size();
157
158     PluginHandle plHandle;
159     PluginMetafileData pluginData;
160     pluginData.m_libraryName = "nfp1 lib_path";
161     plHandle = PluginDAO::registerPlugin(pluginData, "lib_path");
162
163     PluginMetafileData::Feature f0;
164     f0.m_name = std::string("feature5");
165     f0.m_deviceCapabilities.insert("1devicecap");
166     f0.m_deviceCapabilities.insert("2devicecap");
167     f0.m_deviceCapabilities.insert("3devicecap");
168
169     FeatureHandle handle0 = FeatureDAO::RegisterFeature(f0, plHandle);
170     RUNNER_ASSERT_MSG(handle0 != -1, "Already registered");
171
172     RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled(handle0) == true);
173     RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled("feature5") == true);
174     RUNNER_ASSERT(FeatureDAOReadOnly::GetHandleList().size() == size + 1);
175
176     FeatureDAO::UnregisterFeature(handle0);
177
178     RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled(handle0) == false);
179     RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled("feature5") == false);
180     RUNNER_ASSERT(FeatureDAOReadOnly::GetHandleList().size() == size);
181
182     PluginMetafileData::Feature f1;
183     f1.m_name = std::string("feature5");
184
185     FeatureHandle handle1 = FeatureDAO::RegisterFeature(f1, plHandle);
186     RUNNER_ASSERT_MSG(handle1 != -1, "Already registered");
187
188     RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled(handle1) == true);
189     RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled("feature5") == true);
190     RUNNER_ASSERT(FeatureDAOReadOnly::GetHandleList().size() == size + 1);
191
192     FeatureDAO::UnregisterFeature(handle1);
193
194     RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled(handle1) == false);
195     RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled("feature5") == false);
196     RUNNER_ASSERT(FeatureDAOReadOnly::GetHandleList().size() == size);
197
198     PluginDAO::unregisterPlugin(plHandle);
199 }
200
201 /*
202  * Name: feature_dao_test_get_device_capabilities
203  * Description: Checks GetDeviceCapabilities
204  * Expected: no errors found
205  */
206 RUNNER_TEST(feature_dao_test_get_device_capabilities)
207 {
208     FeatureDAOReadOnly f1("feature1");
209     RUNNER_ASSERT(f1.GetDeviceCapabilities().size() == 1);
210
211     FeatureDAOReadOnly f2("feature2");
212     RUNNER_ASSERT(f2.GetDeviceCapabilities().size() == 2);
213
214     FeatureDAOReadOnly f3("feature3");
215     RUNNER_ASSERT(f3.GetDeviceCapabilities().size() == 1);
216
217     FeatureDAOReadOnly f4("feature4");
218     RUNNER_ASSERT(f4.GetDeviceCapabilities().size() == 0);
219 }
220
221 /*
222  * Name: feature_dao_test_get_names
223  * Description: Checks GetNames
224  * Expected: no errors found
225  * directly
226  */
227 RUNNER_TEST(feature_dao_test_get_names)
228 {
229     FeatureDAOReadOnly::NameMap names = FeatureDAOReadOnly::GetNames();
230
231     RUNNER_ASSERT(names.size() == 4);
232
233     int count = 0;
234     FOREACH(n, names){
235         if(n->second == "feature1" || n->second == "feature2" ||
236                 n->second == "feature3" || n->second == "feature4")
237             count++;
238         else RUNNER_ASSERT_MSG(false, "Wrong feature name");
239     }
240
241     RUNNER_ASSERT(count == 4);
242 }
243
244 /*
245  * Name: feature_dao_test_dev_cap_with_feature_handle
246  * Description: Checks GetDevCapWithFeatureHandle
247  * Expected: no errors found
248  */
249 RUNNER_TEST(feature_dao_test_dev_cap_with_feature_handle)
250 {
251     FeatureDAOReadOnly::DeviceCapabilitiesMap map = FeatureDAOReadOnly::GetDevCapWithFeatureHandle();
252     RUNNER_ASSERT(map.size() == 4);
253
254     int count = 0;
255     FOREACH(n, map){
256         if(n->second == "devicecap1" || n->second == "devicecap2" ||
257                 n->second == "devicecap3" || n->second == "devicecap4")
258             count++;
259         else RUNNER_ASSERT_MSG(false, "Wrong device capability");
260     }
261
262     RUNNER_ASSERT(count == 4);
263 }
264
265
266 /*
267  * Name: feature_dao_test_get_features
268  * Description: Checks GetFeatures
269  * Expected: no errors
270  */
271 RUNNER_TEST(feature_dao_test_get_features)
272 {
273     std::list<std::string> fs0;
274     RUNNER_ASSERT(FeatureDAOReadOnly::GetFeatures(fs0).size() == 0);
275
276     std::list<std::string> fs1;
277     fs1.push_front("feature1");
278
279     fs1.push_front("feature2");
280     RUNNER_ASSERT(FeatureDAOReadOnly::GetFeatures(fs1).size() == 2);
281     std::list<std::string> fs2;
282     fs2.push_front("feature");
283     fs2.push_front("feature2");
284     RUNNER_ASSERT(FeatureDAOReadOnly::GetFeatures(fs2).size() == 1);
285
286     std::list<std::string> fs3;
287     fs3.push_front("feature1");
288     fs3.push_front("feature2");
289     fs3.push_front("feature3");
290     fs3.push_front("feature4");
291     RUNNER_ASSERT(FeatureDAOReadOnly::GetFeatures(fs3).size() == 4);
292 }
293
294 /*
295  * Name: feature_dao_test_get_feature_properties
296  * Description: Checks properties of inserted features
297  * Expected: properties of features should match values inserted to database
298  * directly
299  */
300 RUNNER_TEST(feature_dao_test_get_feature_properties)
301 {
302     {
303         FeatureDAOReadOnly dao("feature1");
304         RUNNER_ASSERT_WHAT_EQUALS(dao.GetName(), "feature1");
305         RUNNER_ASSERT_WHAT_EQUALS(dao.GetLibraryName(), "plugin1");
306         RUNNER_ASSERT_WHAT_EQUALS(dao.GetLibraryPath(), "");
307     }
308
309     {
310         FeatureDAOReadOnly dao("feature2");
311         RUNNER_ASSERT_WHAT_EQUALS(dao.GetName(), "feature2");
312         RUNNER_ASSERT_WHAT_EQUALS(dao.GetLibraryName(), "p4");
313         RUNNER_ASSERT_WHAT_EQUALS(dao.GetLibraryPath(), "path_to_p4");
314     }
315
316     {
317         FeatureDAOReadOnly dao("feature3");
318         RUNNER_ASSERT_WHAT_EQUALS(dao.GetName(), "feature3");
319         RUNNER_ASSERT_WHAT_EQUALS(dao.GetLibraryName(), "p4");
320         RUNNER_ASSERT_WHAT_EQUALS(dao.GetLibraryPath(), "path_to_p4");
321     }
322 }
323
324 /*
325  * Name: feature_dao_test_feature_constructor_name
326  * Description: -
327  * Expected: -
328  *
329  * TODO: test
330  */
331 RUNNER_TEST(feature_dao_test_feature_constructor_name)
332 {
333     std::list<const char *> preinstalled;
334     preinstalled.push_back("feature1");
335     preinstalled.push_back("feature2");
336     preinstalled.push_back("feature3");
337     preinstalled.push_back("feature4");
338
339     FOREACH(it, preinstalled)
340     {
341         FeatureDAOReadOnly dao(*it);
342         RUNNER_ASSERT_WHAT_EQUALS(dao.GetName(), *it);
343     }
344
345     //TODO check exception that may occur (feature does not exist)
346 }
347
348 /*
349  * Name: feature_dao_test_feature_handle_list
350  * Description: Checks if list of installed features is returend correctly
351  * Expected: list size should be at last equal number of preinserted features
352  */
353 RUNNER_TEST(feature_dao_test_feature_handle_list)
354 {
355     RUNNER_ASSERT(FeatureDAOReadOnly::GetHandleList().size() == 4);
356 }
357
358 /*
359  * Name: feature_dao_test_is_feature_installed
360  * Description: Checks if installed features are showed correctly.
361  * Expected: correct installed features should be present
362  */
363 RUNNER_TEST(feature_dao_test_is_feature_installed)
364 {
365     //installed
366     {
367         std::list<const char *> preinstalled;
368         preinstalled.push_back("feature1");
369         preinstalled.push_back("feature2");
370         preinstalled.push_back("feature3");
371         preinstalled.push_back("feature4");
372
373         FOREACH(it, preinstalled)
374         RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled(*it));
375     }
376
377     //not installed
378     {
379         RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled(
380                           "not_installed1") == false);
381         RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled(
382                           "plugin1") == false);
383         RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled("") == false);
384         RUNNER_ASSERT(FeatureDAOReadOnly::isFeatureInstalled("ff") == false);
385     }
386 }
387
388 /*
389  * Name: feature_dao_test_is_device_capab_installed
390  * Description: Checks if FeatureDAOReadOnly::isDeviceCapabilityInstalled works
391  * correctly.
392  * Expected: correct capabilities should be present
393  */
394 RUNNER_TEST(feature_dao_test_is_device_capab_installed)
395 {
396     //installed
397     std::list<const char *> preinstalled;
398     preinstalled.push_back("devicecap1");
399     preinstalled.push_back("devicecap2");
400     preinstalled.push_back("devicecap3");
401     preinstalled.push_back("devicecap4");
402
403     FOREACH(it, preinstalled)
404     RUNNER_ASSERT(FeatureDAOReadOnly::isDeviceCapabilityInstalled(*it));
405
406     //not installed
407     std::list<const char *> notinstalled;
408     notinstalled.push_back("notinstalled1");
409     notinstalled.push_back("plugin1");
410     notinstalled.push_back("");
411     notinstalled.push_back("ff");
412
413     FOREACH(it, notinstalled)
414     RUNNER_ASSERT(!FeatureDAOReadOnly::isDeviceCapabilityInstalled(*it));
415 }
416
417 #undef RUNNER_ASSERT_WHAT_EQUALS