[5.0] Add animation workaround in LayoutGroup.
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-AbsoluteLayout.cpp
1 /*
2  * Copyright (c) 2018 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
22 #include <dali/devel-api/actors/actor-devel.h>
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali-toolkit/devel-api/controls/control-devel.h>
25 #include <dali-toolkit/devel-api/layouting/absolute-layout.h>
26
27 #include <layout-utils.h>
28
29 using namespace Dali;
30 using namespace Toolkit;
31
32 void utc_dali_toolkit_absolute_layout_startup(void)
33 {
34   test_return_value = TET_UNDEF;
35 }
36
37 void utc_dali_toolkit_absolute_layoutg_cleanup(void)
38 {
39   test_return_value = TET_PASS;
40 }
41
42
43 int UtcDaliLayouting_AbsoluteLayoutDownCast(void)
44 {
45   TestApplication application;
46   tet_infoline(" UtcDaliLayouting_AbsoluteLayoutDownCast - Testing Downcast");
47
48   AbsoluteLayout absoluteLayout = AbsoluteLayout::New();
49
50   LayoutGroup layoutGroup( absoluteLayout );
51
52   AbsoluteLayout absoluteLayoutCandidate = AbsoluteLayout::DownCast( layoutGroup );
53   DALI_TEST_CHECK( absoluteLayoutCandidate );
54
55   END_TEST;
56 }
57
58 int UtcDaliLayouting_AbsoluteLayoutAssignment(void)
59 {
60   TestApplication application;
61   tet_infoline(" UtcDaliLayouting_AbsoluteLayoutAssignment - Testing operator=");
62
63   AbsoluteLayout absoluteLayout = AbsoluteLayout::New();
64   AbsoluteLayout absoluteLayout2;
65
66   absoluteLayout2 = absoluteLayout;
67
68   DALI_TEST_CHECK( absoluteLayout2 == absoluteLayout );
69
70   END_TEST;
71 }
72
73
74 int UtcDaliLayouting_AbsoluteLayout01(void)
75 {
76   ToolkitTestApplication application;
77   tet_infoline(" UtcDaliLayouting_AbsoluteLayout01 - Position an item with Actor::Property::POSITION");
78
79   Stage stage = Stage::GetCurrent();
80   auto absoluteLayout = Control::New();
81   auto layout = AbsoluteLayout::New();
82   layout.SetAnimateLayout( true );
83   DevelControl::SetLayout( absoluteLayout, layout );
84   absoluteLayout.SetName( "AsoluteLayout");
85
86   std::vector< Control > controls;
87   controls.push_back( CreateLeafControl( 100, 100 ) );
88   controls.push_back( CreateLeafControl( 100, 100 ) );
89   controls.push_back( CreateLeafControl( 100, 100 ) );
90   controls.push_back( CreateLeafControl( 100, 100 ) );
91
92   // Position one of the  controls using the actor property.
93   controls[1].SetProperty(Actor::Property::POSITION, Vector3( 100.0f, 0.0f, 0.0f) );
94
95   for( auto&& iter : controls )
96   {
97     absoluteLayout.Add( iter );
98   }
99   absoluteLayout.SetParentOrigin( ParentOrigin::CENTER );
100   absoluteLayout.SetAnchorPoint( AnchorPoint::CENTER );
101   stage.Add( absoluteLayout );
102
103   // Ensure layouting happens
104   application.SendNotification();
105   application.Render();
106
107   // AbsoluteLayout renders items at the positions given by their Actor::Property::POSITION relative to the top left of the container.
108   // Items can overlap or spill out of their parent container.
109   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
110
111   // The controls[1] was the only control to have a defiend position
112   DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 100.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
113   DALI_TEST_EQUALS( controls[2].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
114   DALI_TEST_EQUALS( controls[3].GetProperty<Vector3>( Actor::Property::POSITION ), Vector3( 0.0f, 0.0f, 0.0f ), 0.0001f, TEST_LOCATION );
115
116   // Items size should not change regardless of parent's size.
117   DALI_TEST_EQUALS( controls[0].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
118   DALI_TEST_EQUALS( controls[1].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
119   DALI_TEST_EQUALS( controls[2].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
120   DALI_TEST_EQUALS( controls[3].GetProperty<Vector3>( Actor::Property::SIZE ), Vector3( 100.0f, 100.0f, 0.0f ), 0.0001f, TEST_LOCATION );
121
122   END_TEST;
123 }
124
125 namespace
126 {
127
128 // Functor to test whether a Finish signal is emitted
129 struct AnimationFinishCheck
130 {
131   AnimationFinishCheck(bool& signalReceived)
132   : mSignalReceived(signalReceived)
133   {
134   }
135
136   void operator()(Animation& animation)
137   {
138     mSignalReceived = true;
139   }
140
141   void Reset()
142   {
143     mSignalReceived = false;
144   }
145
146   void CheckSignalReceived()
147   {
148     if (!mSignalReceived)
149     {
150       tet_printf("Expected Finish signal was not received\n");
151       tet_result(TET_FAIL);
152     }
153     else
154     {
155       tet_result(TET_PASS);
156     }
157   }
158
159   void CheckSignalNotReceived()
160   {
161     if (mSignalReceived)
162     {
163       tet_printf("Unexpected Finish signal was received\n");
164       tet_result(TET_FAIL);
165     }
166     else
167     {
168       tet_result(TET_PASS);
169     }
170   }
171
172   bool& mSignalReceived; // owned by individual tests
173 };
174
175 }
176
177 int UtcDaliLayouting_AbsoluteLayoutWithStandaloneAnimation(void)
178 {
179   ToolkitTestApplication application;
180   tet_infoline(" UtcDaliLayouting_AbsoluteLayoutWithStandaloneAnimation ");
181
182   Stage stage = Stage::GetCurrent();
183   auto root = Control::New();
184   auto layout = AbsoluteLayout::New();
185   layout.SetAnimateLayout( false );
186   DevelControl::SetLayout( root, layout );
187   root.SetName( "AsoluteLayout");
188
189   std::vector< Control > controls;
190   controls.push_back( CreateLeafControl( 100, 100 ) );
191
192   controls[0].SetProperty( Actor::Property::POSITION, Vector3( 100.0f, 100.0f, 0.0f ) );
193
194   for( auto&& iter : controls )
195   {
196     root.Add( iter );
197   }
198
199   root.SetParentOrigin( ParentOrigin::CENTER );
200   root.SetAnchorPoint( AnchorPoint::CENTER );
201   stage.Add( root );
202
203   float durationSeconds = 1.0f;
204   Animation animation = Animation::New( durationSeconds );
205   Vector3 targetPosition( 200.0f, 200.0f, 0.0f );
206   animation.AnimateTo(Property( controls[0], Actor::Property::POSITION ), targetPosition, AlphaFunction::LINEAR );
207   animation.Play();
208
209   DALI_TEST_EQUALS( true, DevelActor::IsPositionOrSizeCurrentlyAnimating( controls[0] ), TEST_LOCATION );
210
211   bool signalReceived( false );
212   AnimationFinishCheck finishCheck( signalReceived );
213   animation.FinishedSignal().Connect( &application, finishCheck );
214
215   application.SendNotification();
216   //  0.1 of a second, 10 pixel delta
217   application.Render( 100u );
218
219   DALI_TEST_EQUALS( controls[0].GetCurrentPosition(), Vector3( 110.0f, 110.0f, 0.0f ), 0.0001f, TEST_LOCATION );
220
221   application.SendNotification();
222   application.Render(static_cast<unsigned int>( durationSeconds * 1000.0f ) - 100u + 1u );
223
224   // We did expect the animation to finish
225   application.SendNotification();
226   finishCheck.CheckSignalReceived();
227
228   DALI_TEST_EQUALS( false, DevelActor::IsPositionOrSizeCurrentlyAnimating( controls[0] ), TEST_LOCATION);
229   DALI_TEST_EQUALS( targetPosition, controls[0].GetCurrentPosition(), TEST_LOCATION );
230
231   END_TEST;
232 }
233
234 int UtcDaliLayouting_AbsoluteLayoutAndLayoutGroupWithStandaloneAnimation(void)
235 {
236   ToolkitTestApplication application;
237   tet_infoline(" UtcDaliLayouting_AbsoluteLayoutAndLayoutGroupWithStandaloneAnimation ");
238
239   Stage stage = Stage::GetCurrent();
240   auto root = Control::New();
241   auto layout = AbsoluteLayout::New();
242   layout.SetAnimateLayout( false );
243   DevelControl::SetLayout( root, layout );
244   root.SetName( "AsoluteLayout" );
245
246   Control container = CreateLeafControl( 200, 200 );
247   DevelControl::SetLayoutingRequired( container, true );
248   container.SetName( "Container" );
249   root.Add( container );
250
251   std::vector< Control > controls;
252   controls.push_back( CreateLeafControl( 100, 100 ) );
253   controls[0].SetProperty( Actor::Property::POSITION, Vector3( 100.0f, 100.0f, 0.0f ) );
254
255   for( auto&& iter : controls )
256   {
257     container.Add( iter );
258   }
259
260   root.SetParentOrigin( ParentOrigin::CENTER );
261   root.SetAnchorPoint( AnchorPoint::CENTER );
262   stage.Add( root );
263
264   float durationSeconds = 1.0f;
265   Animation animation = Animation::New( durationSeconds );
266   Vector3 targetPosition( 200.0f, 200.0f, 0.0f );
267   animation.AnimateTo(Property( controls[0], Actor::Property::POSITION ), targetPosition, AlphaFunction::LINEAR );
268   animation.Play();
269
270   DALI_TEST_EQUALS( true, DevelActor::IsPositionOrSizeCurrentlyAnimating( controls[0] ), TEST_LOCATION );
271
272   bool signalReceived( false );
273   AnimationFinishCheck finishCheck( signalReceived );
274   animation.FinishedSignal().Connect( &application, finishCheck );
275
276   application.SendNotification();
277   //  0.1 of a second, 10 pixel delta
278   application.Render( 100u );
279
280   DALI_TEST_EQUALS( controls[0].GetCurrentPosition(), Vector3( 110.0f, 110.0f, 0.0f ), 0.0001f, TEST_LOCATION );
281
282   application.SendNotification();
283   application.Render(static_cast<unsigned int>( durationSeconds * 1000.0f ) - 100u + 1u );
284
285   // We did expect the animation to finish
286   application.SendNotification();
287   finishCheck.CheckSignalReceived();
288
289   DALI_TEST_EQUALS( false, DevelActor::IsPositionOrSizeCurrentlyAnimating( controls[0] ), TEST_LOCATION);
290   DALI_TEST_EQUALS( targetPosition, controls[0].GetCurrentPosition(), TEST_LOCATION );
291
292   END_TEST;
293 }