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