(AutomatedTests) Merged managed & unmanaged tests
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-AnimatableMesh.cpp
1 /*
2  * Copyright (c) 2014 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 #include <iostream>
18
19 #include <stdlib.h>
20 #include <dali/public-api/dali-core.h>
21 #include <dali-test-suite-utils.h>
22 #include <mesh-builder.h>
23
24 using namespace Dali;
25
26
27 void utc_dali_animatable_mesh_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_animatable_mesh_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 namespace
38 {
39
40 void CreateFaces(Dali::AnimatableMesh::Faces& faces, int numVerts)
41 {
42   for(int i=0; i<numVerts-3; i++)
43   {
44     faces.push_back(i);
45     faces.push_back(i+1);
46     faces.push_back(i+2);
47   }
48 }
49
50 void CreateOutOfRangeFaces(Dali::AnimatableMesh::Faces& faces, int numVerts)
51 {
52   for(int i=numVerts; i<numVerts*2-3; i++)
53   {
54     faces.push_back(i);
55     faces.push_back(i+1);
56     faces.push_back(i+2);
57   }
58 }
59
60 AnimatableMesh CreateMesh()
61 {
62   AnimatableMesh::Faces faces;
63   CreateFaces(faces, 10);
64   AnimatableMesh mesh = AnimatableMesh::New(10, faces);
65   return mesh;
66 }
67
68 } // anon namespace
69
70 // Negative test case for a method
71 int UtcDaliAnimatableMeshConstructor01(void)
72 {
73   TestApplication application;
74
75   AnimatableMesh mesh;
76
77   DALI_TEST_CHECK( ! mesh );
78   END_TEST;
79 }
80
81 int UtcDaliAnimatableMeshConstructor02(void)
82 {
83   TestApplication application;
84
85   Dali::AnimatableMesh::Faces faces;
86   CreateFaces(faces, 10);
87
88   AnimatableMesh mesh = AnimatableMesh::New(10, faces);
89   DALI_TEST_CHECK( mesh );
90
91   AnimatableMesh mesh2 = mesh;
92   DALI_TEST_CHECK( mesh2 );
93
94   AnimatableMesh mesh3 ( mesh2 );
95   DALI_TEST_CHECK( mesh3 );
96   END_TEST;
97 }
98
99 int UtcDaliAnimatableMeshConstructor03(void)
100 {
101   TestApplication application;
102
103   // Heap allocate a handle. Don't do this in real code!
104   AnimatableMesh* mesh = new AnimatableMesh();
105   DALI_TEST_CHECK( ! *mesh );
106   delete mesh;
107   END_TEST;
108 }
109
110
111 // Positive test case for a method
112 int UtcDaliAnimatableMeshNew01(void)
113 {
114   TestApplication application;
115
116   Dali::AnimatableMesh::Faces faces;
117   CreateFaces(faces, 10);
118
119   AnimatableMesh mesh = AnimatableMesh::New(10, faces);
120   DALI_TEST_CHECK( mesh );
121   END_TEST;
122 }
123
124 // Positive test case for a method
125 int UtcDaliAnimatableMeshNew02(void)
126 {
127   TestApplication application;
128
129   Dali::AnimatableMesh::Faces faces;
130   CreateFaces(faces, 10);
131
132   Dali::Material mat = Dali::Material::New("dummy mat");
133   AnimatableMesh mesh = AnimatableMesh::New(10, faces, mat);
134   DALI_TEST_CHECK( mesh );
135   END_TEST;
136 }
137
138
139 // Negative test case for a method
140 int UtcDaliAnimatableMeshNew03(void)
141 {
142   TestApplication application;
143   Dali::AnimatableMesh::Faces faces;
144   try
145   {
146     AnimatableMesh mesh = AnimatableMesh::New(0, faces);
147     DALI_TEST_CHECK( !mesh );
148   }
149   catch (Dali::DaliException& e)
150   {
151     DALI_TEST_PRINT_ASSERT( e );
152     DALI_TEST_ASSERT(e, "numVertices > 0", TEST_LOCATION);
153   }
154   END_TEST;
155 }
156
157 // Negative test case for a method
158 int UtcDaliAnimatableMeshNew04(void)
159 {
160   TestApplication application;
161
162   Dali::AnimatableMesh::Faces faces;
163
164   try
165   {
166     AnimatableMesh mesh = AnimatableMesh::New(10, faces);
167     DALI_TEST_CHECK( !mesh );
168   }
169   catch (Dali::DaliException& e)
170   {
171     DALI_TEST_PRINT_ASSERT( e );
172     DALI_TEST_ASSERT(e, "faceIndices.size() > 0", TEST_LOCATION);
173   }
174   END_TEST;
175 }
176
177 // Negative test case for a method
178 int UtcDaliAnimatableMeshNew05(void)
179 {
180   TestApplication application;
181
182   Dali::AnimatableMesh::Faces faces;
183   CreateOutOfRangeFaces(faces, 10);
184
185   try
186   {
187     AnimatableMesh mesh = AnimatableMesh::New(10, faces);
188     DALI_TEST_CHECK( !mesh );
189   }
190   catch (Dali::DaliException& e)
191   {
192     DALI_TEST_PRINT_ASSERT( e );
193     DALI_TEST_ASSERT(e, "faceIndex < numVertices", TEST_LOCATION);
194   }
195   END_TEST;
196 }
197
198 // Negative test case for a method
199 int UtcDaliAnimatableMeshNew06(void)
200 {
201   TestApplication application;
202
203   Dali::AnimatableMesh::Faces faces;
204   CreateFaces(faces, 10);
205
206   try
207   {
208     AnimatableMesh mesh = AnimatableMesh::New(10, faces, Dali::Material() );
209     DALI_TEST_CHECK( !mesh );
210   }
211   catch (Dali::DaliException& e)
212   {
213     DALI_TEST_PRINT_ASSERT( e );
214     DALI_TEST_ASSERT(e, "material", TEST_LOCATION);
215   }
216   END_TEST;
217 }
218
219 int UtcDaliAnimatableMeshDownCast01(void)
220 {
221   TestApplication application;
222   tet_infoline("Testing Dali::AnimatableMesh::DownCast()");
223
224   Dali::AnimatableMesh::Faces faces;
225   CreateFaces(faces, 10);
226
227   AnimatableMesh mesh = AnimatableMesh::New(10, faces);
228   BaseHandle* bh = &mesh;
229
230   AnimatableMesh mesh2 = AnimatableMesh::DownCast(*bh);
231   DALI_TEST_CHECK( mesh2 );
232   END_TEST;
233 }
234
235 int UtcDaliAnimatableMeshDownCast02(void)
236 {
237   TestApplication application;
238   tet_infoline("Testing Dali::AnimatableMesh::DownCast()");
239
240   MeshData meshData;
241   CreateMeshData(meshData);
242   Mesh mesh = Mesh::New(meshData);
243   BaseHandle* bh = &mesh;
244
245   AnimatableMesh mesh2 = AnimatableMesh::DownCast(*bh);
246   DALI_TEST_CHECK( ! mesh2 );
247   END_TEST;
248 }
249
250 int UtcDaliAnimatableMeshGetPropertyIndex01(void)
251 {
252   TestApplication application;
253   tet_infoline("Testing Dali::AnimatableMesh::operator[]");
254   AnimatableMesh mesh = CreateMesh();
255
256   Property::Index i = mesh.GetPropertyIndex(0, AnimatableVertex::POSITION );
257   DALI_TEST_EQUALS( i, 0*3+0, TEST_LOCATION );
258
259   i = mesh.GetPropertyIndex(5, AnimatableVertex::POSITION );
260   DALI_TEST_EQUALS( i, 5*3+0, TEST_LOCATION );
261
262   i = mesh.GetPropertyIndex(7, AnimatableVertex::COLOR );
263   DALI_TEST_EQUALS( i, 7*3+1, TEST_LOCATION );
264
265   i = mesh.GetPropertyIndex(9, AnimatableVertex::TEXTURE_COORDS );
266   DALI_TEST_EQUALS( i, 9*3+2, TEST_LOCATION );
267   END_TEST;
268 }
269
270 int UtcDaliAnimatableMeshGetPropertyIndex02(void)
271 {
272   TestApplication application;
273   tet_infoline("Testing Dali::AnimatableMesh::GetPropertyIndex");
274
275   AnimatableMesh mesh = CreateMesh();
276   try
277   {
278     Property::Index i = mesh.GetPropertyIndex(12, AnimatableVertex::POSITION );
279     DALI_TEST_CHECK( i==0 );
280   }
281   catch (Dali::DaliException& e)
282   {
283     DALI_TEST_PRINT_ASSERT( e );
284     DALI_TEST_ASSERT(e, "index < GetNumberOfVertices()", TEST_LOCATION);
285   }
286   END_TEST;
287 }
288
289 int UtcDaliAnimatableMeshGetPropertyIndex03(void)
290 {
291   TestApplication application;
292   tet_infoline("Testing Dali::AnimatableMesh::GetPropertyIndex");
293
294   AnimatableMesh mesh = CreateMesh();
295   try
296   {
297     Property::Index i = mesh.GetPropertyIndex(12, AnimatableVertex::COLOR );
298     DALI_TEST_CHECK( i==0 );
299   }
300   catch (Dali::DaliException& e)
301   {
302     DALI_TEST_PRINT_ASSERT( e );
303     DALI_TEST_ASSERT(e, "index < GetNumberOfVertices()", TEST_LOCATION);
304   }
305   END_TEST;
306 }
307
308 int UtcDaliAnimatableMeshGetPropertyIndex04(void)
309 {
310   TestApplication application;
311   tet_infoline("Testing Dali::AnimatableMesh::GetPropertyIndexa");
312
313   AnimatableMesh mesh = CreateMesh();
314   try
315   {
316     Property::Index i = mesh.GetPropertyIndex(12342343, AnimatableVertex::TEXTURE_COORDS );
317     DALI_TEST_CHECK( i==0 );
318   }
319   catch (Dali::DaliException& e)
320   {
321     DALI_TEST_PRINT_ASSERT( e );
322     DALI_TEST_ASSERT(e, "index < GetNumberOfVertices()", TEST_LOCATION);
323   }
324   END_TEST;
325 }
326
327 int UtcDaliAnimatableMeshOperatorArray01(void)
328 {
329   TestApplication application;
330   tet_infoline("Testing Dali::AnimatableMesh::operator[]");
331
332   AnimatableMesh mesh = CreateMesh();
333   {
334     Vector3 initialPos1(0.0f, 200.0f, 0.0f);
335     Vector3 initialPos2(100.0f, 300.0f, 0.0f);
336
337     mesh[1].SetPosition(initialPos1);
338     mesh[3].SetPosition(initialPos2);
339
340     application.Render(0);
341     application.SendNotification();
342     application.Render(16);
343     application.SendNotification();
344     DALI_TEST_EQUALS( mesh[1].GetCurrentPosition(), initialPos1, TEST_LOCATION );
345
346     Vector3 pos = mesh[3].GetCurrentPosition();
347     DALI_TEST_EQUALS( pos, initialPos2, TEST_LOCATION );
348   }
349   END_TEST;
350 }
351
352 int UtcDaliAnimatableMeshOperatorArray02(void)
353 {
354   TestApplication application;
355   tet_infoline("Testing Dali::AnimatableMesh::operator[]");
356
357   AnimatableMesh mesh = CreateMesh();
358   try
359   {
360     mesh[20].SetPosition(Vector3(0.0f, 0.0f, 0.0f));
361   }
362   catch (Dali::DaliException& e)
363   {
364     DALI_TEST_PRINT_ASSERT( e );
365     DALI_TEST_ASSERT(e, "index < GetNumberOfVertices()", TEST_LOCATION);
366   }
367   END_TEST;
368 }
369
370 int UtcDaliAnimatableMeshAnimateVertex01(void)
371 {
372   TestApplication application;
373   tet_infoline("Testing Dali::AnimatableMesh Animating properties");
374
375   AnimatableMesh mesh = CreateMesh();
376   MeshActor meshActor = MeshActor::New(mesh);
377   Stage::GetCurrent().Add(meshActor);
378   {
379     mesh[0].SetPosition(Vector3(0.0f, 200.0f, 0.0f));
380     mesh[1].SetPosition(Vector3(100.0f, 300.0f, 0.0f));
381
382     Animation anim = Animation::New(1);
383     anim.AnimateBy(mesh.GetVertexProperty(0, AnimatableVertex::POSITION), Vector3(  0.0f, 100.0f, 0.0f));
384     anim.AnimateTo(mesh.GetVertexProperty(1, AnimatableVertex::POSITION), Vector3(100.0f,   0.0f, 0.0f));
385     anim.Play();
386
387     application.SendNotification();
388     application.Render(0);
389     application.Render(500);
390     application.SendNotification();
391
392     // 50% progress
393     DALI_TEST_EQUALS( mesh[0].GetCurrentPosition(), Vector3(  0.0f, 250.0f, 0.0f), TEST_LOCATION );
394     DALI_TEST_EQUALS( mesh[1].GetCurrentPosition(), Vector3(100.0f, 150.0f, 0.0f), TEST_LOCATION );
395
396     application.SendNotification();
397     application.Render(501);
398     application.SendNotification();
399
400     DALI_TEST_EQUALS( mesh[0].GetCurrentPosition(), Vector3(  0.0f, 300.0f, 0.0f), TEST_LOCATION );
401     DALI_TEST_EQUALS( mesh[1].GetCurrentPosition(), Vector3(100.0f,   0.0f, 0.0f), TEST_LOCATION );
402   }
403   END_TEST;
404 }
405
406 int UtcDaliAnimatableVertexSettersAndGetters(void)
407 {
408   TestApplication application;
409   tet_infoline("Testing Dali::AnimatableVertex constructors");
410   AnimatableMesh mesh = CreateMesh();
411   Vector3 v1Pos(0.0f, 200.0f, 0.0f);
412   Vector3 v2Pos(100.0f, 300.0f, 0.0f);
413   Vector2 uvs(0.1f, 0.2f);
414   mesh[0].SetPosition(v1Pos);
415   mesh[1].SetPosition(v2Pos);
416   mesh[2].SetColor(Color::BLACK);
417   mesh[3].SetTextureCoords(uvs);
418
419   application.SendNotification();
420   application.Render(16);
421   application.SendNotification();
422   application.Render(16);
423   application.SendNotification();
424
425   DALI_TEST_EQUALS(mesh[0].GetCurrentPosition(), v1Pos, TEST_LOCATION);
426   DALI_TEST_EQUALS(mesh[1].GetCurrentPosition(), v2Pos, TEST_LOCATION);
427   DALI_TEST_EQUALS(mesh[2].GetCurrentColor(), Color::BLACK, TEST_LOCATION);
428   DALI_TEST_EQUALS(mesh[3].GetCurrentTextureCoords(), uvs, TEST_LOCATION);
429   END_TEST;
430 }
431
432 int UtcDaliAnimatableMeshProperties(void)
433 {
434   TestApplication application;
435   AnimatableMesh mesh = CreateMesh();
436
437   Property::IndexContainer indices;
438   mesh.GetPropertyIndices( indices );
439   DALI_TEST_CHECK( ! indices.empty() );
440   DALI_TEST_EQUALS( indices.size(), mesh.GetPropertyCount(), TEST_LOCATION );
441   END_TEST;
442 }
443
444 int UtcDaliAnimatableMeshExceedVertices(void)
445 {
446   TestApplication application;
447
448   AnimatableMesh::Faces faces;
449   CreateFaces(faces, 10);
450
451   try
452   {
453     AnimatableMesh mesh = AnimatableMesh::New(3333334, faces);
454     tet_result( TET_FAIL );
455   }
456   catch ( DaliException& e )
457   {
458     DALI_TEST_ASSERT( e, "( numVertices * 3 ) < DEFAULT_PROPERTY_MAX_COUNT", TEST_LOCATION );
459   }
460   END_TEST;
461 }