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