tizen 2.4 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 #include <dpl/wrt-dao-ro/widget_dao_read_only.h>
31
32 using namespace WrtDB;
33 using namespace WrtDB::PropertyDAOReadOnly;
34
35 // Widgets used "tizenid201", "tizenid202", "tizenid203", 2003(saved by
36 // wrt_dao_tests_prepare_db.sh)
37
38 #define RUNNER_ASSERT_WHAT_EQUALS(in, test)                               \
39     { std::string tmp(in);                                                \
40       RUNNER_ASSERT_MSG(tmp == test, "Equals: [" + tmp + "]"); }
41
42 #define RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(in, test)                      \
43     {                                                                     \
44         if (!in) { RUNNER_ASSERT_MSG(false, "NULL"); }                    \
45         else { RUNNER_ASSERT_WHAT_EQUALS(DPL::ToUTF8String(*in), test); } \
46     }
47
48 RUNNER_TEST_GROUP_INIT(DAO)
49
50
51 /*
52  * Name: property_dao_get_lists
53  * Description: tests returning list of properties for given id
54  * Expected: data received should match those, which were inserted in prepare
55  * script
56  */
57 RUNNER_TEST(property_dao_get_lists)
58 {
59     { //property list
60         struct three_field{
61             WrtDB::TizenAppId   tappid;
62             DbWidgetHandle      whandleid;
63             size_t              nrow;
64         };
65
66         three_field f3;
67         std::list<three_field> prefList;
68         std::list<three_field>::iterator it;
69
70         f3.tappid=L"tizenid201";f3.whandleid=2000;f3.nrow=2;
71         prefList.push_back(f3);
72         f3.tappid=L"tizenid202"; f3.whandleid=2001;f3.nrow=1;
73         prefList.push_back(f3);
74         f3.tappid=L"tizenid203"; f3.whandleid=2002;f3.nrow=2;
75         prefList.push_back(f3);
76         f3.tappid=L"tizenid204"; f3.whandleid=2003;f3.nrow=0;
77         prefList.push_back(f3);
78
79         for(it=prefList.begin();it!=prefList.end();++it)
80         {
81             PropertyDAOReadOnly::WidgetPreferenceList prefs_tid =
82                     PropertyDAOReadOnly::GetPropertyList(it->tappid);
83             RUNNER_ASSERT(prefs_tid.size() == it->nrow);
84         }
85     }
86     { //property key list
87         WidgetPropertyKeyList orig_2000;
88         orig_2000.push_back(DPL::FromUTF8String("key1_for_2000"));
89         orig_2000.push_back(DPL::FromUTF8String("key2_for_2000"));
90
91         WidgetPropertyKeyList orig_2001;
92         orig_2001.push_back(DPL::FromUTF8String("key1_for_2001"));
93
94         WidgetPropertyKeyList orig_2002;
95         orig_2002.push_back(DPL::FromUTF8String("key1_for_2002"));
96         orig_2002.push_back(DPL::FromUTF8String("key2_for_2002"));
97
98         std::map<WrtDB::TizenAppId, WidgetPropertyKeyList *> prefsKeyMap;
99         prefsKeyMap.insert(std::pair<WrtDB::TizenAppId, WidgetPropertyKeyList *>(
100                                L"tizenid201", &orig_2000));
101         prefsKeyMap.insert(std::pair<WrtDB::TizenAppId, WidgetPropertyKeyList *>(
102                                L"tizenid202", &orig_2001));
103         prefsKeyMap.insert(std::pair<WrtDB::TizenAppId, WidgetPropertyKeyList *>(
104                                L"tizenid203", &orig_2002));
105
106         FOREACH(it_out, prefsKeyMap) {
107             WidgetPropertyKeyList got = PropertyDAOReadOnly::GetPropertyKeyList(
108                     it_out->first);
109             RUNNER_ASSERT(got.size() == it_out->second->size());
110             //TODO
111             //            FOREACH(it_in, got)
112             //            {
113             //                RUNNER_ASSERT(it_out->second.
114             //            }
115         }
116     }
117 }
118
119
120 /*
121  * Name: property_dao_get_list_exceptions
122  * Description: tests returning exception for given tizen_appid if is
123  * not inserted into WidgetInfo table
124  * Expected: procedure passes when is exception. Widget absent in the WidgetInfo
125  * table.
126  */
127 RUNNER_TEST(property_dao_get_list_exceptions)
128 {
129     {
130         bool assert_flag;
131         WrtDB::TizenAppId app_id_non_exists = L"non_exists";
132
133         assert_flag=true;
134         Try{
135             PropertyDAOReadOnly::WidgetPreferenceList prefs =
136                     PropertyDAOReadOnly::GetPropertyList(app_id_non_exists);
137         } Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
138         {
139             assert_flag=false;
140         }
141         RUNNER_ASSERT_MSG(!(assert_flag),"Error, value doesn't make exception");
142     }
143 }
144
145
146 /*
147  * Name: property_dao_set_update_remove
148  * Description: tests set new property for widget, updating property and
149  * removing it
150  * Expected: given operation should works
151  */
152 RUNNER_TEST(property_dao_set_update_remove)
153 {
154     WidgetPropertyKeyList keys = PropertyDAOReadOnly::GetPropertyKeyList(
155             L"tizenid201");
156
157     //ADD
158     PropertyDAO::SetProperty(L"tizenid201",
159                              DPL::FromUTF8String("new_key"),
160                              DPL::FromUTF8String("new_value1"));
161
162     RUNNER_ASSERT_MSG(
163         keys.size() + 1 ==
164         PropertyDAOReadOnly::GetPropertyKeyList(L"tizenid201").size(),
165         "new property not added");
166     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
167         PropertyDAOReadOnly::GetPropertyValue(L"tizenid201",
168                                               DPL::FromUTF8String("new_key")),
169         "new_value1");
170
171     //UPDATE
172     PropertyDAO::SetProperty(L"tizenid201",
173                              DPL::FromUTF8String("new_key"),
174                              DPL::FromUTF8String("new_value2"));
175     RUNNER_ASSERT_MSG(
176         keys.size() + 1 ==
177         PropertyDAOReadOnly::GetPropertyKeyList(L"tizenid201").size(),
178         "new property not added");
179     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
180         PropertyDAOReadOnly::GetPropertyValue(L"tizenid201",
181                                               DPL::FromUTF8String("new_key")),
182         "new_value2");
183
184     //REMOVE
185     PropertyDAO::RemoveProperty(L"tizenid201", DPL::FromUTF8String("new_key"));
186
187     RUNNER_ASSERT_MSG(
188         keys.size() == PropertyDAOReadOnly::GetPropertyKeyList(
189             L"tizenid201").size(),
190         "property not removed");
191 }
192
193 /*
194  * Name: property_dao_get_value
195  * Description: tests if properties can be received from database
196  * Expected: value, which were inserted before test, should be present
197  */
198 RUNNER_TEST(property_dao_get_value)
199 {
200     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
201         PropertyDAOReadOnly::GetPropertyValue(
202             L"tizenid201", DPL::FromUTF8String("key1_for_2000")),
203         "value_for_key1_2000");
204
205     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
206         PropertyDAOReadOnly::GetPropertyValue(
207             L"tizenid201", DPL::FromUTF8String("key2_for_2000")),
208         "value_for_key2_2000");
209 }
210
211 #undef RUNNER_ASSERT_WHAT_EQUALS