e23616e41696792dbca6daffe716549503b47c72
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d-internal / utc-Dali-DliLoaderImpl.cpp
1 /*
2  * Copyright (c) 2023 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 // Enable debug log for test coverage
19 #define DEBUG_ENABLED 1
20
21 #include <dali-scene3d/internal/loader/dli-loader-impl.h>
22 #include <dali-scene3d/internal/loader/json-util.h>
23 #include <dali-scene3d/public-api/loader/load-result.h>
24 #include <dali-scene3d/public-api/loader/resource-bundle.h>
25 #include <dali-scene3d/public-api/loader/scene-definition.h>
26 #include <dali-test-suite-utils.h>
27 #include <string_view>
28
29 using namespace Dali;
30 using namespace Dali::Scene3D::Loader;
31
32 namespace
33 {
34 void ConfigureBlendShapeShaders(ResourceBundle& resources, const SceneDefinition& scene, Actor root, std::vector<BlendshapeShaderConfigurationRequest>&& requests)
35 {
36   std::vector<std::string> errors;
37   auto                     onError = [&errors](const std::string& msg)
38   {
39     errors.push_back(msg);
40   };
41
42   if(!scene.ConfigureBlendshapeShaders(resources, root, std::move(requests), onError))
43   {
44     ExceptionFlinger flinger(ASSERT_LOCATION);
45     for(auto& msg : errors)
46     {
47       flinger << msg << '\n';
48     }
49   }
50 }
51
52 struct Context
53 {
54   ResourceBundle::PathProvider pathProvider = [](ResourceType::Value type)
55   {
56     return TEST_RESOURCE_DIR "/";
57   };
58
59   ResourceBundle                        resources;
60   SceneDefinition                       scene;
61   std::vector<CameraParameters>         cameraParameters;
62   std::vector<LightParameters>          lights;
63   std::vector<AnimationDefinition>      animations;
64   std::vector<AnimationGroupDefinition> animGroups;
65
66   SceneMetadata metaData;
67
68   LoadResult output{
69     resources,
70     scene,
71     metaData,
72     animations,
73     animGroups,
74     cameraParameters,
75     lights};
76
77   Dali::Scene3D::Loader::DliInputParameter       input;
78   std::vector<std::string>                       errors;
79   Dali::Scene3D::Loader::Internal::DliLoaderImpl loader;
80
81   StringCallback onError = [this](const std::string& error)
82   {
83     errors.push_back(error);
84     printf("%s\n", error.c_str());
85   };
86
87   Context()
88   {
89     input.mAnimationsPath = pathProvider(ResourceType::Mesh);
90     loader.SetErrorCallback(onError);
91     loader.SetInputParameter(input);
92   }
93 };
94
95 bool StringHasTokens(const char* string, const std::vector<const char*>& tokens)
96 {
97   for(auto& token : tokens)
98   {
99     auto result = strstr(string, token);
100     if(nullptr == result)
101     {
102       return false;
103     }
104     string = result + strlen(token);
105   }
106   return true;
107 }
108
109 } // namespace
110
111 int UtcDaliDliLoaderLoadSceneNotFound(void)
112 {
113   Context ctx;
114
115   DALI_TEST_EQUAL(ctx.loader.LoadModel("does_not_exist.dli", ctx.output), false);
116
117   auto error = ctx.loader.GetParseError();
118   DALI_TEST_CHECK(StringHasTokens(error.c_str(), {"Empty source buffer to parse."}));
119
120   END_TEST;
121 }
122
123 int UtcDaliDliLoaderLoadSceneFailParse(void)
124 {
125   Context ctx;
126
127   auto path = ctx.pathProvider(ResourceType::Mesh) + "invalid.gltf";
128   DALI_TEST_EQUAL(ctx.loader.LoadModel(path, ctx.output), false);
129
130   auto error = ctx.loader.GetParseError();
131   DALI_TEST_CHECK(StringHasTokens(error.c_str(), {"Unexpected character."}));
132
133   END_TEST;
134 }
135
136 int UtcDaliDliLoaderLoadSceneAssertions(void)
137 {
138   const std::pair<std::string, std::string> pathExceptionPairs[]{
139     // from RequireChild()
140     {"scenes-nodes-missing", "Failed to find child node"},
141     {"scenes-missing", "Failed to find child node"},
142     {"nodes-missing", "Failed to find child node"},
143     // from ParseSceneInternal()
144     {"scene-out-of-bounds", "out of bounds"},
145     {"nodes-invalid-type", "invalid type; array required"},
146     {"nodes-array-empty", "must define a node id"},
147     {"root-id-invalid", "invalid value for root node index"},
148     {"root-id-out-of-bounds", "out of bounds"},
149     {"root-node-invalid-type", "invalid JSON type; object required"},
150     // from ParseSkeletons()
151     {"skeleton-node-missing", "Missing required attribute"},
152     {"skeleton-root-not-found", "not defined"},
153     // from ParseShaders()
154     {"shader-vertex-missing", "Missing vertex / fragment shader"},
155     {"shader-fragment-missing", "Missing vertex / fragment shader"},
156     // from ParseMeshes()
157     {"mesh-uri-missing", "Missing required attribute"},
158     {"mesh-indices-read-fail", "Failed to read indices"},
159     {"mesh-positions-read-fail", "Failed to read positions"},
160     // from ParseMaterials()
161     {"material-environment-out-of-bounds", "out of bounds"},
162     // from ParseNodes()
163     {"node-model-mesh-missing", "Missing mesh"},
164     {"node-arc-mesh-missing", "Missing mesh"},
165     {"node-animated-image-mesh-missing", "Missing mesh"},
166     {"node-renderable-mesh-invalid-type", "Invalid Mesh index type"},
167     {"node-renderable-mesh-out-of-bounds", "out of bounds"},
168     {"node-child-invalid-type", "invalid index type"},
169     // from ParseAnimations()
170     {"animation-failed-to-open", "Failed to open animation data"}};
171   for(auto& i : pathExceptionPairs)
172   {
173     Context ctx;
174
175     auto path = ctx.pathProvider(ResourceType::Mesh) + "dli/" + i.first + ".dli";
176     printf("\n\n%s: %s\n", path.c_str(), i.second.c_str());
177     DALI_TEST_ASSERTION(ctx.loader.LoadModel(path, ctx.output), i.second.c_str());
178   }
179
180   END_TEST;
181 }
182
183 int UtcDaliDliLoaderLoadSceneExercise(void)
184 {
185   TestApplication app;
186   Context ctx;
187
188   auto path = ctx.pathProvider(ResourceType::Mesh) + "exercise.dli";
189   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
190   DALI_TEST_CHECK(ctx.errors.empty());
191
192   auto& scene = ctx.scene;
193   auto& roots = scene.GetRoots();
194   DALI_TEST_EQUAL(roots.size(), 2u);
195   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "Backdrop"); // default scene is scene 1 - this one.
196   DALI_TEST_EQUAL(scene.GetNode(roots[1])->mName, "ExerciseDemo");
197
198   DALI_TEST_EQUAL(scene.GetNodeCount(), 96u);
199
200   auto& resources = ctx.resources;
201   DALI_TEST_EQUAL(resources.mMeshes.size(), 11u);
202   DALI_TEST_EQUAL(resources.mMaterials.size(), 13u);
203   DALI_TEST_EQUAL(resources.mShaders.size(), 5u);
204   DALI_TEST_EQUAL(resources.mEnvironmentMaps.size(), 2u);
205   DALI_TEST_EQUAL(resources.mSkeletons.size(), 1u);
206
207   DALI_TEST_EQUAL(ctx.cameraParameters.size(), 1u);
208   DALI_TEST_EQUAL(ctx.lights.size(), 1u);
209   DALI_TEST_EQUAL(ctx.animations.size(), 18u);
210   DALI_TEST_EQUAL(ctx.animGroups.size(), 16u);
211
212   ViewProjection viewProjection;
213   Transforms     xforms{
214     MatrixStack{},
215     viewProjection};
216   NodeDefinition::CreateParams nodeParams{
217     resources,
218     xforms,
219   };
220
221   Customization::Choices choices;
222
223   Actor root = Actor::New();
224   SetActorCentered(root);
225   for(auto iRoot : scene.GetRoots())
226   {
227     auto resourceRefs = resources.CreateRefCounter();
228     scene.CountResourceRefs(iRoot, choices, resourceRefs);
229     resources.mReferenceCounts = std::move(resourceRefs);
230     resources.CountEnvironmentReferences();
231     resources.LoadResources(ctx.pathProvider);
232     if(auto actor = scene.CreateNodes(iRoot, choices, nodeParams))
233     {
234       scene.ConfigureSkeletonJoints(iRoot, resources.mSkeletons, actor);
235       scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
236       ConfigureBlendShapeShaders(resources, scene, actor, std::move(nodeParams.mBlendshapeRequests));
237       scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
238       root.Add(actor);
239     }
240   }
241
242   DALI_TEST_EQUAL(root.GetChildCount(), 2u);
243   DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::NAME).Get<std::string>(), "Backdrop");
244   DALI_TEST_EQUAL(root.GetChildAt(1).GetProperty(Actor::Property::NAME).Get<std::string>(), "ExerciseDemo");
245
246   END_TEST;
247 }
248
249 int UtcDaliDliLoaderLoadSceneMorph(void)
250 {
251   Context ctx;
252
253   std::vector<std::string> metadata;
254   uint32_t                 metadataCount = 0;
255   ctx.input.mPreNodeCategoryProcessors.push_back({"metadata",
256                                                   [&](const Property::Array& array, StringCallback)
257                                                   {
258                                                     std::string key, value;
259                                                     for(uint32_t i0 = 0, i1 = array.Count(); i0 < i1; ++i0)
260                                                     {
261                                                       auto& data = array.GetElementAt(i0);
262                                                       DALI_TEST_EQUAL(data.GetType(), Property::MAP);
263
264                                                       auto map   = data.GetMap();
265                                                       auto key   = map->Find("key");
266                                                       auto value = map->Find("value");
267                                                       DALI_TEST_EQUAL(key->GetType(), Property::STRING);
268                                                       DALI_TEST_EQUAL(value->GetType(), Property::STRING);
269                                                       metadata.push_back(key->Get<std::string>() + ":" + value->Get<std::string>());
270
271                                                       ++metadataCount;
272                                                     }
273                                                   }});
274
275   std::vector<std::string> behaviors;
276   uint32_t                 behaviorCount = 0;
277   ctx.input.mPostNodeCategoryProcessors.push_back({"behaviors",
278                                                    [&](const Property::Array& array, StringCallback)
279                                                    {
280                                                      for(uint32_t i0 = 0, i1 = array.Count(); i0 < i1; ++i0)
281                                                      {
282                                                        auto& data = array.GetElementAt(i0);
283                                                        DALI_TEST_EQUAL(data.GetType(), Property::MAP);
284
285                                                        auto map   = data.GetMap();
286                                                        auto event = map->Find("event");
287                                                        auto url   = map->Find("url");
288                                                        DALI_TEST_EQUAL(event->GetType(), Property::STRING);
289                                                        DALI_TEST_EQUAL(url->GetType(), Property::STRING);
290                                                        behaviors.push_back(event->Get<std::string>() + ":" + url->Get<std::string>());
291
292                                                        ++behaviorCount;
293                                                      }
294                                                    }});
295
296   size_t numNodes                  = 0;
297   ctx.input.mNodePropertyProcessor = [&](const NodeDefinition&, const Property::Map&, StringCallback)
298   {
299     ++numNodes;
300   };
301
302   auto path = ctx.pathProvider(ResourceType::Mesh) + "morph.dli";
303   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
304   DALI_TEST_CHECK(ctx.errors.empty());
305
306   auto& scene = ctx.scene;
307   auto& roots = scene.GetRoots();
308   DALI_TEST_EQUAL(roots.size(), 1u);
309   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "HeadTest_002");
310
311   DALI_TEST_EQUAL(numNodes, 3u);
312   DALI_TEST_EQUAL(scene.GetNodeCount(), numNodes);
313
314   auto& resources = ctx.resources;
315   DALI_TEST_EQUAL(resources.mMeshes.size(), 2u);
316   DALI_TEST_EQUAL(resources.mMaterials.size(), 1u);
317   DALI_TEST_EQUAL(resources.mShaders.size(), 5u);
318   DALI_TEST_EQUAL(resources.mEnvironmentMaps.size(), 2u);
319   DALI_TEST_EQUAL(resources.mSkeletons.size(), 0u);
320
321   DALI_TEST_EQUAL(ctx.cameraParameters.size(), 1u);
322   DALI_TEST_EQUAL(ctx.lights.size(), 1u);
323   DALI_TEST_EQUAL(ctx.animations.size(), 1u);
324   DALI_TEST_EQUAL(ctx.animGroups.size(), 0u);
325
326   DALI_TEST_EQUAL(metadata.size(), 4u);
327   DALI_TEST_EQUAL(behaviors.size(), 1u);
328
329   ViewProjection viewProjection;
330   Transforms     xforms{
331     MatrixStack{},
332     viewProjection};
333   NodeDefinition::CreateParams nodeParams{
334     resources,
335     xforms,
336   };
337
338   Customization::Choices choices;
339
340   TestApplication app;
341
342   Actor root = Actor::New();
343   SetActorCentered(root);
344   for(auto iRoot : scene.GetRoots())
345   {
346     auto resourceRefs = resources.CreateRefCounter();
347     scene.CountResourceRefs(iRoot, choices, resourceRefs);
348     resources.mReferenceCounts = std::move(resourceRefs);
349     resources.CountEnvironmentReferences();
350     resources.LoadResources(ctx.pathProvider);
351     if(auto actor = scene.CreateNodes(iRoot, choices, nodeParams))
352     {
353       scene.ConfigureSkeletonJoints(iRoot, resources.mSkeletons, actor);
354       scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
355       ConfigureBlendShapeShaders(resources, scene, actor, std::move(nodeParams.mBlendshapeRequests));
356       scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
357       root.Add(actor);
358     }
359   }
360
361   DALI_TEST_EQUAL(root.GetChildCount(), 1u);
362   DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::NAME).Get<std::string>(), "HeadTest_002");
363
364   END_TEST;
365 }
366
367 int UtcDaliDliLoaderLoadSceneArc(void)
368 {
369   Context ctx;
370
371   auto path = ctx.pathProvider(ResourceType::Mesh) + "arc.dli";
372   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
373   DALI_TEST_CHECK(ctx.errors.empty());
374
375   auto& scene = ctx.scene;
376   auto& roots = scene.GetRoots();
377   DALI_TEST_EQUAL(roots.size(), 1u);
378   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "root");
379
380   DALI_TEST_EQUAL(scene.GetNodeCount(), 2u);
381
382   auto& resources = ctx.resources;
383   DALI_TEST_EQUAL(resources.mMeshes.size(), 1u);
384   DALI_TEST_EQUAL(resources.mMaterials.size(), 1u);
385   DALI_TEST_EQUAL(resources.mShaders.size(), 1u);
386   DALI_TEST_EQUAL(resources.mEnvironmentMaps.size(), 1u);
387   DALI_TEST_EQUAL(resources.mSkeletons.size(), 0u);
388
389   DALI_TEST_EQUAL(ctx.cameraParameters.size(), 0u);
390   DALI_TEST_EQUAL(ctx.lights.size(), 0u);
391   DALI_TEST_EQUAL(ctx.animations.size(), 0u);
392   DALI_TEST_EQUAL(ctx.animGroups.size(), 0u);
393
394   ViewProjection viewProjection;
395   Transforms     xforms{
396     MatrixStack{},
397     viewProjection};
398   NodeDefinition::CreateParams nodeParams{
399     resources,
400     xforms,
401   };
402
403   Customization::Choices choices;
404
405   TestApplication app;
406
407   Actor root = Actor::New();
408   SetActorCentered(root);
409   for(auto iRoot : scene.GetRoots())
410   {
411     auto resourceRefs = resources.CreateRefCounter();
412     scene.CountResourceRefs(iRoot, choices, resourceRefs);
413     resources.mReferenceCounts = std::move(resourceRefs);
414     resources.CountEnvironmentReferences();
415     resources.LoadResources(ctx.pathProvider);
416     if(auto actor = scene.CreateNodes(iRoot, choices, nodeParams))
417     {
418       scene.ConfigureSkeletonJoints(iRoot, resources.mSkeletons, actor);
419       scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
420       ConfigureBlendShapeShaders(resources, scene, actor, std::move(nodeParams.mBlendshapeRequests));
421       scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
422       root.Add(actor);
423     }
424   }
425
426   DALI_TEST_EQUAL(root.GetChildCount(), 1u);
427   DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::NAME).Get<std::string>(), "root");
428
429   END_TEST;
430 }
431
432 int UtcDaliDliLoaderLoadSceneShaderUniforms(void)
433 {
434   Context ctx;
435
436   auto path = ctx.pathProvider(ResourceType::Mesh) + "dli/shader-uniforms.dli";
437   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
438   DALI_TEST_EQUAL(ctx.errors.size(), 1u);
439   DALI_TEST_CHECK(ctx.errors[0].find("failed to infer type") != std::string::npos);
440
441   auto& scene = ctx.scene;
442   auto& roots = scene.GetRoots();
443   DALI_TEST_EQUAL(roots.size(), 1u);
444   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "root");
445
446   DALI_TEST_EQUAL(scene.GetNodeCount(), 1u);
447
448   auto& resources = ctx.resources;
449   DALI_TEST_EQUAL(resources.mMeshes.size(), 0u);
450   DALI_TEST_EQUAL(resources.mMaterials.size(), 0u);
451   DALI_TEST_EQUAL(resources.mShaders.size(), 1u);
452   DALI_TEST_EQUAL(resources.mEnvironmentMaps.size(), 0u);
453   DALI_TEST_EQUAL(resources.mSkeletons.size(), 0u);
454
455   auto raw = resources.mShaders[0].first.LoadRaw(ctx.pathProvider(ResourceType::Shader));
456
457   TestApplication app;
458
459   auto shader = resources.mShaders[0].first.Load(std::move(raw));
460   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uBool")).Get<float>(), 1.0f);
461   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uInt")).Get<float>(), 255.0f);
462   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uFloat")).Get<float>(), -0.5f);
463   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uVec2")).Get<Vector2>(), Vector2(100.0f, -100.0f));
464   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uVec3")).Get<Vector3>(), Vector3(50.0f, 0.f, -200.0f));
465   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uVec4")).Get<Vector4>(), Vector4(0.1774f, 1.0f, 0.5333f, 0.7997f));
466   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uMat3")).Get<Matrix3>(), Matrix3(9.0f, 8.0f, 7.0f, 6.0f, 5.0f, 4.0f, 3.0f, 2.0f, 1.0f));
467
468   Matrix expectedMatrix;
469   expectedMatrix.SetTransformComponents(Vector3::ONE * 8.0, Quaternion::IDENTITY, Vector3::ZERO);
470   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uMat4")).Get<Matrix>(), expectedMatrix);
471
472   END_TEST;
473 }
474
475 int UtcDaliDliLoaderLoadSceneExtras(void)
476 {
477   Context ctx;
478
479   auto path = ctx.pathProvider(ResourceType::Mesh) + "dli/extras.dli";
480   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
481   DALI_TEST_EQUAL(ctx.errors.size(), 3u);
482   DALI_TEST_CHECK(ctx.errors[0].find("already defined; overriding") != std::string::npos);
483   DALI_TEST_CHECK(ctx.errors[1].find("empty string is invalid for name") != std::string::npos);
484   DALI_TEST_CHECK(ctx.errors[2].find("failed to interpret value") != std::string::npos);
485
486   auto& scene = ctx.scene;
487   auto& roots = scene.GetRoots();
488   DALI_TEST_EQUAL(roots.size(), 1u);
489   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "root");
490
491   DALI_TEST_EQUAL(scene.GetNodeCount(), 1u);
492
493   ViewProjection viewProjection;
494   Transforms     xforms{
495     MatrixStack{},
496     viewProjection};
497   auto&                        resources = ctx.resources;
498   NodeDefinition::CreateParams nodeParams{
499     resources,
500     xforms,
501   };
502
503   Customization::Choices choices;
504
505   TestApplication app;
506   Actor           actor = scene.CreateNodes(0, choices, nodeParams);
507
508   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("fudgeFactor")).Get<float>(), 9000.1f);
509   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("fudgeVector")).Get<Vector2>(), Vector2(-.25f, 17.f));
510   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("isThisTheRealLife")).Get<bool>(), true);
511   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("isThisJustFantasy")).Get<bool>(), false);
512   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("velocity")).Get<Vector3>(), Vector3(.1f, 58.f, -.2f));
513   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("frameOfReference")).Get<Matrix>(), Matrix::IDENTITY);
514
515   END_TEST;
516 }
517
518 int UtcDaliDliLoaderLoadSceneConstraints(void)
519 {
520   Context ctx;
521
522   auto path = ctx.pathProvider(ResourceType::Mesh) + "dli/constraints.dli";
523   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
524   DALI_TEST_EQUAL(ctx.errors.size(), 1u);
525   DALI_TEST_CHECK(ctx.errors[0].find("invalid", ctx.errors[0].find("node ID")) != std::string::npos);
526
527   auto& scene = ctx.scene;
528   auto& roots = scene.GetRoots();
529   DALI_TEST_EQUAL(roots.size(), 1u);
530   DALI_TEST_EQUAL(scene.GetNode(0)->mName, "root");
531   DALI_TEST_EQUAL(scene.GetNode(1)->mName, "Alice");
532   DALI_TEST_EQUAL(scene.GetNode(2)->mName, "Bob");
533   DALI_TEST_EQUAL(scene.GetNode(3)->mName, "Charlie");
534
535   DALI_TEST_EQUAL(scene.GetNodeCount(), 4u);
536
537   ViewProjection viewProjection;
538   Transforms     xforms{
539     MatrixStack{},
540     viewProjection};
541   auto&                        resources = ctx.resources;
542   NodeDefinition::CreateParams nodeParams{
543     resources,
544     xforms,
545   };
546
547   Customization::Choices choices;
548
549   TestApplication app;
550
551   Actor root    = scene.CreateNodes(0, choices, nodeParams);
552   Actor alice   = root.FindChildByName("Alice");
553   Actor bob     = root.FindChildByName("Bob");
554   Actor charlie = root.FindChildByName("Charlie");
555
556   DALI_TEST_EQUAL(nodeParams.mConstrainables.size(), 3u);
557   DALI_TEST_EQUAL(bob.GetProperty(bob.GetPropertyIndex("angularVelocity")).Get<Vector2>(), Vector2(-0.5, 0.0004));
558
559   ctx.errors.clear();
560   scene.ApplyConstraints(root, std::move(nodeParams.mConstrainables), ctx.onError);
561   DALI_TEST_CHECK(ctx.errors.empty());
562
563   app.GetScene().Add(root);
564   app.SendNotification();
565   app.Render();
566   app.SendNotification();
567   app.Render();
568
569   DALI_TEST_EQUAL(charlie.GetCurrentProperty(Actor::Property::ORIENTATION), alice.GetProperty(Actor::Property::ORIENTATION));
570   DALI_TEST_EQUAL(charlie.GetCurrentProperty(Actor::Property::POSITION), bob.GetProperty(Actor::Property::POSITION));
571   DALI_TEST_EQUAL(charlie.GetCurrentProperty(charlie.GetPropertyIndex("angularVelocity")), bob.GetProperty(bob.GetPropertyIndex("angularVelocity")));
572
573   END_TEST;
574 }
575
576 int UtcDaliDliLoaderNodeProcessor(void)
577 {
578   Context ctx;
579
580   std::vector<Property::Map> nodeMaps;
581   ctx.input.mNodePropertyProcessor = [&](const NodeDefinition&, Property::Map&& map, StringCallback)
582   {
583     nodeMaps.push_back(map);
584   };
585
586   auto path = ctx.pathProvider(ResourceType::Mesh) + "dli/node-processor.dli";
587   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
588
589   DALI_TEST_EQUAL(nodeMaps.size(), 2u);
590   DALI_TEST_EQUAL(nodeMaps[0].Count(), 5u);
591   DALI_TEST_EQUAL(nodeMaps[0].Find("name")->Get<std::string>(), "rootA");
592   DALI_TEST_EQUAL(nodeMaps[0].Find("nickname")->Get<std::string>(), "same as name");
593   DALI_TEST_EQUAL(nodeMaps[0].Find("favourite number")->Get<int32_t>(), 63478);
594
595   auto propArray = nodeMaps[0].Find("array");
596   DALI_TEST_EQUAL(propArray->GetType(), Property::ARRAY);
597
598   auto array = propArray->GetArray();
599   DALI_TEST_EQUAL(array->Count(), 5);
600   DALI_TEST_EQUAL(array->GetElementAt(0).Get<int32_t>(), 1);
601   DALI_TEST_EQUAL(array->GetElementAt(1).Get<int32_t>(), 2);
602   DALI_TEST_EQUAL(array->GetElementAt(2).Get<int32_t>(), 4);
603   DALI_TEST_EQUAL(array->GetElementAt(3).Get<int32_t>(), 8);
604   DALI_TEST_EQUAL(array->GetElementAt(4).Get<int32_t>(), -500);
605
606   auto propObject = nodeMaps[0].Find("object");
607   DALI_TEST_EQUAL(propObject->GetType(), Property::MAP);
608
609   auto object = propObject->GetMap();
610   DALI_TEST_EQUAL(object->Count(), 5);
611   DALI_TEST_EQUAL(object->Find("physics")->Get<bool>(), true);
612   DALI_TEST_EQUAL(object->Find("elasticity")->Get<float>(), .27f);
613   DALI_TEST_EQUAL(object->Find("drag")->Get<float>(), .91f);
614
615   auto propInnerArray = object->Find("inner array");
616   DALI_TEST_EQUAL(propInnerArray->GetType(), Property::ARRAY);
617
618   auto innerArray = propInnerArray->GetArray();
619   DALI_TEST_EQUAL(innerArray->Count(), 3);
620   DALI_TEST_EQUAL(innerArray->GetElementAt(0).Get<std::string>(), "why");
621   DALI_TEST_EQUAL(innerArray->GetElementAt(1).Get<std::string>(), "not");
622   DALI_TEST_EQUAL(innerArray->GetElementAt(2).Get<bool>(), false);
623
624   auto propInnerObject = object->Find("inner object");
625   DALI_TEST_EQUAL(propInnerObject->GetType(), Property::MAP);
626
627   auto innerObject = propInnerObject->GetMap();
628   DALI_TEST_EQUAL(innerObject->Count(), 1);
629   DALI_TEST_EQUAL(innerObject->Find("supported")->Get<bool>(), true);
630
631   DALI_TEST_EQUAL(nodeMaps[1].Count(), 1u);
632   DALI_TEST_EQUAL(nodeMaps[1].Find("name")->Get<std::string>(), "rootB");
633
634   END_TEST;
635 }
636
637 int UtcDaliDliLoaderLoadCoverageTest(void)
638 {
639   Context ctx;
640
641   auto path = ctx.pathProvider(ResourceType::Mesh) + "coverageTest.dli";
642   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
643   DALI_TEST_CHECK(ctx.errors.empty());
644
645   auto& scene = ctx.scene;
646   auto& roots = scene.GetRoots();
647   DALI_TEST_EQUAL(roots.size(), 1u);
648   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "root");
649
650   DALI_TEST_EQUAL(scene.GetNodeCount(), 1u);
651
652   auto& resources = ctx.resources;
653   DALI_TEST_EQUAL(resources.mMeshes.size(), 1u);
654   DALI_TEST_EQUAL(resources.mShaders.size(), 1u);
655   DALI_TEST_EQUAL(resources.mEnvironmentMaps.size(), 2u);
656   DALI_TEST_EQUAL(resources.mSkeletons.size(), 0u);
657
658   auto& materials = ctx.resources.mMaterials;
659   DALI_TEST_EQUAL(2u, materials.size());
660
661   auto  iMaterial = materials.begin();
662   auto& md        = iMaterial->first;
663   DALI_TEST_EQUAL(md.mTextureStages.size(), 1u);
664
665   auto iTexture = md.mTextureStages.begin();
666   DALI_TEST_CHECK(MaskMatch(iTexture->mSemantic, MaterialDefinition::OCCLUSION));
667   DALI_TEST_EQUAL(iTexture->mTexture.mImageUri, "exercise/Icons/Icon_Idle.png");
668   ++iTexture;
669
670   DALI_TEST_EQUAL(ctx.cameraParameters.size(), 1u);
671   DALI_TEST_EQUAL(ctx.lights.size(), 1u);
672   DALI_TEST_EQUAL(ctx.animations.size(), 0u);
673   DALI_TEST_EQUAL(ctx.animGroups.size(), 0u);
674
675   ViewProjection viewProjection;
676   Transforms     xforms{
677     MatrixStack{},
678     viewProjection};
679   NodeDefinition::CreateParams nodeParams{
680     resources,
681     xforms,
682   };
683
684   Customization::Choices choices;
685
686   TestApplication app;
687
688   Actor root = Actor::New();
689   SetActorCentered(root);
690   for(auto iRoot : scene.GetRoots())
691   {
692     auto resourceRefs = resources.CreateRefCounter();
693     scene.CountResourceRefs(iRoot, choices, resourceRefs);
694     resources.mReferenceCounts = std::move(resourceRefs);
695     resources.CountEnvironmentReferences();
696     resources.LoadResources(ctx.pathProvider);
697     if(auto actor = scene.CreateNodes(iRoot, choices, nodeParams))
698     {
699       scene.ConfigureSkeletonJoints(iRoot, resources.mSkeletons, actor);
700       scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
701       ConfigureBlendShapeShaders(resources, scene, actor, std::move(nodeParams.mBlendshapeRequests));
702       scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
703       root.Add(actor);
704     }
705   }
706
707   DALI_TEST_EQUAL(root.GetChildCount(), 1u);
708   DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::NAME).Get<std::string>(), "root");
709
710   END_TEST;
711 }