merge with master
[platform/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 "tizenid201", "tizenid202", "tizenid203", 2003(saved by
35 // wrt_dao_tests_prepare_db.sh)
36
37 #define RUNNER_ASSERT_WHAT_EQUALS(in, test)                   \
38     { std::string tmp(in);                                     \
39       RUNNER_ASSERT_MSG(tmp == test, "Equals: [" + tmp + "]"); }
40
41 #define RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(in, test)          \
42     {                                                     \
43         if (in.IsNull()) { RUNNER_ASSERT_MSG(false, "NULL"); } \
44         else { RUNNER_ASSERT_WHAT_EQUALS(DPL::ToUTF8String(*in), test); } \
45     }
46
47 RUNNER_TEST_GROUP_INIT(DAO)
48
49 /*
50  * Name: property_dao_get_lists
51  * Description: tests returning list of properties for given id
52  * Expected: data received should match those, which were inserted in prepare
53  * script
54  */
55 RUNNER_TEST(property_dao_get_lists)
56 {
57     { //property list
58         std::map<WrtDB::TizenAppId, size_t> prefsMap;
59         prefsMap.insert(std::pair<WrtDB::TizenAppId, size_t>(L"tizenid201", 2));
60         prefsMap.insert(std::pair<WrtDB::TizenAppId, size_t>(L"tizenid202", 1));
61         prefsMap.insert(std::pair<WrtDB::TizenAppId, size_t>(L"tizenid203", 2));
62         //no widget
63         prefsMap.insert(std::pair<WrtDB::TizenAppId, size_t>(L"non_exists", 0));
64
65         FOREACH(it, prefsMap) {
66             PropertyDAOReadOnly::WidgetPreferenceList prefs =
67                 PropertyDAOReadOnly::GetPropertyList(it->first);
68             RUNNER_ASSERT(prefs.size() == it->second);
69         }
70     }
71
72     { //property key list
73         WidgetPropertyKeyList orig_2000;
74         orig_2000.push_back(DPL::FromUTF8String("key1_for_2000"));
75         orig_2000.push_back(DPL::FromUTF8String("key2_for_2000"));
76
77         WidgetPropertyKeyList orig_2001;
78         orig_2001.push_back(DPL::FromUTF8String("key1_for_2001"));
79
80         WidgetPropertyKeyList orig_2002;
81         orig_2002.push_back(DPL::FromUTF8String("key1_for_2002"));
82         orig_2002.push_back(DPL::FromUTF8String("key2_for_2002"));
83
84         std::map<WrtDB::TizenAppId, WidgetPropertyKeyList *> prefsKeyMap;
85         prefsKeyMap.insert(std::pair<WrtDB::TizenAppId, WidgetPropertyKeyList *>(
86                                L"tizenid201", &orig_2000));
87         prefsKeyMap.insert(std::pair<WrtDB::TizenAppId, WidgetPropertyKeyList *>(
88                                L"tizenid202", &orig_2001));
89         prefsKeyMap.insert(std::pair<WrtDB::TizenAppId, WidgetPropertyKeyList *>(
90                                L"tizenid203", &orig_2002));
91
92         FOREACH(it_out, prefsKeyMap) {
93             WidgetPropertyKeyList got = PropertyDAOReadOnly::GetPropertyKeyList(
94                     it_out->first);
95             RUNNER_ASSERT(got.size() == it_out->second->size());
96             //TODO
97             //            FOREACH(it_in, got)
98             //            {
99             //                RUNNER_ASSERT(it_out->second.
100             //            }
101         }
102     }
103 }
104
105 /*
106  * Name: property_dao_set_update_remove
107  * Description: tests set new property for widget, updating property and
108  * removing it
109  * Expected: given operation should works
110  */
111 RUNNER_TEST(property_dao_set_update_remove)
112 {
113     WidgetPropertyKeyList keys = PropertyDAOReadOnly::GetPropertyKeyList(
114             L"tizenid201");
115
116     //ADD
117     PropertyDAO::SetProperty(L"tizenid201",
118                              DPL::FromUTF8String("new_key"),
119                              DPL::FromUTF8String("new_value1"));
120
121     RUNNER_ASSERT_MSG(
122         keys.size() + 1 ==
123         PropertyDAOReadOnly::GetPropertyKeyList(L"tizenid201").size(),
124         "new property not added");
125     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
126         PropertyDAOReadOnly::GetPropertyValue(L"tizenid201",
127                                               DPL::FromUTF8String("new_key")),
128         "new_value1");
129
130     //UPDATE
131     PropertyDAO::SetProperty(L"tizenid201",
132                              DPL::FromUTF8String("new_key"),
133                              DPL::FromUTF8String("new_value2"));
134     RUNNER_ASSERT_MSG(
135         keys.size() + 1 ==
136         PropertyDAOReadOnly::GetPropertyKeyList(L"tizenid201").size(),
137         "new property not added");
138     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
139         PropertyDAOReadOnly::GetPropertyValue(L"tizenid201",
140                                               DPL::FromUTF8String("new_key")),
141         "new_value2");
142
143     //REMOVE
144     PropertyDAO::RemoveProperty(L"tizenid201", DPL::FromUTF8String("new_key"));
145
146     RUNNER_ASSERT_MSG(
147         keys.size() == PropertyDAOReadOnly::GetPropertyKeyList(
148             L"tizenid201").size(),
149         "property not removed");
150 }
151
152 /*
153  * Name: property_dao_get_value
154  * Description: tests if properties can be received from database
155  * Expected: value, which were inserted before test, should be present
156  */
157 RUNNER_TEST(property_dao_get_value)
158 {
159     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
160         PropertyDAOReadOnly::GetPropertyValue(
161             L"tizenid201", DPL::FromUTF8String("key1_for_2000")),
162         "value_for_key1_2000");
163
164     RUNNER_ASSERT_WHAT_EQUALS_OPTIONAL(
165         PropertyDAOReadOnly::GetPropertyValue(
166             L"tizenid201", DPL::FromUTF8String("key2_for_2000")),
167         "value_for_key2_2000");
168 }
169
170 #undef RUNNER_ASSERT_WHAT_EQUALS