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