2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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.
22 #include <dali/public-api/dali-core.h>
23 #include <dali-toolkit/public-api/builder/json-parser.h>
25 #include <dali-toolkit-test-suite-utils.h>
28 using namespace Dali::Toolkit;
30 static void Startup();
31 static void Cleanup();
34 void (*tet_startup)() = Startup;
35 void (*tet_cleanup)() = Cleanup;
39 POSITIVE_TC_IDX = 0x01,
43 #define MAX_NUMBER_OF_TESTS 10000
45 struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
48 TEST_FUNCTION( UtcDaliJsonParserMethod01, NEGATIVE_TC_IDX );
49 TEST_FUNCTION( UtcDaliJsonParserMethod02, NEGATIVE_TC_IDX );
50 TEST_FUNCTION( UtcDaliJsonParserMethod03, NEGATIVE_TC_IDX );
51 TEST_FUNCTION( UtcDaliJsonParserMethod04, NEGATIVE_TC_IDX );
52 TEST_FUNCTION( UtcDaliJsonParserMethod05, NEGATIVE_TC_IDX );
53 TEST_FUNCTION( UtcDaliJsonParserMethod06, NEGATIVE_TC_IDX );
54 TEST_FUNCTION( UtcDaliJsonParserMethod07, NEGATIVE_TC_IDX );
55 TEST_FUNCTION( UtcDaliJsonParserMethod08, NEGATIVE_TC_IDX );
56 TEST_FUNCTION( UtcDaliJsonParserMethod09, NEGATIVE_TC_IDX );
57 TEST_FUNCTION( UtcDaliJsonParserMethod10, NEGATIVE_TC_IDX );
59 // Called only once before first test is run.
64 // Called only once after last test is run
69 std::string ReplaceQuotes(const std::string &in_s)
72 // wrong as no embedded quote but had regex link problems
73 std::replace(s.begin(), s.end(), '\'', '"');
78 static void UtcDaliJsonParserMethod01()
80 ToolkitTestApplication application;
82 tet_infoline("JSON basic test");
84 std::string s1( ReplaceQuotes("\
92 'object':{'key':'value'} \
96 JsonParser parser = JsonParser::New();
100 if(parser.ParseError())
102 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
103 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
106 DALI_TEST_CHECK(!parser.ParseError());
108 const TreeNode* root = parser.GetRoot();
110 DALI_TEST_CHECK(root);
112 DALI_TEST_CHECK(root->Size());
114 TreeNode::ConstIterator iter = root->CBegin();
115 DALI_TEST_CHECK(iter != root->CEnd());
117 const TreeNode* node = NULL;
119 node = &((*iter).second);
120 DALI_TEST_CHECK(node);
121 DALI_TEST_CHECK(node->GetType() == TreeNode::STRING);
122 DALI_TEST_CHECK(std::string((*iter).first) == std::string("string"));
123 DALI_TEST_CHECK(std::string(node->GetString()) == std::string("value2"));
126 DALI_TEST_CHECK(iter != root->CEnd());
127 node = &((*iter).second);
128 DALI_TEST_CHECK(node);
129 DALI_TEST_CHECK(node->GetType() == TreeNode::INTEGER);
130 DALI_TEST_CHECK(std::string((*iter).first) == std::string("integer"));
131 DALI_TEST_CHECK(node->GetInteger() == 2);
134 DALI_TEST_CHECK(iter != root->CEnd());
135 node = &((*iter).second);
136 DALI_TEST_CHECK(node);
137 DALI_TEST_CHECK(node->GetType() == TreeNode::FLOAT);
138 DALI_TEST_CHECK(std::string((*iter).first) == std::string("float"));
139 DALI_TEST_CHECK(node->GetFloat() == 2.0);
142 DALI_TEST_CHECK(iter != root->CEnd());
143 node = &((*iter).second);
144 DALI_TEST_CHECK(node);
145 DALI_TEST_CHECK(node->GetType() == TreeNode::BOOLEAN);
146 DALI_TEST_CHECK(std::string((*iter).first) == std::string("boolean"));
147 DALI_TEST_CHECK(node->GetBoolean());
150 DALI_TEST_CHECK(iter != root->CEnd());
151 node = &((*iter).second);
152 DALI_TEST_CHECK(node);
153 DALI_TEST_CHECK(node->GetType() == TreeNode::IS_NULL);
154 DALI_TEST_CHECK(std::string((*iter).first) == std::string("nil"));
157 DALI_TEST_CHECK(iter != root->CEnd());
158 node = &((*iter).second);
159 DALI_TEST_CHECK(node);
160 DALI_TEST_CHECK(node->GetType() == TreeNode::ARRAY);
161 DALI_TEST_CHECK(node->Size() == 3);
162 TreeNode::ConstIterator iterArray = node->CBegin();
164 DALI_TEST_CHECK(iterArray != node->CEnd());
165 DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
166 DALI_TEST_CHECK( (*iterArray).first == NULL );
167 DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 1);
170 DALI_TEST_CHECK(iterArray != node->CEnd());
171 DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
172 DALI_TEST_CHECK( (*iterArray).first == NULL );
173 DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 2);
176 DALI_TEST_CHECK(iterArray != node->CEnd());
177 DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
178 DALI_TEST_CHECK( (*iterArray).first == NULL );
179 DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 3);
182 DALI_TEST_CHECK(iter != root->CEnd());
183 node = &((*iter).second);
184 DALI_TEST_CHECK(node);
185 DALI_TEST_CHECK(node->GetType() == TreeNode::OBJECT);
186 DALI_TEST_CHECK(node->Size() == 1);
188 TreeNode::ConstIterator iterObject = node->CBegin();
189 DALI_TEST_CHECK(iterObject != node->CEnd());
190 DALI_TEST_CHECK( ((*iterObject).second).GetType() == TreeNode::STRING);
191 DALI_TEST_CHECK( std::string((*iterObject).first) == std::string("key" ));
192 DALI_TEST_CHECK( std::string(((*iterObject).second).GetString()) == std::string("value"));
194 tet_result(TET_PASS);
197 static void UtcDaliJsonParserMethod02()
199 ToolkitTestApplication application;
201 tet_infoline("JSON Comments");
203 std::string s1( ReplaceQuotes(" \
204 // some comments with empty line above \n\
206 // inline comments \n\
207 'key':'value', // endline comments \n\
208 // more inline comments \n\
213 JsonParser parser = JsonParser::New();
217 if(parser.ParseError())
219 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
220 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
223 DALI_TEST_CHECK(!parser.ParseError());
225 const TreeNode* root = parser.GetRoot();
227 DALI_TEST_CHECK(root);
229 DALI_TEST_CHECK(root->Size());
231 const TreeNode& node = (*root->CBegin()).second;
233 DALI_TEST_CHECK(node.GetType() == TreeNode::STRING);
235 DALI_TEST_CHECK(node.GetString() == std::string("value"));
237 DALI_TEST_CHECK((*root->CBegin()).first == std::string("key"));
239 tet_result(TET_PASS);
243 static void UtcDaliJsonParserMethod03()
245 ToolkitTestApplication application;
247 tet_infoline("JSON Empty line comment");
249 std::string s1( ReplaceQuotes(
253 "// next empty line comment\n"\
260 JsonParser parser = JsonParser::New();
264 if(parser.ParseError())
266 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
267 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
270 DALI_TEST_CHECK(!parser.ParseError());
272 const TreeNode* root = parser.GetRoot();
274 DALI_TEST_CHECK(root);
276 DALI_TEST_CHECK(root->Size());
278 const TreeNode& node = (*root->CBegin()).second;
280 DALI_TEST_CHECK(node.GetType() == TreeNode::STRING);
282 DALI_TEST_CHECK(node.GetString() == std::string("value"));
284 DALI_TEST_CHECK((*root->CBegin()).first == std::string("key"));
286 tet_result(TET_PASS);
289 static void UtcDaliJsonParserMethod04()
291 ToolkitTestApplication application;
293 tet_infoline("JSON Merge");
295 std::string s1( ReplaceQuotes(" \
304 'actor':'bump-image', \
305 'property':'uLightPosition', \
306 'value':[0.8, 0.0, -1.5], \
307 'alpha-function': 'BOUNCE', \
308 'time-period': { 'duration': 2.5 } \
316 std::string s2( ReplaceQuotes(" \
324 'end-action':'DISCARD' \
330 JsonParser parser = JsonParser::New();
334 if(parser.ParseError())
336 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
337 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
339 DALI_TEST_CHECK(!parser.ParseError());
343 if(parser.ParseError())
345 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
346 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
349 DALI_TEST_CHECK(!parser.ParseError());
351 const TreeNode* root = parser.GetRoot();
352 DALI_TEST_CHECK(root);
354 const TreeNode *node = root->Find("bump");
355 DALI_TEST_CHECK(node);
357 DALI_TEST_CHECK(static_cast<int>(node->Size()) == 4);
359 DALI_TEST_CHECK( node->GetChild("duration") );
360 DALI_TEST_CHECK( node->GetChild("loop") );
361 DALI_TEST_CHECK( node->GetChild("properties") );
364 tet_result(TET_PASS);
367 static void UtcDaliJsonParserMethod05()
369 ToolkitTestApplication application;
371 tet_infoline("JSON Pack & Write");
373 std::string s1( ReplaceQuotes(" \
382 'actor':'bump-image', \
383 'property':'uLightPosition', \
384 'value':[0.8, 0.0, -1.5], \
385 'alpha-function': 'BOUNCE', \
386 'time-period': { 'duration': 2.5 } \
394 JsonParser parser = JsonParser::New();
398 if(parser.ParseError())
400 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
401 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
403 DALI_TEST_CHECK(!parser.ParseError());
413 DALI_TEST_CHECK( a.str() == b.str() );
415 tet_result(TET_PASS);
419 static const int NUMBER_OK_TESTS = 36;
420 char *TEST_OK[NUMBER_OK_TESTS] = {
421 "{ 'hex': '\u0123\u4567\u89AB\uCDEF\uabcd\uef4A' }",
422 "{ 'special': '`1~!@#$%^&*()_+-={:[,]}|;.</>?' }",
423 "{ 'slash': '/ & \' }",
424 "{'object with 1 member':['array with 1 element']}",
425 "[{}, [], -42, true, false, null]",
426 "{ 'integer': 1234567890 }",
427 "{ 'integer': 1234567890 }",
428 "{ 'real': -9876.543210 }",
429 "{ 'e': 0.123456789e-12 }",
430 "{ 'E': 1.234567890E+34 }",
431 "{ '': 23456789012E66 }",
435 "{ 'backslash': '\' }",
436 "{ 'controls': '\\b\\f\\n\\r\\t' }",
437 "{ 'alpha': 'abcdefghijklmnopqrstuvwyz' }",
438 "{ 'ALPHA': 'ABCDEFGHIJKLMNOPQRSTUVWYZ' }",
439 "{ 'digit': '0123456789' }",
440 "{ '0123456789': 'digit' }",
442 "{ 'false': false }",
446 "{ 'address': '1 Communication Centre. South Street' }",
447 "{ 'url': 'http://www.JSON.org/' }",
448 "{ 'comment': '// /* <!-- --' }",
449 "{ '# -- --> */': ' ' }",
450 "{ ' s p a c e d ' :[1,2 , 3,4 , 5 , 6 ,7 ]}",
451 "{ 'compact':[1,2,3,4,5,6,7]}",
452 "{ 'quotes': '" \\u0022 %22 0x22 034 "' }",
453 "{ '\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:': 'A key can be any string'}",
454 "[ 0.5 ,98.6, 99.44,1066,1e1,0.1e1,1e-1,1e00,2e+00,2e-00, 'rosebud']",
455 "{'JSON Test Pattern pass3': { 'The outermost value': 'must be an object or array.', 'In this test': 'It is an object.' } }",
456 "[[[[[[[[[[[[[[[[[[['Not too deep']]]]]]]]]]]]]]]]]]]",
460 static void UtcDaliJsonParserMethod06()
462 ToolkitTestApplication application;
464 tet_infoline("JSON Parse Success");
466 JsonParser parser = JsonParser::New();
468 for(int i = 0; i < NUMBER_OK_TESTS; ++i)
470 parser = JsonParser::New();
472 parser.Parse( ReplaceQuotes(TEST_OK[i]) );
474 if(parser.ParseError())
476 tet_printf("Valid JSON parse test %d Failed", i);
477 tet_printf("%s", ReplaceQuotes(TEST_OK[i]).c_str());
479 tet_printf("JSON Error %d:%d: %s (%d)", parser.GetErrorLineNumber(), parser.GetErrorColumn(), parser.GetErrorDescription().c_str(), parser.GetErrorPosition());
482 DALI_TEST_CHECK(!parser.ParseError());
485 tet_result(TET_PASS);
489 static const int NUMBER_FAIL_TESTS = 32;
490 char *TEST_FAIL[] = {
491 "[' tab\t character \t in\t string ']",
493 "['Colon instead of comma': false]",
494 "{'Numbers cannot have leading zeroes': 013}",
495 "['Bad value', truth]",
496 "['Illegal backslash escape: \017']",
497 "['Bad value', truth]['Illegal backslash escape: \017']",
498 "{'Comma instead if closing brace': true,",
499 "{'Double colon':: null}",
500 "{'Extra comma': true,}",
502 "{'Illegal invocation': alert()}",
503 "{'Missing colon' null}",
505 "{unquoted_key: 'keys must be quoted'}",
506 "'A JSON payload should be an object or array, not a string.'",
508 "{'Illegal expression': 1 + 2}",
509 "{'Extra value after close': true} 'misplaced quoted value'",
513 "['Comma after the close'],",
514 "['double extra comma',,]",
515 "['Illegal backslash escape: \x15']",
517 "{'Comma instead of colon', null}",
521 "{'Numbers cannot be hex': 0x14}",
522 "[ , '<-- missing value']",
525 static void UtcDaliJsonParserMethod07()
527 ToolkitTestApplication application;
529 tet_infoline("JSON Fail");
531 JsonParser parser = JsonParser::New();
533 for(int i = 0; i < NUMBER_FAIL_TESTS; ++i)
535 parser = JsonParser::New();
537 parser.Parse( ReplaceQuotes(TEST_FAIL[i]) );
539 if(!parser.ParseError())
541 tet_printf("Invalid JSON parse test %d Failed", i);
542 tet_printf("%s", ReplaceQuotes(TEST_FAIL[i]).c_str());
543 tet_printf("JSON Error %d:%d %s (%s)", parser.GetErrorLineNumber(), parser.GetErrorColumn(),
544 parser.GetErrorDescription().c_str(), parser.GetErrorPosition());
547 DALI_TEST_CHECK(parser.ParseError());
551 parser = JsonParser::New();
553 parser.Parse( "['single quote']" );
555 if(!parser.ParseError())
557 tet_printf("['single quote']");
560 DALI_TEST_CHECK(parser.ParseError());
562 tet_result(TET_PASS);
565 static void UtcDaliJsonParserMethod08()
567 ToolkitTestApplication application;
569 tet_infoline("JSON error reporting");
571 std::string s1( ReplaceQuotes("\
577 JsonParser parser = JsonParser::New();
581 DALI_TEST_CHECK(parser.ParseError());
583 DALI_TEST_CHECK(1 == parser.GetErrorLineNumber());
584 DALI_TEST_CHECK(53 == parser.GetErrorPosition());
585 DALI_TEST_CHECK(11 == parser.GetErrorColumn());
587 tet_result(TET_PASS);
590 static void UtcDaliJsonParserMethod09()
592 ToolkitTestApplication application;
594 tet_infoline("JSON Pack()");
596 std::string s1( ReplaceQuotes("\
604 'object':{'key':'value'} \
608 JsonParser parser = JsonParser::New();
612 std::stringstream ss1;
613 parser.Write(ss1, 2);
615 parser.Pack(); // Pack() moves strings
617 std::stringstream ss2;
618 parser.Write(ss2, 2);
620 DALI_TEST_CHECK(ss1.str() == ss2.str());
622 tet_result(TET_PASS);
625 static void UtcDaliJsonParserMethod10()
627 ToolkitTestApplication application;
629 tet_infoline("JSON basic test");
631 std::string s1( "" );
633 JsonParser parser = JsonParser::New();
637 DALI_TEST_CHECK(parser.ParseError());
639 tet_result(TET_PASS);