2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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 * @file TestCases_widgetInterfaceDAO.cpp
18 * @author Dominik Duda (d.duda@samsung.com)
20 * @brief This file contains tests for WidgetInterfaceDAO class.
28 #include <dpl/test/test_runner.h>
29 #include <dpl/foreach.h>
30 #include <dpl/exception.h>
31 #include <dpl/optional_typedefs.h>
32 #include <dpl/wrt-dao-rw/widget_dao.h>
33 #include <dpl/wrt-dao-ro/wrt_db_types.h>
34 #include <dpl/string.h>
35 #include <wrt_plugin_export.h>
36 #include <wrt-commons/widget-interface-dao/widget_interface_dao.h>
38 #define WIDGET_ID 2000
40 using namespace WrtDB;
41 using namespace WidgetInterfaceDB;
43 RUNNER_TEST_GROUP_INIT(DAO)
46 * Name: widget_interface_dao_test_01_initialization
47 * Description: Tests database creation.
48 * Expected: The database should be successfully created.
50 RUNNER_TEST(widget_interface_dao_test_01_initialization)
54 WidgetInterfaceDAO dao(WIDGET_ID);
56 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
58 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
63 * Name: widget_interface_dao_test_01_initialization_Exception
64 * Description: Tests if an exception will be thrown when incorrect widgetID
66 * Expected: The exception should be thrown.
68 RUNNER_TEST(widget_interface_dao_test_01_initialization_Exception)
72 WidgetInterfaceDAO dao(5555000);
74 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
76 RUNNER_ASSERT_MSG(true, _rethrown_exception.GetMessage());
78 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
80 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
85 * Name: widget_interface_dao_test_02_readValue
86 * Description: Tests getting a value for a given key.
87 * Expected: The desired value should be returned.
89 RUNNER_TEST(widget_interface_dao_test_02_readValue)
91 DPL::OptionalStdString value;
95 WidgetInterfaceDAO dao(WIDGET_ID);
97 value = dao.getValue("key1_for_2000");
99 RUNNER_ASSERT(value == std::string("value_for_key1_2000"));
101 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
103 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
108 * Name: widget_interface_dao_test_02_readValue_badKey
109 * Description: Tests if an empty value is returned for an incorrect key.
110 * Expected: The empty (NULL) value should be returned.
112 RUNNER_TEST(widget_interface_dao_test_02_readValue_badKey)
114 DPL::OptionalStdString value;
118 WidgetInterfaceDAO dao(WIDGET_ID);
120 value = dao.getValue("key1_for_200011111");
122 RUNNER_ASSERT(!value);
124 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
126 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
131 * Name: widget_interface_dao_test_03_setItem
132 * Description: Tests if a new readonly item can be add into a database.
133 * Expected: The new item should be successfully added.
135 RUNNER_TEST(widget_interface_dao_test_03_setItem)
137 DPL::OptionalStdString value;
141 WidgetInterfaceDAO dao(WIDGET_ID);
143 dao.setItem("key3_for_2000", "value_for_key3_2000", 1);
145 value = dao.getValue("key3_for_2000");
147 RUNNER_ASSERT(value == std::string("value_for_key3_2000"));
149 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
151 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
156 * Name: widget_interface_dao_test_03_setItem_Exception
157 * Description: Tests if a readonly item can be overwritten.
158 * Expected: An exception should be thrown.
160 RUNNER_TEST(widget_interface_dao_test_03_setItem_Exception)
162 DPL::OptionalStdString value;
166 WidgetInterfaceDAO dao(WIDGET_ID);
168 dao.setItem("key3_for_2000", "value_for_key3_2000", 1);
170 RUNNER_ASSERT_MSG(false, "Readonly value should not be overwritten");
172 Catch(WidgetInterfaceDAO::Exception::LocalStorageValueNoModifableException)
176 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
178 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
183 * Name: widget_interface_dao_test_04_setItem
184 * Description: Tests inserting a new item with non readonly and fromConfigXML
186 * Expected: The new item should be added.
188 RUNNER_TEST(widget_interface_dao_test_04_setItem)
190 DPL::OptionalStdString value;
194 WidgetInterfaceDAO dao(WIDGET_ID);
196 dao.setItem("key4_for_2000", "value_for_key4_2000", 0, 1);
198 value = dao.getValue("key4_for_2000");
200 RUNNER_ASSERT(value == std::string("value_for_key4_2000"));
202 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
204 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
209 * Name: widget_interface_dao_test_05_getKeyByIndex
210 * Description: Tests getting a key by a given index.
211 * Expected: The key should be successfully read.
213 RUNNER_TEST(widget_interface_dao_test_05_getKeyByIndex)
219 WidgetInterfaceDAO dao(WIDGET_ID);
221 key = dao.getKeyByIndex(1);
223 RUNNER_ASSERT(key == "key2_for_2000");
225 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
227 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
232 * Name: widget_interface_dao_test_05_getKeyByIndex_badIndex_01
233 * Description: Tests if an exception will be thrown when getting a key
234 * by a negative index.
235 * Expected: An exception should be thrown.
237 RUNNER_TEST(widget_interface_dao_test_05_getKeyByIndex_badIndex_01)
243 WidgetInterfaceDAO dao(WIDGET_ID);
245 key = dao.getKeyByIndex(-111);
247 RUNNER_ASSERT_MSG(false, "A key should not be returned for "
248 "a negative index!");
250 Catch(WidgetInterfaceDAO::Exception::InvalidArgumentException)
252 RUNNER_ASSERT_MSG(true, _rethrown_exception.GetMessage());
254 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
256 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
261 * Name: widget_interface_dao_test_05_getKeyByIndex_badIndex_02
262 * Description: Tests if an exception will be thrown when getting a key
263 * by a non existing index.
264 * Expected: An exception should be thrown.
266 RUNNER_TEST(widget_interface_dao_test_05_getKeyByIndex_badIndex_02)
272 WidgetInterfaceDAO dao(WIDGET_ID);
274 key = dao.getKeyByIndex(1111);
276 RUNNER_ASSERT_MSG(false, "A key should not be returned for "
277 "a non existing index!");
279 Catch(WidgetInterfaceDAO::Exception::InvalidArgumentException)
283 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
285 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
290 * Name: widget_interface_dao_test_06_removeItem
291 * Description: Tests removing an item for a given key.
292 * Expected: An item should be successfully removed. The table size should be
293 * smaller after removing.
295 RUNNER_TEST(widget_interface_dao_test_06_removeItem)
298 size_t sizeBefore, sizeAfter;
299 DPL::OptionalStdString value;
303 WidgetInterfaceDAO dao(WIDGET_ID);
305 sizeBefore = dao.getStorageSize();
309 RUNNER_ASSERT_MSG(false, "Database is empty!");
313 dao.removeItem("key4_for_2000");
315 sizeAfter = dao.getStorageSize();
316 value = dao.getValue("key4_for_2000");
318 RUNNER_ASSERT(sizeAfter == sizeBefore - 1);
319 RUNNER_ASSERT(!value);
321 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
323 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
328 * Name: widget_interface_dao_test_06_removeItem_Exception
329 * Description: Tests if an exception will be thrown when a readonly item
331 * Expected: An exception should be thrown.
333 RUNNER_TEST(widget_interface_dao_test_06_removeItem_Exception)
335 WidgetInterfaceDAO* dao = NULL;
336 DPL::OptionalStdString value;
337 size_t sizeBefore, sizeAfter;
341 dao = new WidgetInterfaceDAO(WIDGET_ID);
343 sizeBefore = dao->getStorageSize();
347 RUNNER_ASSERT_MSG(false, "Database is empty!");
351 dao->removeItem("key3_for_2000");
353 RUNNER_ASSERT_MSG(false, "A readonly item should not be removed!");
355 Catch(WidgetInterfaceDAO::Exception::LocalStorageValueNoModifableException)
357 sizeAfter = dao->getStorageSize();
358 value = dao->getValue("key3_for_2000");
360 RUNNER_ASSERT(sizeAfter == sizeBefore);
361 RUNNER_ASSERT(value == std::string("value_for_key3_2000"));
363 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
365 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
373 * Name: widget_interface_dao_test_07_clear
374 * Description: Tests removing all non readonly rows from the table.
375 * Expected: The size of the table should be smaller than before removing rows.
377 RUNNER_TEST(widget_interface_dao_test_07_clear)
379 size_t sizeBefore, sizeAfter;
380 DPL::OptionalStdString value;
384 WidgetInterfaceDAO dao(WIDGET_ID);
386 sizeBefore = dao.getStorageSize();
389 sizeAfter = dao.getStorageSize();
390 value = dao.getValue("key3_for_2000");
392 RUNNER_ASSERT(sizeAfter == 1);
393 RUNNER_ASSERT(sizeAfter < sizeBefore);
394 RUNNER_ASSERT(value == std::string("value_for_key3_2000"));
396 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
398 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
403 * Name: widget_interface_dao_test_07_clearAll
404 * Description: Tests removing all rows from the table.
405 * Expected: The size of the table should be 0.
407 RUNNER_TEST(widget_interface_dao_test_07_clearAll)
413 WidgetInterfaceDAO dao(WIDGET_ID);
416 sizeAfter = dao.getStorageSize();
418 RUNNER_ASSERT(sizeAfter == 0);
420 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
422 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());