2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <boost/any.hpp>
18 #include <boost/filesystem.hpp>
19 #include <boost/test/unit_test.hpp>
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>
29 BOOST_AUTO_TEST_SUITE(SessionStorage)
31 BOOST_AUTO_TEST_CASE(InitSession)
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")));
38 boost::filesystem::path dbFile(resourceDbDir + sessionDb);
39 boost::filesystem::remove(dbFile);
41 std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
42 std::dynamic_pointer_cast
44 tizen_browser::services::SessionStorage,
45 tizen_browser::core::AbstractService
46 >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
48 BOOST_REQUIRE(sessionService);
49 BOOST_CHECK_EQUAL(sessionService->getName(), DOMAIN_SESSION_STORAE_SERVICE);
51 tizen_browser::Session::SqlStorage* storage=0;
52 storage = sessionService->getStorage();
54 BOOST_REQUIRE(storage);
57 BOOST_AUTO_TEST_CASE(CreateAndPopulateSession)
59 std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
60 std::dynamic_pointer_cast
62 tizen_browser::services::SessionStorage,
63 tizen_browser::core::AbstractService
64 >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
66 BOOST_REQUIRE(sessionService);
68 tizen_browser::Session::SqlStorage* storage=0;
69 storage = sessionService->getStorage();
71 BOOST_REQUIRE(storage);
73 tizen_browser::Session::Session session(storage->createSession());
75 BOOST_CHECK(session.isValid());
77 std::map<std::string, std::string> urls;
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";
83 for(auto iter = urls.begin(), end = urls.end(); iter != end; iter++){
84 session.updateItem(iter->first, iter->second);
88 BOOST_CHECK_EQUAL(session.items().size(), 3);
90 std::string replaceUrl("https://marble.kde.org/");
93 session.updateItem(std::next(urls.begin(),2)->first,replaceUrl);
94 BOOST_CHECK_EQUAL_COLLECTIONS(
97 session.items().at(std::next(urls.begin(),2)->first).begin(),
98 session.items().at(std::next(urls.begin(),2)->first).end()
101 session.removeItem(std::next(urls.begin(),1)->first);
103 BOOST_CHECK_EQUAL(session.items().size(), 2);
107 BOOST_AUTO_TEST_CASE(getLastSession)
109 std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
110 std::dynamic_pointer_cast
112 tizen_browser::services::SessionStorage,
113 tizen_browser::core::AbstractService
114 >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
116 BOOST_REQUIRE(sessionService);
118 tizen_browser::Session::SqlStorage* storage=0;
119 storage = sessionService->getStorage();
121 BOOST_REQUIRE(storage);
124 tizen_browser::Session::Session session(storage->createSession());
125 const std::string newSessionName("theLastOfUs");
127 session.setSessionName("theLastOfUs");
129 BOOST_CHECK(session.isValid());
131 std::map<std::string, std::string> urls;
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";
137 for(auto iter = urls.begin(), end = urls.end(); iter != end; iter++){
138 session.updateItem(iter->first, iter->second);
141 BOOST_CHECK_EQUAL(session.items().size(), 3);
143 tizen_browser::Session::Session lastSession(storage->getLastSession());
145 BOOST_REQUIRE(lastSession.isValid());
147 BOOST_CHECK_EQUAL_COLLECTIONS(
148 newSessionName.begin(),
149 newSessionName.end(),
150 lastSession.sessionName().begin(),
151 lastSession.sessionName().end()
154 BOOST_CHECK_EQUAL(lastSession.items().size(), 3);
157 BOOST_AUTO_TEST_CASE(getAllSessions)
159 std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
160 std::dynamic_pointer_cast
162 tizen_browser::services::SessionStorage,
163 tizen_browser::core::AbstractService
164 >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
166 BOOST_REQUIRE(sessionService);
168 tizen_browser::Session::SqlStorage* storage=0;
169 storage = sessionService->getStorage();
171 BOOST_REQUIRE(storage);
173 //new session should be newer then previous one.
174 tizen_browser::Session::SessionsVector sessions(storage->getAllSessions());
176 BOOST_CHECK_EQUAL(sessions.size(), 2);
182 BOOST_AUTO_TEST_CASE(deleteSession)
184 std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
185 std::dynamic_pointer_cast
187 tizen_browser::services::SessionStorage,
188 tizen_browser::core::AbstractService
189 >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
191 BOOST_REQUIRE(sessionService);
193 tizen_browser::Session::SqlStorage* storage=0;
194 storage = sessionService->getStorage();
196 BOOST_REQUIRE(storage);
198 //new session should be newer then previous one.
199 tizen_browser::Session::SessionsVector sessions(storage->getAllSessions());
201 storage->deleteSession(sessions.at(0));
203 tizen_browser::Session::SessionsVector sessionsBucket(storage->getAllSessions());
205 BOOST_CHECK_EQUAL(sessionsBucket.size(),1);
209 BOOST_AUTO_TEST_CASE(deleteAllSessions)
211 std::shared_ptr<tizen_browser::services::SessionStorage> sessionService =
212 std::dynamic_pointer_cast
214 tizen_browser::services::SessionStorage,
215 tizen_browser::core::AbstractService
216 >(tizen_browser::core::ServiceManager::getInstance().getService(DOMAIN_SESSION_STORAE_SERVICE));
218 BOOST_REQUIRE(sessionService);
220 tizen_browser::Session::SqlStorage* storage=0;
221 storage = sessionService->getStorage();
223 BOOST_REQUIRE(storage);
225 //new session should be newer then previous one.
227 tizen_browser::Session::Session session2(storage->createSession());
229 tizen_browser::Session::Session session3(storage->createSession());
231 tizen_browser::Session::Session session4(storage->createSession());
234 std::map<std::string, std::string> urls;
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";
240 for(auto iter = urls.begin(), end = urls.end(); iter != end; iter++){
241 session2.updateItem(iter->first, iter->second);
243 session3.updateItem(iter->first, iter->second);
245 session4.updateItem(iter->first, iter->second);
249 tizen_browser::Session::SessionsVector sessionsBucket(storage->getAllSessions());
251 BOOST_CHECK_EQUAL(sessionsBucket.size(), 4);
253 storage->deleteAllSessions();
255 sessionsBucket = storage->getAllSessions();
257 BOOST_CHECK_EQUAL(sessionsBucket.size(), 0);
260 BOOST_AUTO_TEST_SUITE_END()