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