Tizen 2.0 Release
[framework/web/wrt-commons.git] / tests / dao / TestCases_PropertyDAO.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_PropertyDAO.cpp
18  * @author  Pawel Sikorski (p.sikorski@samsung.com)
19  * @version 1.0
20  * @brief   This file contains tests for property dao class.
21  */
22
23 #include <list>
24 #include <map>
25 #include <dpl/test/test_runner.h>
26 #include <dpl/foreach.h>
27 #include <dpl/exception.h>
28 #include <dpl/wrt-dao-rw/property_dao.h>
29 #include <dpl/wrt-dao-ro/wrt_db_types.h>
30
31 using namespace WrtDB;
32 using namespace WrtDB::PropertyDAOReadOnly;
33
34 // Widgets used 2000, 2001, 2002, 2003(saved by wrt_dao_tests_prepare_db.sh)
35
36 #define RUNNER_ASSERT_WHAT_EQUALS(in, test)                   \
37     {std::string tmp(in);                                     \
38     RUNNER_ASSERT_MSG(tmp == test, "Equals: [" + tmp + "]");}
39
40 #define RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(in, test)          \
41         {                                                     \
42             if(in.IsNull()) RUNNER_ASSERT_MSG(false, "NULL"); \
43             else RUNNER_ASSERT_WHAT_EQUALS(DPL::ToUTF8String(*in),test);\
44         }
45
46 RUNNER_TEST_GROUP_INIT(DAO)
47
48 /*
49 Name: property_dao_get_lists
50 Description: tests returning list of properties for given id
51 Expected: data received should match those, which were inserted in prepare script
52 */
53 RUNNER_TEST(property_dao_get_lists)
54 {
55     {//property list
56         std::map<WidgetHandle, size_t> prefsMap;
57         prefsMap.insert(std::pair<WidgetHandle, size_t>(2000, 2));
58         prefsMap.insert(std::pair<WidgetHandle, size_t>(2001, 1));
59         prefsMap.insert(std::pair<WidgetHandle, size_t>(2002, 2));
60         prefsMap.insert(std::pair<WidgetHandle, size_t>(1, 0)); //no widget
61
62         FOREACH(it, prefsMap) {
63             PropertyDAOReadOnly::WidgetPreferenceList prefs =
64                     PropertyDAOReadOnly::GetPropertyList(it->first);
65             RUNNER_ASSERT(prefs.size() == it->second);
66         }
67     }
68
69     {//property key list
70         WidgetPropertyKeyList orig_2000;
71         orig_2000.push_back(DPL::FromUTF8String("key1_for_2000"));
72         orig_2000.push_back(DPL::FromUTF8String("key2_for_2000"));
73
74         WidgetPropertyKeyList orig_2001;
75         orig_2001.push_back(DPL::FromUTF8String("key1_for_2001"));
76
77         WidgetPropertyKeyList orig_2002;
78         orig_2002.push_back(DPL::FromUTF8String("key1_for_2002"));
79         orig_2002.push_back(DPL::FromUTF8String("key2_for_2002"));
80
81         std::map<WidgetHandle, WidgetPropertyKeyList *> prefsKeyMap;
82         prefsKeyMap.insert(std::pair<WidgetHandle, WidgetPropertyKeyList *>(
83                 2000, &orig_2000));
84         prefsKeyMap.insert(std::pair<WidgetHandle, WidgetPropertyKeyList *>(
85                 2001, &orig_2001));
86         prefsKeyMap.insert(std::pair<WidgetHandle, WidgetPropertyKeyList *>(
87                 2002, &orig_2002));
88
89         FOREACH(it_out, prefsKeyMap) {
90             WidgetPropertyKeyList got = PropertyDAOReadOnly::GetPropertyKeyList(
91                     it_out->first);
92             RUNNER_ASSERT(got.size() == it_out->second->size());
93             //TODO
94             //            FOREACH(it_in, got)
95             //            {
96             //                RUNNER_ASSERT(it_out->second.
97             //            }
98         }
99     }
100 }
101
102 /*
103 Name: property_dao_set_update_remove
104 Description: tests set new property for widget, updating property and removing it
105 Expected: given operation should works
106 */
107 RUNNER_TEST(property_dao_set_update_remove)
108 {
109     WidgetPropertyKeyList keys = PropertyDAOReadOnly::GetPropertyKeyList(2000);
110
111     //ADD
112     PropertyDAO::SetProperty(2000,
113                              DPL::FromUTF8String("new_key"),
114                              DPL::FromUTF8String("new_value1"));
115
116     RUNNER_ASSERT_MSG(
117         keys.size() + 1 == PropertyDAOReadOnly::GetPropertyKeyList(2000).size(),
118         "new property not added");
119     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
120         PropertyDAOReadOnly::GetPropertyValue(2000,
121                                               DPL::FromUTF8String("new_key")),
122         "new_value1");
123
124     //UPDATE
125     PropertyDAO::SetProperty(2000,
126                              DPL::FromUTF8String("new_key"),
127                              DPL::FromUTF8String("new_value2"));
128     RUNNER_ASSERT_MSG(
129         keys.size() + 1 == PropertyDAOReadOnly::GetPropertyKeyList(2000).size(),
130         "new property not added");
131     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
132         PropertyDAOReadOnly::GetPropertyValue(2000,
133                                               DPL::FromUTF8String("new_key")),
134         "new_value2");
135
136     //REMOVE
137     PropertyDAO::RemoveProperty(2000, DPL::FromUTF8String("new_key"));
138
139     RUNNER_ASSERT_MSG(
140         keys.size() == PropertyDAOReadOnly::GetPropertyKeyList(2000).size(),
141         "property not removed");
142
143 }
144
145 /*
146 Name: property_dao_get_value
147 Description: tests if properties can be received from database
148 Expected: value, which were inserted before test, should be present
149 */
150 RUNNER_TEST(property_dao_get_value)
151 {
152     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
153             PropertyDAOReadOnly::GetPropertyValue(
154                     2000, DPL::FromUTF8String("key1_for_2000")),
155                 "value_for_key1_2000");
156
157     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
158             PropertyDAOReadOnly::GetPropertyValue(
159                     2000, DPL::FromUTF8String("key2_for_2000")),
160                 "value_for_key2_2000");
161 }
162
163 #undef RUNNER_ASSERT_WHAT_EQUALS