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