04909bd2482163f0e2404ce13f4ba1c90c119021
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-test-suite / shader-effects / utc-Dali-DisplacementEffect.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 <tet_api.h>
22
23 #include <dali/public-api/dali-core.h>
24 #include <dali-toolkit/dali-toolkit.h>
25
26 #include <dali-toolkit-test-suite-utils.h>
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 namespace
32 {
33 const char* TEST_IMAGE_FILE_NAME = DALI_IMAGE_DIR "gallery_image_01.jpg";
34 } // namespace
35
36 static void Startup();
37 static void Cleanup();
38
39 extern "C" {
40   void (*tet_startup)() = Startup;
41   void (*tet_cleanup)() = Cleanup;
42 }
43
44 static void UtcDaliDisplacementEffectUninitialized();
45 static void UtcDaliDisplacementEffectNew();
46 static void UtcDaliDisplacementEffectPropertyNames();
47 static void UtcDaliDisplacementEffectTestSetProperty();
48
49 enum {
50   POSITIVE_TC_IDX = 0x01,
51   NEGATIVE_TC_IDX,
52 };
53
54 // Add test functionality for all APIs in the class (Positive and Negative)
55 extern "C" {
56   struct tet_testlist tet_testlist[] = {
57       { UtcDaliDisplacementEffectUninitialized, NEGATIVE_TC_IDX },
58       { UtcDaliDisplacementEffectNew, POSITIVE_TC_IDX },
59       { UtcDaliDisplacementEffectPropertyNames, POSITIVE_TC_IDX },
60       { UtcDaliDisplacementEffectTestSetProperty, POSITIVE_TC_IDX },
61     { NULL, 0 }
62   };
63 }
64
65 // Called only once before first test is run.
66 static void Startup()
67 {
68 }
69
70 // Called only once after last test is run
71 static void Cleanup()
72 {
73 }
74
75 // Negative test case for a method
76 static void UtcDaliDisplacementEffectUninitialized()
77 {
78   ToolkitTestApplication application;
79   tet_infoline("UtcDaliDisplacementEffectUninitialized");
80
81   Toolkit::DisplacementEffect effect;
82
83   try
84   {
85     // New() must be called to create a GaussianBlurView or it wont be valid.
86     effect.SetStateProperty( 1.0f );
87     DALI_TEST_CHECK( false );
88   }
89   catch (Dali::DaliException& e)
90   {
91     // Tests that a negative test of an assertion succeeds
92     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
93     DALI_TEST_CHECK(!effect);
94   }
95 }
96
97 // Positive test case for a method
98 static void UtcDaliDisplacementEffectNew()
99 {
100   ToolkitTestApplication application;
101   tet_infoline("UtcDaliDisplacementEffectNew");
102
103   Toolkit::DisplacementEffect effect = Toolkit::DisplacementEffect::New(Toolkit::DisplacementEffect::DISPLACED);
104   DALI_TEST_CHECK( effect );
105
106   Toolkit::DisplacementEffect effect2 = Toolkit::DisplacementEffect::New(Toolkit::DisplacementEffect::FIXED);
107   DALI_TEST_CHECK( effect2 );
108 }
109
110 // Positive test case for a method
111 static void UtcDaliDisplacementEffectPropertyNames()
112 {
113   ToolkitTestApplication application;
114   tet_infoline("UtcDaliDisplacementEffectPropertyNames");
115
116   Toolkit::DisplacementEffect effect = Toolkit::DisplacementEffect::New(Toolkit::DisplacementEffect::DISPLACED);
117   DALI_TEST_CHECK( effect );
118
119   // Check the names, this names are used in the shaders code,
120   // if they change the shader code has to be updated
121   DALI_TEST_EQUALS( effect.GetLightDirectionPropertyName(), "uLightDirection", TEST_LOCATION );
122   DALI_TEST_EQUALS( effect.GetAmbientLightColorPropertyName(), "uAmbientLightColor", TEST_LOCATION );
123   DALI_TEST_EQUALS( effect.GetDiffuseLightColorPropertyName(), "uDiffuseLightColor", TEST_LOCATION );
124   DALI_TEST_EQUALS( effect.GetLightingMultiplierPropertyName(), "uLightMultiplier", TEST_LOCATION );
125   DALI_TEST_EQUALS( effect.GetStatePropertyName(), "uState", TEST_LOCATION );
126   DALI_TEST_EQUALS( effect.GetHeightScalePropertyName(), "uHightScale", TEST_LOCATION );
127   DALI_TEST_EQUALS( effect.GetFixedNormalPropertyName(), "uFixedNormal", TEST_LOCATION );
128 }
129
130 // Positive test case for a method
131 static void UtcDaliDisplacementEffectTestSetProperty()
132 {
133   ToolkitTestApplication application;
134   tet_infoline("UtcDaliDisplacementEffectTestSetProperty");
135
136   Toolkit::DisplacementEffect effect = Toolkit::DisplacementEffect::New(Toolkit::DisplacementEffect::DISPLACED);
137   DALI_TEST_CHECK( effect );
138
139   ImageActor actor = ImageActor::New( Image::New(TEST_IMAGE_FILE_NAME) );
140   actor.SetSize( 100.0f, 100.0f );
141   actor.SetShaderEffect( effect );
142   Stage::GetCurrent().Add( actor );
143
144   Toolkit::DisplacementEffect effect2 = Toolkit::DisplacementEffect::New(Toolkit::DisplacementEffect::FIXED);
145   DALI_TEST_CHECK( effect );
146
147   ImageActor actor2 = ImageActor::New( Image::New(TEST_IMAGE_FILE_NAME) );
148   actor2.SetSize( 100.0f, 100.0f );
149   actor2.SetShaderEffect( effect2 );
150   Stage::GetCurrent().Add( actor2 );
151
152   Vector3 testVector3 = Vector3(45.0f, 55.0f, 65.0f);
153   float testFloat = 0.623f;
154   effect.SetLightDirection(testVector3);
155   effect.SetAmbientLightColorProperty(testVector3);
156   effect.SetDiffuseLightColorProperty(testVector3);
157   effect.SetStateProperty(testFloat);
158   effect.SetLightingMultiplierProperty(testFloat);
159   effect.SetHeightScaleProperty(testFloat);
160
161   effect2.SetFixedNormalProperty(testVector3);
162
163   application.SendNotification();
164   application.Render(0);
165   application.SendNotification();
166   application.Render();
167
168   DALI_TEST_EQUALS( effect.GetProperty( effect.GetPropertyIndex( effect.GetLightDirectionPropertyName() ) ).Get<Vector3>(), testVector3, TEST_LOCATION );
169   DALI_TEST_EQUALS( effect.GetProperty( effect.GetPropertyIndex( effect.GetAmbientLightColorPropertyName() ) ).Get<Vector3>(), testVector3, TEST_LOCATION );
170   DALI_TEST_EQUALS( effect.GetProperty( effect.GetPropertyIndex( effect.GetDiffuseLightColorPropertyName() ) ).Get<Vector3>(), testVector3, TEST_LOCATION );
171   DALI_TEST_EQUALS( effect.GetProperty( effect.GetPropertyIndex( effect.GetStatePropertyName().c_str() ) ).Get<float>(), testFloat, TEST_LOCATION );
172   DALI_TEST_EQUALS( effect.GetProperty( effect.GetPropertyIndex( effect.GetLightingMultiplierPropertyName().c_str() ) ).Get<float>(), testFloat, TEST_LOCATION );
173   DALI_TEST_EQUALS( effect.GetProperty( effect.GetPropertyIndex( effect.GetHeightScalePropertyName().c_str() ) ).Get<float>(), testFloat, TEST_LOCATION );
174
175   Vector3 normalizedVector3(testVector3);
176   normalizedVector3.Normalize();
177   DALI_TEST_EQUALS( effect2.GetProperty( effect2.GetPropertyIndex( effect2.GetFixedNormalPropertyName() ) ).Get<Vector3>(), normalizedVector3, TEST_LOCATION );
178 }