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.
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
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>
39 std::string g_expectedFile;
40 std::string g_resultFile;
42 const char* cNull = NULL;
47 #define TEST_START(name) \
50 g_expectedFile = "/opt/apps/config_gen/" #name ".xml"; \
51 g_resultFile = "/opt/apps/config_gen/" #name "_result.xml";
55 #define CURRENT_TEST() *g_testnames.rbegin()
59 // Displays the document in logs
60 void DisplayResult(ConfigXml::DocumentPtr doc)
65 std::unique_ptr<char[]> buffer(new char[bq.Size()]);
67 bq.FlattenConsume(buffer.get(),bq.Size());
68 LogInfo("Generated XML:\n\n" << buffer.get());
71 // Save the document to file
72 void SaveResult(ConfigXml::DocumentPtr doc)
74 remove(g_resultFile.c_str());
75 DPL::FileOutput fo(g_resultFile);
80 void closeFile(int* fd)
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.
92 LogDebug("Comparing " << g_expectedFile << " and " << g_resultFile);
94 typedef std::unique_ptr<int,void (*)(int*)> FilePtr;
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);
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);
110 for(;!eEOF && !rEOF;) {
116 if(0 == TEMP_FAILURE_RETRY(read(efd, &eChar, 1))) {
120 } while(!isgraph(eChar));
124 if(0 == TEMP_FAILURE_RETRY(read(rfd, &rChar, 1))) {
128 } while(!isgraph(rChar));
134 "Difference '" << eChar << "' != '" << rChar << "'");
137 RUNNER_ASSERT_MSG(eEOF == rEOF, "Different number of characters");
139 LogDebug("Finished");
142 void DisplaySaveAndCompare(ConfigXml::DocumentPtr doc)
151 TEST_START(test001_basic)
152 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
153 doc->Add<ConfigXml::WIDGET>("http://example.org/exampleWidget",
157 DisplaySaveAndCompare(doc);
160 TEST_START(test002_basic)
161 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
162 doc->Add<ConfigXml::WIDGET>("http://example.org/exampleWidget",
165 DisplaySaveAndCompare(doc);
169 TEST_START(test003_name)
170 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
171 ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
173 root->Add<ConfigXml::NAME>(cNull);
174 root->Add<ConfigXml::NAME>("example");
175 DisplaySaveAndCompare(doc);
179 TEST_START(test004_description)
180 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
181 ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
183 root->Add<ConfigXml::DESCRIPTION>(cNull);
184 root->Add<ConfigXml::DESCRIPTION>("description");
185 DisplaySaveAndCompare(doc);
189 TEST_START(test005_author)
190 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
191 ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
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",
198 root->Add<ConfigXml::AUTHOR>("www.google.pl",
199 "k.janiak@samsung.com",
201 DisplaySaveAndCompare(doc);
205 TEST_START(test006_license)
206 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
207 ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
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);
216 TEST_START(test007_icon)
217 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
218 ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
220 root->Add<ConfigXml::ICON>(cNull);
221 root->Add<ConfigXml::ICON>("icon.png");
222 DisplaySaveAndCompare(doc);
226 TEST_START(test008_content)
227 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
228 ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
230 root->Add<ConfigXml::CONTENT>(cNull);
231 root->Add<ConfigXml::CONTENT>("index.html");
232 DisplaySaveAndCompare(doc);
236 TEST_START(test009_feature)
237 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
238 ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
240 ConfigXml::ElementPtr feature = root->Add<ConfigXml::FEATURE>(
241 "http://tizen.org/api/application",
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);
252 TEST_START(test010_preference)
253 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
254 ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
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);
264 TEST_START(test011_access)
265 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
266 ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
268 root->Add<ConfigXml::ACCESS>("http://www.wp.pl/*");
269 root->Add<ConfigXml::ACCESS>("http://onet.pl", true);
270 DisplaySaveAndCompare(doc);
274 TEST_START(test012_tizen_setting)
275 ConfigXml::DocumentPtr doc = ConfigXml::Document::Create();
276 ConfigXml::ElementPtr root = doc->Add<ConfigXml::WIDGET>(cNull,cNull,cNull);
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);