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/wrt-dao-rw/widget_dao.h>
32 #include <dpl/wrt-dao-ro/wrt_db_types.h>
33 #include <dpl/string.h>
34 #include <wrt_plugin_export.h>
35 #include <wrt-commons/widget-interface-dao/widget_interface_dao.h>
37 #define WIDGET_ID 2000
39 using namespace WrtDB;
40 using namespace WidgetInterfaceDB;
42 RUNNER_TEST_GROUP_INIT(DAO)
45 * Name: widget_interface_dao_test_01_initialization
46 * Description: Tests database creation.
47 * Expected: The database should be successfully created.
49 RUNNER_TEST(widget_interface_dao_test_01_initialization)
53 WidgetInterfaceDAO dao(WIDGET_ID);
55 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
57 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
62 * Name: widget_interface_dao_test_01_initialization_Exception
63 * Description: Tests if an exception will be thrown when incorrect widgetID
65 * Expected: The exception should be thrown.
67 RUNNER_TEST(widget_interface_dao_test_01_initialization_Exception)
71 WidgetInterfaceDAO dao(5555000);
73 Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
75 RUNNER_ASSERT_MSG(true, _rethrown_exception.GetMessage());
77 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
79 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
84 * Name: widget_interface_dao_test_02_readValue
85 * Description: Tests getting a value for a given key.
86 * Expected: The desired value should be returned.
88 RUNNER_TEST(widget_interface_dao_test_02_readValue)
90 DPL::Optional<std::string> value;
94 WidgetInterfaceDAO dao(WIDGET_ID);
96 value = dao.getValue("key1_for_2000");
98 RUNNER_ASSERT(value == "value_for_key1_2000");
100 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
102 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
107 * Name: widget_interface_dao_test_02_readValue_badKey
108 * Description: Tests if an empty value is returned for an incorrect key.
109 * Expected: The empty (NULL) value should be returned.
111 RUNNER_TEST(widget_interface_dao_test_02_readValue_badKey)
113 DPL::Optional<std::string> value;
117 WidgetInterfaceDAO dao(WIDGET_ID);
119 value = dao.getValue("key1_for_200011111");
121 RUNNER_ASSERT(value.IsNull());
123 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
125 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
130 * Name: widget_interface_dao_test_03_setItem
131 * Description: Tests if a new readonly item can be add into a database.
132 * Expected: The new item should be successfully added.
134 RUNNER_TEST(widget_interface_dao_test_03_setItem)
136 DPL::Optional<std::string> value;
140 WidgetInterfaceDAO dao(WIDGET_ID);
142 dao.setItem("key3_for_2000", "value_for_key3_2000", 1);
144 value = dao.getValue("key3_for_2000");
146 RUNNER_ASSERT(value == "value_for_key3_2000");
148 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
150 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
155 * Name: widget_interface_dao_test_03_setItem_Exception
156 * Description: Tests if a readonly item can be overwritten.
157 * Expected: An exception should be thrown.
159 RUNNER_TEST(widget_interface_dao_test_03_setItem_Exception)
161 DPL::Optional<std::string> value;
165 WidgetInterfaceDAO dao(WIDGET_ID);
167 dao.setItem("key3_for_2000", "value_for_key3_2000", 1);
169 RUNNER_ASSERT_MSG(false, "Readonly value should not be overwritten");
171 Catch(WidgetInterfaceDAO::Exception::LocalStorageValueNoModifableException)
175 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
177 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
182 * Name: widget_interface_dao_test_04_setItem
183 * Description: Tests inserting a new item with non readonly and fromConfigXML
185 * Expected: The new item should be added.
187 RUNNER_TEST(widget_interface_dao_test_04_setItem)
189 DPL::Optional<std::string> value;
193 WidgetInterfaceDAO dao(WIDGET_ID);
195 dao.setItem("key4_for_2000", "value_for_key4_2000", 0, 1);
197 value = dao.getValue("key4_for_2000");
199 RUNNER_ASSERT(value == "value_for_key4_2000");
201 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
203 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
208 * Name: widget_interface_dao_test_05_getKeyByIndex
209 * Description: Tests getting a key by a given index.
210 * Expected: The key should be successfully read.
212 RUNNER_TEST(widget_interface_dao_test_05_getKeyByIndex)
218 WidgetInterfaceDAO dao(WIDGET_ID);
220 key = dao.getKeyByIndex(1);
222 RUNNER_ASSERT(key == "key2_for_2000");
224 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
226 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
231 * Name: widget_interface_dao_test_05_getKeyByIndex_badIndex_01
232 * Description: Tests if an exception will be thrown when getting a key
233 * by a negative index.
234 * Expected: An exception should be thrown.
236 RUNNER_TEST(widget_interface_dao_test_05_getKeyByIndex_badIndex_01)
242 WidgetInterfaceDAO dao(WIDGET_ID);
244 key = dao.getKeyByIndex(-111);
246 RUNNER_ASSERT_MSG(false, "A key should not be returned for "
247 "a negative index!");
249 Catch(WidgetInterfaceDAO::Exception::InvalidArgumentException)
251 RUNNER_ASSERT_MSG(true, _rethrown_exception.GetMessage());
253 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
255 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
260 * Name: widget_interface_dao_test_05_getKeyByIndex_badIndex_02
261 * Description: Tests if an exception will be thrown when getting a key
262 * by a non existing index.
263 * Expected: An exception should be thrown.
265 RUNNER_TEST(widget_interface_dao_test_05_getKeyByIndex_badIndex_02)
271 WidgetInterfaceDAO dao(WIDGET_ID);
273 key = dao.getKeyByIndex(1111);
275 RUNNER_ASSERT_MSG(false, "A key should not be returned for "
276 "a non existing index!");
278 Catch(WidgetInterfaceDAO::Exception::InvalidArgumentException)
282 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
284 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
289 * Name: widget_interface_dao_test_06_removeItem
290 * Description: Tests removing an item for a given key.
291 * Expected: An item should be successfully removed. The table size should be
292 * smaller after removing.
294 RUNNER_TEST(widget_interface_dao_test_06_removeItem)
297 size_t sizeBefore, sizeAfter;
298 DPL::Optional<std::string> value;
302 WidgetInterfaceDAO dao(WIDGET_ID);
304 sizeBefore = dao.getStorageSize();
308 RUNNER_ASSERT_MSG(false, "Database is empty!");
312 dao.removeItem("key4_for_2000");
314 sizeAfter = dao.getStorageSize();
315 value = dao.getValue("key4_for_2000");
317 RUNNER_ASSERT(sizeAfter == sizeBefore - 1);
318 RUNNER_ASSERT(value.IsNull());
320 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
322 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
327 * Name: widget_interface_dao_test_06_removeItem_Exception
328 * Description: Tests if an exception will be thrown when a readonly item
330 * Expected: An exception should be thrown.
332 RUNNER_TEST(widget_interface_dao_test_06_removeItem_Exception)
334 WidgetInterfaceDAO* dao = NULL;
335 DPL::Optional<std::string> value;
336 size_t sizeBefore, sizeAfter;
340 dao = new WidgetInterfaceDAO(WIDGET_ID);
342 sizeBefore = dao->getStorageSize();
346 RUNNER_ASSERT_MSG(false, "Database is empty!");
350 dao->removeItem("key3_for_2000");
352 RUNNER_ASSERT_MSG(false, "A readonly item should not be removed!");
354 Catch(WidgetInterfaceDAO::Exception::LocalStorageValueNoModifableException)
356 sizeAfter = dao->getStorageSize();
357 value = dao->getValue("key3_for_2000");
359 RUNNER_ASSERT(sizeAfter == sizeBefore);
360 RUNNER_ASSERT(value == "value_for_key3_2000");
362 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
364 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
372 * Name: widget_interface_dao_test_07_clear
373 * Description: Tests removing all non readonly rows from the table.
374 * Expected: The size of the table should be smaller than before removing rows.
376 RUNNER_TEST(widget_interface_dao_test_07_clear)
378 size_t sizeBefore, sizeAfter;
379 DPL::Optional<std::string> value;
383 WidgetInterfaceDAO dao(WIDGET_ID);
385 sizeBefore = dao.getStorageSize();
388 sizeAfter = dao.getStorageSize();
389 value = dao.getValue("key3_for_2000");
391 RUNNER_ASSERT(sizeAfter == 1);
392 RUNNER_ASSERT(sizeAfter < sizeBefore);
393 RUNNER_ASSERT(value == "value_for_key3_2000");
395 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
397 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
402 * Name: widget_interface_dao_test_07_clearAll
403 * Description: Tests removing all rows from the table.
404 * Expected: The size of the table should be 0.
406 RUNNER_TEST(widget_interface_dao_test_07_clearAll)
412 WidgetInterfaceDAO dao(WIDGET_ID);
415 sizeAfter = dao.getStorageSize();
417 RUNNER_ASSERT(sizeAfter == 0);
419 Catch(WidgetInterfaceDAO::Exception::DatabaseError)
421 RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());