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