Upload package dali_0.9.11.
[platform/core/uifw/dali-core.git] / automated-tests / TET / dali-test-suite / actors / utc-Dali-LightActor.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 using namespace Dali;
27
28 #include <mesh-builder.h>
29
30 static void Startup();
31 static void Cleanup();
32
33 extern "C" {
34   void (*tet_startup)() = Startup;
35   void (*tet_cleanup)() = Cleanup;
36 }
37
38 enum {
39   POSITIVE_TC_IDX = 0x01,
40   NEGATIVE_TC_IDX,
41 };
42
43 #define MAX_NUMBER_OF_TESTS 10000
44 extern "C" {
45   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
46 }
47
48 // Add test functionality for all APIs in the class (Positive and Negative)
49 TEST_FUNCTION( UtcDaliLightActorConstructorVoid,           POSITIVE_TC_IDX );
50 TEST_FUNCTION( UtcDaliLightActorConstructorRefObject,      POSITIVE_TC_IDX );
51 TEST_FUNCTION( UtcDaliLightActorDestructor,                POSITIVE_TC_IDX );
52 TEST_FUNCTION( UtcDaliLightActorNew,                       POSITIVE_TC_IDX );
53 TEST_FUNCTION( UtcDaliLightActorDownCast,                  POSITIVE_TC_IDX );
54 TEST_FUNCTION( UtcDaliLightActorDownCast2,                 NEGATIVE_TC_IDX );
55 TEST_FUNCTION( UtcDaliLightActorSetGetLight,               POSITIVE_TC_IDX );
56 TEST_FUNCTION( UtcDaliLightActorSetGetActive,              POSITIVE_TC_IDX );
57 TEST_FUNCTION( UtcDaliLightActorMeshTest,                  POSITIVE_TC_IDX );
58 TEST_FUNCTION( UtcDaliLightActorDefaultProperties,         POSITIVE_TC_IDX );
59 TEST_FUNCTION( UtcDaliLightActorPropertyIndices,           POSITIVE_TC_IDX );
60
61 // Called only once before first test is run.
62 static void Startup()
63 {
64   // THERE IS NO MAINLOOP IN THE TEST APPLICATION
65 }
66
67 // Called only once after last test is run
68 static void Cleanup()
69 {
70 }
71
72 static void UtcDaliLightActorConstructorVoid()
73 {
74   TestApplication application;
75   tet_infoline("Testing Dali::LightActor::LightActor() UtcDaliLightActorConstructorVoid");
76
77   LightActor actor;
78
79   DALI_TEST_CHECK(!actor);
80 }
81
82 static void UtcDaliLightActorConstructorRefObject()
83 {
84   TestApplication application;
85   tet_infoline("Testing Dali::LightActor::LightActor(Internal::LightActor*) UtcDaliLightActorConstructorRefObject");
86
87   LightActor actor(NULL);
88
89   DALI_TEST_CHECK(!actor);
90 }
91
92 static void UtcDaliLightActorDestructor()
93 {
94   // This test is achieve 100% line and function coverage
95   TestApplication application;
96   tet_infoline("Testing Dali::LightActor::~LightActor() UtcDaliLightActorDestructor");
97
98   LightActor* actor = new LightActor;
99
100   DALI_TEST_CHECK( ! *actor );
101
102   delete actor;
103
104   DALI_TEST_CHECK( true );
105 }
106
107 static void UtcDaliLightActorNew()
108 {
109   TestApplication application;
110   tet_infoline("Testing Dali::LightActor::New() UtcDaliLightActorNew");
111
112   LightActor actor = LightActor::New();
113
114   DALI_TEST_CHECK(actor);
115 }
116
117 static void UtcDaliLightActorDownCast()
118 {
119   TestApplication application;
120   tet_infoline("Testing Dali::LightActor::DownCast() UtcDaliLightActorDownCast");
121
122   LightActor actor1 = LightActor::New();
123   Actor anActor = Actor::New();
124   anActor.Add(actor1);
125
126   Actor child = anActor.GetChildAt(0);
127   LightActor lightActor = LightActor::DownCast(child);
128
129   DALI_TEST_CHECK(lightActor);
130
131   Light light;
132   light = Light::New( "TestLight" );
133   BaseHandle handle = light;
134
135   DALI_TEST_CHECK( Light::DownCast( handle ) );
136 }
137
138 static void UtcDaliLightActorDownCast2()
139 {
140   TestApplication application;
141   tet_infoline("Testing Dali::LightActor::DownCast2() UtcDaliLightActorDownCast2");
142
143   Actor actor1 = Actor::New();
144   Actor anActor = Actor::New();
145   anActor.Add(actor1);
146
147   Actor child = anActor.GetChildAt(0);
148   LightActor lightActor = LightActor::DownCast(child);
149   DALI_TEST_CHECK(!lightActor);
150
151   Actor unInitialzedActor;
152   lightActor = DownCast< LightActor >( unInitialzedActor );
153   DALI_TEST_CHECK(!lightActor);
154 }
155
156 static void UtcDaliLightActorSetGetLight()
157 {
158   TestApplication application;
159   tet_infoline( "Testing UtcDaliLightActorSetGetLight" );
160
161   try
162   {
163     LightActor lightActor = LightActor::New();
164     Light light1 = Light::New( "" );
165     light1.SetName( "TestLight" );
166     light1.SetDirection( Vector3::ZAXIS );
167     light1.SetSpotAngle( Vector2::YAXIS );
168     lightActor.SetLight( light1 );
169
170     Light light2 = lightActor.GetLight();
171
172     DALI_TEST_EQUALS( light1.GetName(), light2.GetName(), TEST_LOCATION );
173     DALI_TEST_EQUALS( light1.GetType(), light2.GetType(), TEST_LOCATION );
174     DALI_TEST_EQUALS( light1.GetFallOff(), light2.GetFallOff(), TEST_LOCATION );
175     DALI_TEST_EQUALS( light1.GetSpotAngle(), light2.GetSpotAngle(), TEST_LOCATION );
176     DALI_TEST_EQUALS( light1.GetAmbientColor(), light2.GetAmbientColor(), TEST_LOCATION );
177     DALI_TEST_EQUALS( light1.GetDiffuseColor(), light2.GetDiffuseColor(), TEST_LOCATION );
178     DALI_TEST_EQUALS( light1.GetSpecularColor(), light2.GetSpecularColor(), TEST_LOCATION );
179     DALI_TEST_EQUALS( light1.GetDirection(), light2.GetDirection(), TEST_LOCATION );
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     tet_result(TET_FAIL);
185   }
186   catch( ... )
187   {
188     tet_infoline( "Unknown exception." );
189     tet_result(TET_FAIL);
190   }
191 }
192
193 static void UtcDaliLightActorSetGetActive()
194 {
195   TestApplication application;
196   tet_infoline( "Testing UtcDaliLightActorSetGetActive" );
197
198   try
199   {
200     LightActor lightActor = LightActor::New();
201     lightActor.SetActive( true );
202
203     DALI_TEST_CHECK( lightActor.GetActive() );
204
205     lightActor.SetActive( false );
206
207     DALI_TEST_CHECK( !lightActor.GetActive() );
208
209     lightActor.SetActive( true );
210
211     DALI_TEST_CHECK( lightActor.GetActive() );
212
213     lightActor.SetActive( false );
214
215     DALI_TEST_CHECK( !lightActor.GetActive() );
216   }
217   catch( Dali::DaliException& e )
218   {
219     tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
220     tet_result(TET_FAIL);
221   }
222   catch( ... )
223   {
224     tet_infoline( "Unknown exception." );
225     tet_result(TET_FAIL);
226   }
227 }
228
229
230 static void UtcDaliLightActorMeshTest()
231 {
232   TestApplication application;
233   tet_infoline( "Testing UtcDaliLightActorMeshTest" );
234
235   try
236   {
237     Mesh mesh = ConstructMesh( 50 );
238     Actor actor = MeshActor::New( mesh );
239     Stage::GetCurrent().Add( actor );
240
241     actor.SetParentOrigin( ParentOrigin::CENTER );
242     actor.SetAnchorPoint( AnchorPoint::CENTER );
243     actor.SetPosition( 0.0f, 0.0f, 0.0f );
244
245     Light light = Light::New("Light");
246     light.SetType( POINT );
247     light.SetAmbientColor( Vector3( 0.22f, 0.33f, 0.44f ) );
248     light.SetDiffuseColor( Vector3( 0.55f, 0.66f, 0.77f) );
249     light.SetSpecularColor( Vector3( 0.88f, 0.99f, 0.11f) );
250     LightActor lightActor = LightActor::New();
251     lightActor.SetParentOrigin( ParentOrigin::CENTER );
252     lightActor.SetPosition( 0.f, 0.f, 100.0f );
253     lightActor.SetLight( light );
254     lightActor.SetName( light.GetName() );
255
256     Stage::GetCurrent().Add( lightActor );
257     lightActor.SetActive( true );
258
259     application.SendNotification();
260     application.Render();
261
262     // Test Ligh ambient.
263     DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uLight0.mAmbient", Vector3( 0.22f, 0.33f, 0.44f ) ) );
264
265     // Test Ligh diffuse.
266     DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uLight0.mDiffuse", Vector3( 0.55f, 0.66f, 0.77f ) ) );
267
268     // Test Ligh specular.
269     DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uLight0.mSpecular", Vector3( 0.88f, 0.99f, 0.11f ) ) );
270
271     // Test Opacity.
272     DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uMaterial.mOpacity", 0.76f ) );
273
274     // Test material Ambient color.
275     DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uMaterial.mAmbient", Vector4(0.2f, 1.0f, 0.6f, 1.0f) ) );
276
277     // Test material Diffuse color.
278     DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uMaterial.mDiffuse", Vector4(0.8f, 0.0f, 0.4f, 1.0f) ) );
279
280     // Test Specular color.
281     DALI_TEST_CHECK( application.GetGlAbstraction().CheckUniformValue( "uMaterial.mSpecular", Vector4(0.5f, 0.6f, 0.7f, 1.0f) ) );
282   }
283   catch( Dali::DaliException& e )
284   {
285     tet_printf( "Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str() );
286     tet_result(TET_FAIL);
287   }
288   catch( ... )
289   {
290     tet_infoline( "Unknown exception." );
291     tet_result(TET_FAIL);
292   }
293
294   DALI_TEST_CHECK( true );
295 }
296
297 static void UtcDaliLightActorDefaultProperties()
298 {
299   TestApplication application;
300   tet_infoline("Testing Dali::LightActor DefaultProperties");
301
302   LightActor actor = LightActor::New();
303
304   std::vector<Property::Index> indices ;
305   indices.push_back(LightActor::LIGHT_TYPE       );
306   indices.push_back(LightActor::ENABLE           );
307   indices.push_back(LightActor::FALL_OFF         );
308   indices.push_back(LightActor::SPOT_ANGLE       );
309   indices.push_back(LightActor::AMBIENT_COLOR    );
310   indices.push_back(LightActor::DIFFUSE_COLOR    );
311   indices.push_back(LightActor::SPECULAR_COLOR   );
312   indices.push_back(LightActor::DIRECTION        );
313
314   DALI_TEST_CHECK(actor.GetPropertyCount() == ( Actor::New().GetPropertyCount() + indices.size() ) );
315
316   for(std::vector<Property::Index>::iterator iter = indices.begin(); iter != indices.end(); ++iter)
317   {
318     DALI_TEST_CHECK( *iter == actor.GetPropertyIndex(actor.GetPropertyName(*iter)) );
319     DALI_TEST_CHECK( actor.IsPropertyWritable(*iter) );
320     DALI_TEST_CHECK( !actor.IsPropertyAnimatable(*iter) );
321     DALI_TEST_CHECK( actor.GetPropertyType(*iter) == actor.GetPropertyType(*iter) );  // just checking call succeeds
322   }
323
324   // set/get one of them
325   actor.GetLight().SetAmbientColor( Vector3( 0.f, 0.f, 0.f ) );
326   Vector3 col( 0.22f, 0.33f, 0.44f ) ;
327   DALI_TEST_CHECK(actor.GetLight().GetAmbientColor() != col);
328   actor.SetProperty(LightActor::AMBIENT_COLOR, col);
329   Property::Value v = actor.GetProperty(LightActor::AMBIENT_COLOR);
330   DALI_TEST_CHECK(v.GetType() == Property::VECTOR3);
331
332   DALI_TEST_CHECK(v.Get<Vector3>() == col);
333
334 }
335
336 void UtcDaliLightActorPropertyIndices()
337 {
338   TestApplication application;
339   Actor basicActor = Actor::New();
340   LightActor light = LightActor::New();
341
342   Property::IndexContainer indices;
343   light.GetPropertyIndices( indices );
344   DALI_TEST_CHECK( indices.size() > basicActor.GetPropertyCount() );
345   DALI_TEST_EQUALS( indices.size(), light.GetPropertyCount(), TEST_LOCATION );
346 }