[dali_2.2.4] 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     Vector3{-100.f, 100.f, -500.f},
105     Quaternion{Radian(Degree(45.f)), Vector3::ZAXIS},
106     Vector3{2.f, 4.f, 8.f},
107     Vector3{100.f, 50.f, 25.f},
108     false,
109   };
110
111   Quaternion frobnicateFactor(0.f, 1.f, 2.f, 3.f);
112   frobnicateFactor.Normalize(); // because it will be (by DALi) once it's set as a property.
113   nodeDef.mExtras.push_back(NodeDefinition::Extra{"frobnicateFactor", frobnicateFactor});
114
115   Context ctx;
116   auto    actor = nodeDef.CreateActor(ctx.createParams);
117   DALI_TEST_EQUAL(nodeDef.mName, actor.GetProperty(Actor::Property::NAME).Get<std::string>());
118   DALI_TEST_EQUAL(nodeDef.mPosition, actor.GetProperty(Actor::Property::POSITION).Get<Vector3>());
119   DALI_TEST_EQUAL(nodeDef.mOrientation, actor.GetProperty(Actor::Property::ORIENTATION).Get<Quaternion>());
120   DALI_TEST_EQUAL(nodeDef.mScale, actor.GetProperty(Actor::Property::SCALE).Get<Vector3>());
121   DALI_TEST_EQUAL(nodeDef.mSize, actor.GetProperty(Actor::Property::SIZE).Get<Vector3>());
122   DALI_TEST_EQUAL(nodeDef.mIsVisible, actor.GetProperty(Actor::Property::VISIBLE).Get<bool>());
123
124   Property::Index propFrobnicateFactor = actor.GetPropertyIndex("frobnicateFactor");
125   DALI_TEST_CHECK(propFrobnicateFactor != Property::INVALID_INDEX);
126
127   auto frobnicateFactorValue = actor.GetProperty(propFrobnicateFactor);
128   DALI_TEST_EQUAL(Property::ROTATION, frobnicateFactorValue.GetType());
129   DALI_TEST_EQUAL(frobnicateFactorValue.Get<Quaternion>(), frobnicateFactor);
130
131   DALI_TEST_EQUAL(0, actor.GetChildCount());
132   DALI_TEST_EQUAL(0, actor.GetRendererCount());
133
134   END_TEST;
135 }
136
137 int UtcDaliNodeDefinitionRenderableRegisterResources(void)
138 {
139   NodeDefinition nodeDef;
140
141   std::unique_ptr<NodeDefinition::Renderable> renderable = std::unique_ptr<NodeDefinition::Renderable>(new NodeDefinition::Renderable());
142   nodeDef.mRenderables.push_back(std::move(renderable));
143   nodeDef.mRenderables[0]->mShaderIdx = 0;
144
145   struct : IResourceReceiver
146   {
147     std::vector<Index> shaders;
148     uint32_t           otherResources = 0;
149
150     void Register(ResourceType::Value type, Index id) override
151     {
152       switch(type)
153       {
154         case ResourceType::Shader:
155           shaders.push_back(id);
156           break;
157
158         default:
159           ++otherResources;
160       }
161     }
162   } resourceReceiver;
163
164   nodeDef.mRenderables[0]->RegisterResources(resourceReceiver);
165   DALI_TEST_EQUAL(1u, resourceReceiver.shaders.size());
166   DALI_TEST_EQUAL(0, resourceReceiver.shaders[0]);
167   DALI_TEST_EQUAL(0, resourceReceiver.otherResources);
168
169   END_TEST;
170 }
171
172 int UtcDaliNodeDefinitionRenderableReflectResources(void)
173 {
174   NodeDefinition nodeDef;
175
176   std::unique_ptr<NodeDefinition::Renderable> renderable = std::unique_ptr<NodeDefinition::Renderable>(new NodeDefinition::Renderable());
177   nodeDef.mRenderables.push_back(std::move(renderable));
178   nodeDef.mRenderables[0]->mShaderIdx = 0;
179
180   struct : IResourceReflector
181   {
182     std::vector<Index*> shaders;
183     uint32_t            otherResources = 0;
184
185     void Reflect(ResourceType::Value type, Index& id) override
186     {
187       switch(type)
188       {
189         case ResourceType::Shader:
190           shaders.push_back(&id);
191           break;
192
193         default:
194           ++otherResources;
195       }
196     }
197   } resourceReflector;
198
199   nodeDef.mRenderables[0]->ReflectResources(resourceReflector);
200   DALI_TEST_EQUAL(1u, resourceReflector.shaders.size());
201   DALI_TEST_EQUAL(&nodeDef.mRenderables[0]->mShaderIdx, resourceReflector.shaders[0]);
202   DALI_TEST_EQUAL(0, resourceReflector.otherResources);
203
204   END_TEST;
205 }
206
207 int UtcDaliNodeDefinitionRenderable(void)
208 {
209   TestApplication testApp;
210   NodeDefinition  nodeDef;
211
212   std::unique_ptr<NodeDefinition::Renderable> renderable = std::unique_ptr<NodeDefinition::Renderable>(new NodeDefinition::Renderable());
213   nodeDef.mRenderables.push_back(std::move(renderable));
214   nodeDef.mRenderables[0]->mShaderIdx = 0;
215
216   Context    ctx;
217   const auto VSH    = "void main() { gl_Position = vec4(0.); }";
218   const auto FSH    = "void main() { gl_FragColor = vec4(1.); }";
219   auto       shader = Shader::New(VSH, FSH);
220   ctx.resources.mShaders.push_back({ShaderDefinition{}, shader});
221
222   auto actor = nodeDef.CreateActor(ctx.createParams);
223   DALI_TEST_EQUAL(1, actor.GetRendererCount());
224
225   auto renderer = actor.GetRendererAt(0);
226   DALI_TEST_EQUAL(renderer.GetShader(), shader);
227
228   END_TEST;
229 }