[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d / utc-Dali-NodeDefinition.cpp
1 /*
2  * Copyright (c) 2020 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-test-suite-utils.h>
22 #include <toolkit-test-application.h>
23 #include <string_view>
24 #include "dali-scene3d/public-api/loader/node-definition.h"
25 #include "dali-scene3d/public-api/loader/view-projection.h"
26
27 using namespace Dali;
28 using namespace Dali::Scene3D::Loader;
29
30 namespace
31 {
32
33 struct Context
34 {
35   ResourceBundle resources;
36
37   ViewProjection viewProjection;
38   Transforms     transforms{MatrixStack{}, viewProjection};
39
40   NodeDefinition::CreateParams createParams{
41     resources,
42     transforms};
43 };
44
45 } // namespace
46
47 int UtcDaliConstraintDefinitionsCompare(void)
48 {
49   ConstraintDefinition cd1{"orientation", 0};
50   ConstraintDefinition cd2{"position", 1};
51
52   DALI_TEST_CHECK(cd1 < cd2);
53   DALI_TEST_CHECK(!(cd2 < cd1));
54   DALI_TEST_CHECK(!(cd1 < cd1));
55   DALI_TEST_CHECK(!(cd2 < cd2));
56
57   DALI_TEST_CHECK(cd1 == cd1);
58   DALI_TEST_CHECK(cd2 == cd2);
59
60   ConstraintDefinition cd3{"position", 0};
61   ConstraintDefinition cd4{"scale", 1};
62   ConstraintDefinition cd5{"position", 1};
63   DALI_TEST_CHECK(cd2 != cd3);
64   DALI_TEST_CHECK(cd2 != cd4);
65   DALI_TEST_CHECK(cd2 == cd5);
66   DALI_TEST_CHECK(cd5 == cd2);
67
68   END_TEST;
69 }
70
71 int UtcDaliBlendshapeShaderConfigurationRequestsCompare(void)
72 {
73   TestApplication                      app;
74   BlendshapeShaderConfigurationRequest bsscr1{"", 0, Shader(nullptr)};
75
76   BlendshapeShaderConfigurationRequest bsscr2{"", 0, Shader::New("void main(){ gl_Position = vec4(0.); }", "void main(){ gl_FragColor = vec4(1.); }")};
77
78   DALI_TEST_CHECK(bsscr1 < bsscr2);
79   DALI_TEST_CHECK(!(bsscr2 < bsscr1));
80   DALI_TEST_CHECK(!(bsscr1 < bsscr1));
81   DALI_TEST_CHECK(!(bsscr2 < bsscr2));
82
83   END_TEST;
84 }
85
86 int UtcDaliNodeDefinitionExtrasCompare(void)
87 {
88   NodeDefinition::Extra e1{"alpha", Vector3::XAXIS * 2.f};
89   NodeDefinition::Extra e2{"beta", 8};
90
91   DALI_TEST_CHECK(e1 < e2);
92   DALI_TEST_CHECK(!(e1 < e1));
93   DALI_TEST_CHECK(!(e2 < e1));
94   DALI_TEST_CHECK(!(e2 < e2));
95
96   END_TEST;
97 }
98
99 int UtcDaliNodeDefinitionProperties(void)
100 {
101   TestApplication testApp;
102   NodeDefinition  nodeDef{
103     "testRootNode",
104     INVALID_INDEX,
105     Vector3{-100.f, 100.f, -500.f},
106     Quaternion{Radian(Degree(45.f)), Vector3::ZAXIS},
107     Vector3{2.f, 4.f, 8.f},
108     Vector3{100.f, 50.f, 25.f},
109     false,
110   };
111
112   Quaternion frobnicateFactor(0.f, 1.f, 2.f, 3.f);
113   frobnicateFactor.Normalize(); // because it will be (by DALi) once it's set as a property.
114   nodeDef.mExtras.push_back(NodeDefinition::Extra{"frobnicateFactor", frobnicateFactor});
115
116   Context ctx;
117   auto    actor = nodeDef.CreateModelNode(ctx.createParams);
118   DALI_TEST_EQUAL(nodeDef.mName, actor.GetProperty(Actor::Property::NAME).Get<std::string>());
119   DALI_TEST_EQUAL(nodeDef.mPosition, actor.GetProperty(Actor::Property::POSITION).Get<Vector3>());
120   DALI_TEST_EQUAL(nodeDef.mOrientation, actor.GetProperty(Actor::Property::ORIENTATION).Get<Quaternion>());
121   DALI_TEST_EQUAL(nodeDef.mScale, actor.GetProperty(Actor::Property::SCALE).Get<Vector3>());
122   DALI_TEST_EQUAL(nodeDef.mSize, actor.GetProperty(Actor::Property::SIZE).Get<Vector3>());
123   DALI_TEST_EQUAL(nodeDef.mIsVisible, actor.GetProperty(Actor::Property::VISIBLE).Get<bool>());
124
125   Property::Index propFrobnicateFactor = actor.GetPropertyIndex("frobnicateFactor");
126   DALI_TEST_CHECK(propFrobnicateFactor != Property::INVALID_INDEX);
127
128   auto frobnicateFactorValue = actor.GetProperty(propFrobnicateFactor);
129   DALI_TEST_EQUAL(Property::ROTATION, frobnicateFactorValue.GetType());
130   DALI_TEST_EQUAL(frobnicateFactorValue.Get<Quaternion>(), frobnicateFactor);
131
132   DALI_TEST_EQUAL(0, actor.GetChildCount());
133   DALI_TEST_EQUAL(0, actor.GetRendererCount());
134
135   END_TEST;
136 }
137
138 int UtcDaliNodeDefinitionRenderableRegisterResources(void)
139 {
140   NodeDefinition nodeDef;
141
142   std::unique_ptr<NodeDefinition::Renderable> renderable = std::unique_ptr<NodeDefinition::Renderable>(new NodeDefinition::Renderable());
143   nodeDef.mRenderables.push_back(std::move(renderable));
144   nodeDef.mRenderables[0]->mShaderIdx = 0;
145
146   struct : IResourceReceiver
147   {
148     std::vector<Index> shaders;
149     uint32_t           otherResources = 0;
150
151     void Register(ResourceType::Value type, Index id) override
152     {
153       switch(type)
154       {
155         case ResourceType::Shader:
156           shaders.push_back(id);
157           break;
158
159         default:
160           ++otherResources;
161       }
162     }
163   } resourceReceiver;
164
165   nodeDef.mRenderables[0]->RegisterResources(resourceReceiver);
166   DALI_TEST_EQUAL(1u, resourceReceiver.shaders.size());
167   DALI_TEST_EQUAL(0, resourceReceiver.shaders[0]);
168   DALI_TEST_EQUAL(0, resourceReceiver.otherResources);
169
170   END_TEST;
171 }
172
173 int UtcDaliNodeDefinitionRenderableReflectResources(void)
174 {
175   NodeDefinition nodeDef;
176
177   std::unique_ptr<NodeDefinition::Renderable> renderable = std::unique_ptr<NodeDefinition::Renderable>(new NodeDefinition::Renderable());
178   nodeDef.mRenderables.push_back(std::move(renderable));
179   nodeDef.mRenderables[0]->mShaderIdx = 0;
180
181   struct : IResourceReflector
182   {
183     std::vector<Index*> shaders;
184     uint32_t            otherResources = 0;
185
186     void Reflect(ResourceType::Value type, Index& id) override
187     {
188       switch(type)
189       {
190         case ResourceType::Shader:
191           shaders.push_back(&id);
192           break;
193
194         default:
195           ++otherResources;
196       }
197     }
198   } resourceReflector;
199
200   nodeDef.mRenderables[0]->ReflectResources(resourceReflector);
201   DALI_TEST_EQUAL(1u, resourceReflector.shaders.size());
202   DALI_TEST_EQUAL(&nodeDef.mRenderables[0]->mShaderIdx, resourceReflector.shaders[0]);
203   DALI_TEST_EQUAL(0, resourceReflector.otherResources);
204
205   END_TEST;
206 }
207
208 int UtcDaliNodeDefinitionRenderable(void)
209 {
210   TestApplication testApp;
211   NodeDefinition  nodeDef;
212
213   std::unique_ptr<NodeDefinition::Renderable> renderable = std::unique_ptr<NodeDefinition::Renderable>(new NodeDefinition::Renderable());
214   nodeDef.mRenderables.push_back(std::move(renderable));
215   nodeDef.mRenderables[0]->mShaderIdx = 0;
216
217   Context    ctx;
218   const auto VSH    = "void main() { gl_Position = vec4(0.); }";
219   const auto FSH    = "void main() { gl_FragColor = vec4(1.); }";
220   auto       shader = Shader::New(VSH, FSH);
221   ctx.resources.mShaders.push_back({ShaderDefinition{}, shader});
222
223   auto actor = nodeDef.CreateModelNode(ctx.createParams);
224   DALI_TEST_EQUAL(1, actor.GetRendererCount());
225
226   auto renderer = actor.GetRendererAt(0);
227   DALI_TEST_EQUAL(renderer.GetShader(), shader);
228
229   END_TEST;
230 }