tizen beta release
[framework/web/wrt-installer.git] / tests / config_generator / TestCases.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.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <dpl/test/test_runner.h>
23 #include <dpl/binary_queue.h>
24 #include <dpl/file_output.h>
25 #include <dpl/file_input.h>
26 #include <dpl/log/log.h>
27 #include <dpl/foreach.h>
28 #include <config_generator.h>
29 #include <libxml/parser.h>
30 #include <stdio.h>
31 #include <list>
32 #include <string>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <memory>
36
37 namespace {
38
39 std::string g_expectedFile;
40 std::string g_resultFile;
41
42 const char* cNull = NULL;
43
44 } // namespace
45
46 // helper macros
47 #define TEST_START(name) \
48     RUNNER_TEST(name) \
49     { \
50         g_expectedFile = "/opt/apps/config_gen/" #name ".xml"; \
51         g_resultFile = "/opt/apps/config_gen/" #name "_result.xml";
52
53 #define TEST_END }
54
55 #define CURRENT_TEST() *g_testnames.rbegin()
56
57 namespace {
58
59 // Displays the document in logs
60 void DisplayResult(ConfigXml::DocumentPtr doc)
61 {
62     DPL::BinaryQueue bq;
63     doc->Write(bq);
64
65     std::unique_ptr<char[]> buffer(new char[bq.Size()]);
66
67     bq.FlattenConsume(buffer.get(),bq.Size());
68     LogInfo("Generated XML:\n\n" << buffer.get());
69 }
70
71 // Save the document to file
72 void SaveResult(ConfigXml::DocumentPtr doc)
73 {
74     remove(g_resultFile.c_str());
75     DPL::FileOutput fo(g_resultFile);
76     doc->Write(fo);
77     fo.Close();
78 }
79
80 void closeFile(int* fd)
81 {
82     close(*fd);
83 }
84
85 /*
86  * Simple XML comparison method. Performs simple character by character
87  * comparison (ignores whitespaces). Sensitive to element order and formatting.
88  * Does not ignore comments.
89  */
90 void CompareResult()
91 {
92     LogDebug("Comparing " << g_expectedFile << " and " << g_resultFile);
93
94     typedef std::unique_ptr<int,void (*)(int*)> FilePtr;
95
96     // open expected
97     int efd = TEMP_FAILURE_RETRY(
98             open(g_expectedFile.c_str(), O_RDONLY | O_NONBLOCK));
99     RUNNER_ASSERT_MSG(efd != -1, "Failed to open " << g_expectedFile);
100     FilePtr efdPtr(&efd, closeFile);
101
102     // open result
103     int rfd = TEMP_FAILURE_RETRY(
104             open(g_resultFile.c_str(), O_RDONLY | O_NONBLOCK));
105     RUNNER_ASSERT_MSG(rfd != -1, "Failed to open " << g_resultFile);
106     FilePtr rfdPtr(&rfd, closeFile);
107
108     bool eEOF = false;
109     bool rEOF = false;
110     for(;!eEOF && !rEOF;) {
111         unsigned char eChar;
112         unsigned char rChar;
113
114         // read expected
115         do {
116             if(0 == TEMP_FAILURE_RETRY(read(efd, &eChar, 1))) {
117                 eEOF = true;
118                 break;
119             }
120         } while(!isgraph(eChar));
121
122         // read result
123         do {
124             if(0 == TEMP_FAILURE_RETRY(read(rfd, &rChar, 1))) {
125                 rEOF = true;
126                 break;
127             }
128         } while(!isgraph(rChar));
129
130         // compare
131         if(!eEOF && !rEOF) {
132             RUNNER_ASSERT_MSG(
133                     eChar == rChar,
134                     "Difference '" << eChar << "' != '" << rChar << "'");
135         }
136     }
137     RUNNER_ASSERT_MSG(eEOF == rEOF, "Different number of characters");
138
139     LogDebug("Finished");
140 }
141
142 void DisplaySaveAndCompare(ConfigXml::DocumentPtr doc)
143 {
144     DisplayResult(doc);
145     SaveResult(doc);
146     CompareResult();
147 }
148
149 } // namespace
150
151 TEST_START(test001_basic)
152     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
153     doc->Add<ConfigXml::WIDGET>("http://example.org/exampleWidget",
154                                 "2.0 Beta",
155                                 640,
156                                 480);
157     DisplaySaveAndCompare(doc);
158 TEST_END
159
160 TEST_START(test002_basic)
161     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
162     doc->Add<ConfigXml::WIDGET>("http://example.org/exampleWidget",
163                                 "2.0 Beta",
164                                 "fullscreen");
165     DisplaySaveAndCompare(doc);
166 TEST_END
167
168
169 TEST_START(test003_name)
170     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
171     ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
172
173     root->Add<ConfigXml::NAME>(cNull);
174     root->Add<ConfigXml::NAME>("example");
175     DisplaySaveAndCompare(doc);
176 TEST_END
177
178
179 TEST_START(test004_description)
180     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
181     ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
182
183     root->Add<ConfigXml::DESCRIPTION>(cNull);
184     root->Add<ConfigXml::DESCRIPTION>("description");
185     DisplaySaveAndCompare(doc);
186 TEST_END
187
188
189 TEST_START(test005_author)
190     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
191     ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
192
193     root->Add<ConfigXml::AUTHOR>(cNull, cNull, cNull);
194     root->Add<ConfigXml::AUTHOR>(cNull, cNull, "Krzysztof Janiak");
195     root->Add<ConfigXml::AUTHOR>(cNull,
196                                  "k.janiak@samsung.com",
197                                  "Krzysztof Janiak");
198     root->Add<ConfigXml::AUTHOR>("www.google.pl",
199                                  "k.janiak@samsung.com",
200                                  "Krzysztof Janiak");
201     DisplaySaveAndCompare(doc);
202 TEST_END
203
204
205 TEST_START(test006_license)
206     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
207     ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
208
209     root->Add<ConfigXml::LICENSE>(cNull, cNull);
210     root->Add<ConfigXml::LICENSE>(cNull, "Public domain.");
211     root->Add<ConfigXml::LICENSE>("www.samsung.com", "Apache 2.0");
212     DisplaySaveAndCompare(doc);
213 TEST_END
214
215
216 TEST_START(test007_icon)
217     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
218     ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
219
220     root->Add<ConfigXml::ICON>(cNull);
221     root->Add<ConfigXml::ICON>("icon.png");
222     DisplaySaveAndCompare(doc);
223 TEST_END
224
225
226 TEST_START(test008_content)
227     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
228     ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
229
230     root->Add<ConfigXml::CONTENT>(cNull);
231     root->Add<ConfigXml::CONTENT>("index.html");
232     DisplaySaveAndCompare(doc);
233 TEST_END
234
235
236 TEST_START(test009_feature)
237     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
238     ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
239
240     ConfigXml::ElementPtr feature = root->Add<ConfigXml::FEATURE>(
241             "http://tizen.org/api/application",
242             true);
243
244     feature->Add<ConfigXml::PARAM>(cNull, cNull);
245     feature->Add<ConfigXml::PARAM>("accuracy", cNull);
246     feature->Add<ConfigXml::PARAM>("accuracy", "low");
247     feature->Add<ConfigXml::PARAM>("enabled", false);
248     DisplaySaveAndCompare(doc);
249 TEST_END
250
251
252 TEST_START(test010_preference)
253     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
254     ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
255
256     root->Add<ConfigXml::PREFERENCE>(cNull, cNull);
257     root->Add<ConfigXml::PREFERENCE>("skin", cNull);
258     root->Add<ConfigXml::PREFERENCE>("skin", "alien");
259     root->Add<ConfigXml::PREFERENCE>("api-key", "f6d3a312f9d742", true);
260     DisplaySaveAndCompare(doc);
261 TEST_END
262
263
264 TEST_START(test011_access)
265     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
266     ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
267
268     root->Add<ConfigXml::ACCESS>("http://www.wp.pl/*");
269     root->Add<ConfigXml::ACCESS>("http://onet.pl", true);
270     DisplaySaveAndCompare(doc);
271 TEST_END
272
273
274 TEST_START(test012_tizen_setting)
275     ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
276     ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
277
278     root->Add<ConfigXml::TIZEN_SETTING>("rotation-lock","portrait");
279     root->Add<ConfigXml::TIZEN_SETTING>("backbutton-presence","disable");
280     root->Add<ConfigXml::TIZEN_SETTING>("indicator-presence","disable");
281     DisplaySaveAndCompare(doc);
282 TEST_END