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.
22 #include <dali/public-api/dali-core.h>
23 #include <dali/integration-api/context-notifier.h>
24 #include <dali/integration-api/events/key-event-integ.h>
25 #include <dali/integration-api/events/touch-event-integ.h>
26 #include <dali/devel-api/dynamics/dynamics.h>
28 #include <dali-test-suite-utils.h>
32 void stage_test_startup(void)
34 test_return_value = TET_UNDEF;
37 void stage_test_cleanup(void)
39 test_return_value = TET_PASS;
46 * Functor for EventProcessingFinished signal
48 struct EventProcessingFinishedFunctor
51 * @param[in] eventProcessingFinished reference to a boolean variable used to check if signal has been called.
53 EventProcessingFinishedFunctor( bool& eventProcessingFinished )
54 : mEventProcessingFinished( eventProcessingFinished )
59 mEventProcessingFinished = true;
62 bool& mEventProcessingFinished;
65 // Stores data that is populated in the key-event callback and will be read by the TET cases
66 struct KeyEventSignalData
69 : functorCalled(false)
74 functorCalled = false;
76 receivedKeyEvent.keyModifier = 0;
77 receivedKeyEvent.keyPressedName.clear();
78 receivedKeyEvent.keyPressed.clear();
82 KeyEvent receivedKeyEvent;
85 // Functor that sets the data when called
86 struct KeyEventReceivedFunctor
88 KeyEventReceivedFunctor( KeyEventSignalData& data ) : signalData( data ) { }
90 bool operator()( const KeyEvent& keyEvent )
92 signalData.functorCalled = true;
93 signalData.receivedKeyEvent = keyEvent;
98 KeyEventSignalData& signalData;
101 // Stores data that is populated in the touched signal callback and will be read by the TET cases
102 struct TouchedSignalData
105 : functorCalled(false)
110 functorCalled = false;
112 receivedTouchEvent.points.clear();
113 receivedTouchEvent.time = 0;
117 TouchEvent receivedTouchEvent;
120 // Functor that sets the data when touched signal is received
121 struct TouchedFunctor
123 TouchedFunctor( TouchedSignalData& data ) : signalData( data ) { }
125 void operator()( const TouchEvent& touch )
127 signalData.functorCalled = true;
128 signalData.receivedTouchEvent = touch;
131 TouchedSignalData& signalData;
134 bool DummyTouchCallback( Actor actor, const TouchEvent& touch )
140 struct ContextStatusFunctor
142 ContextStatusFunctor(bool& calledFlag) : mCalledFlag( calledFlag )
159 struct SceneCreatedStatusFunctor
161 SceneCreatedStatusFunctor(bool& calledFlag) : mCalledFlag( calledFlag )
178 } // unnamed namespace
181 int UtcDaliStageDefaultConstructor(void)
183 TestApplication application;
186 DALI_TEST_CHECK(!stage);
190 int UtcDaliStageDestructor(void)
192 TestApplication application;
193 Stage* stage = new Stage();
197 DALI_TEST_CHECK( true );
201 int UtcDaliStageGetCurrent(void)
203 TestApplication application;
204 Stage stage = Stage::GetCurrent();
206 DALI_TEST_CHECK(stage);
210 int UtcDaliStageAssign(void)
212 TestApplication application;
213 Stage stage = Stage::GetCurrent();
217 DALI_TEST_CHECK(stage2);
221 int UtcDaliStageIsInstalled(void)
223 DALI_TEST_CHECK(!Stage::IsInstalled());
225 TestApplication application;
229 DALI_TEST_CHECK(Stage::IsInstalled());
233 int UtcDaliStageAdd(void)
235 TestApplication application;
237 Stage stage = Stage::GetCurrent();
239 Actor actor = Actor::New();
240 DALI_TEST_CHECK(!actor.OnStage());
243 DALI_TEST_CHECK(actor.OnStage());
247 int UtcDaliStageRemove(void)
249 TestApplication application;
251 Stage stage = Stage::GetCurrent();
253 Actor actor = Actor::New();
254 DALI_TEST_CHECK(!actor.OnStage());
257 DALI_TEST_CHECK(actor.OnStage());
260 DALI_TEST_CHECK(!actor.OnStage());
264 int UtcDaliStageGetSize(void)
266 TestApplication application;
268 Stage stage = Stage::GetCurrent();
270 Vector2 size = stage.GetSize();
272 DALI_TEST_EQUALS(size.width, static_cast<float>(TestApplication::DEFAULT_SURFACE_WIDTH), TEST_LOCATION);
273 DALI_TEST_EQUALS(size.height, static_cast<float>(TestApplication::DEFAULT_SURFACE_HEIGHT), TEST_LOCATION);
277 int UtcDaliStageGetDpi01(void)
279 TestApplication application; // Initializes core DPI to default values
281 Stage stage = Stage::GetCurrent();
283 // Test the default DPI.
284 Vector2 dpi = stage.GetDpi();
285 DALI_TEST_EQUALS(dpi.x, static_cast<float>(TestApplication::DEFAULT_HORIZONTAL_DPI), TEST_LOCATION);
286 DALI_TEST_EQUALS(dpi.y, static_cast<float>(TestApplication::DEFAULT_VERTICAL_DPI), TEST_LOCATION);
290 int UtcDaliStageGetDpi02(void)
292 TestApplication application; // Initializes core DPI to default values
294 // Test that setting core DPI explicitly also sets up the Stage's DPI.
295 application.GetCore().SetDpi(200, 180);
297 Stage stage = Stage::GetCurrent();
298 Vector2 dpi = stage.GetDpi();
299 DALI_TEST_EQUALS(dpi.x, 200.0f, TEST_LOCATION);
300 DALI_TEST_EQUALS(dpi.y, 180.0f, TEST_LOCATION);
305 int UtcDaliStageGetDpi03(void)
307 TestApplication application(480, 800, 72.0f, 120.0f); // Initializes core DPI with specific values
309 Stage stage = Stage::GetCurrent();
311 // Test that setting core DPI explicitly also sets up the Stage's DPI.
312 Vector2 dpi = stage.GetDpi();
313 DALI_TEST_EQUALS(dpi.x, 72.0f, TEST_LOCATION);
314 DALI_TEST_EQUALS(dpi.y, 120.0f, TEST_LOCATION);
318 int UtcDaliStageInitializeDynamicsP(void)
320 TestApplication application;
321 Stage stage = Stage::GetCurrent();
322 DynamicsWorld world = stage.InitializeDynamics( DynamicsWorldConfig::New() );
324 #if !defined(DYNAMICS_SUPPORT)
325 DALI_TEST_CHECK(true);
327 DALI_TEST_CHECK( world );
333 int UtcDaliStageGetLayerCount(void)
335 TestApplication application;
337 Stage stage = Stage::GetCurrent();
339 // Initially we have a default layer
340 DALI_TEST_EQUALS(stage.GetLayerCount(), 1u, TEST_LOCATION);
342 Layer layer = Layer::New();
345 DALI_TEST_EQUALS(stage.GetLayerCount(), 2u, TEST_LOCATION);
349 int UtcDaliStageGetLayer(void)
351 TestApplication application;
353 Stage stage = Stage::GetCurrent();
355 Layer rootLayer = stage.GetLayer(0);
356 DALI_TEST_CHECK(rootLayer);
358 Layer layer = Layer::New();
361 Layer sameLayer = stage.GetLayer(1);
362 DALI_TEST_CHECK(layer == sameLayer);
367 int UtcDaliStageGetRootLayer(void)
369 TestApplication application;
371 Stage stage = Stage::GetCurrent();
373 Layer rootLayer = stage.GetLayer(0);
374 DALI_TEST_CHECK( rootLayer );
376 Layer layer = Layer::New();
378 layer.LowerToBottom();
380 DALI_TEST_CHECK( stage.GetRootLayer() == rootLayer );
384 int UtcDaliStageSetBackgroundColor(void)
386 TestApplication application;
388 Stage stage = Stage::GetCurrent();
390 Vector4 testColor(0.1f, 0.2f, 0.3f, 1.0f);
391 stage.SetBackgroundColor(testColor);
393 DALI_TEST_EQUALS(testColor, stage.GetBackgroundColor(), TEST_LOCATION);
397 int UtcDaliStageGetBackgroundColor(void)
399 TestApplication application;
401 Stage stage = Stage::GetCurrent();
403 DALI_TEST_EQUALS(Stage::DEFAULT_BACKGROUND_COLOR, stage.GetBackgroundColor(), TEST_LOCATION);
407 int UtcDaliStageKeepRendering(void)
409 TestApplication application;
411 Stage stage = Stage::GetCurrent();
413 // Run core until it wants to sleep
414 bool keepUpdating( true );
415 while ( keepUpdating )
417 application.SendNotification();
418 keepUpdating = application.Render(1000.0f /*1 second*/);
421 // Force rendering for the next 5 seconds
422 stage.KeepRendering( 5.0f );
424 application.SendNotification();
426 // Test that core wants to sleep after 10 seconds
427 keepUpdating = application.Render(1000.0f /*1 second*/);
428 DALI_TEST_CHECK( keepUpdating );
429 keepUpdating = application.Render(1000.0f /*2 seconds*/);
430 DALI_TEST_CHECK( keepUpdating );
431 keepUpdating = application.Render(1000.0f /*3 seconds*/);
432 DALI_TEST_CHECK( keepUpdating );
433 keepUpdating = application.Render(1000.0f /*4 seconds*/);
434 DALI_TEST_CHECK( keepUpdating );
435 keepUpdating = application.Render(1000.0f /*5 seconds*/);
436 DALI_TEST_CHECK( !keepUpdating );
440 int UtcDaliStageEventProcessingFinished(void)
442 TestApplication application;
443 Stage stage = Stage::GetCurrent();
445 bool eventProcessingFinished = false;
446 EventProcessingFinishedFunctor functor( eventProcessingFinished );
447 stage.EventProcessingFinishedSignal().Connect( &application, functor );
449 Actor actor( Actor::New() );
452 application.SendNotification();
453 application.Render();
455 DALI_TEST_CHECK( eventProcessingFinished );
460 int UtcDaliStageSignalKeyEvent(void)
462 TestApplication application;
463 Stage stage = Stage::GetCurrent();
465 KeyEventSignalData data;
466 KeyEventReceivedFunctor functor( data );
467 stage.KeyEventSignal().Connect( &application, functor );
469 Integration::KeyEvent event( "i","i", 0, 0, 0, Integration::KeyEvent::Down );
470 application.ProcessEvent( event );
472 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
473 DALI_TEST_CHECK( event.keyModifier == data.receivedKeyEvent.keyModifier );
474 DALI_TEST_CHECK( event.keyName == data.receivedKeyEvent.keyPressedName );
475 DALI_TEST_CHECK( event.keyString == data.receivedKeyEvent.keyPressed );
476 DALI_TEST_CHECK( event.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
480 Integration::KeyEvent event2( "i","i", 0, 0, 0, Integration::KeyEvent::Up );
481 application.ProcessEvent( event2 );
483 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
484 DALI_TEST_CHECK( event2.keyModifier == data.receivedKeyEvent.keyModifier );
485 DALI_TEST_CHECK( event2.keyName == data.receivedKeyEvent.keyPressedName );
486 DALI_TEST_CHECK( event2.keyString == data.receivedKeyEvent.keyPressed );
487 DALI_TEST_CHECK( event2.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
491 Integration::KeyEvent event3( "a","a", 0, 0, 0, Integration::KeyEvent::Down );
492 application.ProcessEvent( event3 );
494 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
495 DALI_TEST_CHECK( event3.keyModifier == data.receivedKeyEvent.keyModifier );
496 DALI_TEST_CHECK( event3.keyName == data.receivedKeyEvent.keyPressedName );
497 DALI_TEST_CHECK( event3.keyString == data.receivedKeyEvent.keyPressed );
498 DALI_TEST_CHECK( event3.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
502 Integration::KeyEvent event4( "a","a", 0, 0, 0, Integration::KeyEvent::Up );
503 application.ProcessEvent( event4 );
505 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
506 DALI_TEST_CHECK( event4.keyModifier == data.receivedKeyEvent.keyModifier );
507 DALI_TEST_CHECK( event4.keyName == data.receivedKeyEvent.keyPressedName );
508 DALI_TEST_CHECK( event4.keyString == data.receivedKeyEvent.keyPressed );
509 DALI_TEST_CHECK( event4.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
513 int UtcDaliStageTouchedSignal(void)
515 TestApplication application;
516 Stage stage = Stage::GetCurrent();
518 TouchedSignalData data;
519 TouchedFunctor functor( data );
520 stage.TouchedSignal().Connect( &application, functor );
523 application.SendNotification();
524 application.Render();
526 // NO ACTORS, SINGLE TOUCH, DOWN, MOTION THEN UP
528 Integration::TouchEvent touchEvent;
529 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
530 application.ProcessEvent( touchEvent );
532 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
533 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
534 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
537 touchEvent.points[0].state = TouchPoint::Motion;
538 touchEvent.points[0].screen.x = 12.0f; // Some motion
539 application.ProcessEvent( touchEvent );
541 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
544 touchEvent.points[0].state = TouchPoint::Up;
545 application.ProcessEvent( touchEvent );
547 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
548 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
549 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
553 // Add an actor to the scene
555 Actor actor = Actor::New();
556 actor.SetSize( 100.0f, 100.0f );
557 actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
558 actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
559 actor.TouchedSignal().Connect( &DummyTouchCallback );
563 application.SendNotification();
564 application.Render();
566 // ACTOR ON SCENE, SINGLE TOUCH, DOWN IN ACTOR, MOTION THEN UP OUTSIDE ACTOR
568 Integration::TouchEvent touchEvent;
569 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
570 application.ProcessEvent( touchEvent );
572 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
573 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
574 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].hitActor == actor );
577 touchEvent.points[0].state = TouchPoint::Motion;
578 touchEvent.points[0].screen.x = 150.0f; // Some motion
579 application.ProcessEvent( touchEvent );
581 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
584 touchEvent.points[0].state = TouchPoint::Up;
585 application.ProcessEvent( touchEvent );
587 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
588 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
589 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
593 // INTERRUPTED BEFORE DOWN AND INTERRUPTED AFTER DOWN
595 Integration::TouchEvent touchEvent;
596 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Interrupted, 10.0f, 10.0f ) );
597 application.ProcessEvent( touchEvent );
599 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
600 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
601 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
602 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Interrupted );
605 touchEvent.points[0].state = TouchPoint::Down;
606 application.ProcessEvent( touchEvent );
608 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
609 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
610 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].hitActor == actor );
611 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Down );
614 touchEvent.points[0].state = TouchPoint::Interrupted;
615 application.ProcessEvent( touchEvent );
617 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
618 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
619 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
620 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Interrupted );
624 // MULTIPLE TOUCH, SHOULD ONLY RECEIVE TOUCH ON FIRST DOWN AND LAST UP
626 Integration::TouchEvent touchEvent;
629 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
630 application.ProcessEvent( touchEvent );
631 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
635 touchEvent.points[0].state = TouchPoint::Stationary;
636 touchEvent.points.push_back( TouchPoint( 1, TouchPoint::Down, 50.0f, 50.0f ) );
637 application.ProcessEvent( touchEvent );
638 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
641 // Primary point is up
642 touchEvent.points[0].state = TouchPoint::Up;
643 touchEvent.points[1].state = TouchPoint::Stationary;
644 application.ProcessEvent( touchEvent );
645 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
648 // Remove 1st point now, 2nd point is now in motion
649 touchEvent.points.erase( touchEvent.points.begin() );
650 touchEvent.points[0].state = TouchPoint::Motion;
651 touchEvent.points[0].screen.x = 150.0f;
652 application.ProcessEvent( touchEvent );
653 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
657 touchEvent.points[0].state = TouchPoint::Up;
658 application.ProcessEvent( touchEvent );
659 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
665 int UtcDaliStageContextLostRegainedSignals(void)
668 Stage stage = Stage::GetCurrent();
670 bool contextLost = false;
671 bool contextRegained = false;
672 ContextStatusFunctor contextLostFunctor( contextLost );
673 ContextStatusFunctor contextRegainedFunctor( contextRegained );
674 stage.ContextLostSignal().Connect(&app, contextLostFunctor );
675 stage.ContextRegainedSignal().Connect(&app, contextRegainedFunctor );
677 Integration::Core& core = app.GetCore();
678 Integration::ContextNotifierInterface* notifier = core.GetContextNotifier();
679 notifier->NotifyContextLost();
680 DALI_TEST_EQUALS( contextLost, true, TEST_LOCATION );
682 notifier->NotifyContextRegained();
683 DALI_TEST_EQUALS( contextRegained, true, TEST_LOCATION );
688 int UtcDaliStageSceneCreatedSignal(void)
691 Stage stage = Stage::GetCurrent();
693 bool signalCalled = false;
694 SceneCreatedStatusFunctor sceneCreatedFunctor( signalCalled );
695 stage.SceneCreatedSignal().Connect(&app, sceneCreatedFunctor );
697 Integration::Core& core = app.GetCore();
699 DALI_TEST_EQUALS( signalCalled, true, TEST_LOCATION );