Adding new test harness
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-unmanaged / utc-Dali-ScrollViewEffect.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 <dali-toolkit-test-suite-utils.h>
21 #include <dali-toolkit/dali-toolkit.h>
22 #include <dali/integration-api/events/touch-event-integ.h>
23 #include <dali/integration-api/events/pan-gesture-event.h>
24
25
26 using namespace Dali;
27 using namespace Toolkit;
28
29 void dali_scroll_view_effect_startup(void)
30 {
31   test_return_value = TET_UNDEF;
32 }
33
34 void dali_scroll_view_effect_cleanup(void)
35 {
36   test_return_value = TET_PASS;
37 }
38
39 namespace
40 {
41
42 const int MILLISECONDS_PER_SECOND = 1000;
43 const int RENDER_FRAME_INTERVAL = 16;                           ///< Duration of each frame in ms. (at approx 60FPS)
44 const int RENDER_ANIMATION_TEST_DURATION_MS = 1000;             ///< 1000ms to test animation
45 const int RENDER_DELAY_SCROLL = 1000;                           ///< duration to wait for any scroll to complete.
46
47 /*
48  * Simulate time passed by.
49  *
50  * @note this will always process at least 1 frame (1/60 sec)
51  *
52  * @param application Test application instance
53  * @param duration Time to pass in milliseconds.
54  * @return The actual time passed in milliseconds
55  */
56 int Wait(ToolkitTestApplication& application, int duration = 0)
57 {
58   int time = 0;
59
60   for(int i = 0; i <= ( duration / RENDER_FRAME_INTERVAL); i++)
61   {
62     application.SendNotification();
63     application.Render(RENDER_FRAME_INTERVAL);
64     time += RENDER_FRAME_INTERVAL;
65   }
66
67   return time;
68 }
69
70 /**
71  * Creates a Ruler that snaps to a specified grid size.
72  * If that grid size is 0.0 then this ruler does not
73  * snap.
74  *
75  * @param[in] gridSize (optional) The grid size for the ruler,
76  * (Default = 0.0 i.e. no snapping)
77  * @return The ruler is returned.
78  */
79 RulerPtr CreateRuler(float gridSize = 0.0f)
80 {
81   if(gridSize <= Math::MACHINE_EPSILON_0)
82   {
83       return new DefaultRuler();
84   }
85   return new FixedRuler(gridSize);
86 }
87
88 // Callback probes.
89
90 static bool gOnScrollStartCalled;                       ///< Whether the OnScrollStart signal was invoked.
91 static bool gOnScrollUpdateCalled;                      ///< Whether the OnScrollUpdate signal was invoked.
92 static bool gOnScrollCompleteCalled;                    ///< Whether the OnScrollComplete signal was invoked.
93 static Vector3 gConstraintResult;                       ///< Result from constraint.
94
95 static ActorContainer gPages;                                ///< Keeps track of all the pages for applying effects.
96
97 static void ResetScrollCallbackResults()
98 {
99   gOnScrollStartCalled = false;
100   gOnScrollUpdateCalled = false;
101   gOnScrollCompleteCalled = false;
102 }
103
104 /**
105  * Invoked when scrolling starts.
106  *
107  * @param[in] position The current scroll position.
108  */
109 static void OnScrollStart( const Vector3& position )
110 {
111   gOnScrollStartCalled = true;
112 }
113
114 /**
115  * Invoked when scrolling updates (via dragging)
116  *
117  * @param[in] position The current scroll position.
118  */
119 static void OnScrollUpdate( const Vector3& position )
120 {
121   gOnScrollUpdateCalled = true;
122 }
123
124 /**
125  * Invoked when scrolling finishes
126  *
127  * @param[in] position The current scroll position.
128  */
129 static void OnScrollComplete( const Vector3& position )
130 {
131   gOnScrollCompleteCalled = true;
132 }
133
134
135 ScrollView SetupTestScrollView(int rows, int columns, Vector2 size)
136 {
137   ScrollView scrollView = ScrollView::New();
138   scrollView.SetSize(size);
139   scrollView.SetAnchorPoint(AnchorPoint::CENTER);
140   scrollView.SetParentOrigin(ParentOrigin::CENTER);
141   scrollView.ApplyConstraint( Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::EqualToConstraint() ) );
142   // Disable Refresh signal (TET environment cannot use adaptor's Timer)
143   scrollView.SetWrapMode(false);
144   scrollView.SetRefreshInterval(0);
145   scrollView.ScrollStartedSignal().Connect( &OnScrollStart );
146   scrollView.ScrollUpdatedSignal().Connect( &OnScrollUpdate );
147   scrollView.ScrollCompletedSignal().Connect( &OnScrollComplete );
148   Stage::GetCurrent().Add( scrollView );
149   RulerPtr rulerX = CreateRuler(size.width);
150   RulerPtr rulerY = CreateRuler(size.height);
151   if(columns > 1)
152   {
153     rulerX->SetDomain(RulerDomain(0.0f, size.width * columns));
154   }
155   else
156   {
157     rulerX->Disable();
158   }
159   if(rows > 1)
160   {
161     rulerY->SetDomain(RulerDomain(0.0f, size.width * rows));
162   }
163   else
164   {
165     rulerY->Disable();
166   }
167
168   scrollView.SetRulerX( rulerX );
169   scrollView.SetRulerY( rulerY );
170   Stage::GetCurrent().Add( scrollView );
171
172   Actor container = Actor::New();
173   container.SetParentOrigin(ParentOrigin::CENTER);
174   container.SetAnchorPoint(AnchorPoint::CENTER);
175   container.SetSize( size );
176   scrollView.Add( container );
177   container.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
178
179   gPages.clear();
180   for(int row = 0;row<rows;row++)
181   {
182     for(int column = 0;column<columns;column++)
183     {
184       Actor page = Actor::New();
185       page.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
186       page.SetParentOrigin( ParentOrigin::CENTER );
187       page.SetAnchorPoint( AnchorPoint::CENTER );
188       page.SetPosition( column * size.x, row * size.y );
189       container.Add(page);
190
191       gPages.push_back(page);
192     }
193   }
194
195   ResetScrollCallbackResults();
196   return scrollView;
197 }
198
199 void CleanupTest()
200 {
201   gPages.clear();
202   ResetScrollCallbackResults();
203 }
204
205 Actor AddActorToPage(Actor page, float x, float y, float cols, float rows)
206 {
207   Stage stage = Stage::GetCurrent();
208   Vector2 stageSize = stage.GetSize();
209
210   const float margin = 10.0f;
211   const Vector2 actorSize((stageSize.x / cols) - margin, (stageSize.y / rows) - margin);
212
213   Actor actor = Actor::New();
214   actor.SetParentOrigin( ParentOrigin::CENTER );
215   actor.SetAnchorPoint( AnchorPoint::CENTER );
216
217   Vector3 position( margin * 0.5f + (actorSize.x + margin) * x - stageSize.width * 0.5f,
218                     margin * 0.5f + (actorSize.y + margin) * y - stageSize.height * 0.5f,
219                     0.0f);
220   Vector3 positionEnd( margin * 0.5f + (actorSize.x + margin) * (x + cols) - stageSize.width * 0.5f - margin,
221                        margin * 0.5f + (actorSize.y + margin) * (y + rows) - stageSize.height * 0.5f - margin,
222                        0.0f);
223   Vector3 size(positionEnd - position);
224   actor.SetPosition( position + size * 0.5f);
225   actor.SetSize( positionEnd - position );
226   page.Add(actor);
227   return actor;
228 }
229
230 } // unnamed namespace
231
232
233 int UtcDaliScrollViewPageCubeEffectSetup(void)
234 {
235   tet_infoline(" UtcDaliScrollViewPageCubeEffectSetup");
236
237   ScrollViewPageCubeEffect effect;
238
239   DALI_TEST_CHECK( !effect );
240
241   BaseHandle handle = ScrollViewPageCubeEffect::New();
242
243   DALI_TEST_CHECK( handle );
244
245   effect = ScrollViewPageCubeEffect::DownCast(handle);
246
247   DALI_TEST_CHECK( effect );
248   END_TEST;
249 }
250
251
252 int UtcDaliScrollViewPageCarouselEffectSetup(void)
253 {
254   tet_infoline(" UtcDaliScrollViewCarouselEffectSetup");
255
256   ScrollViewPageCarouselEffect effect;
257
258   DALI_TEST_CHECK( !effect );
259
260   BaseHandle handle = ScrollViewPageCarouselEffect::New();
261
262   DALI_TEST_CHECK( handle );
263
264   effect = ScrollViewPageCarouselEffect::DownCast(handle);
265
266   DALI_TEST_CHECK( effect );
267   END_TEST;
268 }
269
270 int UtcDaliScrollViewCarouselEffectSetup(void)
271 {
272   tet_infoline(" UtcDaliScrollViewCarouselEffectSetup");
273
274   ScrollViewCarouselEffect effect;
275
276   DALI_TEST_CHECK( !effect );
277
278   BaseHandle handle = ScrollViewCarouselEffect::New();
279
280   DALI_TEST_CHECK( handle );
281
282   effect = ScrollViewCarouselEffect::DownCast(handle);
283
284   DALI_TEST_CHECK( effect );
285   END_TEST;
286 }
287
288 int UtcDaliScrollViewDepthEffectSetup(void)
289 {
290   tet_infoline(" UtcDaliScrollViewDepthEffectSetup");
291
292   ScrollViewDepthEffect effect;
293
294   DALI_TEST_CHECK( !effect );
295
296   BaseHandle handle = ScrollViewDepthEffect::New();
297
298   DALI_TEST_CHECK( handle );
299
300   effect = ScrollViewDepthEffect::DownCast(handle);
301
302   DALI_TEST_CHECK( effect );
303   END_TEST;
304 }
305
306
307 int UtcDaliScrollViewPageCubeEffectTest(void)
308 {
309   ToolkitTestApplication application;
310   tet_infoline(" UtcDaliScrollViewPageCubeEffectTest");
311
312   Vector2 size = Stage::GetCurrent().GetSize();
313
314   ScrollView scrollView = SetupTestScrollView(1, 3, size);
315   Actor testPage = gPages[1];
316   Wait(application, 500);
317
318   ScrollViewPageCubeEffect effect = ScrollViewPageCubeEffect::New();
319   scrollView.ApplyEffect(effect);
320
321   for(ActorIter pageIter = gPages.begin(); pageIter != gPages.end(); ++pageIter)
322   {
323     Actor page = *pageIter;
324     page.RemoveConstraints();
325     page.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
326     effect.ApplyToPage(page, Vector2(Math::PI_2, 0.0f));
327   }
328   Wait(application);
329
330   scrollView.ScrollTo(1);
331   while(!gOnScrollCompleteCalled)
332   {
333     Wait(application);
334   }
335   // test that the first page has reached centre of screen
336   Vector3 pagePos = testPage.GetCurrentPosition();
337   DALI_TEST_EQUALS(pagePos, Vector3::ZERO, Math::MACHINE_EPSILON_0, TEST_LOCATION);
338   CleanupTest();
339   END_TEST;
340 }
341
342 int UtcDaliScrollViewPageCarouselEffectTest(void)
343 {
344   ToolkitTestApplication application;
345   tet_infoline(" UtcDaliScrollViewPageCarouselEffectTest");
346
347   Vector2 size = Stage::GetCurrent().GetSize();
348
349   ScrollView scrollView = SetupTestScrollView(1, 3, size);
350   Actor testPage = gPages[1];
351   Wait(application, 500);
352
353   ScrollViewPageCarouselEffect effect = ScrollViewPageCarouselEffect::New();
354   scrollView.ApplyEffect(effect);
355
356   for(ActorIter pageIter = gPages.begin(); pageIter != gPages.end(); ++pageIter)
357   {
358     Actor page = *pageIter;
359     page.RemoveConstraints();
360     page.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
361     effect.ApplyToPage(page);
362   }
363   Wait(application);
364
365   scrollView.ScrollTo(1, 0.5f, DirectionBiasNone);
366   while(!gOnScrollCompleteCalled)
367   {
368     Wait(application);
369   }
370   // test that the first page has reached centre of screen
371   Vector3 pagePos = testPage.GetCurrentPosition();
372   DALI_TEST_EQUALS(pagePos, Vector3::ZERO, Math::MACHINE_EPSILON_0, TEST_LOCATION);
373   CleanupTest();
374   END_TEST;
375 }
376
377 int UtcDaliScrollViewCarouselEffectTest(void)
378 {
379   ToolkitTestApplication application;
380   tet_infoline(" UtcDaliScrollViewCarouselEffectTest");
381
382   Vector2 size = Stage::GetCurrent().GetSize();
383
384   ScrollView scrollView = SetupTestScrollView(1, 3, size);
385   Actor testPage = gPages[1];
386   Wait(application, 500);
387
388   ScrollViewCarouselEffect effect = ScrollViewCarouselEffect::New();
389   scrollView.ApplyEffect(effect);
390
391   Actor actor = AddActorToPage(testPage, 0.5f, 0.5f, 3, 3);
392   Wait(application);
393   Vector3 actorPrePosition = actor.GetCurrentPosition();
394
395   effect.ApplyToActor( actor, Vector2(1.2f, 1.2f) );
396
397   scrollView.ScrollTo(Vector3(size.x, 0.0f, 0.0f), 0.5f, DirectionBiasNone, DirectionBiasNone);
398   while(!gOnScrollCompleteCalled)
399   {
400     Wait(application);
401   }
402   // test that the first page has reached centre of screen
403   Vector3 actorPostPosition = actor.GetCurrentPosition();
404   // just check the actor has moved
405   DALI_TEST_CHECK((actorPostPosition - actorPrePosition).Length() > Math::MACHINE_EPSILON_1);
406   CleanupTest();
407   END_TEST;
408 }
409
410 int UtcDaliScrollViewDepthEffectTest(void)
411 {
412   ToolkitTestApplication application;
413   tet_infoline(" UtcDaliScrollViewDepthEffectTest");
414
415   Vector2 size = Stage::GetCurrent().GetSize();
416
417   ScrollView scrollView = SetupTestScrollView(1, 3, size);
418   Actor testPage = gPages[1];
419   Wait(application, 500);
420
421   ScrollViewDepthEffect effect = ScrollViewDepthEffect::New();
422   scrollView.ApplyEffect(effect);
423
424   Actor actor = AddActorToPage(testPage, 0.5f, 0.5f, 3, 3);
425   Wait(application);
426   Vector3 actorPrePosition = actor.GetCurrentPosition();
427
428   const Vector2 positionExtent(0.5f, 2.5f);
429   const Vector2 offsetExtent(1.0f, 1.0f);
430   const float positionScale(1.5f);
431   const float scaleExtent(0.5f);
432
433   effect.ApplyToActor( actor, positionExtent, offsetExtent, positionScale, scaleExtent );
434
435   scrollView.ScrollTo(1);
436   while(!gOnScrollCompleteCalled)
437   {
438     Wait(application);
439   }
440   // test that the first page has reached centre of screen
441   Vector3 actorPostPosition = actor.GetCurrentPosition();
442   // just check the actor has moved
443   DALI_TEST_CHECK((actorPostPosition - actorPrePosition).Length() > Math::MACHINE_EPSILON_1);
444   CleanupTest();
445   END_TEST;
446 }