tizen 2.4 release
[framework/web/wrt-commons.git] / tests / dao / TestCases_WidgetInterfaceDAO.cpp
1 /*
2  * Copyright (c) 2011 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_widgetInterfaceDAO.cpp
18  * @author  Dominik Duda (d.duda@samsung.com)
19  * @version 1.0
20  * @brief   This file contains tests for WidgetInterfaceDAO class.
21  */
22
23 #include <list>
24 #include <cstdlib>
25 #include <cstdio>
26 #include <string>
27 #include <algorithm>
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>
37
38 #define WIDGET_ID 2000
39
40 using namespace WrtDB;
41 using namespace WidgetInterfaceDB;
42
43 RUNNER_TEST_GROUP_INIT(DAO)
44
45 /*
46  * Name: widget_interface_dao_test_01_initialization
47  * Description: Tests database creation.
48  * Expected: The database should be successfully created.
49  */
50 RUNNER_TEST(widget_interface_dao_test_01_initialization)
51 {
52     Try
53     {
54         WidgetInterfaceDAO dao(WIDGET_ID);
55     }
56     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
57     {
58         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
59     }
60 }
61
62 /*
63  * Name: widget_interface_dao_test_01_initialization_Exception
64  * Description: Tests if an exception will be thrown when incorrect widgetID
65  * is used.
66  * Expected: The exception should be thrown.
67  */
68 RUNNER_TEST(widget_interface_dao_test_01_initialization_Exception)
69 {
70     Try
71     {
72         WidgetInterfaceDAO dao(5555000);
73     }
74     Catch(WidgetDAOReadOnly::Exception::WidgetNotExist)
75     {
76         RUNNER_ASSERT_MSG(true, _rethrown_exception.GetMessage());
77     }
78     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
79     {
80         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
81     }
82 }
83
84 /*
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.
88  */
89 RUNNER_TEST(widget_interface_dao_test_02_readValue)
90 {
91     DPL::OptionalStdString value;
92
93     Try
94     {
95         WidgetInterfaceDAO dao(WIDGET_ID);
96
97         value = dao.getValue("key1_for_2000");
98
99         RUNNER_ASSERT(value == std::string("value_for_key1_2000"));
100     }
101     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
102     {
103         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
104     }
105 }
106
107 /*
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.
111  */
112 RUNNER_TEST(widget_interface_dao_test_02_readValue_badKey)
113 {
114     DPL::OptionalStdString value;
115
116     Try
117     {
118         WidgetInterfaceDAO dao(WIDGET_ID);
119
120         value = dao.getValue("key1_for_200011111");
121
122         RUNNER_ASSERT(!value);
123     }
124     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
125     {
126         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
127     }
128 }
129
130 /*
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.
134  */
135 RUNNER_TEST(widget_interface_dao_test_03_setItem)
136 {
137     DPL::OptionalStdString value;
138
139     Try
140     {
141         WidgetInterfaceDAO dao(WIDGET_ID);
142
143         dao.setItem("key3_for_2000", "value_for_key3_2000", 1);
144
145         value = dao.getValue("key3_for_2000");
146
147         RUNNER_ASSERT(value == std::string("value_for_key3_2000"));
148     }
149     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
150     {
151         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
152     }
153 }
154
155 /*
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.
159  */
160 RUNNER_TEST(widget_interface_dao_test_03_setItem_Exception)
161 {
162     DPL::OptionalStdString value;
163
164     Try
165     {
166         WidgetInterfaceDAO dao(WIDGET_ID);
167
168         dao.setItem("key3_for_2000", "value_for_key3_2000", 1);
169
170         RUNNER_ASSERT_MSG(false, "Readonly value should not be overwritten");
171     }
172     Catch(WidgetInterfaceDAO::Exception::LocalStorageValueNoModifableException)
173     {
174         RUNNER_ASSERT(true);
175     }
176     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
177     {
178         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
179     }
180 }
181
182 /*
183  * Name: widget_interface_dao_test_04_setItem
184  * Description: Tests inserting a new item with non readonly and fromConfigXML
185  * properties set.
186  * Expected: The new item should be added.
187  */
188 RUNNER_TEST(widget_interface_dao_test_04_setItem)
189 {
190     DPL::OptionalStdString value;
191
192     Try
193     {
194         WidgetInterfaceDAO dao(WIDGET_ID);
195
196         dao.setItem("key4_for_2000", "value_for_key4_2000", 0, 1);
197
198         value = dao.getValue("key4_for_2000");
199
200         RUNNER_ASSERT(value == std::string("value_for_key4_2000"));
201     }
202     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
203     {
204         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
205     }
206 }
207
208 /*
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.
212  */
213 RUNNER_TEST(widget_interface_dao_test_05_getKeyByIndex)
214 {
215     std::string key;
216
217     Try
218     {
219         WidgetInterfaceDAO dao(WIDGET_ID);
220
221         key = dao.getKeyByIndex(1);
222
223         RUNNER_ASSERT(key == "key2_for_2000");
224     }
225     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
226     {
227         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
228     }
229 }
230
231 /*
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.
236  */
237 RUNNER_TEST(widget_interface_dao_test_05_getKeyByIndex_badIndex_01)
238 {
239     std::string key;
240
241     Try
242     {
243         WidgetInterfaceDAO dao(WIDGET_ID);
244
245         key = dao.getKeyByIndex(-111);
246
247         RUNNER_ASSERT_MSG(false, "A key should not be returned for "
248                 "a negative index!");
249     }
250     Catch(WidgetInterfaceDAO::Exception::InvalidArgumentException)
251     {
252         RUNNER_ASSERT_MSG(true, _rethrown_exception.GetMessage());
253     }
254     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
255     {
256         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
257     }
258 }
259
260 /*
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.
265  */
266 RUNNER_TEST(widget_interface_dao_test_05_getKeyByIndex_badIndex_02)
267 {
268     std::string key;
269
270     Try
271     {
272         WidgetInterfaceDAO dao(WIDGET_ID);
273
274         key = dao.getKeyByIndex(1111);
275
276         RUNNER_ASSERT_MSG(false, "A key should not be returned for "
277                 "a non existing index!");
278     }
279     Catch(WidgetInterfaceDAO::Exception::InvalidArgumentException)
280     {
281         RUNNER_ASSERT(true);
282     }
283     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
284     {
285         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
286     }
287 }
288
289 /*
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.
294  */
295 RUNNER_TEST(widget_interface_dao_test_06_removeItem)
296 {
297     std::string key;
298     size_t sizeBefore, sizeAfter;
299     DPL::OptionalStdString value;
300
301     Try
302     {
303         WidgetInterfaceDAO dao(WIDGET_ID);
304
305         sizeBefore = dao.getStorageSize();
306
307         if (sizeBefore == 0)
308         {
309             RUNNER_ASSERT_MSG(false, "Database is empty!");
310             return;
311         }
312
313         dao.removeItem("key4_for_2000");
314
315         sizeAfter = dao.getStorageSize();
316         value = dao.getValue("key4_for_2000");
317
318         RUNNER_ASSERT(sizeAfter == sizeBefore - 1);
319         RUNNER_ASSERT(!value);
320     }
321     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
322     {
323         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
324     }
325 }
326
327 /*
328  * Name: widget_interface_dao_test_06_removeItem_Exception
329  * Description: Tests if an exception will be thrown when a readonly item
330  * is removed.
331  * Expected: An exception should be thrown.
332  */
333 RUNNER_TEST(widget_interface_dao_test_06_removeItem_Exception)
334 {
335     WidgetInterfaceDAO* dao = NULL;
336     DPL::OptionalStdString value;
337     size_t sizeBefore, sizeAfter;
338
339     Try
340     {
341         dao = new WidgetInterfaceDAO(WIDGET_ID);
342
343         sizeBefore = dao->getStorageSize();
344
345         if (sizeBefore == 0)
346         {
347             RUNNER_ASSERT_MSG(false, "Database is empty!");
348             return;
349         }
350
351         dao->removeItem("key3_for_2000");
352
353         RUNNER_ASSERT_MSG(false, "A readonly item should not be removed!");
354     }
355     Catch(WidgetInterfaceDAO::Exception::LocalStorageValueNoModifableException)
356     {
357         sizeAfter = dao->getStorageSize();
358         value = dao->getValue("key3_for_2000");
359
360         RUNNER_ASSERT(sizeAfter == sizeBefore);
361         RUNNER_ASSERT(value == std::string("value_for_key3_2000"));
362     }
363     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
364     {
365         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
366     }
367
368     if (dao != NULL)
369         delete dao;
370 }
371
372 /*
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.
376  */
377 RUNNER_TEST(widget_interface_dao_test_07_clear)
378 {
379     size_t sizeBefore, sizeAfter;
380     DPL::OptionalStdString value;
381
382     Try
383     {
384         WidgetInterfaceDAO dao(WIDGET_ID);
385
386         sizeBefore = dao.getStorageSize();
387         dao.clear(false);
388
389         sizeAfter = dao.getStorageSize();
390         value = dao.getValue("key3_for_2000");
391
392         RUNNER_ASSERT(sizeAfter == 1);
393         RUNNER_ASSERT(sizeAfter < sizeBefore);
394         RUNNER_ASSERT(value == std::string("value_for_key3_2000"));
395     }
396     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
397     {
398         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
399     }
400 }
401
402 /*
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.
406  */
407 RUNNER_TEST(widget_interface_dao_test_07_clearAll)
408 {
409     size_t sizeAfter;
410
411     Try
412     {
413         WidgetInterfaceDAO dao(WIDGET_ID);
414
415         dao.clear(true);
416         sizeAfter = dao.getStorageSize();
417
418         RUNNER_ASSERT(sizeAfter == 0);
419     }
420     Catch(WidgetInterfaceDAO::Exception::DatabaseError)
421     {
422         RUNNER_ASSERT_MSG(false, _rethrown_exception.GetMessage());
423     }
424 }