8fc8fbaae01091d70062d999ddd9a578574978c0
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-Fade.cpp
1 /*
2  * Copyright (c) 2021 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 #include <stdlib.h>
20 #include <dali-toolkit-test-suite-utils.h>
21 #include <dali-toolkit/dali-toolkit.h>
22 #include <dali/devel-api/actors/actor-devel.h>
23 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
24 #include <dali-toolkit/public-api/transition/transition-set.h>
25 #include <dali-toolkit/public-api/transition/transition-base.h>
26 #include <dali-toolkit/public-api/transition/fade.h>
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 // Functor to test whether a Finish signal is emitted
32 struct TransitionFinishCheck
33 {
34   TransitionFinishCheck(bool& signalReceived)
35   : mSignalReceived(signalReceived)
36   {
37   }
38
39   void operator()(TransitionSet& transitionSet)
40   {
41     mSignalReceived = true;
42   }
43
44   void Reset()
45   {
46     mSignalReceived = false;
47   }
48
49   void CheckSignalReceived()
50   {
51     if(!mSignalReceived)
52     {
53       tet_printf("Expected Finish signal was not received\n");
54       tet_result(TET_FAIL);
55     }
56     else
57     {
58       tet_result(TET_PASS);
59     }
60   }
61
62   void CheckSignalNotReceived()
63   {
64     if(mSignalReceived)
65     {
66       tet_printf("Unexpected Finish signal was received\n");
67       tet_result(TET_FAIL);
68     }
69     else
70     {
71       tet_result(TET_PASS);
72     }
73   }
74
75   bool& mSignalReceived; // owned by individual tests
76 };
77
78 int UtcDaliFadeSetGetProperty(void)
79 {
80   ToolkitTestApplication application;
81   tet_infoline("UtcDaliFadeSetGetProperty");
82
83   Control control = Control::New();
84
85   Fade fade = Fade::New(control, 0.5, TimePeriod(-0.5f, -0.5f));
86
87   TimePeriod timePeriod = fade.GetTimePeriod();
88   DALI_TEST_EQUALS(0.0f, timePeriod.delaySeconds, TEST_LOCATION);
89   DALI_TEST_EQUALS(0.0f, timePeriod.durationSeconds, TEST_LOCATION);
90
91   END_TEST;
92 }
93
94 int UtcDaliFadeWithOffScene(void)
95 {
96   ToolkitTestApplication application;
97   tet_infoline("UtcDaliFadeWithOffScene");
98
99   Control control = Control::New();
100   control.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
101   control.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
102   control.SetProperty(Actor::Property::POSITION, Vector3(100, 200, 0));
103   control.SetProperty(Actor::Property::SIZE, Vector3(150, 150, 0));
104   control.SetProperty(Actor::Property::OPACITY, 1.0f);
105   Property::Map controlProperty;
106   controlProperty.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
107   controlProperty.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, Vector4(1.0f, 0.0f, 0.0f, 1.0f));
108   control.SetProperty(Toolkit::Control::Property::BACKGROUND, controlProperty);
109
110   application.SendNotification();
111   application.Render(20);
112
113   DALI_TEST_EQUALS(1.0f, control.GetCurrentProperty<float>(Actor::Property::OPACITY), TEST_LOCATION);
114
115   Fade fade = Fade::New(control, 0.5, TimePeriod(0.5f));
116   fade.SetAppearingTransition(false); // set fade out
117   TransitionSet transitionSet = TransitionSet::New();
118   transitionSet.AddTransition(fade);
119   transitionSet.Play();
120
121   bool signalReceived(false);
122   TransitionFinishCheck finishCheck(signalReceived);
123   transitionSet.FinishedSignal().Connect(&application, finishCheck);
124
125   application.SendNotification();
126   application.Render(400);
127
128   // We didn't expect the animation to finish yet
129   application.SendNotification();
130   finishCheck.CheckSignalNotReceived();
131
132   DALI_TEST_EQUALS(1.0f, control.GetCurrentProperty<float>(Actor::Property::OPACITY), TEST_LOCATION);
133   
134   application.SendNotification();
135   application.Render(200);
136
137   // We did expect the animation to finish
138   application.SendNotification();
139   finishCheck.CheckSignalReceived();
140
141   application.SendNotification();
142   application.Render(20);
143
144   DALI_TEST_EQUALS(1.0f, control.GetCurrentProperty<float>(Actor::Property::OPACITY), TEST_LOCATION);
145
146   END_TEST;
147 }
148
149 int UtcDaliFadeOut(void)
150 {
151   ToolkitTestApplication application;
152   tet_infoline("UtcDaliFadeOut");
153
154   Control control = Control::New();
155   control.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
156   control.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
157   control.SetProperty(Actor::Property::POSITION, Vector3(100, 200, 0));
158   control.SetProperty(Actor::Property::SIZE, Vector3(150, 150, 0));
159   control.SetProperty(Actor::Property::OPACITY, 1.0f);
160   Property::Map controlProperty;
161   controlProperty.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
162   controlProperty.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, Vector4(1.0f, 0.0f, 0.0f, 1.0f));
163   control.SetProperty(Toolkit::Control::Property::BACKGROUND, controlProperty);
164
165   application.GetScene().Add(control);
166
167   application.SendNotification();
168   application.Render(20);
169
170   DALI_TEST_EQUALS(1.0f, control.GetCurrentProperty<float>(Actor::Property::OPACITY), TEST_LOCATION);
171
172   Fade fade = Fade::New(control, 0.5, TimePeriod(0.5f));
173   fade.SetAppearingTransition(false); // set fade out
174   TransitionSet transitionSet = TransitionSet::New();
175   transitionSet.AddTransition(fade);
176   transitionSet.Play();
177
178   bool signalReceived(false);
179   TransitionFinishCheck finishCheck(signalReceived);
180   transitionSet.FinishedSignal().Connect(&application, finishCheck);
181
182   application.SendNotification();
183   application.Render(400);
184
185   // We didn't expect the animation to finish yet
186   application.SendNotification();
187   finishCheck.CheckSignalNotReceived();
188
189   float currentOpacity = control.GetCurrentProperty<float>(Actor::Property::OPACITY);
190   DALI_TEST_CHECK(currentOpacity <= 0.7 && currentOpacity >= 0.5);
191   
192   application.SendNotification();
193   application.Render(200);
194
195   // We did expect the animation to finish
196   application.SendNotification();
197   finishCheck.CheckSignalReceived();
198
199   application.SendNotification();
200   application.Render(20);
201
202   // Property is reset after animation.
203   DALI_TEST_EQUALS(1.0f, control.GetCurrentProperty<float>(Actor::Property::OPACITY), TEST_LOCATION);
204
205   END_TEST;
206 }
207
208 int UtcDaliFadeIn(void)
209 {
210   ToolkitTestApplication application;
211   tet_infoline("UtcDaliFadeIn");
212
213   Control control = Control::New();
214   control.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
215   control.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
216   control.SetProperty(Actor::Property::POSITION, Vector3(100, 200, 0));
217   control.SetProperty(Actor::Property::SIZE, Vector3(150, 150, 0));
218   control.SetProperty(Actor::Property::OPACITY, 1.0f);
219   Property::Map controlProperty;
220   controlProperty.Insert(Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR);
221   controlProperty.Insert(Toolkit::ColorVisual::Property::MIX_COLOR, Vector4(1.0f, 0.0f, 0.0f, 1.0f));
222   control.SetProperty(Toolkit::Control::Property::BACKGROUND, controlProperty);
223
224   application.GetScene().Add(control);
225
226   application.SendNotification();
227   application.Render(20);
228
229   DALI_TEST_EQUALS(1.0f, control.GetCurrentProperty<float>(Actor::Property::OPACITY), TEST_LOCATION);
230
231   Fade fade = Fade::New(control, 0.5, TimePeriod(0.5f));
232   fade.SetAppearingTransition(true); // set fade in
233   TransitionSet transitionSet = TransitionSet::New();
234   transitionSet.AddTransition(fade);
235   transitionSet.Play();
236
237   bool signalReceived(false);
238   TransitionFinishCheck finishCheck(signalReceived);
239   transitionSet.FinishedSignal().Connect(&application, finishCheck);
240
241   application.SendNotification();
242   application.Render(400);
243
244   // We didn't expect the animation to finish yet
245   application.SendNotification();
246   finishCheck.CheckSignalNotReceived();
247
248   float currentOpacity = control.GetCurrentProperty<float>(Actor::Property::OPACITY);
249   DALI_TEST_CHECK(currentOpacity <= 1.0 && currentOpacity >= 0.8);
250   
251   application.SendNotification();
252   application.Render(200);
253
254   // We did expect the animation to finish
255   application.SendNotification();
256   finishCheck.CheckSignalReceived();
257
258   application.SendNotification();
259   application.Render(20);
260
261   DALI_TEST_EQUALS(1.0f, control.GetCurrentProperty<float>(Actor::Property::OPACITY), TEST_LOCATION);
262
263   END_TEST;
264 }