Removing GetDefaultFontDescription from Platform Abstraction Test
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ScrollViewEffect.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 #include <stdlib.h>
20
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
24
25 #include <dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27 #include <dali/integration-api/events/touch-event-integ.h>
28 #include <dali/integration-api/events/pan-gesture-event.h>
29
30 using namespace Dali;
31 using namespace Toolkit;
32
33 void utc_dali_toolkit_scroll_view_effect_startup(void)
34 {
35   test_return_value = TET_UNDEF;
36 }
37
38 void utc_dali_toolkit_scroll_view_effect_cleanup(void)
39 {
40   test_return_value = TET_PASS;
41 }
42
43 namespace
44 {
45
46 const int RENDER_FRAME_INTERVAL = 16;                           ///< Duration of each frame in ms. (at approx 60FPS)
47
48 /*
49  * Simulate time passed by.
50  *
51  * @note this will always process at least 1 frame (1/60 sec)
52  *
53  * @param application Test application instance
54  * @param duration Time to pass in milliseconds.
55  * @return The actual time passed in milliseconds
56  */
57 int Wait(ToolkitTestApplication& application, int duration = 0)
58 {
59   int time = 0;
60
61   for(int i = 0; i <= ( duration / RENDER_FRAME_INTERVAL); i++)
62   {
63     application.SendNotification();
64     application.Render(RENDER_FRAME_INTERVAL);
65     time += RENDER_FRAME_INTERVAL;
66   }
67
68   return time;
69 }
70
71 /**
72  * Creates a Ruler that snaps to a specified grid size.
73  * If that grid size is 0.0 then this ruler does not
74  * snap.
75  *
76  * @param[in] gridSize (optional) The grid size for the ruler,
77  * (Default = 0.0 i.e. no snapping)
78  * @return The ruler is returned.
79  */
80 RulerPtr CreateRuler(float gridSize = 0.0f)
81 {
82   if(gridSize <= Math::MACHINE_EPSILON_0)
83   {
84       return new DefaultRuler();
85   }
86   return new FixedRuler(gridSize);
87 }
88
89 // Callback probes.
90
91 static bool gOnScrollStartCalled;                       ///< Whether the OnScrollStart signal was invoked.
92 static bool gOnScrollUpdateCalled;                      ///< Whether the OnScrollUpdate signal was invoked.
93 static bool gOnScrollCompleteCalled;                    ///< Whether the OnScrollComplete signal was invoked.
94 static Vector3 gConstraintResult;                       ///< Result from constraint.
95
96 static std::vector< Actor > gPages;                                ///< Keeps track of all the pages for applying effects.
97 typedef std::vector< Actor >::iterator ActorIter;
98
99 static void ResetScrollCallbackResults()
100 {
101   gOnScrollStartCalled = false;
102   gOnScrollUpdateCalled = false;
103   gOnScrollCompleteCalled = false;
104 }
105
106 /**
107  * Invoked when scrolling starts.
108  *
109  * @param[in] position The current scroll position.
110  */
111 static void OnScrollStart( const Vector2& position )
112 {
113   gOnScrollStartCalled = true;
114 }
115
116 /**
117  * Invoked when scrolling updates (via dragging)
118  *
119  * @param[in] position The current scroll position.
120  */
121 static void OnScrollUpdate( const Vector2& position )
122 {
123   gOnScrollUpdateCalled = true;
124 }
125
126 /**
127  * Invoked when scrolling finishes
128  *
129  * @param[in] position The current scroll position.
130  */
131 static void OnScrollComplete( const Vector2& position )
132 {
133   gOnScrollCompleteCalled = true;
134 }
135
136 ScrollView SetupTestScrollView(int rows, int columns, Vector2 size)
137 {
138   Constraint constraint;
139
140   ScrollView scrollView = ScrollView::New();
141   scrollView.SetSize(size);
142   scrollView.SetAnchorPoint(AnchorPoint::CENTER);
143   scrollView.SetParentOrigin(ParentOrigin::CENTER);
144
145   constraint = Constraint::New<Dali::Vector3>( scrollView, Dali::Actor::Property::SIZE, Dali::EqualToConstraint() );
146   constraint.AddSource( Dali::ParentSource( Dali::Actor::Property::SIZE ) );
147   constraint.Apply();
148
149   scrollView.SetWrapMode(false);
150   scrollView.ScrollStartedSignal().Connect( &OnScrollStart );
151   scrollView.ScrollUpdatedSignal().Connect( &OnScrollUpdate );
152   scrollView.ScrollCompletedSignal().Connect( &OnScrollComplete );
153   Stage::GetCurrent().Add( scrollView );
154   RulerPtr rulerX = CreateRuler(size.width);
155   RulerPtr rulerY = CreateRuler(size.height);
156   if(columns > 1)
157   {
158     rulerX->SetDomain(RulerDomain(0.0f, size.width * columns));
159   }
160   else
161   {
162     rulerX->Disable();
163   }
164   if(rows > 1)
165   {
166     rulerY->SetDomain(RulerDomain(0.0f, size.width * rows));
167   }
168   else
169   {
170     rulerY->Disable();
171   }
172
173   scrollView.SetRulerX( rulerX );
174   scrollView.SetRulerY( rulerY );
175   Stage::GetCurrent().Add( scrollView );
176
177   Actor container = Actor::New();
178   container.SetParentOrigin(ParentOrigin::CENTER);
179   container.SetAnchorPoint(AnchorPoint::CENTER);
180   container.SetSize( size );
181   scrollView.Add( container );
182
183   constraint = Constraint::New<Vector3>( container, Actor::Property::SIZE, EqualToConstraint() );
184   constraint.AddSource( Dali::ParentSource( Dali::Actor::Property::SIZE ) );
185   constraint.Apply();
186
187   gPages.clear();
188   for(int row = 0;row<rows;row++)
189   {
190     for(int column = 0;column<columns;column++)
191     {
192       Actor page = Actor::New();
193
194       constraint = Constraint::New<Vector3>( page, Actor::Property::SIZE, EqualToConstraint() );
195       constraint.AddSource( Dali::ParentSource( Dali::Actor::Property::SIZE ) );
196       constraint.Apply();
197       page.SetParentOrigin( ParentOrigin::CENTER );
198       page.SetAnchorPoint( AnchorPoint::CENTER );
199       page.SetPosition( column * size.x, row * size.y );
200       container.Add(page);
201
202       gPages.push_back(page);
203     }
204   }
205
206   ResetScrollCallbackResults();
207   return scrollView;
208 }
209
210 void CleanupTest()
211 {
212   gPages.clear();
213   ResetScrollCallbackResults();
214 }
215
216 Actor AddActorToPage(Actor page, float x, float y, float cols, float rows)
217 {
218   Stage stage = Stage::GetCurrent();
219   Vector2 stageSize = stage.GetSize();
220
221   const float margin = 10.0f;
222   const Vector2 actorSize((stageSize.x / cols) - margin, (stageSize.y / rows) - margin);
223
224   Actor actor = Actor::New();
225   actor.SetParentOrigin( ParentOrigin::CENTER );
226   actor.SetAnchorPoint( AnchorPoint::CENTER );
227
228   Vector3 position( margin * 0.5f + (actorSize.x + margin) * x - stageSize.width * 0.5f,
229                     margin * 0.5f + (actorSize.y + margin) * y - stageSize.height * 0.5f,
230                     0.0f);
231   Vector3 positionEnd( margin * 0.5f + (actorSize.x + margin) * (x + cols) - stageSize.width * 0.5f - margin,
232                        margin * 0.5f + (actorSize.y + margin) * (y + rows) - stageSize.height * 0.5f - margin,
233                        0.0f);
234   Vector3 size(positionEnd - position);
235   actor.SetPosition( position + size * 0.5f);
236   actor.SetSize( positionEnd - position );
237   page.Add(actor);
238   return actor;
239 }
240
241 } // unnamed namespace
242
243 int UtcDaliScrollViewPagePathEffectSetup(void)
244 {
245   tet_infoline(" UtcDaliScrollViewPagePathEffectSetup");
246
247   ScrollViewPagePathEffect effect;
248
249   DALI_TEST_CHECK( !effect );
250
251   BaseHandle handle = ScrollViewPagePathEffect::New(Dali::Path::New(), Vector3::ZERO,Toolkit::ScrollView::Property::SCROLL_FINAL_X, Vector3::ZERO,0);
252
253   DALI_TEST_CHECK( handle );
254
255   effect = ScrollViewPagePathEffect::DownCast(handle);
256
257   DALI_TEST_CHECK( effect );
258   END_TEST;
259 }
260
261 int UtcDaliScrollViewPagePathEffectTest(void)
262 {
263   ToolkitTestApplication application;
264   tet_infoline(" UtcDaliScrollViewPagePathEffectTest");
265
266   Vector2 size = Stage::GetCurrent().GetSize();
267
268   ScrollView scrollView = SetupTestScrollView(1, 3, size);
269   Actor testPage = gPages[2];
270   Wait(application, 500);
271
272   //Create path
273   float xHalfSize( size.x * 0.5f);
274
275   Dali::Path path = Dali::Path::New();
276   Dali::Property::Array points;
277   points.Resize(3);
278   points[0] = Vector3( xHalfSize, 0.0f,  -xHalfSize);
279   points[1] = Vector3( 0.0f, 0.0f, 0.0f );
280   points[2] = Vector3( -xHalfSize, 0.0f,  -xHalfSize);
281   path.SetProperty( Path::Property::POINTS, points );
282
283   Dali::Property::Array controlPoints;
284   controlPoints.Resize(4);
285   controlPoints[0] = Vector3( xHalfSize, 0.0f, 0.0f );
286   controlPoints[1] = Vector3( xHalfSize, 0.0f, 0.0f );
287   controlPoints[2] = Vector3(-xHalfSize, 0.0f, 0.0f );
288   controlPoints[3] = Vector3(-xHalfSize, 0.0f, 0.0f );
289   path.SetProperty( Path::Property::CONTROL_POINTS, controlPoints );
290
291   ScrollViewPagePathEffect effect = ScrollViewPagePathEffect::New(path,
292                                                                   Vector3::ZERO,
293                                                                   Toolkit::ScrollView::Property::SCROLL_FINAL_X,
294                                                                   Vector3(size.x,size.y,0.0f),
295                                                                   3);
296   scrollView.ApplyEffect(effect);
297
298   unsigned int pageCounter(0);
299   for(ActorIter pageIter = gPages.begin(); pageIter != gPages.end(); ++pageIter)
300   {
301     Actor page = *pageIter;
302     page.RemoveConstraints();
303     Constraint constraint = Constraint::New<Vector3>( page, Actor::Property::SIZE, EqualToConstraint() );
304     constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
305     constraint.Apply();
306     effect.ApplyToPage(page, pageCounter++);
307   }
308   Wait(application);
309
310   scrollView.ScrollTo(1);
311   while(!gOnScrollCompleteCalled)
312   {
313     Wait(application);
314   }
315
316   // test that the test page has reached centre of screen
317   Vector3 pagePos = testPage.GetCurrentPosition();
318   DALI_TEST_EQUALS(pagePos, Vector3::ZERO, Math::MACHINE_EPSILON_0, TEST_LOCATION);
319
320   CleanupTest();
321   END_TEST;
322 }
323