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