[dali_2.3.21] Merge branch 'devel/master'
[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.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
235       ConfigureBlendShapeShaders(resources, scene, actor, std::move(nodeParams.mBlendshapeRequests));
236       scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
237       root.Add(actor);
238     }
239   }
240
241   DALI_TEST_EQUAL(root.GetChildCount(), 2u);
242   DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::NAME).Get<std::string>(), "Backdrop");
243   DALI_TEST_EQUAL(root.GetChildAt(1).GetProperty(Actor::Property::NAME).Get<std::string>(), "ExerciseDemo");
244
245   END_TEST;
246 }
247
248 int UtcDaliDliLoaderLoadSceneMorph(void)
249 {
250   Context ctx;
251
252   std::vector<std::string> metadata;
253   uint32_t                 metadataCount = 0;
254   ctx.input.mPreNodeCategoryProcessors.push_back({"metadata",
255                                                   [&](const Property::Array& array, StringCallback)
256                                                   {
257                                                     std::string key, value;
258                                                     for(uint32_t i0 = 0, i1 = array.Count(); i0 < i1; ++i0)
259                                                     {
260                                                       auto& data = array.GetElementAt(i0);
261                                                       DALI_TEST_EQUAL(data.GetType(), Property::MAP);
262
263                                                       auto map   = data.GetMap();
264                                                       auto key   = map->Find("key");
265                                                       auto value = map->Find("value");
266                                                       DALI_TEST_EQUAL(key->GetType(), Property::STRING);
267                                                       DALI_TEST_EQUAL(value->GetType(), Property::STRING);
268                                                       metadata.push_back(key->Get<std::string>() + ":" + value->Get<std::string>());
269
270                                                       ++metadataCount;
271                                                     }
272                                                   }});
273
274   std::vector<std::string> behaviors;
275   uint32_t                 behaviorCount = 0;
276   ctx.input.mPostNodeCategoryProcessors.push_back({"behaviors",
277                                                    [&](const Property::Array& array, StringCallback)
278                                                    {
279                                                      for(uint32_t i0 = 0, i1 = array.Count(); i0 < i1; ++i0)
280                                                      {
281                                                        auto& data = array.GetElementAt(i0);
282                                                        DALI_TEST_EQUAL(data.GetType(), Property::MAP);
283
284                                                        auto map   = data.GetMap();
285                                                        auto event = map->Find("event");
286                                                        auto url   = map->Find("url");
287                                                        DALI_TEST_EQUAL(event->GetType(), Property::STRING);
288                                                        DALI_TEST_EQUAL(url->GetType(), Property::STRING);
289                                                        behaviors.push_back(event->Get<std::string>() + ":" + url->Get<std::string>());
290
291                                                        ++behaviorCount;
292                                                      }
293                                                    }});
294
295   size_t numNodes                  = 0;
296   ctx.input.mNodePropertyProcessor = [&](const NodeDefinition&, const Property::Map&, StringCallback)
297   {
298     ++numNodes;
299   };
300
301   auto path = ctx.pathProvider(ResourceType::Mesh) + "morph.dli";
302   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
303   DALI_TEST_CHECK(ctx.errors.empty());
304
305   auto& scene = ctx.scene;
306   auto& roots = scene.GetRoots();
307   DALI_TEST_EQUAL(roots.size(), 1u);
308   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "HeadTest_002");
309
310   DALI_TEST_EQUAL(numNodes, 3u);
311   DALI_TEST_EQUAL(scene.GetNodeCount(), numNodes);
312
313   auto& resources = ctx.resources;
314   DALI_TEST_EQUAL(resources.mMeshes.size(), 2u);
315   DALI_TEST_EQUAL(resources.mMaterials.size(), 1u);
316   DALI_TEST_EQUAL(resources.mShaders.size(), 5u);
317   DALI_TEST_EQUAL(resources.mEnvironmentMaps.size(), 2u);
318   DALI_TEST_EQUAL(resources.mSkeletons.size(), 0u);
319
320   DALI_TEST_EQUAL(ctx.cameraParameters.size(), 1u);
321   DALI_TEST_EQUAL(ctx.lights.size(), 1u);
322   DALI_TEST_EQUAL(ctx.animations.size(), 1u);
323   DALI_TEST_EQUAL(ctx.animations[0].GetDuration(), 1.0f);
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.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
354       ConfigureBlendShapeShaders(resources, scene, actor, std::move(nodeParams.mBlendshapeRequests));
355       scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
356       root.Add(actor);
357     }
358   }
359
360   DALI_TEST_EQUAL(root.GetChildCount(), 1u);
361   DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::NAME).Get<std::string>(), "HeadTest_002");
362
363   END_TEST;
364 }
365
366 int UtcDaliDliLoaderLoadSceneArc(void)
367 {
368   Context ctx;
369
370   auto path = ctx.pathProvider(ResourceType::Mesh) + "arc.dli";
371   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
372   DALI_TEST_CHECK(ctx.errors.empty());
373
374   auto& scene = ctx.scene;
375   auto& roots = scene.GetRoots();
376   DALI_TEST_EQUAL(roots.size(), 1u);
377   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "root");
378
379   DALI_TEST_EQUAL(scene.GetNodeCount(), 2u);
380
381   auto& resources = ctx.resources;
382   DALI_TEST_EQUAL(resources.mMeshes.size(), 1u);
383   DALI_TEST_EQUAL(resources.mMaterials.size(), 1u);
384   DALI_TEST_EQUAL(resources.mShaders.size(), 1u);
385   DALI_TEST_EQUAL(resources.mEnvironmentMaps.size(), 1u);
386   DALI_TEST_EQUAL(resources.mSkeletons.size(), 0u);
387
388   DALI_TEST_EQUAL(ctx.cameraParameters.size(), 0u);
389   DALI_TEST_EQUAL(ctx.lights.size(), 0u);
390   DALI_TEST_EQUAL(ctx.animations.size(), 0u);
391   DALI_TEST_EQUAL(ctx.animGroups.size(), 0u);
392
393   ViewProjection viewProjection;
394   Transforms     xforms{
395     MatrixStack{},
396     viewProjection};
397   NodeDefinition::CreateParams nodeParams{
398     resources,
399     xforms,
400   };
401
402   Customization::Choices choices;
403
404   TestApplication app;
405
406   Actor root = Actor::New();
407   SetActorCentered(root);
408   for(auto iRoot : scene.GetRoots())
409   {
410     auto resourceRefs = resources.CreateRefCounter();
411     scene.CountResourceRefs(iRoot, choices, resourceRefs);
412     resources.mReferenceCounts = std::move(resourceRefs);
413     resources.CountEnvironmentReferences();
414     resources.LoadResources(ctx.pathProvider);
415     if(auto actor = scene.CreateNodes(iRoot, choices, nodeParams))
416     {
417       scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
418       ConfigureBlendShapeShaders(resources, scene, actor, std::move(nodeParams.mBlendshapeRequests));
419       scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
420       root.Add(actor);
421     }
422   }
423
424   DALI_TEST_EQUAL(root.GetChildCount(), 1u);
425   DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::NAME).Get<std::string>(), "root");
426
427   END_TEST;
428 }
429
430 int UtcDaliDliLoaderLoadSceneShaderUniforms(void)
431 {
432   Context ctx;
433
434   auto path = ctx.pathProvider(ResourceType::Mesh) + "dli/shader-uniforms.dli";
435   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
436   DALI_TEST_EQUAL(ctx.errors.size(), 1u);
437   DALI_TEST_CHECK(ctx.errors[0].find("failed to infer type") != std::string::npos);
438
439   auto& scene = ctx.scene;
440   auto& roots = scene.GetRoots();
441   DALI_TEST_EQUAL(roots.size(), 1u);
442   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "root");
443
444   DALI_TEST_EQUAL(scene.GetNodeCount(), 1u);
445
446   auto& resources = ctx.resources;
447   DALI_TEST_EQUAL(resources.mMeshes.size(), 0u);
448   DALI_TEST_EQUAL(resources.mMaterials.size(), 0u);
449   DALI_TEST_EQUAL(resources.mShaders.size(), 1u);
450   DALI_TEST_EQUAL(resources.mEnvironmentMaps.size(), 0u);
451   DALI_TEST_EQUAL(resources.mSkeletons.size(), 0u);
452
453   auto raw = resources.mShaders[0].first.LoadRaw(ctx.pathProvider(ResourceType::Shader));
454
455   TestApplication app;
456
457   auto shader = resources.mShaders[0].first.Load(std::move(raw));
458   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uBool")).Get<float>(), 1.0f);
459   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uInt")).Get<float>(), 255.0f);
460   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uFloat")).Get<float>(), -0.5f);
461   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uVec2")).Get<Vector2>(), Vector2(100.0f, -100.0f));
462   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uVec3")).Get<Vector3>(), Vector3(50.0f, 0.f, -200.0f));
463   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uVec4")).Get<Vector4>(), Vector4(0.1774f, 1.0f, 0.5333f, 0.7997f));
464   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));
465
466   Matrix expectedMatrix;
467   expectedMatrix.SetTransformComponents(Vector3::ONE * 8.0, Quaternion::IDENTITY, Vector3::ZERO);
468   DALI_TEST_EQUAL(shader.GetProperty(shader.GetPropertyIndex("uMat4")).Get<Matrix>(), expectedMatrix);
469
470   END_TEST;
471 }
472
473 int UtcDaliDliLoaderLoadSceneExtras(void)
474 {
475   Context ctx;
476
477   auto path = ctx.pathProvider(ResourceType::Mesh) + "dli/extras.dli";
478   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
479   DALI_TEST_EQUAL(ctx.errors.size(), 3u);
480   DALI_TEST_CHECK(ctx.errors[0].find("already defined; overriding") != std::string::npos);
481   DALI_TEST_CHECK(ctx.errors[1].find("empty string is invalid for name") != std::string::npos);
482   DALI_TEST_CHECK(ctx.errors[2].find("failed to interpret value") != std::string::npos);
483
484   auto& scene = ctx.scene;
485   auto& roots = scene.GetRoots();
486   DALI_TEST_EQUAL(roots.size(), 1u);
487   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "root");
488
489   DALI_TEST_EQUAL(scene.GetNodeCount(), 1u);
490
491   ViewProjection viewProjection;
492   Transforms     xforms{
493     MatrixStack{},
494     viewProjection};
495   auto&                        resources = ctx.resources;
496   NodeDefinition::CreateParams nodeParams{
497     resources,
498     xforms,
499   };
500
501   Customization::Choices choices;
502
503   TestApplication app;
504   Actor           actor = scene.CreateNodes(0, choices, nodeParams);
505
506   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("fudgeFactor")).Get<float>(), 9000.1f);
507   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("fudgeVector")).Get<Vector2>(), Vector2(-.25f, 17.f));
508   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("isThisTheRealLife")).Get<bool>(), true);
509   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("isThisJustFantasy")).Get<bool>(), false);
510   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("velocity")).Get<Vector3>(), Vector3(.1f, 58.f, -.2f));
511   DALI_TEST_EQUAL(actor.GetProperty(actor.GetPropertyIndex("frameOfReference")).Get<Matrix>(), Matrix::IDENTITY);
512
513   END_TEST;
514 }
515
516 int UtcDaliDliLoaderLoadSceneConstraints(void)
517 {
518   Context ctx;
519
520   auto path = ctx.pathProvider(ResourceType::Mesh) + "dli/constraints.dli";
521   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
522   DALI_TEST_EQUAL(ctx.errors.size(), 1u);
523   DALI_TEST_CHECK(ctx.errors[0].find("invalid", ctx.errors[0].find("node ID")) != std::string::npos);
524
525   auto& scene = ctx.scene;
526   auto& roots = scene.GetRoots();
527   DALI_TEST_EQUAL(roots.size(), 1u);
528   DALI_TEST_EQUAL(scene.GetNode(0)->mName, "root");
529   DALI_TEST_EQUAL(scene.GetNode(1)->mName, "Alice");
530   DALI_TEST_EQUAL(scene.GetNode(2)->mName, "Bob");
531   DALI_TEST_EQUAL(scene.GetNode(3)->mName, "Charlie");
532
533   DALI_TEST_EQUAL(scene.GetNodeCount(), 4u);
534
535   ViewProjection viewProjection;
536   Transforms     xforms{
537     MatrixStack{},
538     viewProjection};
539   auto&                        resources = ctx.resources;
540   NodeDefinition::CreateParams nodeParams{
541     resources,
542     xforms,
543   };
544
545   Customization::Choices choices;
546
547   TestApplication app;
548
549   Actor root    = scene.CreateNodes(0, choices, nodeParams);
550   Actor alice   = root.FindChildByName("Alice");
551   Actor bob     = root.FindChildByName("Bob");
552   Actor charlie = root.FindChildByName("Charlie");
553
554   DALI_TEST_EQUAL(nodeParams.mConstrainables.size(), 3u);
555   DALI_TEST_EQUAL(bob.GetProperty(bob.GetPropertyIndex("angularVelocity")).Get<Vector2>(), Vector2(-0.5, 0.0004));
556
557   ctx.errors.clear();
558   scene.ApplyConstraints(root, std::move(nodeParams.mConstrainables), ctx.onError);
559   DALI_TEST_CHECK(ctx.errors.empty());
560
561   app.GetScene().Add(root);
562   app.SendNotification();
563   app.Render();
564   app.SendNotification();
565   app.Render();
566
567   DALI_TEST_EQUAL(charlie.GetCurrentProperty(Actor::Property::ORIENTATION), alice.GetProperty(Actor::Property::ORIENTATION));
568   DALI_TEST_EQUAL(charlie.GetCurrentProperty(Actor::Property::POSITION), bob.GetProperty(Actor::Property::POSITION));
569   DALI_TEST_EQUAL(charlie.GetCurrentProperty(charlie.GetPropertyIndex("angularVelocity")), bob.GetProperty(bob.GetPropertyIndex("angularVelocity")));
570
571   END_TEST;
572 }
573
574 int UtcDaliDliLoaderNodeProcessor(void)
575 {
576   Context ctx;
577
578   std::vector<Property::Map> nodeMaps;
579   ctx.input.mNodePropertyProcessor = [&](const NodeDefinition&, Property::Map&& map, StringCallback)
580   {
581     nodeMaps.push_back(map);
582   };
583
584   auto path = ctx.pathProvider(ResourceType::Mesh) + "dli/node-processor.dli";
585   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
586
587   DALI_TEST_EQUAL(nodeMaps.size(), 2u);
588   DALI_TEST_EQUAL(nodeMaps[0].Count(), 5u);
589   DALI_TEST_EQUAL(nodeMaps[0].Find("name")->Get<std::string>(), "rootA");
590   DALI_TEST_EQUAL(nodeMaps[0].Find("nickname")->Get<std::string>(), "same as name");
591   DALI_TEST_EQUAL(nodeMaps[0].Find("favourite number")->Get<int32_t>(), 63478);
592
593   auto propArray = nodeMaps[0].Find("array");
594   DALI_TEST_EQUAL(propArray->GetType(), Property::ARRAY);
595
596   auto array = propArray->GetArray();
597   DALI_TEST_EQUAL(array->Count(), 5);
598   DALI_TEST_EQUAL(array->GetElementAt(0).Get<int32_t>(), 1);
599   DALI_TEST_EQUAL(array->GetElementAt(1).Get<int32_t>(), 2);
600   DALI_TEST_EQUAL(array->GetElementAt(2).Get<int32_t>(), 4);
601   DALI_TEST_EQUAL(array->GetElementAt(3).Get<int32_t>(), 8);
602   DALI_TEST_EQUAL(array->GetElementAt(4).Get<int32_t>(), -500);
603
604   auto propObject = nodeMaps[0].Find("object");
605   DALI_TEST_EQUAL(propObject->GetType(), Property::MAP);
606
607   auto object = propObject->GetMap();
608   DALI_TEST_EQUAL(object->Count(), 5);
609   DALI_TEST_EQUAL(object->Find("physics")->Get<bool>(), true);
610   DALI_TEST_EQUAL(object->Find("elasticity")->Get<float>(), .27f);
611   DALI_TEST_EQUAL(object->Find("drag")->Get<float>(), .91f);
612
613   auto propInnerArray = object->Find("inner array");
614   DALI_TEST_EQUAL(propInnerArray->GetType(), Property::ARRAY);
615
616   auto innerArray = propInnerArray->GetArray();
617   DALI_TEST_EQUAL(innerArray->Count(), 3);
618   DALI_TEST_EQUAL(innerArray->GetElementAt(0).Get<std::string>(), "why");
619   DALI_TEST_EQUAL(innerArray->GetElementAt(1).Get<std::string>(), "not");
620   DALI_TEST_EQUAL(innerArray->GetElementAt(2).Get<bool>(), false);
621
622   auto propInnerObject = object->Find("inner object");
623   DALI_TEST_EQUAL(propInnerObject->GetType(), Property::MAP);
624
625   auto innerObject = propInnerObject->GetMap();
626   DALI_TEST_EQUAL(innerObject->Count(), 1);
627   DALI_TEST_EQUAL(innerObject->Find("supported")->Get<bool>(), true);
628
629   DALI_TEST_EQUAL(nodeMaps[1].Count(), 1u);
630   DALI_TEST_EQUAL(nodeMaps[1].Find("name")->Get<std::string>(), "rootB");
631
632   END_TEST;
633 }
634
635 int UtcDaliDliLoaderLoadCoverageTest(void)
636 {
637   Context ctx;
638
639   auto path = ctx.pathProvider(ResourceType::Mesh) + "coverageTest.dli";
640   DALI_TEST_CHECK(ctx.loader.LoadModel(path, ctx.output));
641   DALI_TEST_CHECK(ctx.errors.empty());
642
643   auto& scene = ctx.scene;
644   auto& roots = scene.GetRoots();
645   DALI_TEST_EQUAL(roots.size(), 1u);
646   DALI_TEST_EQUAL(scene.GetNode(roots[0])->mName, "root");
647
648   DALI_TEST_EQUAL(scene.GetNodeCount(), 1u);
649
650   auto& resources = ctx.resources;
651   DALI_TEST_EQUAL(resources.mMeshes.size(), 1u);
652   DALI_TEST_EQUAL(resources.mShaders.size(), 1u);
653   DALI_TEST_EQUAL(resources.mEnvironmentMaps.size(), 2u);
654   DALI_TEST_EQUAL(resources.mSkeletons.size(), 0u);
655
656   auto& materials = ctx.resources.mMaterials;
657   DALI_TEST_EQUAL(2u, materials.size());
658
659   auto  iMaterial = materials.begin();
660   auto& md        = iMaterial->first;
661   DALI_TEST_EQUAL(md.mTextureStages.size(), 1u);
662
663   auto iTexture = md.mTextureStages.begin();
664   DALI_TEST_CHECK(MaskMatch(iTexture->mSemantic, MaterialDefinition::OCCLUSION));
665   DALI_TEST_EQUAL(iTexture->mTexture.mImageUri, "exercise/Icons/Icon_Idle.png");
666   ++iTexture;
667
668   DALI_TEST_EQUAL(ctx.cameraParameters.size(), 1u);
669   DALI_TEST_EQUAL(ctx.lights.size(), 1u);
670   DALI_TEST_EQUAL(ctx.animations.size(), 0u);
671   DALI_TEST_EQUAL(ctx.animGroups.size(), 0u);
672
673   ViewProjection viewProjection;
674   Transforms     xforms{
675     MatrixStack{},
676     viewProjection};
677   NodeDefinition::CreateParams nodeParams{
678     resources,
679     xforms,
680   };
681
682   Customization::Choices choices;
683
684   TestApplication app;
685
686   Actor root = Actor::New();
687   SetActorCentered(root);
688   for(auto iRoot : scene.GetRoots())
689   {
690     auto resourceRefs = resources.CreateRefCounter();
691     scene.CountResourceRefs(iRoot, choices, resourceRefs);
692     resources.mReferenceCounts = std::move(resourceRefs);
693     resources.CountEnvironmentReferences();
694     resources.LoadResources(ctx.pathProvider);
695     if(auto actor = scene.CreateNodes(iRoot, choices, nodeParams))
696     {
697       scene.ConfigureSkinningShaders(resources, actor, std::move(nodeParams.mSkinnables));
698       ConfigureBlendShapeShaders(resources, scene, actor, std::move(nodeParams.mBlendshapeRequests));
699       scene.ApplyConstraints(actor, std::move(nodeParams.mConstrainables));
700       root.Add(actor);
701     }
702   }
703
704   DALI_TEST_EQUAL(root.GetChildCount(), 1u);
705   DALI_TEST_EQUAL(root.GetChildAt(0).GetProperty(Actor::Property::NAME).Get<std::string>(), "root");
706
707   END_TEST;
708 }