2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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.
23 #include <dali/public-api/dali-core.h>
24 #include <dali-toolkit/public-api/builder/json-parser.h>
26 #include <dali-toolkit-test-suite-utils.h>
29 using namespace Dali::Toolkit;
31 static void Startup();
32 static void Cleanup();
35 void (*tet_startup)() = Startup;
36 void (*tet_cleanup)() = Cleanup;
40 POSITIVE_TC_IDX = 0x01,
44 #define MAX_NUMBER_OF_TESTS 10000
46 struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
49 TEST_FUNCTION( UtcDaliJsonParserMethod01, NEGATIVE_TC_IDX );
50 TEST_FUNCTION( UtcDaliJsonParserMethod02, NEGATIVE_TC_IDX );
51 TEST_FUNCTION( UtcDaliJsonParserMethod03, NEGATIVE_TC_IDX );
52 TEST_FUNCTION( UtcDaliJsonParserMethod04, NEGATIVE_TC_IDX );
53 TEST_FUNCTION( UtcDaliJsonParserMethod05, NEGATIVE_TC_IDX );
54 TEST_FUNCTION( UtcDaliJsonParserMethod06, NEGATIVE_TC_IDX );
55 TEST_FUNCTION( UtcDaliJsonParserMethod07, NEGATIVE_TC_IDX );
56 TEST_FUNCTION( UtcDaliJsonParserMethod08, NEGATIVE_TC_IDX );
57 TEST_FUNCTION( UtcDaliJsonParserMethod09, NEGATIVE_TC_IDX );
58 TEST_FUNCTION( UtcDaliJsonParserMethod10, NEGATIVE_TC_IDX );
60 // Called only once before first test is run.
65 // Called only once after last test is run
70 std::string ReplaceQuotes(const std::string &in_s)
73 // wrong as no embedded quote but had regex link problems
74 std::replace(s.begin(), s.end(), '\'', '"');
79 static void UtcDaliJsonParserMethod01()
81 ToolkitTestApplication application;
83 tet_infoline("JSON basic test");
85 std::string s1( ReplaceQuotes("\
93 'object':{'key':'value'} \
97 JsonParser parser = JsonParser::New();
101 if(parser.ParseError())
103 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
104 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
107 DALI_TEST_CHECK(!parser.ParseError());
109 const TreeNode* root = parser.GetRoot();
111 DALI_TEST_CHECK(root);
113 DALI_TEST_CHECK(root->Size());
115 TreeNode::ConstIterator iter = root->CBegin();
116 DALI_TEST_CHECK(iter != root->CEnd());
118 const TreeNode* node = NULL;
120 node = &((*iter).second);
121 DALI_TEST_CHECK(node);
122 DALI_TEST_CHECK(node->GetType() == TreeNode::STRING);
123 DALI_TEST_CHECK(std::string((*iter).first) == std::string("string"));
124 DALI_TEST_CHECK(std::string(node->GetString()) == std::string("value2"));
127 DALI_TEST_CHECK(iter != root->CEnd());
128 node = &((*iter).second);
129 DALI_TEST_CHECK(node);
130 DALI_TEST_CHECK(node->GetType() == TreeNode::INTEGER);
131 DALI_TEST_CHECK(std::string((*iter).first) == std::string("integer"));
132 DALI_TEST_CHECK(node->GetInteger() == 2);
135 DALI_TEST_CHECK(iter != root->CEnd());
136 node = &((*iter).second);
137 DALI_TEST_CHECK(node);
138 DALI_TEST_CHECK(node->GetType() == TreeNode::FLOAT);
139 DALI_TEST_CHECK(std::string((*iter).first) == std::string("float"));
140 DALI_TEST_CHECK(node->GetFloat() == 2.0);
143 DALI_TEST_CHECK(iter != root->CEnd());
144 node = &((*iter).second);
145 DALI_TEST_CHECK(node);
146 DALI_TEST_CHECK(node->GetType() == TreeNode::BOOLEAN);
147 DALI_TEST_CHECK(std::string((*iter).first) == std::string("boolean"));
148 DALI_TEST_CHECK(node->GetBoolean());
151 DALI_TEST_CHECK(iter != root->CEnd());
152 node = &((*iter).second);
153 DALI_TEST_CHECK(node);
154 DALI_TEST_CHECK(node->GetType() == TreeNode::IS_NULL);
155 DALI_TEST_CHECK(std::string((*iter).first) == std::string("nil"));
158 DALI_TEST_CHECK(iter != root->CEnd());
159 node = &((*iter).second);
160 DALI_TEST_CHECK(node);
161 DALI_TEST_CHECK(node->GetType() == TreeNode::ARRAY);
162 DALI_TEST_CHECK(node->Size() == 3);
163 TreeNode::ConstIterator iterArray = node->CBegin();
165 DALI_TEST_CHECK(iterArray != node->CEnd());
166 DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
167 DALI_TEST_CHECK( (*iterArray).first == NULL );
168 DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 1);
171 DALI_TEST_CHECK(iterArray != node->CEnd());
172 DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
173 DALI_TEST_CHECK( (*iterArray).first == NULL );
174 DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 2);
177 DALI_TEST_CHECK(iterArray != node->CEnd());
178 DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
179 DALI_TEST_CHECK( (*iterArray).first == NULL );
180 DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 3);
183 DALI_TEST_CHECK(iter != root->CEnd());
184 node = &((*iter).second);
185 DALI_TEST_CHECK(node);
186 DALI_TEST_CHECK(node->GetType() == TreeNode::OBJECT);
187 DALI_TEST_CHECK(node->Size() == 1);
189 TreeNode::ConstIterator iterObject = node->CBegin();
190 DALI_TEST_CHECK(iterObject != node->CEnd());
191 DALI_TEST_CHECK( ((*iterObject).second).GetType() == TreeNode::STRING);
192 DALI_TEST_CHECK( std::string((*iterObject).first) == std::string("key" ));
193 DALI_TEST_CHECK( std::string(((*iterObject).second).GetString()) == std::string("value"));
195 tet_result(TET_PASS);
198 static void UtcDaliJsonParserMethod02()
200 ToolkitTestApplication application;
202 tet_infoline("JSON Comments");
204 std::string s1( ReplaceQuotes(" \
205 // some comments with empty line above \n\
207 // inline comments \n\
208 'key':'value', // endline comments \n\
209 // more inline comments \n\
214 JsonParser parser = JsonParser::New();
218 if(parser.ParseError())
220 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
221 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
224 DALI_TEST_CHECK(!parser.ParseError());
226 const TreeNode* root = parser.GetRoot();
228 DALI_TEST_CHECK(root);
230 DALI_TEST_CHECK(root->Size());
232 const TreeNode& node = (*root->CBegin()).second;
234 DALI_TEST_CHECK(node.GetType() == TreeNode::STRING);
236 DALI_TEST_CHECK(node.GetString() == std::string("value"));
238 DALI_TEST_CHECK((*root->CBegin()).first == std::string("key"));
240 tet_result(TET_PASS);
244 static void UtcDaliJsonParserMethod03()
246 ToolkitTestApplication application;
248 tet_infoline("JSON Empty line comment");
250 std::string s1( ReplaceQuotes(
254 "// next empty line comment\n"\
261 JsonParser parser = JsonParser::New();
265 if(parser.ParseError())
267 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
268 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
271 DALI_TEST_CHECK(!parser.ParseError());
273 const TreeNode* root = parser.GetRoot();
275 DALI_TEST_CHECK(root);
277 DALI_TEST_CHECK(root->Size());
279 const TreeNode& node = (*root->CBegin()).second;
281 DALI_TEST_CHECK(node.GetType() == TreeNode::STRING);
283 DALI_TEST_CHECK(node.GetString() == std::string("value"));
285 DALI_TEST_CHECK((*root->CBegin()).first == std::string("key"));
287 tet_result(TET_PASS);
290 static void UtcDaliJsonParserMethod04()
292 ToolkitTestApplication application;
294 tet_infoline("JSON Merge");
296 std::string s1( ReplaceQuotes(" \
305 'actor':'bump-image', \
306 'property':'uLightPosition', \
307 'value':[0.8, 0.0, -1.5], \
308 'alpha-function': 'BOUNCE', \
309 'time-period': { 'duration': 2.5 } \
317 std::string s2( ReplaceQuotes(" \
325 'end-action':'DISCARD' \
331 JsonParser parser = JsonParser::New();
335 if(parser.ParseError())
337 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
338 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
340 DALI_TEST_CHECK(!parser.ParseError());
344 if(parser.ParseError())
346 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
347 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
350 DALI_TEST_CHECK(!parser.ParseError());
352 const TreeNode* root = parser.GetRoot();
353 DALI_TEST_CHECK(root);
355 const TreeNode *node = root->Find("bump");
356 DALI_TEST_CHECK(node);
358 DALI_TEST_CHECK(static_cast<int>(node->Size()) == 4);
360 DALI_TEST_CHECK( node->GetChild("duration") );
361 DALI_TEST_CHECK( node->GetChild("loop") );
362 DALI_TEST_CHECK( node->GetChild("properties") );
365 tet_result(TET_PASS);
368 static void UtcDaliJsonParserMethod05()
370 ToolkitTestApplication application;
372 tet_infoline("JSON Pack & Write");
374 std::string s1( ReplaceQuotes(" \
383 'actor':'bump-image', \
384 'property':'uLightPosition', \
385 'value':[0.8, 0.0, -1.5], \
386 'alpha-function': 'BOUNCE', \
387 'time-period': { 'duration': 2.5 } \
395 JsonParser parser = JsonParser::New();
399 if(parser.ParseError())
401 std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
402 std::cout << " at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
404 DALI_TEST_CHECK(!parser.ParseError());
414 DALI_TEST_CHECK( a.str() == b.str() );
416 tet_result(TET_PASS);
420 static const int NUMBER_OK_TESTS = 36;
421 char *TEST_OK[NUMBER_OK_TESTS] = {
422 "{ 'hex': '\u0123\u4567\u89AB\uCDEF\uabcd\uef4A' }",
423 "{ 'special': '`1~!@#$%^&*()_+-={:[,]}|;.</>?' }",
424 "{ 'slash': '/ & \' }",
425 "{'object with 1 member':['array with 1 element']}",
426 "[{}, [], -42, true, false, null]",
427 "{ 'integer': 1234567890 }",
428 "{ 'integer': 1234567890 }",
429 "{ 'real': -9876.543210 }",
430 "{ 'e': 0.123456789e-12 }",
431 "{ 'E': 1.234567890E+34 }",
432 "{ '': 23456789012E66 }",
436 "{ 'backslash': '\' }",
437 "{ 'controls': '\\b\\f\\n\\r\\t' }",
438 "{ 'alpha': 'abcdefghijklmnopqrstuvwyz' }",
439 "{ 'ALPHA': 'ABCDEFGHIJKLMNOPQRSTUVWYZ' }",
440 "{ 'digit': '0123456789' }",
441 "{ '0123456789': 'digit' }",
443 "{ 'false': false }",
447 "{ 'address': '1 Communication Centre. South Street' }",
448 "{ 'url': 'http://www.JSON.org/' }",
449 "{ 'comment': '// /* <!-- --' }",
450 "{ '# -- --> */': ' ' }",
451 "{ ' s p a c e d ' :[1,2 , 3,4 , 5 , 6 ,7 ]}",
452 "{ 'compact':[1,2,3,4,5,6,7]}",
453 "{ 'quotes': '" \\u0022 %22 0x22 034 "' }",
454 "{ '\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:': 'A key can be any string'}",
455 "[ 0.5 ,98.6, 99.44,1066,1e1,0.1e1,1e-1,1e00,2e+00,2e-00, 'rosebud']",
456 "{'JSON Test Pattern pass3': { 'The outermost value': 'must be an object or array.', 'In this test': 'It is an object.' } }",
457 "[[[[[[[[[[[[[[[[[[['Not too deep']]]]]]]]]]]]]]]]]]]",
461 static void UtcDaliJsonParserMethod06()
463 ToolkitTestApplication application;
465 tet_infoline("JSON Parse Success");
467 JsonParser parser = JsonParser::New();
469 for(int i = 0; i < NUMBER_OK_TESTS; ++i)
471 parser = JsonParser::New();
473 parser.Parse( ReplaceQuotes(TEST_OK[i]) );
475 if(parser.ParseError())
477 tet_printf("Valid JSON parse test %d Failed", i);
478 tet_printf("%s", ReplaceQuotes(TEST_OK[i]).c_str());
480 tet_printf("JSON Error %d:%d: %s (%d)", parser.GetErrorLineNumber(), parser.GetErrorColumn(), parser.GetErrorDescription().c_str(), parser.GetErrorPosition());
483 DALI_TEST_CHECK(!parser.ParseError());
486 tet_result(TET_PASS);
490 static const int NUMBER_FAIL_TESTS = 32;
491 char *TEST_FAIL[] = {
492 "[' tab\t character \t in\t string ']",
494 "['Colon instead of comma': false]",
495 "{'Numbers cannot have leading zeroes': 013}",
496 "['Bad value', truth]",
497 "['Illegal backslash escape: \017']",
498 "['Bad value', truth]['Illegal backslash escape: \017']",
499 "{'Comma instead if closing brace': true,",
500 "{'Double colon':: null}",
501 "{'Extra comma': true,}",
503 "{'Illegal invocation': alert()}",
504 "{'Missing colon' null}",
506 "{unquoted_key: 'keys must be quoted'}",
507 "'A JSON payload should be an object or array, not a string.'",
509 "{'Illegal expression': 1 + 2}",
510 "{'Extra value after close': true} 'misplaced quoted value'",
514 "['Comma after the close'],",
515 "['double extra comma',,]",
516 "['Illegal backslash escape: \x15']",
518 "{'Comma instead of colon', null}",
522 "{'Numbers cannot be hex': 0x14}",
523 "[ , '<-- missing value']",
526 static void UtcDaliJsonParserMethod07()
528 ToolkitTestApplication application;
530 tet_infoline("JSON Fail");
532 JsonParser parser = JsonParser::New();
534 for(int i = 0; i < NUMBER_FAIL_TESTS; ++i)
536 parser = JsonParser::New();
538 parser.Parse( ReplaceQuotes(TEST_FAIL[i]) );
540 if(!parser.ParseError())
542 tet_printf("Invalid JSON parse test %d Failed", i);
543 tet_printf("%s", ReplaceQuotes(TEST_FAIL[i]).c_str());
544 tet_printf("JSON Error %d:%d %s (%s)", parser.GetErrorLineNumber(), parser.GetErrorColumn(),
545 parser.GetErrorDescription().c_str(), parser.GetErrorPosition());
548 DALI_TEST_CHECK(parser.ParseError());
552 parser = JsonParser::New();
554 parser.Parse( "['single quote']" );
556 if(!parser.ParseError())
558 tet_printf("['single quote']");
561 DALI_TEST_CHECK(parser.ParseError());
563 tet_result(TET_PASS);
566 static void UtcDaliJsonParserMethod08()
568 ToolkitTestApplication application;
570 tet_infoline("JSON error reporting");
572 std::string s1( ReplaceQuotes("\
578 JsonParser parser = JsonParser::New();
582 DALI_TEST_CHECK(parser.ParseError());
584 DALI_TEST_CHECK(1 == parser.GetErrorLineNumber());
585 DALI_TEST_CHECK(53 == parser.GetErrorPosition());
586 DALI_TEST_CHECK(11 == parser.GetErrorColumn());
588 tet_result(TET_PASS);
591 static void UtcDaliJsonParserMethod09()
593 ToolkitTestApplication application;
595 tet_infoline("JSON Pack()");
597 std::string s1( ReplaceQuotes("\
605 'object':{'key':'value'} \
609 JsonParser parser = JsonParser::New();
613 std::stringstream ss1;
614 parser.Write(ss1, 2);
616 parser.Pack(); // Pack() moves strings
618 std::stringstream ss2;
619 parser.Write(ss2, 2);
621 DALI_TEST_CHECK(ss1.str() == ss2.str());
623 tet_result(TET_PASS);
626 static void UtcDaliJsonParserMethod10()
628 ToolkitTestApplication application;
630 tet_infoline("JSON basic test");
632 std::string s1( "" );
634 JsonParser parser = JsonParser::New();
638 DALI_TEST_CHECK(parser.ParseError());
640 tet_result(TET_PASS);