Merge remote-tracking branch 'origin/tizen' into new_text
[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 MILLISECONDS_PER_SECOND = 1000;
47 const int RENDER_FRAME_INTERVAL = 16;                           ///< Duration of each frame in ms. (at approx 60FPS)
48 const int RENDER_ANIMATION_TEST_DURATION_MS = 1000;             ///< 1000ms to test animation
49 const int RENDER_DELAY_SCROLL = 1000;                           ///< duration to wait for any scroll to complete.
50
51 /*
52  * Simulate time passed by.
53  *
54  * @note this will always process at least 1 frame (1/60 sec)
55  *
56  * @param application Test application instance
57  * @param duration Time to pass in milliseconds.
58  * @return The actual time passed in milliseconds
59  */
60 int Wait(ToolkitTestApplication& application, int duration = 0)
61 {
62   int time = 0;
63
64   for(int i = 0; i <= ( duration / RENDER_FRAME_INTERVAL); i++)
65   {
66     application.SendNotification();
67     application.Render(RENDER_FRAME_INTERVAL);
68     time += RENDER_FRAME_INTERVAL;
69   }
70
71   return time;
72 }
73
74 /**
75  * Creates a Ruler that snaps to a specified grid size.
76  * If that grid size is 0.0 then this ruler does not
77  * snap.
78  *
79  * @param[in] gridSize (optional) The grid size for the ruler,
80  * (Default = 0.0 i.e. no snapping)
81  * @return The ruler is returned.
82  */
83 RulerPtr CreateRuler(float gridSize = 0.0f)
84 {
85   if(gridSize <= Math::MACHINE_EPSILON_0)
86   {
87       return new DefaultRuler();
88   }
89   return new FixedRuler(gridSize);
90 }
91
92 // Callback probes.
93
94 static bool gOnScrollStartCalled;                       ///< Whether the OnScrollStart signal was invoked.
95 static bool gOnScrollUpdateCalled;                      ///< Whether the OnScrollUpdate signal was invoked.
96 static bool gOnScrollCompleteCalled;                    ///< Whether the OnScrollComplete signal was invoked.
97 static Vector3 gConstraintResult;                       ///< Result from constraint.
98
99 static ActorContainer gPages;                                ///< Keeps track of all the pages for applying effects.
100
101 static void ResetScrollCallbackResults()
102 {
103   gOnScrollStartCalled = false;
104   gOnScrollUpdateCalled = false;
105   gOnScrollCompleteCalled = false;
106 }
107
108 /**
109  * Invoked when scrolling starts.
110  *
111  * @param[in] position The current scroll position.
112  */
113 static void OnScrollStart( const Vector3& position )
114 {
115   gOnScrollStartCalled = true;
116 }
117
118 /**
119  * Invoked when scrolling updates (via dragging)
120  *
121  * @param[in] position The current scroll position.
122  */
123 static void OnScrollUpdate( const Vector3& position )
124 {
125   gOnScrollUpdateCalled = true;
126 }
127
128 /**
129  * Invoked when scrolling finishes
130  *
131  * @param[in] position The current scroll position.
132  */
133 static void OnScrollComplete( const Vector3& position )
134 {
135   gOnScrollCompleteCalled = true;
136 }
137
138 ScrollView SetupTestScrollView(int rows, int columns, Vector2 size)
139 {
140   Constraint constraint;
141
142   ScrollView scrollView = ScrollView::New();
143   scrollView.SetSize(size);
144   scrollView.SetAnchorPoint(AnchorPoint::CENTER);
145   scrollView.SetParentOrigin(ParentOrigin::CENTER);
146
147   constraint = Constraint::New<Dali::Vector3>( scrollView, Dali::Actor::Property::SIZE, Dali::EqualToConstraint() );
148   constraint.AddSource( Dali::ParentSource( Dali::Actor::Property::SIZE ) );
149   constraint.Apply();
150
151   scrollView.SetWrapMode(false);
152   scrollView.ScrollStartedSignal().Connect( &OnScrollStart );
153   scrollView.ScrollUpdatedSignal().Connect( &OnScrollUpdate );
154   scrollView.ScrollCompletedSignal().Connect( &OnScrollComplete );
155   Stage::GetCurrent().Add( scrollView );
156   RulerPtr rulerX = CreateRuler(size.width);
157   RulerPtr rulerY = CreateRuler(size.height);
158   if(columns > 1)
159   {
160     rulerX->SetDomain(RulerDomain(0.0f, size.width * columns));
161   }
162   else
163   {
164     rulerX->Disable();
165   }
166   if(rows > 1)
167   {
168     rulerY->SetDomain(RulerDomain(0.0f, size.width * rows));
169   }
170   else
171   {
172     rulerY->Disable();
173   }
174
175   scrollView.SetRulerX( rulerX );
176   scrollView.SetRulerY( rulerY );
177   Stage::GetCurrent().Add( scrollView );
178
179   Actor container = Actor::New();
180   container.SetParentOrigin(ParentOrigin::CENTER);
181   container.SetAnchorPoint(AnchorPoint::CENTER);
182   container.SetSize( size );
183   scrollView.Add( container );
184
185   constraint = Constraint::New<Vector3>( container, Actor::Property::SIZE, EqualToConstraint() );
186   constraint.AddSource( Dali::ParentSource( Dali::Actor::Property::SIZE ) );
187   constraint.Apply();
188
189   gPages.clear();
190   for(int row = 0;row<rows;row++)
191   {
192     for(int column = 0;column<columns;column++)
193     {
194       Actor page = Actor::New();
195
196       constraint = Constraint::New<Vector3>( page, Actor::Property::SIZE, EqualToConstraint() );
197       constraint.AddSource( Dali::ParentSource( Dali::Actor::Property::SIZE ) );
198       constraint.Apply();
199       page.SetParentOrigin( ParentOrigin::CENTER );
200       page.SetAnchorPoint( AnchorPoint::CENTER );
201       page.SetPosition( column * size.x, row * size.y );
202       container.Add(page);
203
204       gPages.push_back(page);
205     }
206   }
207
208   ResetScrollCallbackResults();
209   return scrollView;
210 }
211
212 void CleanupTest()
213 {
214   gPages.clear();
215   ResetScrollCallbackResults();
216 }
217
218 Actor AddActorToPage(Actor page, float x, float y, float cols, float rows)
219 {
220   Stage stage = Stage::GetCurrent();
221   Vector2 stageSize = stage.GetSize();
222
223   const float margin = 10.0f;
224   const Vector2 actorSize((stageSize.x / cols) - margin, (stageSize.y / rows) - margin);
225
226   Actor actor = Actor::New();
227   actor.SetParentOrigin( ParentOrigin::CENTER );
228   actor.SetAnchorPoint( AnchorPoint::CENTER );
229
230   Vector3 position( margin * 0.5f + (actorSize.x + margin) * x - stageSize.width * 0.5f,
231                     margin * 0.5f + (actorSize.y + margin) * y - stageSize.height * 0.5f,
232                     0.0f);
233   Vector3 positionEnd( margin * 0.5f + (actorSize.x + margin) * (x + cols) - stageSize.width * 0.5f - margin,
234                        margin * 0.5f + (actorSize.y + margin) * (y + rows) - stageSize.height * 0.5f - margin,
235                        0.0f);
236   Vector3 size(positionEnd - position);
237   actor.SetPosition( position + size * 0.5f);
238   actor.SetSize( positionEnd - position );
239   page.Add(actor);
240   return actor;
241 }
242
243 } // unnamed namespace
244
245
246 int UtcDaliScrollViewCubeEffectSetup(void)
247 {
248   tet_infoline(" UtcDaliScrollViewCubeEffectSetup");
249
250   ScrollViewCubeEffect effect;
251
252   DALI_TEST_CHECK( !effect );
253
254   BaseHandle handle = ScrollViewCubeEffect::New();
255
256   DALI_TEST_CHECK( handle );
257
258   effect = ScrollViewCubeEffect::DownCast(handle);
259
260   DALI_TEST_CHECK( effect );
261   END_TEST;
262 }
263
264 int UtcDaliScrollViewSpiralEffectSetup(void)
265 {
266   tet_infoline(" UtcDaliScrollViewSpiralEffectSetup");
267
268   ScrollViewPageSpiralEffect effect;
269
270   DALI_TEST_CHECK( !effect );
271
272   BaseHandle handle = ScrollViewPageSpiralEffect::New();
273
274   DALI_TEST_CHECK( handle );
275
276   effect = ScrollViewPageSpiralEffect::DownCast(handle);
277
278   DALI_TEST_CHECK( effect );
279   END_TEST;
280 }
281
282 int UtcDaliScrollViewCubeEffectTest(void)
283 {
284   ToolkitTestApplication application;
285   tet_infoline(" UtcDaliScrollViewCubeEffectTest");
286
287   Vector2 size = Stage::GetCurrent().GetSize();
288
289   ScrollView scrollView = SetupTestScrollView(1, 3, size);
290   Actor page = gPages[1];
291   Wait(application, 500);
292
293   ScrollViewCubeEffect effect = ScrollViewCubeEffect::New();
294   scrollView.ApplyEffect(effect);
295
296   Actor actor = AddActorToPage(page, 0.5f, 0.5f, 3, 3);
297   Wait(application);
298   Vector3 actorPrePosition = actor.GetCurrentPosition();
299
300   effect.ApplyToActor(actor, page, Vector3(-105.0f, 30.0f, -240.0f), Vector2(Math::PI * 0.5f, Math::PI * 0.5f), Vector2(0.25f, 0.25f) * size);
301
302   Actor actor2 = AddActorToPage(page, 0.5f, 0.5f, 3, 3);
303   effect.ApplyToActor(actor2, Vector3(-105.0f, 30.0f, -240.0f), Vector2(Math::PI * 0.5f, Math::PI * 0.5f), Vector2(0.25f, 0.25f) * size);
304
305   scrollView.ScrollTo(1);
306   while(!gOnScrollCompleteCalled)
307   {
308     Wait(application);
309   }
310   // test that the first page has reached centre of screen
311   Vector3 actorPostPosition = actor.GetCurrentPosition();
312   // just check the actor has moved
313   DALI_TEST_CHECK((actorPostPosition - actorPrePosition).Length() > Math::MACHINE_EPSILON_1);
314   CleanupTest();
315   END_TEST;
316 }
317
318
319 int UtcDaliScrollViewSpiralEffectTest(void)
320 {
321   ToolkitTestApplication application;
322   tet_infoline(" UtcDaliScrollViewSpiralEffectTest");
323
324   Vector2 size = Stage::GetCurrent().GetSize();
325
326   ScrollView scrollView = SetupTestScrollView(1, 3, size);
327   Actor testPage = gPages[1];
328   Wait(application, 500);
329
330   ScrollViewPageSpiralEffect effect = ScrollViewPageSpiralEffect::New();
331   scrollView.ApplyEffect(effect);
332
333   for(ActorIter pageIter = gPages.begin(); pageIter != gPages.end(); ++pageIter)
334   {
335     Actor page = *pageIter;
336     page.RemoveConstraints();
337
338     Constraint constraint = Constraint::New<Vector3>( page, Actor::Property::SIZE, EqualToConstraint() );
339     constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
340     constraint.Apply();
341     effect.ApplyToPage(page, Vector2(Math::PI_2, 0.0f));
342   }
343   Wait(application);
344
345   scrollView.ScrollTo(1);
346   while(!gOnScrollCompleteCalled)
347   {
348     Wait(application);
349   }
350   // test that the first page has reached centre of screen
351   Vector3 pagePos = testPage.GetCurrentPosition();
352   DALI_TEST_EQUALS(pagePos, Vector3::ZERO, Math::MACHINE_EPSILON_0, TEST_LOCATION);
353   CleanupTest();
354   END_TEST;
355 }
356
357 int UtcDaliScrollViewPageCubeEffectSetup(void)
358 {
359   tet_infoline(" UtcDaliScrollViewPageCubeEffectSetup");
360
361   ScrollViewPageCubeEffect effect;
362
363   DALI_TEST_CHECK( !effect );
364
365   BaseHandle handle = ScrollViewPageCubeEffect::New();
366
367   DALI_TEST_CHECK( handle );
368
369   effect = ScrollViewPageCubeEffect::DownCast(handle);
370
371   DALI_TEST_CHECK( effect );
372   END_TEST;
373 }
374
375
376 int UtcDaliScrollViewPageCarouselEffectSetup(void)
377 {
378   tet_infoline(" UtcDaliScrollViewCarouselEffectSetup");
379
380   ScrollViewPageCarouselEffect effect;
381
382   DALI_TEST_CHECK( !effect );
383
384   BaseHandle handle = ScrollViewPageCarouselEffect::New();
385
386   DALI_TEST_CHECK( handle );
387
388   effect = ScrollViewPageCarouselEffect::DownCast(handle);
389
390   DALI_TEST_CHECK( effect );
391   END_TEST;
392 }
393
394 int UtcDaliScrollViewCarouselEffectSetup(void)
395 {
396   tet_infoline(" UtcDaliScrollViewCarouselEffectSetup");
397
398   ScrollViewCarouselEffect effect;
399
400   DALI_TEST_CHECK( !effect );
401
402   BaseHandle handle = ScrollViewCarouselEffect::New();
403
404   DALI_TEST_CHECK( handle );
405
406   effect = ScrollViewCarouselEffect::DownCast(handle);
407
408   DALI_TEST_CHECK( effect );
409   END_TEST;
410 }
411
412 int UtcDaliScrollViewDepthEffectSetup(void)
413 {
414   tet_infoline(" UtcDaliScrollViewDepthEffectSetup");
415
416   ScrollViewDepthEffect effect;
417
418   DALI_TEST_CHECK( !effect );
419
420   BaseHandle handle = ScrollViewDepthEffect::New();
421
422   DALI_TEST_CHECK( handle );
423
424   effect = ScrollViewDepthEffect::DownCast(handle);
425
426   DALI_TEST_CHECK( effect );
427   END_TEST;
428 }
429
430
431 int UtcDaliScrollViewPageCubeEffectTest(void)
432 {
433   ToolkitTestApplication application;
434   tet_infoline(" UtcDaliScrollViewPageCubeEffectTest");
435
436   Vector2 size = Stage::GetCurrent().GetSize();
437
438   ScrollView scrollView = SetupTestScrollView(1, 3, size);
439   Actor testPage = gPages[1];
440   Wait(application, 500);
441
442   ScrollViewPageCubeEffect effect = ScrollViewPageCubeEffect::New();
443   scrollView.ApplyEffect(effect);
444
445   for(ActorIter pageIter = gPages.begin(); pageIter != gPages.end(); ++pageIter)
446   {
447     Actor page = *pageIter;
448     page.RemoveConstraints();
449     Constraint constraint = Constraint::New<Vector3>( page, Actor::Property::SIZE, EqualToConstraint() );
450     constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
451     constraint.Apply();
452     effect.ApplyToPage(page, Vector2(Math::PI_2, 0.0f));
453   }
454   Wait(application);
455
456   scrollView.ScrollTo(1);
457   while(!gOnScrollCompleteCalled)
458   {
459     Wait(application);
460   }
461   // test that the first page has reached centre of screen
462   Vector3 pagePos = testPage.GetCurrentPosition();
463   DALI_TEST_EQUALS(pagePos, Vector3::ZERO, Math::MACHINE_EPSILON_0, TEST_LOCATION);
464   CleanupTest();
465   END_TEST;
466 }
467
468 int UtcDaliScrollViewPageCarouselEffectTest(void)
469 {
470   ToolkitTestApplication application;
471   tet_infoline(" UtcDaliScrollViewPageCarouselEffectTest");
472
473   Vector2 size = Stage::GetCurrent().GetSize();
474
475   ScrollView scrollView = SetupTestScrollView(1, 3, size);
476   Actor testPage = gPages[1];
477   Wait(application, 500);
478
479   ScrollViewPageCarouselEffect effect = ScrollViewPageCarouselEffect::New();
480   scrollView.ApplyEffect(effect);
481
482   for(ActorIter pageIter = gPages.begin(); pageIter != gPages.end(); ++pageIter)
483   {
484     Actor page = *pageIter;
485     page.RemoveConstraints();
486     Constraint constraint = Constraint::New<Vector3>( page, Actor::Property::SIZE, EqualToConstraint() );
487     constraint.AddSource( ParentSource( Actor::Property::SIZE ) );
488     constraint.Apply();
489     effect.ApplyToPage(page);
490   }
491   Wait(application);
492
493   scrollView.ScrollTo(1, 0.5f, DirectionBiasNone);
494   while(!gOnScrollCompleteCalled)
495   {
496     Wait(application);
497   }
498   // test that the first page has reached centre of screen
499   Vector3 pagePos = testPage.GetCurrentPosition();
500   DALI_TEST_EQUALS(pagePos, Vector3::ZERO, Math::MACHINE_EPSILON_0, TEST_LOCATION);
501   CleanupTest();
502   END_TEST;
503 }
504
505 int UtcDaliScrollViewCarouselEffectTest(void)
506 {
507   ToolkitTestApplication application;
508   tet_infoline(" UtcDaliScrollViewCarouselEffectTest");
509
510   Vector2 size = Stage::GetCurrent().GetSize();
511
512   ScrollView scrollView = SetupTestScrollView(1, 3, size);
513   Actor testPage = gPages[1];
514   Wait(application, 500);
515
516   ScrollViewCarouselEffect effect = ScrollViewCarouselEffect::New();
517   scrollView.ApplyEffect(effect);
518
519   Actor actor = AddActorToPage(testPage, 0.5f, 0.5f, 3, 3);
520   Wait(application);
521   Vector3 actorPrePosition = actor.GetCurrentPosition();
522
523   effect.ApplyToActor( actor, Vector2(1.2f, 1.2f) );
524
525   scrollView.ScrollTo(Vector3(size.x, 0.0f, 0.0f), 0.5f, DirectionBiasNone, DirectionBiasNone);
526   while(!gOnScrollCompleteCalled)
527   {
528     Wait(application);
529   }
530   // test that the first page has reached centre of screen
531   Vector3 actorPostPosition = actor.GetCurrentPosition();
532   // just check the actor has moved
533   DALI_TEST_CHECK((actorPostPosition - actorPrePosition).Length() > Math::MACHINE_EPSILON_1);
534   CleanupTest();
535   END_TEST;
536 }
537
538 int UtcDaliScrollViewDepthEffectTest(void)
539 {
540   ToolkitTestApplication application;
541   tet_infoline(" UtcDaliScrollViewDepthEffectTest");
542
543   Vector2 size = Stage::GetCurrent().GetSize();
544
545   ScrollView scrollView = SetupTestScrollView(1, 3, size);
546   Actor testPage = gPages[1];
547   Wait(application, 500);
548
549   ScrollViewDepthEffect effect = ScrollViewDepthEffect::New();
550   scrollView.ApplyEffect(effect);
551
552   Actor actor = AddActorToPage(testPage, 0.5f, 0.5f, 3, 3);
553   Wait(application);
554   Vector3 actorPrePosition = actor.GetCurrentPosition();
555
556   const Vector2 positionExtent(0.5f, 2.5f);
557   const Vector2 offsetExtent(1.0f, 1.0f);
558   const float positionScale(1.5f);
559   const float scaleExtent(0.5f);
560
561   effect.ApplyToActor( actor, positionExtent, offsetExtent, positionScale, scaleExtent );
562
563   scrollView.ScrollTo(1);
564   while(!gOnScrollCompleteCalled)
565   {
566     Wait(application);
567   }
568   // test that the first page has reached centre of screen
569   Vector3 actorPostPosition = actor.GetCurrentPosition();
570   // just check the actor has moved
571   DALI_TEST_CHECK((actorPostPosition - actorPrePosition).Length() > Math::MACHINE_EPSILON_1);
572   CleanupTest();
573   END_TEST;
574 }