[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d-internal / utc-Dali-GlbLoaderImpl.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/glb-loader-impl.h>
22 #include <dali-scene3d/public-api/loader/load-result.h>
23 #include <dali-scene3d/public-api/loader/resource-bundle.h>
24 #include <dali-scene3d/public-api/loader/scene-definition.h>
25 #include <dali-scene3d/public-api/loader/shader-manager.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 #define DALI_TEST_THROW(expression, exception, predicate) \
33   {                                                       \
34     bool daliTestThrowSuccess__ = false;                  \
35     try                                                   \
36     {                                                     \
37       do                                                  \
38       {                                                   \
39         expression;                                       \
40       } while(0);                                         \
41       printf("No exception was thrown.\n");               \
42     }                                                     \
43     catch(std::decay<exception>::type & ex)               \
44     {                                                     \
45       daliTestThrowSuccess__ = predicate(ex);             \
46     }                                                     \
47     catch(...)                                            \
48     {                                                     \
49       printf("Wrong type of exception thrown.\n");        \
50     }                                                     \
51     DALI_TEST_CHECK(daliTestThrowSuccess__);              \
52   }
53
54 namespace
55 {
56 struct Context
57 {
58   ResourceBundle::PathProvider pathProvider = [](ResourceType::Value type) {
59     return TEST_RESOURCE_DIR "/";
60   };
61
62   ResourceBundle  resources;
63   SceneDefinition scene;
64   SceneMetadata   metaData;
65
66   std::vector<AnimationDefinition>      animations;
67   std::vector<AnimationGroupDefinition> animationGroups;
68   std::vector<CameraParameters>         cameras;
69   std::vector<LightParameters>          lights;
70
71   LoadResult loadResult{
72     resources,
73     scene,
74     metaData,
75     animations,
76     animationGroups,
77     cameras,
78     lights};
79
80   Dali::Scene3D::Loader::Internal::GlbLoaderImpl loader;
81 };
82
83 struct ExceptionMessageStartsWith
84 {
85   const std::string_view expected;
86
87   bool operator()(const std::runtime_error& e)
88   {
89     const bool success = (0 == strncmp(e.what(), expected.data(), expected.size()));
90     if(!success)
91     {
92       printf("Expected: %s, got: %s.\n", expected.data(), e.what());
93     }
94     return success;
95   }
96 };
97
98 } // namespace
99
100 int UtcDaliGlbLoaderFailedToLoad(void)
101 {
102   Context ctx;
103
104   DALI_TEST_EQUAL(ctx.loader.LoadModel("non-existent.glb", ctx.loadResult), false);
105
106   DALI_TEST_EQUAL(0, ctx.scene.GetRoots().size());
107   DALI_TEST_EQUAL(0, ctx.scene.GetNodeCount());
108
109   DALI_TEST_EQUAL(0, ctx.resources.mEnvironmentMaps.size());
110   DALI_TEST_EQUAL(0, ctx.resources.mMaterials.size());
111   DALI_TEST_EQUAL(0, ctx.resources.mMeshes.size());
112   DALI_TEST_EQUAL(0, ctx.resources.mShaders.size());
113   DALI_TEST_EQUAL(0, ctx.resources.mSkeletons.size());
114
115   DALI_TEST_EQUAL(0, ctx.cameras.size());
116   DALI_TEST_EQUAL(0, ctx.lights.size());
117   DALI_TEST_EQUAL(0, ctx.animations.size());
118   DALI_TEST_EQUAL(0, ctx.animationGroups.size());
119
120   END_TEST;
121 }
122
123 int UtcDaliGlbLoaderFailedToParse(void)
124 {
125   Context ctx;
126   DALI_TEST_EQUAL(ctx.loader.LoadModel(TEST_RESOURCE_DIR "/invalid.glb", ctx.loadResult), false);
127
128   DALI_TEST_EQUAL(0, ctx.scene.GetRoots().size());
129   DALI_TEST_EQUAL(0, ctx.scene.GetNodeCount());
130
131   DALI_TEST_EQUAL(0, ctx.resources.mEnvironmentMaps.size());
132   DALI_TEST_EQUAL(0, ctx.resources.mMaterials.size());
133   DALI_TEST_EQUAL(0, ctx.resources.mMeshes.size());
134   DALI_TEST_EQUAL(0, ctx.resources.mSkeletons.size());
135
136   DALI_TEST_EQUAL(0, ctx.cameras.size());
137   DALI_TEST_EQUAL(0, ctx.lights.size());
138   DALI_TEST_EQUAL(0, ctx.animations.size());
139   DALI_TEST_EQUAL(0, ctx.animationGroups.size());
140
141   END_TEST;
142 }
143
144 int UtcDaliGlbLoaderSuccess1(void)
145 {
146   Context                 ctx;
147   ctx.loader.LoadModel(TEST_RESOURCE_DIR "/BoxAnimated.glb", ctx.loadResult);
148
149   DALI_TEST_EQUAL(1u, ctx.scene.GetRoots().size());
150   DALI_TEST_EQUAL(5u, ctx.scene.GetNodeCount());
151
152   TestApplication app;
153
154   Customization::Choices choices;
155   for(auto iRoot : ctx.scene.GetRoots())
156   {
157     auto resourceRefs = ctx.resources.CreateRefCounter();
158     ctx.scene.CountResourceRefs(iRoot, choices, resourceRefs);
159     ctx.resources.mReferenceCounts = std::move(resourceRefs);
160     ctx.resources.LoadResources(ctx.pathProvider);
161   }
162
163   DALI_TEST_EQUAL(true, ctx.resources.mMeshes[0u].first.mPositions.IsDefined());
164   DALI_TEST_EQUAL(1152, ctx.resources.mMeshes[0u].first.mPositions.mBlob.mLength);
165
166   END_TEST;
167 }