77edab631886a37936cf629f501822e5c8f387ad
[framework/web/wrt-plugins-common.git] / tests / test_widget_interface_dao.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        test_widget_interface_dao.cpp
18  * @author      Lukasz Marek (l.marek@samsung.com)
19  * @version     1.0
20  * @brief       This is the implementation file of test widget interface dao.
21  */
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <cstdlib>
26 #include <unistd.h>
27 #include <string>
28 #include <sstream>
29 #include <dpl/test/test_runner.h>
30 #include <dpl/log/log.h>
31 #include <Commons/Exception.h>
32 #include <WidgetInterfaceDAO/WidgetInterfaceDAO.h>
33
34 namespace {
35 const std::string FAKE_WIDGET_DIR = "/opt/apps/org.tizen.";
36 const std::string WIDGET_DATA_SUBDIR = "data/";
37 const std::string DATABASE_NAME = "widget_interface.db";
38
39 //Following variable is an arbitraty number.
40 //It must match value inserted to wrt database by
41 //wrt_plugins_commons_test_prepare_db script.
42 const int FAKE_WIDGET_ID = 65537;
43 }
44
45 using namespace WrtDeviceApis;
46
47 RUNNER_TEST_GROUP_INIT(WidgetInterfaceDAO)
48
49 class FakeWidget
50 {
51 public:
52     FakeWidget() :
53         m_fakeWidgetId(FAKE_WIDGET_ID)
54     {
55         std::stringstream baseDir;
56         baseDir << FAKE_WIDGET_DIR << m_fakeWidgetId << "/";
57         m_fakeWidgetBaseDir = baseDir.str();
58
59         std::string widgetDataDir =
60             m_fakeWidgetBaseDir + WIDGET_DATA_SUBDIR;
61
62         m_fakeWidgetDatabaseFile = widgetDataDir + DATABASE_NAME;
63
64         if ((mkdir(baseDir.str().c_str(), 0777) < 0) ||
65             (mkdir(widgetDataDir.c_str(), 0777) < 0))
66         {
67             LogError("Cannot create test directory");
68         }
69     }
70
71     ~FakeWidget()
72     {
73         std::string command = "rm -rf ";
74         command.append(m_fakeWidgetBaseDir);
75         int ret = system(command.c_str());
76         if (!WIFEXITED(ret) || WEXITSTATUS(ret))
77         {
78             LogWarning("Fake widget directory is not deleted!");
79         }
80     }
81
82     int widgetId() const
83     {
84         return m_fakeWidgetId;
85     }
86
87     std::string dataBaseFile() const
88     {
89         return m_fakeWidgetDatabaseFile;
90     }
91
92 private:
93     int m_fakeWidgetId;
94     std::string m_fakeWidgetBaseDir;
95     std::string m_fakeWidgetDatabaseFile;
96 };
97
98 RUNNER_TEST(DatabaseCreateOnFirstUse)
99 {
100     FakeWidget w;
101     WidgetInterfaceDAO dao(w.widgetId());
102
103     int ret = access(w.dataBaseFile().c_str(), F_OK);
104
105     LogDebug("filename " << w.dataBaseFile() << " result " << ret);
106
107     RUNNER_ASSERT(ret == 0);
108 }
109
110 RUNNER_TEST(GetStorageSize)
111 {
112     FakeWidget w;
113     WidgetInterfaceDAO dao(w.widgetId());
114
115     RUNNER_ASSERT(dao.getStorageSize() == 0);
116
117     dao.setItem("key", "value", false);
118
119     RUNNER_ASSERT(dao.getStorageSize() == 1);
120
121     dao.removeItem("key");
122
123     RUNNER_ASSERT(dao.getStorageSize() == 0);
124 }
125
126 RUNNER_TEST(RemoveItem)
127 {
128     FakeWidget w;
129     WidgetInterfaceDAO dao(w.widgetId());
130
131     RUNNER_ASSERT(dao.getStorageSize() == 0);
132
133     dao.setItem("key", "value", false);
134
135     RUNNER_ASSERT(dao.getStorageSize() == 1);
136
137     dao.setItem("key", "value", true);
138
139     RUNNER_ASSERT(dao.getStorageSize() == 1);
140
141     bool exCaught = false;
142     try
143     {
144         dao.removeItem("key");
145     }
146     catch(const Commons::LocalStorageValueNoModifableException &ex)
147     {
148         exCaught = true;
149     }
150
151     RUNNER_ASSERT(dao.getStorageSize() == 1 && exCaught);
152 }
153
154 RUNNER_TEST(GetValueExisting)
155 {
156     FakeWidget w;
157     WidgetInterfaceDAO dao(w.widgetId());
158
159     RUNNER_ASSERT(dao.getStorageSize() == 0);
160
161     dao.setItem("key1", "value1", false);
162     dao.setItem("key2", "value2", false);
163
164     DPL::Optional<std::string> val1 = dao.getValue("key1");
165     DPL::Optional<std::string> val2 = dao.getValue("key2");
166
167     RUNNER_ASSERT(!val1.IsNull() && *val1 == "value1");
168     RUNNER_ASSERT(!val2.IsNull() && *val2 == "value2");
169
170 }
171
172 RUNNER_TEST(GetValueNonExisting)
173 {
174     FakeWidget w;
175     WidgetInterfaceDAO dao(w.widgetId());
176
177     RUNNER_ASSERT(dao.getStorageSize() == 0);
178
179     DPL::Optional<std::string> val1 = dao.getValue("key1");
180
181     RUNNER_ASSERT(val1.IsNull());
182 }
183
184 RUNNER_TEST(Clear)
185 {
186     FakeWidget w;
187     WidgetInterfaceDAO dao(w.widgetId());
188
189     RUNNER_ASSERT(dao.getStorageSize() == 0);
190
191     dao.setItem("key1", "value1", false);
192     dao.setItem("key2", "value1", false);
193     RUNNER_ASSERT(dao.getStorageSize() == 2);
194
195     dao.clear(false);
196     RUNNER_ASSERT(dao.getStorageSize() == 0);
197
198     dao.setItem("key1", "value1", true);
199     dao.setItem("key2", "value1", true);
200     RUNNER_ASSERT(dao.getStorageSize() == 2);
201
202     dao.clear(false);
203     RUNNER_ASSERT(dao.getStorageSize() == 2);
204
205     dao.clear(true);
206     RUNNER_ASSERT(dao.getStorageSize() == 0);
207 }
208
209 RUNNER_TEST(UpdateValue)
210 {
211     FakeWidget w;
212     WidgetInterfaceDAO dao(w.widgetId());
213
214     RUNNER_ASSERT(dao.getStorageSize() == 0);
215
216     dao.setItem("key1", "value1", false);
217     DPL::Optional<std::string> val1 = dao.getValue("key1");
218     RUNNER_ASSERT(!val1.IsNull() && *val1 == "value1");
219
220     dao.setItem("key1", "value2", false);
221     val1 = dao.getValue("key1");
222     RUNNER_ASSERT(!val1.IsNull() && *val1 == "value2");
223
224     RUNNER_ASSERT(dao.getStorageSize() == 1);
225 }
226
227 RUNNER_TEST(UpdateReadOnlyValue)
228 {
229     FakeWidget w;
230     WidgetInterfaceDAO dao(w.widgetId());
231
232     RUNNER_ASSERT(dao.getStorageSize() == 0);
233
234     dao.setItem("key1", "value1", true);
235     DPL::Optional<std::string> val1 = dao.getValue("key1");
236     RUNNER_ASSERT(!val1.IsNull() && *val1 == "value1");
237
238     bool exCaught = false;
239     try
240     {
241         dao.setItem("key1", "value2", false);
242     }
243     catch(const Commons::LocalStorageValueNoModifableException &ex)
244     {
245         exCaught = true;
246     }
247
248     RUNNER_ASSERT(dao.getStorageSize() == 1 && exCaught);
249 }
250
251 RUNNER_TEST(GetValueByIndex)
252 {
253     FakeWidget w;
254     WidgetInterfaceDAO dao(w.widgetId());
255
256     RUNNER_ASSERT(dao.getStorageSize() == 0);
257
258     dao.setItem("key1", "value1", false);
259     dao.setItem("key2", "value2", false);
260     RUNNER_ASSERT(dao.getStorageSize() == 2);
261
262     std::string val1 = dao.getValueByIndex(0);
263     std::string val2 = dao.getValueByIndex(1);
264
265     RUNNER_ASSERT((val1 == "value1" && val2 == "value2") ||
266                   (val2 == "value1" && val1 == "value2"));
267 }
268
269 RUNNER_TEST(DataPersistent)
270 {
271     FakeWidget w;
272     {
273         WidgetInterfaceDAO dao(w.widgetId());
274
275         RUNNER_ASSERT(dao.getStorageSize() == 0);
276
277         dao.setItem("key1", "value1", false);
278         dao.setItem("key2", "value2", false);
279     }
280
281     {
282         WidgetInterfaceDAO dao(w.widgetId());
283         DPL::Optional<std::string> val1 = dao.getValue("key1");
284         DPL::Optional<std::string> val2 = dao.getValue("key2");
285
286         RUNNER_ASSERT(!val1.IsNull() && *val1 == "value1");
287         RUNNER_ASSERT(!val2.IsNull() && *val2 == "value2");
288     }
289 }