Upload package dali_0.9.11.
[platform/core/uifw/dali-core.git] / automated-tests / TET / dali-test-suite / dynamics / utc-Dali-DynamicsBody.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
17 #include <iostream>
18
19 #include <stdlib.h>
20 #include <tet_api.h>
21
22 #include <dali/public-api/dali-core.h>
23
24 #include <dali-test-suite-utils.h>
25
26
27 using namespace Dali;
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 static void UtcDaliDynamicsBodyConstructor();
38 static void UtcDaliDynamicsBodyGetMass();
39 static void UtcDaliDynamicsBodyGetElasticity();
40 static void UtcDaliDynamicsBodySetLinearVelocity();
41 static void UtcDaliDynamicsBodySetAngularVelocity();
42 static void UtcDaliDynamicsBodySetKinematic();
43 static void UtcDaliDynamicsBodyIsKinematic();
44 static void UtcDaliDynamicsBodySetSleepEnabled();
45 static void UtcDaliDynamicsBodyGetSleepEnabled();
46 static void UtcDaliDynamicsBodyWakeUp();
47 static void UtcDaliDynamicsBodyAddAnchor();
48 static void UtcDaliDynamicsBodyConserveVolume();
49 static void UtcDaliDynamicsBodyConserveShape();
50
51 enum {
52   POSITIVE_TC_IDX = 0x01,
53   NEGATIVE_TC_IDX,
54 };
55
56 // Add test functionality for all APIs in the class (Positive and Negative)
57 extern "C" {
58   struct tet_testlist tet_testlist[] =
59   {
60    { UtcDaliDynamicsBodyConstructor,        POSITIVE_TC_IDX },
61    { UtcDaliDynamicsBodyGetMass,            POSITIVE_TC_IDX },
62    { UtcDaliDynamicsBodyGetElasticity,      POSITIVE_TC_IDX },
63    { UtcDaliDynamicsBodySetLinearVelocity,  POSITIVE_TC_IDX },
64    { UtcDaliDynamicsBodySetAngularVelocity, POSITIVE_TC_IDX },
65    { UtcDaliDynamicsBodySetKinematic,       POSITIVE_TC_IDX },
66    { UtcDaliDynamicsBodyIsKinematic,        POSITIVE_TC_IDX },
67    { UtcDaliDynamicsBodySetSleepEnabled,    POSITIVE_TC_IDX },
68    { UtcDaliDynamicsBodyGetSleepEnabled,    POSITIVE_TC_IDX },
69    { UtcDaliDynamicsBodyWakeUp,             POSITIVE_TC_IDX },
70    { UtcDaliDynamicsBodyAddAnchor,          POSITIVE_TC_IDX },
71    { UtcDaliDynamicsBodyConserveVolume,     POSITIVE_TC_IDX },
72    { UtcDaliDynamicsBodyConserveShape,      POSITIVE_TC_IDX },
73    { NULL, 0 }
74   };
75 }
76
77 // Called only once before first test is run.
78 void Startup()
79 {
80 }
81
82 // Called only once after last test is run
83 void Cleanup()
84 {
85 }
86
87 void UtcDaliDynamicsBodyConstructor()
88 {
89   tet_infoline("UtcDaliDynamicsBodyConstructor - DynamicsBody::DynamicsBody()");
90
91   TestApplication application;
92
93   // start up
94   application.SendNotification();
95   application.Render();
96   application.Render();
97
98   // Default constructor - create an uninitialized handle
99   DynamicsBody body;
100   DALI_TEST_CHECK( !body );
101
102   // create world and actor
103   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
104   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
105
106   if( !world )
107   {
108     // cannot create dynamics world, log failure and exit
109     DALI_TEST_CHECK( false );
110     return;
111   }
112
113   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
114   Actor actor(Actor::New());
115
116   // enable dynamics on the actor to create the Dynamicsbody
117   actor.EnableDynamics(bodyConfig);
118
119   // initialize handle
120   body = actor.GetDynamicsBody();
121
122   DALI_TEST_CHECK( body );
123 }
124
125 void UtcDaliDynamicsBodyGetMass()
126 {
127   TestApplication application;
128
129   // start up
130   application.SendNotification();
131   application.Render();
132   application.Render();
133
134   const float testMass = 1.23f;
135
136   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
137   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
138
139   if( !world )
140   {
141     // cannot create dynamics world, log failure and exit
142     DALI_TEST_CHECK( false );
143     return;
144   }
145
146   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
147   bodyConfig.SetMass(testMass);
148   Actor actor(Actor::New());
149
150   // enable dynamics on the actor to create the Dynamicsbody
151   actor.EnableDynamics(bodyConfig);
152
153   tet_infoline("UtcDaliDynamicsBodyGetMass - DynamicsBody::GetMass");
154   DALI_TEST_EQUALS( testMass, actor.GetDynamicsBody().GetMass(), Math::MACHINE_EPSILON_0, TEST_LOCATION );
155 }
156
157 void UtcDaliDynamicsBodyGetElasticity()
158 {
159   TestApplication application;
160
161   // start up
162   application.SendNotification();
163   application.Render();
164   application.Render();
165
166   const float testElasticity = 1.23f;
167
168   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
169   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
170
171   if( !world )
172   {
173     // cannot create dynamics world, log failure and exit
174     DALI_TEST_CHECK( false );
175     return;
176   }
177
178   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
179   bodyConfig.SetElasticity(testElasticity);
180   Actor actor(Actor::New());
181
182   // enable dynamics on the actor to create the Dynamicsbody
183   actor.EnableDynamics(bodyConfig);
184
185   tet_infoline("UtcDaliDynamicsBodyGetMass - DynamicsBody::GetElasticity");
186   DALI_TEST_EQUALS( testElasticity, actor.GetDynamicsBody().GetElasticity(), Math::MACHINE_EPSILON_0, TEST_LOCATION );
187 }
188
189 void UtcDaliDynamicsBodySetLinearVelocity()
190 {
191   tet_infoline("UtcDaliDynamicsBodySetLinearVelocity - DynamicsBody::SetLinearVelocity");
192
193   TestApplication application;
194
195   // start up
196   application.SendNotification();
197   application.Render();
198   application.Render();
199
200   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
201   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
202
203   if( !world )
204   {
205     // cannot create dynamics world, log failure and exit
206     DALI_TEST_CHECK( false );
207     return;
208   }
209
210   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
211   Actor actor(Actor::New());
212
213   // enable dynamics on the actor to create the Dynamicsbody
214   actor.EnableDynamics(bodyConfig);
215
216   DynamicsBody body(actor.GetDynamicsBody());
217   body.SetLinearVelocity(Vector3::ONE);
218
219   DALI_TEST_CHECK( true );
220 }
221
222 void UtcDaliDynamicsBodySetAngularVelocity()
223 {
224   tet_infoline("UtcDaliDynamicsBodySetAngularVelocity - DynamicsBody::SetAngularVelocity");
225
226   TestApplication application;
227
228   // start up
229   application.SendNotification();
230   application.Render();
231   application.Render();
232
233   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
234   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
235
236   if( !world )
237   {
238     // cannot create dynamics world, log failure and exit
239     DALI_TEST_CHECK( false );
240     return;
241   }
242
243   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
244   Actor actor(Actor::New());
245
246   // enable dynamics on the actor to create the Dynamicsbody
247   actor.EnableDynamics(bodyConfig);
248
249   DynamicsBody body(actor.GetDynamicsBody());
250   body.SetAngularVelocity(Vector3::ONE);
251
252   DALI_TEST_CHECK( true );
253 }
254
255 void UtcDaliDynamicsBodySetKinematic()
256 {
257   TestApplication application;
258
259   // start up
260   application.SendNotification();
261   application.Render();
262   application.Render();
263
264   const float testMass = 1.0f;
265   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
266   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
267
268   if( !world )
269   {
270     // cannot create dynamics world, log failure and exit
271     DALI_TEST_CHECK( false );
272     return;
273   }
274
275   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
276   bodyConfig.SetMass(testMass);
277   Actor actor(Actor::New());
278
279   // enable dynamics on the actor to create the Dynamicsbody
280   actor.EnableDynamics(bodyConfig);
281
282   DynamicsBody body(actor.GetDynamicsBody());
283
284   DALI_TEST_EQUALS( testMass, body.GetMass(), Math::MACHINE_EPSILON_0, TEST_LOCATION );
285
286   tet_infoline("UtcDaliDynamicsBodySetKinematic - DynamicsBody::SetKinematic(true)");
287   body.SetKinematic(true);
288
289   DALI_TEST_CHECK( body.IsKinematic() );
290   DALI_TEST_EQUALS( 0.0f, body.GetMass(), Math::MACHINE_EPSILON_0, TEST_LOCATION );
291
292   tet_infoline("UtcDaliDynamicsBodySetKinematic - DynamicsBody::SetKinematic(false)");
293   body.SetKinematic(false);
294   DALI_TEST_CHECK( !body.IsKinematic() );
295   DALI_TEST_EQUALS( testMass, body.GetMass(), Math::MACHINE_EPSILON_0, TEST_LOCATION );
296 }
297
298 void UtcDaliDynamicsBodyIsKinematic()
299 {
300   TestApplication application;
301
302   // start up
303   application.SendNotification();
304   application.Render();
305   application.Render();
306
307   const float testMass = 1.0f;
308   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
309   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
310
311   if( !world )
312   {
313     // cannot create dynamics world, log failure and exit
314     DALI_TEST_CHECK( false );
315     return;
316   }
317
318   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
319   bodyConfig.SetMass(testMass);
320   Actor actor(Actor::New());
321
322   // enable dynamics on the actor to create the Dynamicsbody
323   actor.EnableDynamics(bodyConfig);
324
325   DynamicsBody body(actor.GetDynamicsBody());
326
327   DALI_TEST_EQUALS( testMass, body.GetMass(), Math::MACHINE_EPSILON_0, TEST_LOCATION );
328
329   tet_infoline("UtcDaliDynamicsBodySetKinematic - DynamicsBody::IsSetKinematic");
330   body.SetKinematic(true);
331
332   DALI_TEST_CHECK( body.IsKinematic() );
333   body.SetKinematic(false);
334   DALI_TEST_CHECK( !body.IsKinematic() );
335 }
336
337 void UtcDaliDynamicsBodySetSleepEnabled()
338 {
339   tet_infoline("UtcDaliDynamicsBodySetSleepEnabled");
340
341   TestApplication application;
342
343   // start up
344   application.SendNotification();
345   application.Render();
346   application.Render();
347
348   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
349   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
350
351   if( !world )
352   {
353     // cannot create dynamics world, log failure and exit
354     DALI_TEST_CHECK( false );
355     return;
356   }
357
358   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
359   Actor actor(Actor::New());
360
361   // enable dynamics on the actor to create the Dynamicsbody
362   actor.EnableDynamics(bodyConfig);
363
364   DynamicsBody body(actor.GetDynamicsBody());
365
366   // SleepEnabled true by default
367   DALI_TEST_CHECK( body.GetSleepEnabled() );
368   body.SetSleepEnabled(false);
369   DALI_TEST_CHECK( !body.GetSleepEnabled() );
370 }
371
372 void UtcDaliDynamicsBodyGetSleepEnabled()
373 {
374   tet_infoline("UtcDaliDynamicsBodyGetSleepEnabled");
375
376   TestApplication application;
377
378   // start up
379   application.SendNotification();
380   application.Render();
381   application.Render();
382
383   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
384   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
385
386   if( !world )
387   {
388     // cannot create dynamics world, log failure and exit
389     DALI_TEST_CHECK( false );
390     return;
391   }
392
393   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
394   Actor actor(Actor::New());
395
396   // enable dynamics on the actor to create the Dynamicsbody
397   actor.EnableDynamics(bodyConfig);
398
399   DynamicsBody body(actor.GetDynamicsBody());
400
401   // SleepEnabled true by default
402   DALI_TEST_CHECK( body.GetSleepEnabled() );
403   body.SetSleepEnabled(false);
404   DALI_TEST_CHECK( !body.GetSleepEnabled() );
405 }
406
407 void UtcDaliDynamicsBodyWakeUp()
408 {
409   tet_infoline("UtcDaliDynamicsBodyWakeUp");
410
411   TestApplication application;
412
413   // start up
414   application.SendNotification();
415   application.Render();
416   application.Render();
417
418   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
419   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
420
421   if( !world )
422   {
423     // cannot create dynamics world, log failure and exit
424     DALI_TEST_CHECK( false );
425     return;
426   }
427
428   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
429   Actor actor(Actor::New());
430
431   // enable dynamics on the actor to create the Dynamicsbody
432   actor.EnableDynamics(bodyConfig);
433
434   DynamicsBody body(actor.GetDynamicsBody());
435
436   body.WakeUp();
437   DALI_TEST_CHECK( true );
438 }
439
440 void UtcDaliDynamicsBodyAddAnchor()
441 {
442   tet_infoline("UtcDaliDynamicsBodyAddAnchor - DynamicsBody::AddAnchor()");
443
444   TestApplication application;
445
446   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
447   worldConfig.SetType(DynamicsWorldConfig::SOFT);
448   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
449
450   if( !world )
451   {
452     // cannot create dynamics world, log failure and exit
453     DALI_TEST_CHECK( false );
454     return;
455   }
456
457   Actor rootActor(Actor::New());
458   world.SetRootActor(rootActor);
459   Stage::GetCurrent().Add(rootActor);
460
461   DynamicsBodyConfig softConfig( DynamicsBodyConfig::New() );
462   softConfig.SetType(DynamicsBodyConfig::SOFT);
463   Mesh mesh(Mesh::NewPlane(10.0f, 10.0f, 10, 10));
464   DynamicsShape meshShape(DynamicsShape::NewMesh(mesh));
465   softConfig.SetShape( meshShape );
466   softConfig.SetMass(1.0f);
467   MeshActor softActor(MeshActor::New(mesh));
468
469   rootActor.Add(softActor);
470   softActor.EnableDynamics(softConfig);
471   DynamicsBody softBody(softActor.GetDynamicsBody());
472
473   DynamicsBodyConfig anchorConfig(DynamicsBodyConfig::New());
474   anchorConfig.SetMass(0.0f);
475   Actor anchor(Actor::New());
476   rootActor.Add(anchor);
477   anchor.EnableDynamics(anchorConfig);
478   DynamicsBody anchorBody(anchor.GetDynamicsBody());
479   anchorBody.SetKinematic(true);
480   try
481   {
482     softBody.AddAnchor(0, anchorBody, false);
483
484     DALI_TEST_CHECK(true)
485   }
486   catch(Dali::DaliException& e)
487   {
488     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
489     DALI_TEST_CHECK( false );
490   }
491
492   rootActor.Remove(softActor);
493   rootActor.Remove(anchor);
494   Stage::GetCurrent().Remove(rootActor);
495   softActor.DisableDynamics();
496   anchor.DisableDynamics();
497 }
498
499 void UtcDaliDynamicsBodyConserveVolume()
500 {
501   tet_infoline("UtcDaliDynamicsBodyConserveVolume");
502
503   TestApplication application;
504
505   // start up
506   application.SendNotification();
507   application.Render();
508   application.Render();
509
510   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
511   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
512
513   if( !world )
514   {
515     // cannot create dynamics world, log failure and exit
516     DALI_TEST_CHECK( false );
517     return;
518   }
519
520   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
521   Actor actor(Actor::New());
522
523   // enable dynamics on the actor to create the Dynamicsbody
524   actor.EnableDynamics(bodyConfig);
525
526   DynamicsBody body(actor.GetDynamicsBody());
527
528   body.ConserveVolume(false);
529   DALI_TEST_CHECK( true );
530 }
531
532 void UtcDaliDynamicsBodyConserveShape()
533 {
534   tet_infoline("UtcDaliDynamicsBodyConserveShape");
535
536   TestApplication application;
537
538   // start up
539   application.SendNotification();
540   application.Render();
541   application.Render();
542
543   DynamicsWorldConfig worldConfig(DynamicsWorldConfig::New());
544   DynamicsWorld world( Stage::GetCurrent().InitializeDynamics(worldConfig) );
545
546   if( !world )
547   {
548     // cannot create dynamics world, log failure and exit
549     DALI_TEST_CHECK( false );
550     return;
551   }
552
553   DynamicsBodyConfig bodyConfig(DynamicsBodyConfig::New());
554   Actor actor(Actor::New());
555
556   // enable dynamics on the actor to create the Dynamicsbody
557   actor.EnableDynamics(bodyConfig);
558
559   DynamicsBody body(actor.GetDynamicsBody());
560
561   body.ConserveShape(false);
562   DALI_TEST_CHECK( true );
563 }
564