Removed dead code from SimpleUI
[profile/tv/apps/web/browser.git] / unit_tests / ut_SessionStorage.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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 #include <boost/any.hpp>
18 #include <boost/filesystem.hpp>
19 #include <boost/test/unit_test.hpp>
20 #include <iterator>
21 #include "core/ServiceManager/ServiceManager.h"
22 #include "core/ServiceManager/AbstractService.h"
23 #include "services/SessionStorage/SessionStorage.h"
24 #include "services/SessionStorage/SqlStorage.h"
25 #include "services/SessionStorage/Session.h"
26 #include "AbstractWebEngine/TabId.h"
27 #include <core/Config/Config.h>
28
29 BOOST_AUTO_TEST_SUITE(SessionStorage)
30
31 BOOST_AUTO_TEST_CASE(InitSession)
32 {
33     tizen_browser::config::DefaultConfig config;
34     config.load("not used text");
35     std::string resourceDbDir(boost::any_cast < std::string > (config.get("resourcedb/dir")));
36     std::string sessionDb(boost::any_cast < std::string > (config.get("DB_SESSION")));
37
38     boost::filesystem::path dbFile(resourceDbDir + sessionDb);
39     boost::filesystem::remove(dbFile);
40
41     std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
42     std::dynamic_pointer_cast
43     <
44         tizen_browser::services::SessionStorage,
45         tizen_browser::core::AbstractService
46     >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
47
48     BOOST_REQUIRE(sessionService);
49     BOOST_CHECK_EQUAL(sessionService->getName(), DOMAIN_SESSION_STORAE_SERVICE);
50
51     tizen_browser::Session::SqlStorage* storage=0;
52     storage = sessionService->getStorage();
53
54     BOOST_REQUIRE(storage);
55 }
56
57 BOOST_AUTO_TEST_CASE(CreateAndPopulateSession)
58 {
59     std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
60     std::dynamic_pointer_cast
61     <
62         tizen_browser::services::SessionStorage,
63         tizen_browser::core::AbstractService
64     >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
65
66     BOOST_REQUIRE(sessionService);
67
68     tizen_browser::Session::SqlStorage* storage=0;
69     storage = sessionService->getStorage();
70
71     BOOST_REQUIRE(storage);
72
73     tizen_browser::Session::Session session(storage->createSession());
74
75     BOOST_CHECK(session.isValid());
76
77     std::map<std::string, std::string> urls;
78
79     urls["54379ff6-f9ff-4ef3-99b0-a0de00edd473"] = "http://www.calligra.org";
80     urls["7b5719d4-c2f5-4d87-89ff-9cd70da1710f"] = "http://www.kde.org";
81     urls["ce18e8e2-8d33-4ba7-9fc4-d602cdf3fa36"] = "http://www.krita.org";
82
83     for(auto iter = urls.begin(), end = urls.end(); iter != end; iter++){
84         session.updateItem(iter->first, iter->second);
85     }
86
87
88     BOOST_CHECK_EQUAL(session.items().size(), 3);
89
90     std::string replaceUrl("https://marble.kde.org/");
91
92
93     session.updateItem(std::next(urls.begin(),2)->first,replaceUrl);
94     BOOST_CHECK_EQUAL_COLLECTIONS(
95         replaceUrl.begin(),
96         replaceUrl.end(),
97         session.items().at(std::next(urls.begin(),2)->first).begin(),
98         session.items().at(std::next(urls.begin(),2)->first).end()
99     );
100
101     session.removeItem(std::next(urls.begin(),1)->first);
102
103     BOOST_CHECK_EQUAL(session.items().size(), 2);
104
105 }
106
107 BOOST_AUTO_TEST_CASE(getLastSession)
108 {
109     std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
110     std::dynamic_pointer_cast
111     <
112         tizen_browser::services::SessionStorage,
113         tizen_browser::core::AbstractService
114     >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
115
116     BOOST_REQUIRE(sessionService);
117
118     tizen_browser::Session::SqlStorage* storage=0;
119     storage = sessionService->getStorage();
120
121     BOOST_REQUIRE(storage);
122
123     sleep(2);
124     tizen_browser::Session::Session session(storage->createSession());
125     const std::string newSessionName("theLastOfUs");
126
127     session.setSessionName("theLastOfUs");
128
129     BOOST_CHECK(session.isValid());
130
131     std::map<std::string, std::string> urls;
132
133     urls["54379ff6-f9ff-4ef3-99b0-a0de00edd473"] = "http://www.calligra.org";
134     urls["7b5719d4-c2f5-4d87-89ff-9cd70da1710f"] = "http://www.kde.org";
135     urls["ce18e8e2-8d33-4ba7-9fc4-d602cdf3fa36"] = "http://www.krita.org";
136
137     for(auto iter = urls.begin(), end = urls.end(); iter != end; iter++){
138         session.updateItem(iter->first, iter->second);
139     }
140
141     BOOST_CHECK_EQUAL(session.items().size(), 3);
142
143     tizen_browser::Session::Session lastSession(storage->getLastSession());
144
145     BOOST_REQUIRE(lastSession.isValid());
146
147     BOOST_CHECK_EQUAL_COLLECTIONS(
148         newSessionName.begin(),
149         newSessionName.end(),
150         lastSession.sessionName().begin(),
151         lastSession.sessionName().end()
152     );
153
154     BOOST_CHECK_EQUAL(lastSession.items().size(), 3);
155 }
156
157 BOOST_AUTO_TEST_CASE(getAllSessions)
158 {
159     std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
160     std::dynamic_pointer_cast
161     <
162         tizen_browser::services::SessionStorage,
163         tizen_browser::core::AbstractService
164     >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
165
166     BOOST_REQUIRE(sessionService);
167
168     tizen_browser::Session::SqlStorage* storage=0;
169     storage = sessionService->getStorage();
170
171     BOOST_REQUIRE(storage);
172
173     //new session should be newer then previous one.
174     tizen_browser::Session::SessionsVector sessions(storage->getAllSessions());
175
176     BOOST_CHECK_EQUAL(sessions.size(), 2);
177
178
179 }
180
181
182 BOOST_AUTO_TEST_CASE(deleteSession)
183 {
184     std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
185     std::dynamic_pointer_cast
186     <
187         tizen_browser::services::SessionStorage,
188         tizen_browser::core::AbstractService
189     >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
190
191     BOOST_REQUIRE(sessionService);
192
193     tizen_browser::Session::SqlStorage* storage=0;
194     storage = sessionService->getStorage();
195
196     BOOST_REQUIRE(storage);
197
198     //new session should be newer then previous one.
199     tizen_browser::Session::SessionsVector sessions(storage->getAllSessions());
200
201     storage->deleteSession(sessions.at(0));
202
203     tizen_browser::Session::SessionsVector sessionsBucket(storage->getAllSessions());
204
205     BOOST_CHECK_EQUAL(sessionsBucket.size(),1);
206 }
207
208
209 BOOST_AUTO_TEST_CASE(deleteAllSessions)
210 {
211     std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
212     std::dynamic_pointer_cast
213     <
214         tizen_browser::services::SessionStorage,
215         tizen_browser::core::AbstractService
216     >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
217
218     BOOST_REQUIRE(sessionService);
219
220     tizen_browser::Session::SqlStorage* storage=0;
221     storage = sessionService->getStorage();
222
223     BOOST_REQUIRE(storage);
224
225     //new session should be newer then previous one.
226     sleep(1);
227     tizen_browser::Session::Session session2(storage->createSession());
228     sleep(1);
229     tizen_browser::Session::Session session3(storage->createSession());
230     sleep(1);
231     tizen_browser::Session::Session session4(storage->createSession());
232
233
234     std::map<std::string, std::string> urls;
235
236     urls["54379ff6-f9ff-4ef3-99b0-a0de00edd473"] = "http://www.calligra.org";
237     urls["7b5719d4-c2f5-4d87-89ff-9cd70da1710f"] = "http://www.kde.org";
238     urls["ce18e8e2-8d33-4ba7-9fc4-d602cdf3fa36"] = "http://www.krita.org";
239
240     for(auto iter = urls.begin(), end = urls.end(); iter != end; iter++){
241         session2.updateItem(iter->first, iter->second);
242         sleep(1);
243         session3.updateItem(iter->first, iter->second);
244         sleep(1);
245         session4.updateItem(iter->first, iter->second);
246     }
247
248
249     tizen_browser::Session::SessionsVector sessionsBucket(storage->getAllSessions());
250
251     BOOST_CHECK_EQUAL(sessionsBucket.size(), 4);
252
253     storage->deleteAllSessions();
254
255     sessionsBucket = storage->getAllSessions();
256
257     BOOST_CHECK_EQUAL(sessionsBucket.size(), 0);
258 }
259
260 BOOST_AUTO_TEST_SUITE_END()