AlphaFunction refactoring
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-AlphaFunction.cpp
1 /*
2  * Copyright (c) 2015 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 float customAlphaFunction( float progress )
27 {
28   return progress;
29 }
30
31 void utc_dali_alpha_function_startup(void)
32 {
33   test_return_value = TET_UNDEF;
34 }
35
36 void utc_dali_alpha_function_cleanup(void)
37 {
38   test_return_value = TET_PASS;
39 }
40
41 int UtcDaliAlphaFunctionDefaultConstructorP(void)
42 {
43   TestApplication application;
44   AlphaFunction alpha;
45
46   //Should return the default alpha function
47   DALI_TEST_EQUALS( alpha.GetBuiltinFunction(), AlphaFunction::DEFAULT, TEST_LOCATION);
48
49   //Check the mode is BUILTIN_FUNCTION
50   DALI_TEST_EQUALS( alpha.GetMode(), AlphaFunction::BUILTIN_FUNCTION, TEST_LOCATION);
51
52   END_TEST;
53 }
54
55 int UtcDaliAlphaFunctionConstructorFromBuiltinP(void)
56 {
57   TestApplication application;
58
59   //Construct the alpha function with a built-in function
60   AlphaFunction alpha( AlphaFunction::EASE_IN_OUT);
61
62   //Check if the built-in alpha function is EASE_IN_OUT
63   DALI_TEST_EQUALS( alpha.GetBuiltinFunction(), AlphaFunction::EASE_IN_OUT, TEST_LOCATION);
64
65   //Check the mode is BUILTIN_FUNCTION
66   DALI_TEST_EQUALS( alpha.GetMode(), AlphaFunction::BUILTIN_FUNCTION, TEST_LOCATION);
67
68   END_TEST;
69 }
70
71 int UtcDaliAlphaFunctionConstructorFromFunctionPointerdP(void)
72 {
73   TestApplication application;
74
75   //Construct the alpha function with a function pointer
76   AlphaFunction alpha( &customAlphaFunction );
77
78   //Check that the custom function points to the custom alpha function
79   DALI_TEST_EQUALS( alpha.GetCustomFunction(), &customAlphaFunction, TEST_LOCATION);
80
81   //Check the mode is CUSTOM_FUNCTION
82   DALI_TEST_EQUALS( alpha.GetMode(), AlphaFunction::CUSTOM_FUNCTION, TEST_LOCATION);
83
84
85   END_TEST;
86 }
87
88 int UtcDaliAlphaFunctionConstructorFromControlPointsP(void)
89 {
90   TestApplication application;
91
92   //Construct the alpha function with two control points
93   Vector2 controlPoint0 = Vector2(0.0f,1.0f);
94   Vector2 controlPoint1 = Vector2(1.0f,0.0f);
95   AlphaFunction alpha(controlPoint0,controlPoint1);
96
97   //Check if the control points have the correct value
98   Vector4 controlPoints = alpha.GetBezierControlPoints();
99   DALI_TEST_EQUALS( Vector2(controlPoints.x,controlPoints.y), controlPoint0, TEST_LOCATION);
100   DALI_TEST_EQUALS( Vector2(controlPoints.z,controlPoints.w), controlPoint1, TEST_LOCATION);
101
102   //Check the mode is BEZIER
103   DALI_TEST_EQUALS( alpha.GetMode(), AlphaFunction::BEZIER, TEST_LOCATION);
104
105   END_TEST;
106 }
107
108 int UtcDaliAlphaFunctionConstructorFromControlPointsN(void)
109 {
110   TestApplication application;
111
112   //Construct the alpha function with two control points
113   Vector2 controlPoint0 = Vector2(-10.0f,1.0f);
114   Vector2 controlPoint1 = Vector2(10.0f,0.0f);
115   AlphaFunction alpha(controlPoint0,controlPoint1);
116
117   //x components of the control points should have been clamped to [0,1] to ensure the curve is monotonic
118   Vector4 controlPoints = alpha.GetBezierControlPoints();
119   DALI_TEST_EQUALS( Vector2(controlPoints.x,controlPoints.y), Vector2(0.0f,1.0f), TEST_LOCATION);
120   DALI_TEST_EQUALS( Vector2(controlPoints.z,controlPoints.w), Vector2(1.0f,0.0f), TEST_LOCATION);
121
122   //Check the mode is BEZIER
123   DALI_TEST_EQUALS( alpha.GetMode(), AlphaFunction::BEZIER, TEST_LOCATION);
124
125   END_TEST;
126 }
127
128 int UtcDaliAlphaFunctionGetBuiltinFunctionP(void)
129 {
130   TestApplication application;
131   AlphaFunction alpha( AlphaFunction::EASE_IN);
132
133   //Check if the builtin alpha function is EASE_IN
134   DALI_TEST_EQUALS( alpha.GetBuiltinFunction(), AlphaFunction::EASE_IN, TEST_LOCATION);
135
136   //Check the mode is BUILTIN_FUNCTION
137   DALI_TEST_EQUALS( alpha.GetMode(), AlphaFunction::BUILTIN_FUNCTION, TEST_LOCATION);
138
139   END_TEST;
140 }
141
142 int UtcDaliAlphaFunctionGetCustomFunctionP(void)
143 {
144   TestApplication application;
145   AlphaFunction alpha( &customAlphaFunction );
146
147   //Check that the custom function points to the custom alpha function
148   DALI_TEST_EQUALS( alpha.GetCustomFunction(), &customAlphaFunction, TEST_LOCATION);
149
150   //Check the mode is CUSTOM_FUNCTION
151   DALI_TEST_EQUALS( alpha.GetMode(), AlphaFunction::CUSTOM_FUNCTION, TEST_LOCATION);
152
153   END_TEST;
154 }
155
156 int UtcDaliAlphaFunctionGetControlPointsFunctionP(void)
157 {
158   TestApplication application;
159
160   Vector2 controlPoint0 = Vector2(0.0f,1.0f);
161   Vector2 controlPoint1 = Vector2(1.0f,0.0f);
162   AlphaFunction alpha( controlPoint0,controlPoint1 );
163
164   //Check if the control points have the correct value
165   Vector4 controlPoints = alpha.GetBezierControlPoints();
166   DALI_TEST_EQUALS( Vector2(controlPoints.x,controlPoints.y), controlPoint0, TEST_LOCATION);
167   DALI_TEST_EQUALS( Vector2(controlPoints.z,controlPoints.w), controlPoint1, TEST_LOCATION);
168
169   //Check the mode is BEZIER
170   DALI_TEST_EQUALS( alpha.GetMode(), AlphaFunction::BEZIER, TEST_LOCATION);
171
172   END_TEST;
173 }
174
175 int UtcDaliAlphaFunctionGetModeP(void)
176 {
177   TestApplication application;
178
179   //Create alpha function using a built-in function
180   AlphaFunction alphaBuiltin( AlphaFunction::EASE_IN);
181
182   //Check the mode is BUILTIN_FUNCTION
183   DALI_TEST_EQUALS( alphaBuiltin.GetMode(), AlphaFunction::BUILTIN_FUNCTION, TEST_LOCATION);
184
185   //Create alpha function with pointer to function
186   AlphaFunction alphaCustom( &customAlphaFunction );
187   //Check the mode is CUSTOM_FUNCTION
188   DALI_TEST_EQUALS( alphaCustom.GetMode(), AlphaFunction::CUSTOM_FUNCTION, TEST_LOCATION);
189
190   //Create alpha function with control points
191   Vector2 controlPoint0 = Vector2(0.0f,1.0f);
192   Vector2 controlPoint1 = Vector2(1.0f,0.0f);
193   AlphaFunction alphaBezier( controlPoint0,controlPoint1 );
194   //Check the mode is BEZIER
195   DALI_TEST_EQUALS( alphaBezier.GetMode(), AlphaFunction::BEZIER, TEST_LOCATION);
196
197   END_TEST;
198 }
199
200 int UtcDaliAlphaFunctionBezier(void)
201 {
202   TestApplication application;
203
204   Actor actor = Actor::New();
205
206   // Register a float property
207   float startValue(0.0f);
208   Property::Index index = actor.RegisterProperty( "test-property", startValue );
209   Stage::GetCurrent().Add(actor);
210   DALI_TEST_EQUALS( actor.GetProperty<float>(index), startValue, TEST_LOCATION );
211
212   // Build the animation
213   float durationSeconds(1.0f);
214   Animation animation = Animation::New(durationSeconds);
215   float targetValue(1.0f);
216
217   Vector2 controlPoint0 = Vector2(0.25f,0.5f);
218   Vector2 controlPoint1 = Vector2(0.75f,0.5f);
219   animation.AnimateTo(Property(actor, "test-property"), targetValue, AlphaFunction(controlPoint0,controlPoint1));
220
221   // Start the animation
222   animation.Play();
223
224   application.SendNotification();
225   application.Render(static_cast<unsigned int>(durationSeconds*200.0f)/* 20% progress */);
226   application.SendNotification();
227   float epsilon(0.01f);
228   DALI_TEST_EQUALS( actor.GetProperty<float>(index), 0.271964f, epsilon, TEST_LOCATION );
229
230   application.SendNotification();
231   application.Render(static_cast<unsigned int>(durationSeconds*200.0f) + 1u/*40% progress*/);
232   application.SendNotification();
233   DALI_TEST_EQUALS( actor.GetProperty<float>(index), 0.432387f, epsilon, TEST_LOCATION );
234
235   application.SendNotification();
236   application.Render(static_cast<unsigned int>(durationSeconds*200.0f) + 1u/*60% progress*/);
237   application.SendNotification();
238   DALI_TEST_EQUALS( actor.GetProperty<float>(index), 0.567613f, epsilon, TEST_LOCATION );
239
240   application.SendNotification();
241   application.Render(static_cast<unsigned int>(durationSeconds*200.0f) + 1u/*80% progress*/);
242   application.SendNotification();
243   DALI_TEST_EQUALS( actor.GetProperty<float>(index), 0.728037f, epsilon, TEST_LOCATION );
244
245   application.SendNotification();
246   application.Render(static_cast<unsigned int>(durationSeconds*200.0f) + 1u/*just beyond the animation duration*/);
247   application.SendNotification();
248   DALI_TEST_EQUALS( actor.GetProperty<float>(index), targetValue, TEST_LOCATION );
249
250   END_TEST;
251 }
252