Change ModelView to Model
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-scene3d / utc-Dali-Model.cpp
1 /*
2  * Copyright (c) 2022 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 #include <dali-toolkit-test-suite-utils.h>
19 #include <dali-toolkit/dali-toolkit.h>
20 #include <stdlib.h>
21 #include <iostream>
22
23 #include <dali-scene3d/public-api/controls/model/model.h>
24
25 using namespace Dali;
26 using namespace Dali::Toolkit;
27
28 void model_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void model_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38 namespace
39 {
40 /**
41  * For the AnimatedCube.gltf and its Assets
42  * Donated by Norbert Nopper for glTF testing.
43  * Take from https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/AnimatedCube
44  */
45 const char* TEST_GLTF_FILE_NAME                = TEST_RESOURCE_DIR "/AnimatedCube.gltf";
46 const char* TEST_GLTF_ANIMATION_TEST_FILE_NAME = TEST_RESOURCE_DIR "/animationTest.gltf";
47 const char* TEST_DLI_FILE_NAME                 = TEST_RESOURCE_DIR "/arc.dli";
48 /**
49  * For the diffuse and specular cube map texture.
50  * These textures are based off version of Wave engine sample
51  * Take from https://github.com/WaveEngine/Samples
52  *
53  * Copyright (c) 2022 Wave Coorporation
54  *
55  * Permission is hereby granted, free of charge, to any person obtaining a copy
56  * of this software and associated documentation files (the "Software"), to
57  * deal in the Software without restriction, including without limitation the
58  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
59  * sell copies of the Software, and to permit persons to whom the Software is
60  * furnished to do so, subject to the following conditions:
61  *
62  * The above copyright notice and this permission notice shall be included in
63  * all copies or substantial portions of the Software.
64  *
65  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
66  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
67  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
68  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
69  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
70  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
71  * THE SOFTWARE.
72  */
73 const char* TEST_DIFFUSE_TEXTURE  = TEST_RESOURCE_DIR "/forest_irradiance.ktx";
74 const char* TEST_SPECULAR_TEXTURE = TEST_RESOURCE_DIR "/forest_radiance.ktx";
75 } // namespace
76
77 // Negative test case for a method
78 int UtcDaliModelUninitialized(void)
79 {
80   ToolkitTestApplication application;
81   tet_infoline(" UtcDaliModelUninitialized");
82
83   Scene3D::Model model;
84
85   try
86   {
87     // New() must be called to create a Model or it wont be valid.
88     Actor a = Actor::New();
89     model.Add(a);
90     DALI_TEST_CHECK(false);
91   }
92   catch(Dali::DaliException& e)
93   {
94     // Tests that a negative test of an assertion succeeds
95     DALI_TEST_PRINT_ASSERT(e);
96     DALI_TEST_CHECK(!model);
97   }
98   END_TEST;
99 }
100
101 // Positive test case for a method
102 int UtcDaliModelNew(void)
103 {
104   ToolkitTestApplication application;
105   tet_infoline(" UtcDaliModelNew");
106
107   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
108   DALI_TEST_CHECK(model);
109   END_TEST;
110 }
111
112 // Positive test case for a method
113 int UtcDaliModelDownCast(void)
114 {
115   ToolkitTestApplication application;
116   tet_infoline(" UtcDaliModelDownCast");
117
118   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
119   BaseHandle         handle(model);
120
121   Scene3D::Model model2 = Scene3D::Model::DownCast(handle);
122   DALI_TEST_CHECK(model);
123   DALI_TEST_CHECK(model2);
124   DALI_TEST_CHECK(model2 == model);
125   END_TEST;
126 }
127
128 int UtcDaliModelTypeRegistry(void)
129 {
130   ToolkitTestApplication application;
131
132   TypeRegistry typeRegistry = TypeRegistry::Get();
133   DALI_TEST_CHECK(typeRegistry);
134
135   TypeInfo typeInfo = typeRegistry.GetTypeInfo("Model");
136   DALI_TEST_CHECK(typeInfo);
137
138   BaseHandle handle = typeInfo.CreateInstance();
139   DALI_TEST_CHECK(handle);
140
141   Scene3D::Model model = Scene3D::Model::DownCast(handle);
142   DALI_TEST_CHECK(model);
143
144   END_TEST;
145 }
146
147 // Positive test case for a method
148 int UtcDaliModelAddRemove(void)
149 {
150   ToolkitTestApplication application;
151   tet_infoline(" UtcDaliModelAddRemove");
152
153   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
154   DALI_TEST_CHECK(model);
155
156   Actor actor = Actor::New();
157   DALI_TEST_CHECK(!actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE));
158
159   model.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
160   model.SetProperty(Actor::Property::SIZE, application.GetScene().GetSize());
161   model.Add(actor);
162   application.GetScene().Add(model);
163
164   DALI_TEST_CHECK(actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE));
165
166   model.Remove(actor);
167
168   DALI_TEST_CHECK(!actor.GetProperty<bool>(Actor::Property::CONNECTED_TO_SCENE));
169   END_TEST;
170 }
171
172 int UtcDaliModelCopyAndAssignment(void)
173 {
174   ToolkitTestApplication application;
175
176   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
177   DALI_TEST_CHECK(model);
178
179   Scene3D::Model copy(model);
180   DALI_TEST_CHECK(model == copy);
181
182   Scene3D::Model assign;
183   DALI_TEST_CHECK(!assign);
184
185   assign = copy;
186   DALI_TEST_CHECK(assign == model);
187
188   END_TEST;
189 }
190
191 int UtcDaliModelMoveConstructor(void)
192 {
193   ToolkitTestApplication application;
194
195   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
196   DALI_TEST_EQUALS(1, model.GetBaseObject().ReferenceCount(), TEST_LOCATION);
197   model.SetProperty(Actor::Property::SENSITIVE, false);
198   DALI_TEST_CHECK(false == model.GetProperty<bool>(Actor::Property::SENSITIVE));
199
200   Scene3D::Model moved = std::move(model);
201   DALI_TEST_CHECK(moved);
202   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
203   DALI_TEST_CHECK(false == moved.GetProperty<bool>(Actor::Property::SENSITIVE));
204   DALI_TEST_CHECK(!model);
205
206   END_TEST;
207 }
208
209 int UtcDaliModelMoveAssignment(void)
210 {
211   ToolkitTestApplication application;
212
213   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
214   DALI_TEST_EQUALS(1, model.GetBaseObject().ReferenceCount(), TEST_LOCATION);
215   model.SetProperty(Actor::Property::SENSITIVE, false);
216   DALI_TEST_CHECK(false == model.GetProperty<bool>(Actor::Property::SENSITIVE));
217
218   Scene3D::Model moved;
219   moved = std::move(model);
220   DALI_TEST_CHECK(moved);
221   DALI_TEST_EQUALS(1, moved.GetBaseObject().ReferenceCount(), TEST_LOCATION);
222   DALI_TEST_CHECK(false == moved.GetProperty<bool>(Actor::Property::SENSITIVE));
223   DALI_TEST_CHECK(!model);
224
225   END_TEST;
226 }
227
228 int UtcDaliModelOnScene01(void)
229 {
230   ToolkitTestApplication application;
231
232   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
233
234   application.GetScene().Add(model);
235
236   application.SendNotification();
237   application.Render();
238
239   uint32_t modelCount = model.GetModelRoot().GetChildCount();
240   DALI_TEST_EQUALS(1, modelCount, TEST_LOCATION);
241
242   END_TEST;
243 }
244
245 int UtcDaliModelOnScene02(void)
246 {
247   ToolkitTestApplication application;
248
249   Scene3D::Model model = Scene3D::Model::New(TEST_DLI_FILE_NAME);
250
251   application.GetScene().Add(model);
252
253   application.SendNotification();
254   application.Render();
255
256   uint32_t modelCount = model.GetModelRoot().GetChildCount();
257   DALI_TEST_EQUALS(1, modelCount, TEST_LOCATION);
258
259   Actor   rootActor = model.GetModelRoot();
260   Vector3 rootSize  = rootActor.GetProperty<Vector3>(Dali::Actor::Property::SIZE);
261   DALI_TEST_EQUALS(Vector3(2, 2, 1), rootSize, TEST_LOCATION);
262
263   END_TEST;
264 }
265
266 int UtcDaliModelOnSizeSet(void)
267 {
268   ToolkitTestApplication application;
269
270   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
271
272   application.GetScene().Add(model);
273
274   application.SendNotification();
275   application.Render();
276
277   Vector2 size(200.0f, 300.0f);
278   model.SetProperty(Actor::Property::SIZE, size);
279
280   application.SendNotification();
281   application.Render();
282
283   DALI_TEST_EQUALS(model.GetCurrentProperty<Vector2>(Actor::Property::SIZE), size, TEST_LOCATION);
284
285   END_TEST;
286 }
287
288 int UtcDaliModelGetNaturalSize(void)
289 {
290   ToolkitTestApplication application;
291
292   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
293
294   Vector3 naturalSize = model.GetNaturalSize();
295
296   DALI_TEST_EQUALS(Vector3(2, 2, 2), naturalSize, TEST_LOCATION);
297
298   Actor root = model.GetModelRoot();
299   DALI_TEST_CHECK(root);
300
301   END_TEST;
302 }
303
304 int UtcDaliModelSetImageBasedLightSource01(void)
305 {
306   ToolkitTestApplication application;
307
308   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
309
310   application.GetScene().Add(model);
311
312   application.SendNotification();
313   application.Render();
314
315   Actor meshActor = model.FindChildByName("AnimatedCube");
316   DALI_TEST_CHECK(meshActor);
317
318   Renderer renderer = meshActor.GetRendererAt(0u);
319   DALI_TEST_CHECK(renderer);
320
321   TextureSet textureSet = renderer.GetTextures();
322   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 7u, TEST_LOCATION);
323
324   Texture diffuseTexture  = textureSet.GetTexture(5u);
325   Texture specularTexture = textureSet.GetTexture(6u);
326
327   model.SetImageBasedLightSource(TEST_DIFFUSE_TEXTURE, TEST_SPECULAR_TEXTURE);
328
329   Texture newDiffuseTexture  = textureSet.GetTexture(5u);
330   Texture newSpecularTexture = textureSet.GetTexture(6u);
331
332   DALI_TEST_NOT_EQUALS(diffuseTexture, newDiffuseTexture, 0.0f, TEST_LOCATION);
333   DALI_TEST_NOT_EQUALS(specularTexture, newSpecularTexture, 0.0f, TEST_LOCATION);
334
335   END_TEST;
336 }
337
338 int UtcDaliModelSetImageBasedLightSource02(void)
339 {
340   ToolkitTestApplication application;
341
342   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
343
344   application.GetScene().Add(model);
345
346   application.SendNotification();
347   application.Render();
348
349   Actor meshActor = model.FindChildByName("AnimatedCube");
350   DALI_TEST_CHECK(meshActor);
351
352   Renderer renderer = meshActor.GetRendererAt(0u);
353   DALI_TEST_CHECK(renderer);
354
355   TextureSet textureSet = renderer.GetTextures();
356   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 7u, TEST_LOCATION);
357
358   Texture diffuseTexture  = textureSet.GetTexture(5u);
359   Texture specularTexture = textureSet.GetTexture(6u);
360
361   model.SetImageBasedLightSource("", "");
362
363   Texture newDiffuseTexture  = textureSet.GetTexture(5u);
364   Texture newSpecularTexture = textureSet.GetTexture(6u);
365
366   DALI_TEST_EQUALS(diffuseTexture, newDiffuseTexture, TEST_LOCATION);
367   DALI_TEST_EQUALS(specularTexture, newSpecularTexture, TEST_LOCATION);
368
369   END_TEST;
370 }
371
372 int UtcDaliModelSetImageBasedLightSource03(void)
373 {
374   ToolkitTestApplication application;
375
376   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
377
378   application.GetScene().Add(model);
379
380   application.SendNotification();
381   application.Render();
382
383   Actor meshActor = model.FindChildByName("AnimatedCube");
384   DALI_TEST_CHECK(meshActor);
385
386   Renderer renderer = meshActor.GetRendererAt(0u);
387   DALI_TEST_CHECK(renderer);
388
389   TextureSet textureSet = renderer.GetTextures();
390   DALI_TEST_EQUALS(textureSet.GetTextureCount(), 7u, TEST_LOCATION);
391
392   Texture diffuseTexture  = textureSet.GetTexture(5u);
393   Texture specularTexture = textureSet.GetTexture(6u);
394
395   model.SetImageBasedLightSource("dummy.ktx", "dummy.ktx");
396
397   Texture newDiffuseTexture  = textureSet.GetTexture(5u);
398   Texture newSpecularTexture = textureSet.GetTexture(6u);
399
400   DALI_TEST_EQUALS(diffuseTexture, newDiffuseTexture, TEST_LOCATION);
401   DALI_TEST_EQUALS(specularTexture, newSpecularTexture, TEST_LOCATION);
402
403   END_TEST;
404 }
405
406 int UtcDaliModelImageBasedFactor(void)
407 {
408   ToolkitTestApplication application;
409
410   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
411
412   DALI_TEST_EQUALS(model.GetImageBasedLightScaleFactor(), 1.0f, TEST_LOCATION);
413
414   model.SetImageBasedLightScaleFactor(0.5f);
415   DALI_TEST_EQUALS(model.GetImageBasedLightScaleFactor(), 0.5f, TEST_LOCATION);
416   END_TEST;
417 }
418
419 int UtcDaliModelAnimation01(void)
420 {
421   ToolkitTestApplication application;
422
423   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_FILE_NAME);
424   model.SetProperty(Dali::Actor::Property::SIZE, Vector2(50, 50));
425
426   application.GetScene().Add(model);
427
428   application.SendNotification();
429   application.Render();
430
431   uint32_t animationCount = model.GetAnimationCount();
432   DALI_TEST_EQUALS(1, animationCount, TEST_LOCATION);
433
434   Animation animationByIndex = model.GetAnimation(0u);
435   DALI_TEST_CHECK(animationByIndex);
436
437   Animation animationByName = model.GetAnimation("animation_AnimatedCube");
438   DALI_TEST_CHECK(animationByName);
439   DALI_TEST_EQUALS(animationByIndex, animationByName, TEST_LOCATION);
440
441   END_TEST;
442 }
443
444 int UtcDaliModelAnimation02(void)
445 {
446   ToolkitTestApplication application;
447
448   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_ANIMATION_TEST_FILE_NAME);
449   model.SetProperty(Dali::Actor::Property::SIZE, Vector2(50, 50));
450
451   application.GetScene().Add(model);
452
453   application.SendNotification();
454   application.Render();
455
456   uint32_t animationCount = model.GetAnimationCount();
457   DALI_TEST_EQUALS(9, animationCount, TEST_LOCATION);
458
459   Animation animation1 = model.GetAnimation("Step Scale");
460   DALI_TEST_CHECK(animation1);
461   DALI_TEST_EQUALS(1.66667f, animation1.GetDuration(), 0.001f, TEST_LOCATION);
462
463   Animation animation2 = model.GetAnimation("CubicSpline Scale");
464   DALI_TEST_CHECK(animation2);
465   DALI_TEST_EQUALS(1.66667f, animation2.GetDuration(), 0.001f, TEST_LOCATION);
466
467   DALI_TEST_NOT_EQUALS(animation1, animation2, 0.0f, TEST_LOCATION);
468
469   END_TEST;
470 }
471
472 // For ResourceReady
473 namespace
474 {
475 static bool gOnRelayoutCallBackCalled = false;
476 void OnRelayoutCallback(Actor actor)
477 {
478   gOnRelayoutCallBackCalled = true;
479 }
480
481 static bool gResourceReadyCalled = false;
482 void OnResourceReady(Control control)
483 {
484   gResourceReadyCalled = true;
485 }
486 }
487
488 int UtcDaliModelResourceReady(void)
489 {
490   ToolkitTestApplication application;
491
492   gOnRelayoutCallBackCalled = false;
493   gResourceReadyCalled = false;
494   Scene3D::Model model = Scene3D::Model::New(TEST_GLTF_ANIMATION_TEST_FILE_NAME);
495   model.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
496   model.OnRelayoutSignal().Connect(OnRelayoutCallback);
497   model.ResourceReadySignal().Connect(OnResourceReady);
498   DALI_TEST_EQUALS(model.IsResourceReady(), false, TEST_LOCATION);
499
500   // Sanity check
501   DALI_TEST_CHECK(!gOnRelayoutCallBackCalled);
502   DALI_TEST_CHECK(!gResourceReadyCalled);
503
504   application.GetScene().Add(model);
505
506   application.SendNotification();
507   application.Render();
508
509   DALI_TEST_EQUALS(gOnRelayoutCallBackCalled, false, TEST_LOCATION);
510   DALI_TEST_EQUALS(model.IsResourceReady(), true, TEST_LOCATION);
511   DALI_TEST_EQUALS(gResourceReadyCalled, true, TEST_LOCATION);
512
513   END_TEST;
514 }