Tizen 2.0 Release
[framework/web/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 {
34 const DPL::String P_TARGET(L"p_target");
35 const DPL::String P_BASE_URL(L"p_base_url");
36 const DPL::String P_URL(L"p_url");
37 const DPL::String P_TITLE(L"p_title");
38
39 const DPL::String C_TARGET(L"c_target");
40 const DPL::String C_BASE_URL(L"c_base_url");
41 const DPL::String C_URL(L"c_url");
42 const DPL::String C_TITLE(L"c_title");
43
44 void checkHandlersExistence(CustomHandlerDAOReadOnly& dao, bool protocol, bool content)
45 {
46     CustomHandlerDB::CustomHandlerPtr handler;
47     handler = dao.getProtocolHandler(P_TARGET, P_URL);
48     RUNNER_ASSERT_MSG((!!handler) == protocol, "Protocol handler check");
49     handler = dao.getContentHandler(C_TARGET, C_URL);
50     RUNNER_ASSERT_MSG((!!handler) == content, "Content handler check");
51 }
52
53 } // namespace
54
55 RUNNER_TEST(custom_handler_empty_db_read)
56 {
57     CustomHandlerDAOReadOnly dao(DPL::String(L"test"));
58 }
59
60
61 RUNNER_TEST(custom_handlers)
62 {
63     CustomHandlerDAOReadOnly dao_ro(L"test");
64     CustomHandlerDAO dao_rw(L"test");
65
66     CustomHandlerDB::CustomHandlerPtr handler;
67     CustomHandlerDB::CustomHandler p_handler;
68     p_handler.target = P_TARGET;
69     p_handler.base_url = P_BASE_URL;
70     p_handler.url = P_URL;
71     p_handler.title = P_TITLE;
72     p_handler.user_decision = Agreed;
73
74     // initial check
75     checkHandlersExistence(dao_ro,false,false);
76
77     // Protocol handler registration
78     dao_rw.registerProtocolHandler(p_handler);
79     checkHandlersExistence(dao_ro,true,false);
80
81     handler = dao_ro.getProtocolHandler(P_TARGET, P_URL);
82     RUNNER_ASSERT(handler);
83     RUNNER_ASSERT(handler->target == P_TARGET);
84     RUNNER_ASSERT(handler->base_url == P_BASE_URL);
85     RUNNER_ASSERT(handler->url == P_URL);
86     RUNNER_ASSERT(handler->title == P_TITLE);
87     RUNNER_ASSERT(handler->user_decision == Agreed);
88
89
90     // Content handler registration
91     CustomHandlerDB::CustomHandler c_handler;
92     c_handler.target = C_TARGET;
93     c_handler.base_url = C_BASE_URL;
94     c_handler.url = C_URL;
95     c_handler.title = C_TITLE;
96     c_handler.user_decision = DeclinedPermanently;
97
98     dao_rw.registerContentHandler(c_handler);
99     checkHandlersExistence(dao_ro,true,true);
100     handler = dao_ro.getContentHandler(C_TARGET, C_URL);
101
102     RUNNER_ASSERT(handler);
103     RUNNER_ASSERT(handler->target == C_TARGET);
104     RUNNER_ASSERT(handler->base_url == C_BASE_URL);
105     RUNNER_ASSERT(handler->url == C_URL);
106     RUNNER_ASSERT(handler->title == C_TITLE);
107     RUNNER_ASSERT(handler->user_decision == DeclinedPermanently);
108
109     // Handler unregistration
110     dao_rw.unregisterProtocolHandler(P_TARGET, P_URL);
111     checkHandlersExistence(dao_ro,false,true);
112
113     // Nonexistent unregistration
114     dao_rw.unregisterContentHandler(L"blah", L"blah");
115     checkHandlersExistence(dao_ro,false,true);
116
117     // Cleanup
118     dao_rw.unregisterContentHandler(C_TARGET, C_URL);
119     checkHandlersExistence(dao_ro,false,false);
120 }