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