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