2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <dali-toolkit-test-suite-utils.h>
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali/integration-api/events/touch-event-integ.h>
24 #include <dali/integration-api/events/pan-gesture-event.h>
28 using namespace Toolkit;
30 void dali_scroll_view_effect_startup(void)
32 test_return_value = TET_UNDEF;
35 void dali_scroll_view_effect_cleanup(void)
37 test_return_value = TET_PASS;
43 const int MILLISECONDS_PER_SECOND = 1000;
44 const int RENDER_FRAME_INTERVAL = 16; ///< Duration of each frame in ms. (at approx 60FPS)
45 const int RENDER_ANIMATION_TEST_DURATION_MS = 1000; ///< 1000ms to test animation
46 const int RENDER_DELAY_SCROLL = 1000; ///< duration to wait for any scroll to complete.
49 * Simulate time passed by.
51 * @note this will always process at least 1 frame (1/60 sec)
53 * @param application Test application instance
54 * @param duration Time to pass in milliseconds.
55 * @return The actual time passed in milliseconds
57 int Wait(ToolkitTestApplication& application, int duration = 0)
61 for(int i = 0; i <= ( duration / RENDER_FRAME_INTERVAL); i++)
63 application.SendNotification();
64 application.Render(RENDER_FRAME_INTERVAL);
65 time += RENDER_FRAME_INTERVAL;
72 * Creates a Ruler that snaps to a specified grid size.
73 * If that grid size is 0.0 then this ruler does not
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.
80 RulerPtr CreateRuler(float gridSize = 0.0f)
82 if(gridSize <= Math::MACHINE_EPSILON_0)
84 return new DefaultRuler();
86 return new FixedRuler(gridSize);
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.
96 static ActorContainer gPages; ///< Keeps track of all the pages for applying effects.
98 static void ResetScrollCallbackResults()
100 gOnScrollStartCalled = false;
101 gOnScrollUpdateCalled = false;
102 gOnScrollCompleteCalled = false;
106 * Invoked when scrolling starts.
108 * @param[in] position The current scroll position.
110 static void OnScrollStart( const Vector3& position )
112 gOnScrollStartCalled = true;
116 * Invoked when scrolling updates (via dragging)
118 * @param[in] position The current scroll position.
120 static void OnScrollUpdate( const Vector3& position )
122 gOnScrollUpdateCalled = true;
126 * Invoked when scrolling finishes
128 * @param[in] position The current scroll position.
130 static void OnScrollComplete( const Vector3& position )
132 gOnScrollCompleteCalled = true;
136 ScrollView SetupTestScrollView(int rows, int columns, Vector2 size)
138 ScrollView scrollView = ScrollView::New();
139 scrollView.SetSize(size);
140 scrollView.SetAnchorPoint(AnchorPoint::CENTER);
141 scrollView.SetParentOrigin(ParentOrigin::CENTER);
142 scrollView.ApplyConstraint( Constraint::New<Dali::Vector3>( Dali::Actor::SIZE, Dali::ParentSource( Dali::Actor::SIZE ), Dali::EqualToConstraint() ) );
143 scrollView.SetWrapMode(false);
144 scrollView.ScrollStartedSignal().Connect( &OnScrollStart );
145 scrollView.ScrollUpdatedSignal().Connect( &OnScrollUpdate );
146 scrollView.ScrollCompletedSignal().Connect( &OnScrollComplete );
147 Stage::GetCurrent().Add( scrollView );
148 RulerPtr rulerX = CreateRuler(size.width);
149 RulerPtr rulerY = CreateRuler(size.height);
152 rulerX->SetDomain(RulerDomain(0.0f, size.width * columns));
160 rulerY->SetDomain(RulerDomain(0.0f, size.width * rows));
167 scrollView.SetRulerX( rulerX );
168 scrollView.SetRulerY( rulerY );
169 Stage::GetCurrent().Add( scrollView );
171 Actor container = Actor::New();
172 container.SetParentOrigin(ParentOrigin::CENTER);
173 container.SetAnchorPoint(AnchorPoint::CENTER);
174 container.SetSize( size );
175 scrollView.Add( container );
176 container.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
179 for(int row = 0;row<rows;row++)
181 for(int column = 0;column<columns;column++)
183 Actor page = Actor::New();
184 page.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
185 page.SetParentOrigin( ParentOrigin::CENTER );
186 page.SetAnchorPoint( AnchorPoint::CENTER );
187 page.SetPosition( column * size.x, row * size.y );
190 gPages.push_back(page);
194 ResetScrollCallbackResults();
201 ResetScrollCallbackResults();
204 Actor AddActorToPage(Actor page, float x, float y, float cols, float rows)
206 Stage stage = Stage::GetCurrent();
207 Vector2 stageSize = stage.GetSize();
209 const float margin = 10.0f;
210 const Vector2 actorSize((stageSize.x / cols) - margin, (stageSize.y / rows) - margin);
212 Actor actor = Actor::New();
213 actor.SetParentOrigin( ParentOrigin::CENTER );
214 actor.SetAnchorPoint( AnchorPoint::CENTER );
216 Vector3 position( margin * 0.5f + (actorSize.x + margin) * x - stageSize.width * 0.5f,
217 margin * 0.5f + (actorSize.y + margin) * y - stageSize.height * 0.5f,
219 Vector3 positionEnd( margin * 0.5f + (actorSize.x + margin) * (x + cols) - stageSize.width * 0.5f - margin,
220 margin * 0.5f + (actorSize.y + margin) * (y + rows) - stageSize.height * 0.5f - margin,
222 Vector3 size(positionEnd - position);
223 actor.SetPosition( position + size * 0.5f);
224 actor.SetSize( positionEnd - position );
229 } // unnamed namespace
232 int UtcDaliScrollViewPageCubeEffectSetup(void)
234 tet_infoline(" UtcDaliScrollViewPageCubeEffectSetup");
236 ScrollViewPageCubeEffect effect;
238 DALI_TEST_CHECK( !effect );
240 BaseHandle handle = ScrollViewPageCubeEffect::New();
242 DALI_TEST_CHECK( handle );
244 effect = ScrollViewPageCubeEffect::DownCast(handle);
246 DALI_TEST_CHECK( effect );
251 int UtcDaliScrollViewPageCarouselEffectSetup(void)
253 tet_infoline(" UtcDaliScrollViewCarouselEffectSetup");
255 ScrollViewPageCarouselEffect effect;
257 DALI_TEST_CHECK( !effect );
259 BaseHandle handle = ScrollViewPageCarouselEffect::New();
261 DALI_TEST_CHECK( handle );
263 effect = ScrollViewPageCarouselEffect::DownCast(handle);
265 DALI_TEST_CHECK( effect );
269 int UtcDaliScrollViewCarouselEffectSetup(void)
271 tet_infoline(" UtcDaliScrollViewCarouselEffectSetup");
273 ScrollViewCarouselEffect effect;
275 DALI_TEST_CHECK( !effect );
277 BaseHandle handle = ScrollViewCarouselEffect::New();
279 DALI_TEST_CHECK( handle );
281 effect = ScrollViewCarouselEffect::DownCast(handle);
283 DALI_TEST_CHECK( effect );
287 int UtcDaliScrollViewDepthEffectSetup(void)
289 tet_infoline(" UtcDaliScrollViewDepthEffectSetup");
291 ScrollViewDepthEffect effect;
293 DALI_TEST_CHECK( !effect );
295 BaseHandle handle = ScrollViewDepthEffect::New();
297 DALI_TEST_CHECK( handle );
299 effect = ScrollViewDepthEffect::DownCast(handle);
301 DALI_TEST_CHECK( effect );
306 int UtcDaliScrollViewPageCubeEffectTest(void)
308 ToolkitTestApplication application;
309 tet_infoline(" UtcDaliScrollViewPageCubeEffectTest");
311 Vector2 size = Stage::GetCurrent().GetSize();
313 ScrollView scrollView = SetupTestScrollView(1, 3, size);
314 Actor testPage = gPages[1];
315 Wait(application, 500);
317 ScrollViewPageCubeEffect effect = ScrollViewPageCubeEffect::New();
318 scrollView.ApplyEffect(effect);
320 for(ActorIter pageIter = gPages.begin(); pageIter != gPages.end(); ++pageIter)
322 Actor page = *pageIter;
323 page.RemoveConstraints();
324 page.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
325 effect.ApplyToPage(page, Vector2(Math::PI_2, 0.0f));
329 scrollView.ScrollTo(1);
330 while(!gOnScrollCompleteCalled)
334 // test that the first page has reached centre of screen
335 Vector3 pagePos = testPage.GetCurrentPosition();
336 DALI_TEST_EQUALS(pagePos, Vector3::ZERO, Math::MACHINE_EPSILON_0, TEST_LOCATION);
341 int UtcDaliScrollViewPageCarouselEffectTest(void)
343 ToolkitTestApplication application;
344 tet_infoline(" UtcDaliScrollViewPageCarouselEffectTest");
346 Vector2 size = Stage::GetCurrent().GetSize();
348 ScrollView scrollView = SetupTestScrollView(1, 3, size);
349 Actor testPage = gPages[1];
350 Wait(application, 500);
352 ScrollViewPageCarouselEffect effect = ScrollViewPageCarouselEffect::New();
353 scrollView.ApplyEffect(effect);
355 for(ActorIter pageIter = gPages.begin(); pageIter != gPages.end(); ++pageIter)
357 Actor page = *pageIter;
358 page.RemoveConstraints();
359 page.ApplyConstraint( Constraint::New<Vector3>( Actor::SIZE, ParentSource( Actor::SIZE ), EqualToConstraint() ) );
360 effect.ApplyToPage(page);
364 scrollView.ScrollTo(1, 0.5f, DirectionBiasNone);
365 while(!gOnScrollCompleteCalled)
369 // test that the first page has reached centre of screen
370 Vector3 pagePos = testPage.GetCurrentPosition();
371 DALI_TEST_EQUALS(pagePos, Vector3::ZERO, Math::MACHINE_EPSILON_0, TEST_LOCATION);
376 int UtcDaliScrollViewCarouselEffectTest(void)
378 ToolkitTestApplication application;
379 tet_infoline(" UtcDaliScrollViewCarouselEffectTest");
381 Vector2 size = Stage::GetCurrent().GetSize();
383 ScrollView scrollView = SetupTestScrollView(1, 3, size);
384 Actor testPage = gPages[1];
385 Wait(application, 500);
387 ScrollViewCarouselEffect effect = ScrollViewCarouselEffect::New();
388 scrollView.ApplyEffect(effect);
390 Actor actor = AddActorToPage(testPage, 0.5f, 0.5f, 3, 3);
392 Vector3 actorPrePosition = actor.GetCurrentPosition();
394 effect.ApplyToActor( actor, Vector2(1.2f, 1.2f) );
396 scrollView.ScrollTo(Vector3(size.x, 0.0f, 0.0f), 0.5f, DirectionBiasNone, DirectionBiasNone);
397 while(!gOnScrollCompleteCalled)
401 // test that the first page has reached centre of screen
402 Vector3 actorPostPosition = actor.GetCurrentPosition();
403 // just check the actor has moved
404 DALI_TEST_CHECK((actorPostPosition - actorPrePosition).Length() > Math::MACHINE_EPSILON_1);
409 int UtcDaliScrollViewDepthEffectTest(void)
411 ToolkitTestApplication application;
412 tet_infoline(" UtcDaliScrollViewDepthEffectTest");
414 Vector2 size = Stage::GetCurrent().GetSize();
416 ScrollView scrollView = SetupTestScrollView(1, 3, size);
417 Actor testPage = gPages[1];
418 Wait(application, 500);
420 ScrollViewDepthEffect effect = ScrollViewDepthEffect::New();
421 scrollView.ApplyEffect(effect);
423 Actor actor = AddActorToPage(testPage, 0.5f, 0.5f, 3, 3);
425 Vector3 actorPrePosition = actor.GetCurrentPosition();
427 const Vector2 positionExtent(0.5f, 2.5f);
428 const Vector2 offsetExtent(1.0f, 1.0f);
429 const float positionScale(1.5f);
430 const float scaleExtent(0.5f);
432 effect.ApplyToActor( actor, positionExtent, offsetExtent, positionScale, scaleExtent );
434 scrollView.ScrollTo(1);
435 while(!gOnScrollCompleteCalled)
439 // test that the first page has reached centre of screen
440 Vector3 actorPostPosition = actor.GetCurrentPosition();
441 // just check the actor has moved
442 DALI_TEST_CHECK((actorPostPosition - actorPrePosition).Length() > Math::MACHINE_EPSILON_1);