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