[dali_2.0.15] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-JsonParser.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
18 #include <iostream>
19 #include <stdlib.h>
20 #include <dali-toolkit-test-suite-utils.h>
21 #include <dali-toolkit/dali-toolkit.h>
22 #include <dali-toolkit/devel-api/builder/json-parser.h>
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 void utc_json_parser_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_json_parser_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 namespace
38 {
39
40 std::string ReplaceQuotes(const std::string &in_s)
41 {
42   std::string s(in_s);
43   // wrong as no embedded quote but had regex link problems
44   std::replace(s.begin(), s.end(), '\'', '"');
45   return s;
46 }
47
48 void CompareTrees(const TreeNode& a, const TreeNode& b)
49 {
50   DALI_TEST_CHECK( a.GetType() == b.GetType() );
51
52   DALI_TEST_CHECK( a.Size() == b.Size() );
53
54   if( a.GetName() )
55   {
56     DALI_TEST_CHECK( std::string( a.GetName() ) == std::string( b.GetName() ) );
57   }
58
59   DALI_TEST_CHECK( a.HasSubstitution() == b.HasSubstitution() );
60
61   switch( a.GetType() )
62   {
63     case TreeNode::OBJECT:
64     case TreeNode::ARRAY:
65     {
66       for( TreeNode::ConstIterator aiter = a.CBegin(), biter = b.CBegin();
67            aiter != a.CEnd() && biter != b.CEnd(); ++aiter, ++biter )
68       {
69         CompareTrees( (*aiter).second, (*biter).second );
70       }
71       break;
72     }
73     case TreeNode::STRING:
74     {
75       DALI_TEST_CHECK( std::string( a.GetString() ) == std::string( b.GetString() ) );
76       break;
77     }
78     case TreeNode::FLOAT:
79     {
80       DALI_TEST_CHECK( a.GetFloat() == b.GetFloat() );
81       break;
82     }
83     case TreeNode::INTEGER:
84     {
85       DALI_TEST_CHECK( a.GetInteger() == b.GetInteger());
86       break;
87     }
88     case TreeNode::BOOLEAN:
89     {
90       DALI_TEST_CHECK( a.GetBoolean() == b.GetBoolean() );
91       break;
92     }
93     default:
94     {
95       break;
96     }
97   }
98 }
99
100
101 }
102
103
104 int UtcDaliJsonParserMethod01(void)
105 {
106   ToolkitTestApplication application;
107
108   tet_infoline("JSON basic test");
109
110   std::string s1( ReplaceQuotes(
111 "{ \
112   'string':'value2', \
113   'integer':2, \
114   'float':2.0, \
115   'boolean':true, \
116   'nil':null, \
117   'array':[1,2,3], \
118   'object':{'key':'value'} \
119 }"));
120
121   JsonParser parser = JsonParser::New();
122
123   parser.Parse( s1 );
124
125   if(parser.ParseError())
126   {
127     std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
128     std::cout << "   at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
129   }
130
131   DALI_TEST_CHECK(!parser.ParseError());
132
133   const TreeNode* root = parser.GetRoot();
134
135   DALI_TEST_CHECK(root);
136
137   DALI_TEST_CHECK(root->Size());
138
139   TreeNode::ConstIterator iter = root->CBegin();
140   DALI_TEST_CHECK(iter != root->CEnd());
141
142   const TreeNode* node = NULL;
143
144   node = &((*iter).second);
145   DALI_TEST_CHECK(node);
146   DALI_TEST_CHECK(node->GetType() == TreeNode::STRING);
147   DALI_TEST_CHECK(std::string((*iter).first) == std::string("string"));
148   DALI_TEST_CHECK(std::string(node->GetString()) == std::string("value2"));
149
150   ++iter;
151   DALI_TEST_CHECK(iter != root->CEnd());
152   node = &((*iter).second);
153   DALI_TEST_CHECK(node);
154   DALI_TEST_CHECK(node->GetType() == TreeNode::INTEGER);
155   DALI_TEST_CHECK(std::string((*iter).first) == std::string("integer"));
156   DALI_TEST_CHECK(node->GetInteger() == 2);
157
158   ++iter;
159   DALI_TEST_CHECK(iter != root->CEnd());
160   node = &((*iter).second);
161   DALI_TEST_CHECK(node);
162   DALI_TEST_CHECK(node->GetType() == TreeNode::FLOAT);
163   DALI_TEST_CHECK(std::string((*iter).first) == std::string("float"));
164   DALI_TEST_CHECK(node->GetFloat() == 2.0);
165
166   ++iter;
167   DALI_TEST_CHECK(iter != root->CEnd());
168   node = &((*iter).second);
169   DALI_TEST_CHECK(node);
170   DALI_TEST_CHECK(node->GetType() == TreeNode::BOOLEAN);
171   DALI_TEST_CHECK(std::string((*iter).first) == std::string("boolean"));
172   DALI_TEST_CHECK(node->GetBoolean());
173
174   ++iter;
175   DALI_TEST_CHECK(iter != root->CEnd());
176   node = &((*iter).second);
177   DALI_TEST_CHECK(node);
178   DALI_TEST_CHECK(node->GetType() == TreeNode::IS_NULL);
179   DALI_TEST_CHECK(std::string((*iter).first) == std::string("nil"));
180
181   ++iter;
182   DALI_TEST_CHECK(iter != root->CEnd());
183   node = &((*iter).second);
184   DALI_TEST_CHECK(node);
185   DALI_TEST_CHECK(node->GetType() == TreeNode::ARRAY);
186   DALI_TEST_CHECK(node->Size() == 3);
187   TreeNode::ConstIterator iterArray = node->CBegin();
188
189   DALI_TEST_CHECK(iterArray != node->CEnd());
190   DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
191   DALI_TEST_CHECK( (*iterArray).first == NULL );
192   DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 1);
193
194   ++iterArray;
195   DALI_TEST_CHECK(iterArray != node->CEnd());
196   DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
197   DALI_TEST_CHECK( (*iterArray).first == NULL );
198   DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 2);
199
200   ++iterArray;
201   DALI_TEST_CHECK(iterArray != node->CEnd());
202   DALI_TEST_CHECK( ((*iterArray).second).GetType() == TreeNode::INTEGER);
203   DALI_TEST_CHECK( (*iterArray).first == NULL );
204   DALI_TEST_CHECK( ((*iterArray).second).GetInteger() == 3);
205
206   ++iter;
207   DALI_TEST_CHECK(iter != root->CEnd());
208   node = &((*iter).second);
209   DALI_TEST_CHECK(node);
210   DALI_TEST_CHECK(node->GetType() == TreeNode::OBJECT);
211   DALI_TEST_CHECK(node->Size() == 1);
212
213   TreeNode::ConstIterator iterObject = node->CBegin();
214   DALI_TEST_CHECK(iterObject != node->CEnd());
215   DALI_TEST_CHECK( ((*iterObject).second).GetType() == TreeNode::STRING);
216   DALI_TEST_CHECK( std::string((*iterObject).first) == std::string("key" ));
217   DALI_TEST_CHECK( std::string(((*iterObject).second).GetString()) == std::string("value"));
218
219   ++iter;
220   DALI_TEST_CHECK(!( iter != root->CEnd() ));
221
222   ++iter; // Go past the end
223
224   iter++; // Use the other operator using the post increment operator
225
226   tet_result(TET_PASS);
227   END_TEST;
228 }
229
230 int UtcDaliJsonParserMethod02(void)
231 {
232   ToolkitTestApplication application;
233
234   tet_infoline("JSON Comments");
235
236   std::string s1( ReplaceQuotes("         \
237 // some comments with empty line above  \n\
238 {                                         \
239   // inline comments                    \n\
240   'key':'value', // endline comments    \n\
241   // more inline comments               \n\
242   'key2':'value2'                         \
243 }                                         \
244 "));
245
246   JsonParser parser = JsonParser::New();
247
248   parser.Parse( s1 );
249
250   if(parser.ParseError())
251   {
252     std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
253     std::cout << "   at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
254   }
255
256   DALI_TEST_CHECK(!parser.ParseError());
257
258   const TreeNode* root = parser.GetRoot();
259
260   DALI_TEST_CHECK(root);
261
262   DALI_TEST_CHECK(root->Size());
263
264   const TreeNode& node = (*root->CBegin()).second;
265
266   DALI_TEST_CHECK(node.GetType() == TreeNode::STRING);
267
268   DALI_TEST_CHECK(node.GetString() == std::string("value"));
269
270   DALI_TEST_CHECK((*root->CBegin()).first == std::string("key"));
271
272   tet_result(TET_PASS);
273   END_TEST;
274 }
275
276
277 int UtcDaliJsonParserMethod03(void)
278 {
279   ToolkitTestApplication application;
280
281   tet_infoline("JSON Empty line comment");
282
283   std::string s1( ReplaceQuotes(
284 "/*\n" \
285 "c comment\n" \
286 "*/"\
287 "// next empty line comment\n"\
288 "//\n"\
289 "{\n"\
290 "  'key':'value'\n"\
291 "}\n"\
292 ));
293
294   JsonParser parser = JsonParser::New();
295
296   parser.Parse( s1 );
297
298   if(parser.ParseError())
299   {
300     std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
301     std::cout << "   at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
302   }
303
304   DALI_TEST_CHECK(!parser.ParseError());
305
306   const TreeNode* root = parser.GetRoot();
307
308   DALI_TEST_CHECK(root);
309
310   DALI_TEST_CHECK(root->Size());
311
312   const TreeNode& node = (*root->CBegin()).second;
313
314   DALI_TEST_CHECK(node.GetType() == TreeNode::STRING);
315
316   DALI_TEST_CHECK(node.GetString() == std::string("value"));
317
318   DALI_TEST_CHECK((*root->CBegin()).first == std::string("key"));
319
320   tet_result(TET_PASS);
321   END_TEST;
322 }
323
324 int UtcDaliJsonParserMethod04(void)
325 {
326   ToolkitTestApplication application;
327
328   tet_infoline("JSON Merge");
329
330   std::string s1( ReplaceQuotes("                                       \
331 {                                                                       \
332   'animations':                                                         \
333   {                                                                     \
334     'bump':                                                             \
335     {                                                                   \
336       'properties':                                                     \
337       [                                                                 \
338         {                                                               \
339           'actor':'bump-image',                                         \
340           'property':'uLightPosition',                                  \
341           'value':[0.8, 0.0, -1.5],                                     \
342           'alphaFunction': 'BOUNCE',                                    \
343           'timePeriod': { 'duration': 2.5 }                            \
344         }                                                               \
345       ]                                                                 \
346     }                                                                   \
347   }                                                                     \
348 }                                                                       \
349 "));
350
351   std::string s2( ReplaceQuotes("                                       \
352 {                                                                       \
353   'animations':                                                         \
354   {                                                                     \
355     'bump':                                                             \
356     {                                                                   \
357       'duration': 5.0,                                                  \
358       'loop': true,                                                     \
359       'endAction':'DISCARD'                                            \
360     }                                                                   \
361   }                                                                     \
362 }                                                                       \
363 "));
364
365   JsonParser parser = JsonParser::New();
366
367   parser.Parse( s1 );
368
369   if(parser.ParseError())
370   {
371     std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
372     std::cout << "   at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
373   }
374   DALI_TEST_CHECK(!parser.ParseError());
375
376   parser.Parse( s2 );
377
378   if(parser.ParseError())
379   {
380     std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
381     std::cout << "   at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
382   }
383
384   DALI_TEST_CHECK(!parser.ParseError());
385
386   const TreeNode* root = parser.GetRoot();
387   DALI_TEST_CHECK(root);
388
389   const TreeNode *node = root->Find("bump");
390   DALI_TEST_CHECK(node);
391
392   DALI_TEST_CHECK(static_cast<int>(node->Size()) == 4);
393
394   DALI_TEST_CHECK( node->GetChild("duration") );
395   DALI_TEST_CHECK( node->GetChild("loop") );
396   DALI_TEST_CHECK( node->GetChild("properties") );
397
398
399   tet_result(TET_PASS);
400   END_TEST;
401 }
402
403 int UtcDaliJsonParserMethod05(void)
404 {
405   ToolkitTestApplication application;
406
407   tet_infoline("JSON Pack & Write");
408
409   std::string s1( ReplaceQuotes("                                       \
410 {                                                                       \
411   'animations':                                                         \
412   {                                                                     \
413     'bump':                                                             \
414     {                                                                   \
415       'properties':                                                     \
416       [                                                                 \
417         {                                                               \
418           'actor':'bump-image',                                         \
419           'property':'uLightPosition',                                  \
420           'value':[0.8, 0.0, -1.5],                                     \
421           'alphaFunction': 'BOUNCE',                                    \
422           'timePeriod': { 'duration': 2.5 }                            \
423         }                                                               \
424       ]                                                                 \
425     }                                                                   \
426   }                                                                     \
427 }                                                                       \
428 "));
429
430   JsonParser parser = JsonParser::New();
431
432   parser.Parse( s1 );
433
434   if(parser.ParseError())
435   {
436     std::cout << "Error: " << parser.GetErrorDescription() << std::endl;
437     std::cout << "   at: " << parser.GetErrorLineNumber() << "(" << parser.GetErrorPosition() << ")" << std::endl;
438   }
439   DALI_TEST_CHECK(!parser.ParseError());
440
441   std::stringstream a;
442   parser.Write(a, 2);
443
444   parser.Pack();
445
446   std::stringstream b;
447   parser.Write(b, 2);
448
449   DALI_TEST_CHECK( a.str() == b.str() );
450
451   tet_result(TET_PASS);
452   END_TEST;
453 }
454
455 namespace
456 {
457
458 static const int NUMBER_OK_TESTS = 36;
459 const char *TEST_OK[NUMBER_OK_TESTS] = {
460   "{ 'hex': '\u0123\u4567\u89AB\uCDEF\uabcd\uef4A' }",
461   "{ 'special': '`1~!@#$%^&*()_+-={:[,]}|;.</>?' }",
462   "{ 'slash': '/ & \' }",
463   "{'object with 1 member':['array with 1 element']}",
464   "[{}, [], -42, true, false, null]",
465   "{ 'integer': 1234567890 }",
466   "{ 'integer': 1234567890 }",
467   "{ 'real': -9876.543210 }",
468   "{ 'e': 0.123456789e-12 }",
469   "{ 'E': 1.234567890E+34 }",
470   "{ '':  23456789012E66 }",
471   "{ 'zero': 0 }",
472   "{ 'one': 1 }",
473   "{ 'space': ' ' }",
474   "{ 'backslash': '\' }",
475   "{ 'controls': '\\b\\f\\n\\r\\t' }",
476   "{ 'alpha': 'abcdefghijklmnopqrstuvwyz' }",
477   "{ 'ALPHA': 'ABCDEFGHIJKLMNOPQRSTUVWYZ' }",
478   "{ 'digit': '0123456789' }",
479   "{ '0123456789': 'digit' }",
480   "{ 'true': true }",
481   "{ 'false': false }",
482   "{ 'null': null }",
483   "{ 'array':[  ] }",
484   "{ 'object':{  } }",
485   "{ 'address': '1 Communication Centre. South Street' }",
486   "{ 'url': 'http://www.JSON.org/' }",
487   "{ 'comment': '// /* <!-- --' }",
488   "{ '# -- --> */': ' ' }",
489   "{ ' s p a c e d ' :[1,2 , 3,4 , 5        ,          6           ,7        ]}",
490   "{ 'compact':[1,2,3,4,5,6,7]}",
491   "{ 'quotes': '&#34; \\u0022 %22 0x22 034 &#x22;' }",
492   "{ '\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:': 'A key can be any string'}",
493   "[ 0.5 ,98.6, 99.44,1066,1e1,0.1e1,1e-1,1e00,2e+00,2e-00, 'rosebud']",
494   "{'JSON Test Pattern pass3': { 'The outermost value': 'must be an object or array.', 'In this test': 'It is an object.' } }",
495   "[[[[[[[[[[[[[[[[[[['Not too deep']]]]]]]]]]]]]]]]]]]",
496 };
497 }
498
499
500 int UtcDaliJsonParserMethod06(void)
501 {
502   ToolkitTestApplication application;
503
504   tet_infoline("JSON Parse Success");
505
506   JsonParser parser = JsonParser::New();
507
508   for(int i = 0; i < NUMBER_OK_TESTS; ++i)
509   {
510     parser = JsonParser::New();
511
512     parser.Parse( ReplaceQuotes(TEST_OK[i]) );
513
514     if(parser.ParseError())
515     {
516       tet_printf("Valid JSON parse test %d Failed", i);
517       tet_printf("%s", ReplaceQuotes(TEST_OK[i]).c_str());
518
519       tet_printf("JSON Error %d:%d: %s (%d)", parser.GetErrorLineNumber(), parser.GetErrorColumn(), parser.GetErrorDescription().c_str(), parser.GetErrorPosition());
520     }
521
522     DALI_TEST_CHECK(!parser.ParseError());
523   }
524
525   tet_result(TET_PASS);
526   END_TEST;
527 }
528
529 namespace
530 {
531
532 static const int NUMBER_FAIL_TESTS = 34;
533 const char *TEST_FAIL[] = {
534   "[' tab\t   character  \t in\t string   ']",
535   "['Extra close']]",
536   "['Colon instead of comma': false]",
537   "{'Numbers cannot have leading zeroes': 013}",
538   "['Bad value', truth]",
539   "['Illegal backslash escape: \017']",
540   "['Bad value', truth]['Illegal backslash escape: \017']",
541   "{'Comma instead if closing brace': true,",
542   "{'Double colon':: null}",
543   "{'Extra comma': true,}",
544   "['Unclosed array'",
545   "{'Illegal invocation': alert()}",
546   "{'Missing colon' null}",
547   "[0e]",
548   "{unquoted_key: 'keys must be quoted'}",
549   "'A JSON payload should be an object or array, not a string.'",
550   "[\naked]",
551   "{'Illegal expression': 1 + 2}",
552   "{'Extra value after close': true} 'misplaced quoted value'",
553   "[0e+]",
554   "[+23456789012E66]",
555   "['extra comma',]",
556   "['Comma after the close'],",
557   "['double extra comma',,]",
558   "['Illegal backslash escape: \x15']",
559   "['line\nbreak']",
560   "{'Comma instead of colon', null}",
561   "['mismatch'}",
562   "['line\nbreak']",
563   "[0e+-1]",
564   "{'Numbers cannot be hex': 0x14}",
565   "[   , '<-- missing value']",
566   "[{'no comma':1} {'b:2}]",
567   "{'extra comma':1,}",
568 };
569 }
570
571 int UtcDaliJsonParserMethod07(void)
572 {
573   ToolkitTestApplication application;
574
575   tet_infoline("JSON Fail");
576
577   JsonParser parser = JsonParser::New();
578
579   for(int i = 0; i < NUMBER_FAIL_TESTS; ++i)
580   {
581     parser = JsonParser::New();
582
583     parser.Parse( ReplaceQuotes(TEST_FAIL[i]) );
584
585     if(!parser.ParseError())
586     {
587       tet_printf("Invalid JSON parse test %d Failed", i);
588       tet_printf("%s", ReplaceQuotes(TEST_FAIL[i]).c_str());
589       tet_printf("JSON Error %d:%d %s (%s)", parser.GetErrorLineNumber(), parser.GetErrorColumn(),
590                  parser.GetErrorDescription().c_str(), parser.GetErrorPosition());
591     }
592
593     DALI_TEST_CHECK(parser.ParseError());
594   }
595
596
597   parser = JsonParser::New();
598
599   parser.Parse( "['single quote']" );
600
601   if(!parser.ParseError())
602   {
603     tet_printf("['single quote']");
604   }
605
606   DALI_TEST_CHECK(parser.ParseError());
607
608   tet_result(TET_PASS);
609   END_TEST;
610 }
611
612 int UtcDaliJsonParserMethod08(void)
613 {
614   ToolkitTestApplication application;
615
616   tet_infoline("JSON error reporting");
617
618   std::string s1( ReplaceQuotes("\
619 {                                         \n\
620   'float':,],                             \n\
621 }                                         \n\
622 "));
623
624   JsonParser parser = JsonParser::New();
625
626   parser.Parse( s1 );
627
628   DALI_TEST_CHECK(parser.ParseError());
629
630   DALI_TEST_CHECK(1  == parser.GetErrorLineNumber());
631   DALI_TEST_CHECK(53 == parser.GetErrorPosition());
632   DALI_TEST_CHECK(11 == parser.GetErrorColumn());
633   DALI_TEST_CHECK("Missing Value" == parser.GetErrorDescription());
634
635   tet_result(TET_PASS);
636   END_TEST;
637 }
638
639 int UtcDaliJsonParserMethod09(void)
640 {
641   ToolkitTestApplication application;
642
643   tet_infoline("JSON Pack()");
644
645   std::string s1( ReplaceQuotes("\
646 {                                         \
647   'string':'value2',                      \
648   'integer':2,                            \
649   'float':2.3,                            \
650   'boolean':true,                         \
651   'nil':null,                             \
652   'array':[1,2,3],                        \
653   'object':{'key':'value'}                \
654 }                                         \
655 "));
656
657   JsonParser parser = JsonParser::New();
658
659   parser.Parse( s1 );
660
661   std::stringstream ss1;
662   parser.Write(ss1, 2);
663
664   parser.Pack(); // Pack() moves strings
665
666   std::stringstream ss2;
667   parser.Write(ss2, 2);
668
669   DALI_TEST_CHECK(ss1.str() == ss2.str());
670
671   tet_result(TET_PASS);
672   END_TEST;
673 }
674
675 int UtcDaliJsonParserMethod10(void)
676 {
677   ToolkitTestApplication application;
678
679   tet_infoline("JSON empty data");
680
681   std::string s1( "" );
682
683   JsonParser parser = JsonParser::New();
684
685   parser.Parse( s1 );
686
687   DALI_TEST_CHECK(parser.ParseError());
688
689   tet_result(TET_PASS);
690   END_TEST;
691 }
692
693 int UtcDaliJsonParserMethod11(void)
694 {
695   ToolkitTestApplication application;
696   tet_infoline("JSON tree copy");
697
698   std::string s1( ReplaceQuotes("                                       \
699 {                                                                       \
700   'animations':                                                         \
701   {                                                                     \
702     'bump':                                                             \
703     {                                                                   \
704       'properties':                                                     \
705       [                                                                 \
706         {                                                               \
707           'actor':'bump-image',                                         \
708           'property':'uLightPosition',                                  \
709           'value':[0.8, 0.0, -1.5],                                     \
710           'alphaFunction': 'BOUNCE',                                    \
711           'timePeriod': { 'duration': 2.5 }                            \
712         }                                                               \
713       ]                                                                 \
714     }                                                                   \
715   }                                                                     \
716 }                                                                       \
717 "));
718
719   JsonParser parser = JsonParser::New();
720
721   parser.Parse( s1 );
722
723   JsonParser parser2 = JsonParser::New(*parser.GetRoot());
724
725   DALI_TEST_CHECK(parser.GetRoot());
726   DALI_TEST_CHECK(parser2.GetRoot());
727
728   CompareTrees( *parser.GetRoot(), *parser2.GetRoot() );
729
730   tet_result(TET_PASS);
731   END_TEST;
732 }
733
734
735 int UtcDaliJsonParserMerge1(void)
736 {
737   ToolkitTestApplication application;
738   tet_infoline("JSON tree merge");
739
740   std::string s1( ReplaceQuotes("                                       \
741 {                                                                       \
742   'styles':                                                             \
743   {                                                                     \
744     'button':                                                           \
745     {                                                                   \
746       'backgroundColor':[0.8, 0.0, 1.0, 1.0],                           \
747       'foregroundColor':[1, 1, 1, 1]                                    \
748     }                                                                   \
749   }                                                                     \
750 }                                                                       \
751 "));
752
753   JsonParser parser = JsonParser::New();
754   JsonParser testParser = JsonParser::New();
755
756   testParser.Parse( s1 );
757
758   parser.Parse( s1 );
759   parser.Parse( s1 ); // Merge the tree into itself. The value array should not grow.
760
761   DALI_TEST_CHECK(parser.GetRoot());
762
763   CompareTrees( *parser.GetRoot(), *testParser.GetRoot() );
764
765   END_TEST;
766 }
767
768 int UtcDaliJsonParserDownCast(void)
769 {
770   BaseHandle handle = JsonParser::New();
771   JsonParser parser = JsonParser::DownCast( handle );
772   DALI_TEST_CHECK( parser );
773   END_TEST;
774 }
775
776 int UtcDaliJsonParserTreeNodeCount(void)
777 {
778   std::string s1( ReplaceQuotes("                                       \
779 {                                                                       \
780   'styles':                                                             \
781   {                                                                     \
782     'button':                                                           \
783     {                                                                   \
784       'backgroundColor':[0.8, 0.0, 1.0, 1.0],                           \
785       'foregroundColor':[1, 1, 1, 1]                                    \
786     }                                                                   \
787   }                                                                     \
788 }                                                                       \
789 "));
790
791   JsonParser parser = JsonParser::New();
792   parser.Parse( s1 );
793
794   const TreeNode* treeNode = parser.GetRoot();
795   DALI_TEST_EQUALS(1, treeNode->Count("styles"), TEST_LOCATION );
796   DALI_TEST_EQUALS(0, treeNode->Count("random"), TEST_LOCATION );
797
798   END_TEST;
799 }
800
801 int UtcDaliJsonParserTreeNodeFind(void)
802 {
803   std::string s1( ReplaceQuotes("\
804 {                                         \
805   'string':'value2',                      \
806   'integer':2,                            \
807   'float':2.3,                            \
808   'boolean':true,                         \
809   'nil':null,                             \
810   'array':[1,2,3],                        \
811   'object':{'key':'value'}                \
812 }                                         \
813 "));
814
815   JsonParser parser = JsonParser::New();
816   parser.Parse( s1 );
817
818   const TreeNode* treeNode = parser.GetRoot();
819   const TreeNode* childNode = treeNode->Find("string");
820   DALI_TEST_CHECK( childNode );
821   const TreeNode* sameNode = childNode->Find("string");
822   DALI_TEST_EQUALS( sameNode, childNode, TEST_LOCATION );
823
824   END_TEST;
825 }