tizen 2.3 release
[framework/web/wearable/wrt-commons.git] / tests / dao / TestCases_CustomHandlerDAO.cpp
1 /*
2  * Copyright (c) 2012 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_CustomHandlerDAO.cpp
18  * @author  Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version 1.0
20  * @brief   This file contains tests for custom handler dao class.
21  */
22
23 #include <dpl/test/test_runner.h>
24 #include <dpl/foreach.h>
25 #include <wrt-commons/custom-handler-dao-rw/custom_handler_dao.h>
26 #include <wrt-commons/custom-handler-dao-ro/common_dao_types.h>
27
28 using namespace CustomHandlerDB;
29
30 RUNNER_TEST_GROUP_INIT(DAO)
31
32 namespace {
33 const DPL::String P_TARGET(L"p_target");
34 const DPL::String P_BASE_URL(L"p_base_url");
35 const DPL::String P_URL(L"p_url");
36 const DPL::String P_TITLE(L"p_title");
37
38 const DPL::String C_TARGET(L"c_target");
39 const DPL::String C_BASE_URL(L"c_base_url");
40 const DPL::String C_URL(L"c_url");
41 const DPL::String C_TITLE(L"c_title");
42
43 void checkHandlersExistence(CustomHandlerDAOReadOnly& dao,
44                             bool protocol,
45                             bool content)
46 {
47     CustomHandlerDB::CustomHandlerPtr handler;
48     handler = dao.getProtocolHandler(P_TARGET, P_URL);
49     RUNNER_ASSERT_MSG((!!handler) == protocol, "Protocol handler check");
50     handler = dao.getContentHandler(C_TARGET, C_URL);
51     RUNNER_ASSERT_MSG((!!handler) == content, "Content handler check");
52 }
53 } // namespace
54
55 RUNNER_TEST(custom_handler_empty_db_read)
56 {
57     CustomHandlerDAOReadOnly dao(DPL::String(L"test"));
58 }
59
60 RUNNER_TEST(custom_handlers)
61 {
62     CustomHandlerDAOReadOnly dao_ro(L"test");
63     CustomHandlerDAO dao_rw(L"test");
64
65     CustomHandlerDB::CustomHandlerPtr handler;
66     CustomHandlerDB::CustomHandler p_handler;
67     p_handler.target = P_TARGET;
68     p_handler.base_url = P_BASE_URL;
69     p_handler.url = P_URL;
70     p_handler.title = P_TITLE;
71     p_handler.user_decision = Agreed;
72
73     // initial check
74     checkHandlersExistence(dao_ro, false, false);
75
76     // Protocol handler registration
77     dao_rw.registerProtocolHandler(p_handler);
78     checkHandlersExistence(dao_ro, true, false);
79
80     handler = dao_ro.getProtocolHandler(P_TARGET, P_URL);
81     RUNNER_ASSERT(handler);
82     RUNNER_ASSERT(handler->target == P_TARGET);
83     RUNNER_ASSERT(handler->base_url == P_BASE_URL);
84     RUNNER_ASSERT(handler->url == P_URL);
85     RUNNER_ASSERT(handler->title == P_TITLE);
86     RUNNER_ASSERT(handler->user_decision == Agreed);
87
88     // Content handler registration
89     CustomHandlerDB::CustomHandler c_handler;
90     c_handler.target = C_TARGET;
91     c_handler.base_url = C_BASE_URL;
92     c_handler.url = C_URL;
93     c_handler.title = C_TITLE;
94     c_handler.user_decision = DeclinedPermanently;
95
96     dao_rw.registerContentHandler(c_handler);
97     checkHandlersExistence(dao_ro, true, true);
98     handler = dao_ro.getContentHandler(C_TARGET, C_URL);
99
100     RUNNER_ASSERT(handler);
101     RUNNER_ASSERT(handler->target == C_TARGET);
102     RUNNER_ASSERT(handler->base_url == C_BASE_URL);
103     RUNNER_ASSERT(handler->url == C_URL);
104     RUNNER_ASSERT(handler->title == C_TITLE);
105     RUNNER_ASSERT(handler->user_decision == DeclinedPermanently);
106
107     // Handler unregistration
108     dao_rw.unregisterProtocolHandler(P_TARGET, P_URL);
109     checkHandlersExistence(dao_ro, false, true);
110
111     // Nonexistent unregistration
112     dao_rw.unregisterContentHandler(L"blah", L"blah");
113     checkHandlersExistence(dao_ro, false, true);
114
115     // Cleanup
116     dao_rw.unregisterContentHandler(C_TARGET, C_URL);
117     checkHandlersExistence(dao_ro, false, false);
118 }
119
120 RUNNER_TEST(custom_handler_unregister)
121 {
122     CustomHandlerDAOReadOnly dao_ro(L"test");
123     CustomHandlerDAO dao_rw(L"test");
124
125     // initial check
126     checkHandlersExistence(dao_ro, false, false);
127
128     CustomHandlerDB::CustomHandler p_handler;
129     p_handler.target = P_TARGET;
130     p_handler.base_url = P_BASE_URL;
131     p_handler.url = P_URL;
132     p_handler.title = P_TITLE;
133     p_handler.user_decision = Agreed;
134
135     // Protocol handler registration
136     dao_rw.registerProtocolHandler(p_handler);
137     checkHandlersExistence(dao_ro, true, false);
138
139     // Content handler registration
140     CustomHandlerDB::CustomHandler c_handler;
141     c_handler.target = C_TARGET;
142     c_handler.base_url = C_BASE_URL;
143     c_handler.url = C_URL;
144     c_handler.title = C_TITLE;
145     c_handler.user_decision = DeclinedPermanently;
146
147     dao_rw.registerContentHandler(c_handler);
148     checkHandlersExistence(dao_ro, true, true);
149
150     // Handler unregistration
151     dao_rw.unregisterProtocolHandler(P_TARGET, P_URL, P_BASE_URL);
152     checkHandlersExistence(dao_ro, false, true);
153
154     // Cleanup
155     dao_rw.unregisterContentHandler(C_TARGET, C_URL, C_BASE_URL);
156     checkHandlersExistence(dao_ro, false, false);
157 }
158
159 RUNNER_TEST(custom_handler_update)
160 {
161     CustomHandlerDAOReadOnly dao_ro(L"test");
162     CustomHandlerDAO dao_rw(L"test");
163
164     // initial check
165     checkHandlersExistence(dao_ro, false, false);
166
167     CustomHandlerDB::CustomHandler p_handler;
168     p_handler.target = P_TARGET;
169     p_handler.base_url = P_BASE_URL;
170     p_handler.url = P_URL;
171     p_handler.title = P_TITLE;
172     p_handler.user_decision = Agreed;
173
174     // Protocol handler registration
175     dao_rw.registerProtocolHandler(p_handler);
176     checkHandlersExistence(dao_ro, true, false);
177
178     // Content handler registration
179     CustomHandlerDB::CustomHandlerPtr handler;
180     CustomHandlerDB::CustomHandler c_handler;
181     c_handler.target = C_TARGET;
182     c_handler.base_url = C_BASE_URL;
183     c_handler.url = C_URL;
184     c_handler.title = C_TITLE;
185     c_handler.user_decision = DeclinedPermanently;
186
187     dao_rw.registerContentHandler(c_handler);
188     checkHandlersExistence(dao_ro, true, true);
189
190     p_handler.title = L"newTitle";
191     p_handler.user_decision = AgreedPermanently;
192
193     // Protocol handler update
194     dao_rw.registerProtocolHandler(p_handler);
195
196     handler = dao_ro.getProtocolHandler(P_TARGET, P_URL);
197     RUNNER_ASSERT(handler);
198     RUNNER_ASSERT(handler->target == P_TARGET);
199     RUNNER_ASSERT(handler->base_url == P_BASE_URL);
200     RUNNER_ASSERT(handler->url == P_URL);
201     RUNNER_ASSERT(handler->title == L"newTitle");
202     RUNNER_ASSERT(handler->user_decision == AgreedPermanently);
203
204     c_handler.title =  L"newTitle2";
205     c_handler.user_decision = Agreed;
206     // Content handler update
207     dao_rw.registerContentHandler(c_handler);
208
209     handler = dao_ro.getContentHandler(C_TARGET, C_URL);
210
211     RUNNER_ASSERT(handler);
212     RUNNER_ASSERT(handler->target == C_TARGET);
213     RUNNER_ASSERT(handler->base_url == C_BASE_URL);
214     RUNNER_ASSERT(handler->url == C_URL);
215     RUNNER_ASSERT(handler->title == L"newTitle2");
216     RUNNER_ASSERT(handler->user_decision == Agreed);
217
218     // Handler unregistration
219     dao_rw.removeWidgetProtocolHandlers();
220     checkHandlersExistence(dao_ro, false, true);
221
222     // Cleanup
223     dao_rw.removeWidgetContentHandlers();
224     checkHandlersExistence(dao_ro, false, false);
225 }
226
227 RUNNER_TEST(custom_handler_get_active_protocol)
228 {
229     CustomHandlerDAOReadOnly dao_ro(L"test");
230     CustomHandlerDAO dao_rw(L"test");
231
232     CustomHandlerDB::CustomHandler p_handler;
233     p_handler.target = P_TARGET;
234     p_handler.base_url = P_BASE_URL;
235     p_handler.url = L"url1";
236     p_handler.title = L"title1";
237     p_handler.user_decision = DeclinedPermanently;
238     // Protocol handler registration
239     dao_rw.registerProtocolHandler(p_handler);
240
241     CustomHandlerDB::CustomHandlerPtr handler = dao_ro.getActivProtocolHandler(P_TARGET);
242     RUNNER_ASSERT(!handler);
243
244     CustomHandlerDB::CustomHandler p_handler2;
245     p_handler2.target = P_TARGET;
246     p_handler2.base_url = P_BASE_URL;
247     p_handler2.url = L"url2";
248     p_handler2.title = L"title2";
249     p_handler2.user_decision = AgreedPermanently;
250     // Protocol handler registration
251     dao_rw.registerProtocolHandler(p_handler2);
252
253     handler = dao_ro.getActivProtocolHandler(P_TARGET);
254     RUNNER_ASSERT(handler);
255     RUNNER_ASSERT(handler->target == P_TARGET);
256     RUNNER_ASSERT(handler->base_url == P_BASE_URL);
257     RUNNER_ASSERT(handler->url == L"url2");
258     RUNNER_ASSERT(handler->title == L"title2");
259     RUNNER_ASSERT(handler->user_decision == AgreedPermanently);
260
261     // Handler unregistration
262     dao_rw.removeWidgetProtocolHandlers();
263 }
264
265 RUNNER_TEST(custom_handler_get_active_content)
266 {
267     CustomHandlerDAOReadOnly dao_ro(L"test");
268     CustomHandlerDAO dao_rw(L"test");
269
270     CustomHandlerDB::CustomHandler c_handler;
271     c_handler.target = C_TARGET;
272     c_handler.base_url = C_BASE_URL;
273     c_handler.url = L"url2";
274     c_handler.title = L"title2";
275     c_handler.user_decision = DeclinedPermanently;
276     // Protocol handler registration
277     dao_rw.registerContentHandler(c_handler);
278
279     CustomHandlerDB::CustomHandlerPtr handler = dao_ro.getActivProtocolHandler(C_TARGET);
280     RUNNER_ASSERT(!handler);
281
282     CustomHandlerDB::CustomHandler c_handler2;
283     c_handler2.target = C_TARGET;
284     c_handler2.base_url = C_BASE_URL;
285     c_handler2.url = L"url1";
286     c_handler2.title = C_TITLE;
287     c_handler2.user_decision = Agreed;
288     // Protocol handler registration
289     dao_rw.registerContentHandler(c_handler2);
290
291     handler = dao_ro.getActivContentHandler(C_TARGET);
292     RUNNER_ASSERT(handler);
293     RUNNER_ASSERT(handler->target == C_TARGET);
294     RUNNER_ASSERT(handler->base_url == C_BASE_URL);
295     RUNNER_ASSERT(handler->url == L"url1");
296     RUNNER_ASSERT(handler->title == C_TITLE);
297     RUNNER_ASSERT(handler->user_decision == Agreed);
298
299     // Handler unregistration
300     dao_rw.removeWidgetContentHandlers();
301 }