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/integration-api/events/wheel-event-integ.h>
27 #include <dali/devel-api/dynamics/dynamics.h>
29 #include <dali-test-suite-utils.h>
33 void stage_test_startup(void)
35 test_return_value = TET_UNDEF;
38 void stage_test_cleanup(void)
40 test_return_value = TET_PASS;
47 * Functor for EventProcessingFinished signal
49 struct EventProcessingFinishedFunctor
52 * @param[in] eventProcessingFinished reference to a boolean variable used to check if signal has been called.
54 EventProcessingFinishedFunctor( bool& eventProcessingFinished )
55 : mEventProcessingFinished( eventProcessingFinished )
60 mEventProcessingFinished = true;
63 bool& mEventProcessingFinished;
66 // Stores data that is populated in the key-event callback and will be read by the TET cases
67 struct KeyEventSignalData
70 : functorCalled(false)
75 functorCalled = false;
77 receivedKeyEvent.keyModifier = 0;
78 receivedKeyEvent.keyPressedName.clear();
79 receivedKeyEvent.keyPressed.clear();
83 KeyEvent receivedKeyEvent;
86 // Functor that sets the data when called
87 struct KeyEventReceivedFunctor
89 KeyEventReceivedFunctor( KeyEventSignalData& data ) : signalData( data ) { }
91 bool operator()( const KeyEvent& keyEvent )
93 signalData.functorCalled = true;
94 signalData.receivedKeyEvent = keyEvent;
99 KeyEventSignalData& signalData;
102 // Stores data that is populated in the touched signal callback and will be read by the TET cases
103 struct TouchedSignalData
106 : functorCalled(false)
111 functorCalled = false;
113 receivedTouchEvent.points.clear();
114 receivedTouchEvent.time = 0;
118 TouchEvent receivedTouchEvent;
121 // Functor that sets the data when touched signal is received
122 struct TouchedFunctor
124 TouchedFunctor( TouchedSignalData& data ) : signalData( data ) { }
126 void operator()( const TouchEvent& touch )
128 signalData.functorCalled = true;
129 signalData.receivedTouchEvent = touch;
132 TouchedSignalData& signalData;
135 // Stores data that is populated in the wheel-event callback and will be read by the TET cases
136 struct WheelEventSignalData
138 WheelEventSignalData()
139 : functorCalled(false)
144 functorCalled = false;
148 WheelEvent receivedWheelEvent;
151 // Functor that sets the data when wheel-event signal is received
152 struct WheelEventReceivedFunctor
154 WheelEventReceivedFunctor( WheelEventSignalData& data ) : signalData( data ) { }
156 bool operator()( const WheelEvent& wheelEvent )
158 signalData.functorCalled = true;
159 signalData.receivedWheelEvent = wheelEvent;
164 WheelEventSignalData& signalData;
167 bool DummyTouchCallback( Actor actor, const TouchEvent& touch )
173 struct ContextStatusFunctor
175 ContextStatusFunctor(bool& calledFlag) : mCalledFlag( calledFlag )
192 struct SceneCreatedStatusFunctor
194 SceneCreatedStatusFunctor(bool& calledFlag) : mCalledFlag( calledFlag )
211 } // unnamed namespace
214 int UtcDaliStageDefaultConstructor(void)
216 TestApplication application;
219 DALI_TEST_CHECK(!stage);
223 int UtcDaliStageDestructor(void)
225 TestApplication application;
226 Stage* stage = new Stage();
230 DALI_TEST_CHECK( true );
234 int UtcDaliStageGetCurrent(void)
236 TestApplication application;
237 Stage stage = Stage::GetCurrent();
239 DALI_TEST_CHECK(stage);
243 int UtcDaliStageAssign(void)
245 TestApplication application;
246 Stage stage = Stage::GetCurrent();
250 DALI_TEST_CHECK(stage2);
254 int UtcDaliStageIsInstalled(void)
256 DALI_TEST_CHECK(!Stage::IsInstalled());
258 TestApplication application;
262 DALI_TEST_CHECK(Stage::IsInstalled());
266 int UtcDaliStageAdd(void)
268 TestApplication application;
270 Stage stage = Stage::GetCurrent();
272 Actor actor = Actor::New();
273 DALI_TEST_CHECK(!actor.OnStage());
276 DALI_TEST_CHECK(actor.OnStage());
280 int UtcDaliStageRemove(void)
282 TestApplication application;
284 Stage stage = Stage::GetCurrent();
286 Actor actor = Actor::New();
287 DALI_TEST_CHECK(!actor.OnStage());
290 DALI_TEST_CHECK(actor.OnStage());
293 DALI_TEST_CHECK(!actor.OnStage());
297 int UtcDaliStageGetSize(void)
299 TestApplication application;
301 Stage stage = Stage::GetCurrent();
303 Vector2 size = stage.GetSize();
305 DALI_TEST_EQUALS(size.width, static_cast<float>(TestApplication::DEFAULT_SURFACE_WIDTH), TEST_LOCATION);
306 DALI_TEST_EQUALS(size.height, static_cast<float>(TestApplication::DEFAULT_SURFACE_HEIGHT), TEST_LOCATION);
310 int UtcDaliStageGetDpi01(void)
312 TestApplication application; // Initializes core DPI to default values
314 Stage stage = Stage::GetCurrent();
316 // Test the default DPI.
317 Vector2 dpi = stage.GetDpi();
318 DALI_TEST_EQUALS(dpi.x, static_cast<float>(TestApplication::DEFAULT_HORIZONTAL_DPI), TEST_LOCATION);
319 DALI_TEST_EQUALS(dpi.y, static_cast<float>(TestApplication::DEFAULT_VERTICAL_DPI), TEST_LOCATION);
323 int UtcDaliStageGetDpi02(void)
325 TestApplication application; // Initializes core DPI to default values
327 // Test that setting core DPI explicitly also sets up the Stage's DPI.
328 application.GetCore().SetDpi(200, 180);
330 Stage stage = Stage::GetCurrent();
331 Vector2 dpi = stage.GetDpi();
332 DALI_TEST_EQUALS(dpi.x, 200.0f, TEST_LOCATION);
333 DALI_TEST_EQUALS(dpi.y, 180.0f, TEST_LOCATION);
338 int UtcDaliStageGetDpi03(void)
340 TestApplication application(480, 800, 72.0f, 120.0f); // Initializes core DPI with specific values
342 Stage stage = Stage::GetCurrent();
344 // Test that setting core DPI explicitly also sets up the Stage's DPI.
345 Vector2 dpi = stage.GetDpi();
346 DALI_TEST_EQUALS(dpi.x, 72.0f, TEST_LOCATION);
347 DALI_TEST_EQUALS(dpi.y, 120.0f, TEST_LOCATION);
351 int UtcDaliStageInitializeDynamicsP(void)
353 TestApplication application;
354 Stage stage = Stage::GetCurrent();
355 DynamicsWorld world = stage.InitializeDynamics( DynamicsWorldConfig::New() );
357 #if !defined(DYNAMICS_SUPPORT)
358 DALI_TEST_CHECK(true);
360 DALI_TEST_CHECK( world );
366 int UtcDaliStageGetLayerCount(void)
368 TestApplication application;
370 Stage stage = Stage::GetCurrent();
372 // Initially we have a default layer
373 DALI_TEST_EQUALS(stage.GetLayerCount(), 1u, TEST_LOCATION);
375 Layer layer = Layer::New();
378 DALI_TEST_EQUALS(stage.GetLayerCount(), 2u, TEST_LOCATION);
382 int UtcDaliStageGetLayer(void)
384 TestApplication application;
386 Stage stage = Stage::GetCurrent();
388 Layer rootLayer = stage.GetLayer(0);
389 DALI_TEST_CHECK(rootLayer);
391 Layer layer = Layer::New();
394 Layer sameLayer = stage.GetLayer(1);
395 DALI_TEST_CHECK(layer == sameLayer);
400 int UtcDaliStageGetRootLayer(void)
402 TestApplication application;
404 Stage stage = Stage::GetCurrent();
406 Layer rootLayer = stage.GetLayer(0);
407 DALI_TEST_CHECK( rootLayer );
409 Layer layer = Layer::New();
411 layer.LowerToBottom();
413 DALI_TEST_CHECK( stage.GetRootLayer() == rootLayer );
417 int UtcDaliStageSetBackgroundColor(void)
419 TestApplication application;
421 Stage stage = Stage::GetCurrent();
423 Vector4 testColor(0.1f, 0.2f, 0.3f, 1.0f);
424 stage.SetBackgroundColor(testColor);
426 DALI_TEST_EQUALS(testColor, stage.GetBackgroundColor(), TEST_LOCATION);
430 int UtcDaliStageGetBackgroundColor(void)
432 TestApplication application;
434 Stage stage = Stage::GetCurrent();
436 DALI_TEST_EQUALS(Stage::DEFAULT_BACKGROUND_COLOR, stage.GetBackgroundColor(), TEST_LOCATION);
440 int UtcDaliStageKeepRendering(void)
442 TestApplication application;
444 Stage stage = Stage::GetCurrent();
446 // Run core until it wants to sleep
447 bool keepUpdating( true );
448 while ( keepUpdating )
450 application.SendNotification();
451 keepUpdating = application.Render(1000.0f /*1 second*/);
454 // Force rendering for the next 5 seconds
455 stage.KeepRendering( 5.0f );
457 application.SendNotification();
459 // Test that core wants to sleep after 10 seconds
460 keepUpdating = application.Render(1000.0f /*1 second*/);
461 DALI_TEST_CHECK( keepUpdating );
462 keepUpdating = application.Render(1000.0f /*2 seconds*/);
463 DALI_TEST_CHECK( keepUpdating );
464 keepUpdating = application.Render(1000.0f /*3 seconds*/);
465 DALI_TEST_CHECK( keepUpdating );
466 keepUpdating = application.Render(1000.0f /*4 seconds*/);
467 DALI_TEST_CHECK( keepUpdating );
468 keepUpdating = application.Render(1000.0f /*5 seconds*/);
469 DALI_TEST_CHECK( !keepUpdating );
473 int UtcDaliStageEventProcessingFinished(void)
475 TestApplication application;
476 Stage stage = Stage::GetCurrent();
478 bool eventProcessingFinished = false;
479 EventProcessingFinishedFunctor functor( eventProcessingFinished );
480 stage.EventProcessingFinishedSignal().Connect( &application, functor );
482 Actor actor( Actor::New() );
485 application.SendNotification();
486 application.Render();
488 DALI_TEST_CHECK( eventProcessingFinished );
493 int UtcDaliStageSignalKeyEvent(void)
495 TestApplication application;
496 Stage stage = Stage::GetCurrent();
498 KeyEventSignalData data;
499 KeyEventReceivedFunctor functor( data );
500 stage.KeyEventSignal().Connect( &application, functor );
502 Integration::KeyEvent event( "i","i", 0, 0, 0, Integration::KeyEvent::Down );
503 application.ProcessEvent( event );
505 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
506 DALI_TEST_CHECK( event.keyModifier == data.receivedKeyEvent.keyModifier );
507 DALI_TEST_CHECK( event.keyName == data.receivedKeyEvent.keyPressedName );
508 DALI_TEST_CHECK( event.keyString == data.receivedKeyEvent.keyPressed );
509 DALI_TEST_CHECK( event.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
513 Integration::KeyEvent event2( "i","i", 0, 0, 0, Integration::KeyEvent::Up );
514 application.ProcessEvent( event2 );
516 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
517 DALI_TEST_CHECK( event2.keyModifier == data.receivedKeyEvent.keyModifier );
518 DALI_TEST_CHECK( event2.keyName == data.receivedKeyEvent.keyPressedName );
519 DALI_TEST_CHECK( event2.keyString == data.receivedKeyEvent.keyPressed );
520 DALI_TEST_CHECK( event2.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
524 Integration::KeyEvent event3( "a","a", 0, 0, 0, Integration::KeyEvent::Down );
525 application.ProcessEvent( event3 );
527 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
528 DALI_TEST_CHECK( event3.keyModifier == data.receivedKeyEvent.keyModifier );
529 DALI_TEST_CHECK( event3.keyName == data.receivedKeyEvent.keyPressedName );
530 DALI_TEST_CHECK( event3.keyString == data.receivedKeyEvent.keyPressed );
531 DALI_TEST_CHECK( event3.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
535 Integration::KeyEvent event4( "a","a", 0, 0, 0, Integration::KeyEvent::Up );
536 application.ProcessEvent( event4 );
538 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
539 DALI_TEST_CHECK( event4.keyModifier == data.receivedKeyEvent.keyModifier );
540 DALI_TEST_CHECK( event4.keyName == data.receivedKeyEvent.keyPressedName );
541 DALI_TEST_CHECK( event4.keyString == data.receivedKeyEvent.keyPressed );
542 DALI_TEST_CHECK( event4.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
546 int UtcDaliStageTouchedSignal(void)
548 TestApplication application;
549 Stage stage = Stage::GetCurrent();
551 TouchedSignalData data;
552 TouchedFunctor functor( data );
553 stage.TouchedSignal().Connect( &application, functor );
556 application.SendNotification();
557 application.Render();
559 // NO ACTORS, SINGLE TOUCH, DOWN, MOTION THEN UP
561 Integration::TouchEvent touchEvent;
562 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
563 application.ProcessEvent( touchEvent );
565 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
566 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
567 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
570 touchEvent.points[0].state = TouchPoint::Motion;
571 touchEvent.points[0].screen.x = 12.0f; // Some motion
572 application.ProcessEvent( touchEvent );
574 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
577 touchEvent.points[0].state = TouchPoint::Up;
578 application.ProcessEvent( touchEvent );
580 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
581 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
582 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
586 // Add an actor to the scene
588 Actor actor = Actor::New();
589 actor.SetSize( 100.0f, 100.0f );
590 actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
591 actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
592 actor.TouchedSignal().Connect( &DummyTouchCallback );
596 application.SendNotification();
597 application.Render();
599 // ACTOR ON SCENE, SINGLE TOUCH, DOWN IN ACTOR, MOTION THEN UP OUTSIDE ACTOR
601 Integration::TouchEvent touchEvent;
602 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
603 application.ProcessEvent( touchEvent );
605 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
606 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
607 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].hitActor == actor );
610 touchEvent.points[0].state = TouchPoint::Motion;
611 touchEvent.points[0].screen.x = 150.0f; // Some motion
612 application.ProcessEvent( touchEvent );
614 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
617 touchEvent.points[0].state = TouchPoint::Up;
618 application.ProcessEvent( touchEvent );
620 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
621 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
622 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
626 // INTERRUPTED BEFORE DOWN AND INTERRUPTED AFTER DOWN
628 Integration::TouchEvent touchEvent;
629 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Interrupted, 10.0f, 10.0f ) );
630 application.ProcessEvent( touchEvent );
632 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
633 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
634 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
635 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Interrupted );
638 touchEvent.points[0].state = TouchPoint::Down;
639 application.ProcessEvent( touchEvent );
641 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
642 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
643 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].hitActor == actor );
644 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Down );
647 touchEvent.points[0].state = TouchPoint::Interrupted;
648 application.ProcessEvent( touchEvent );
650 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
651 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
652 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
653 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Interrupted );
657 // MULTIPLE TOUCH, SHOULD ONLY RECEIVE TOUCH ON FIRST DOWN AND LAST UP
659 Integration::TouchEvent touchEvent;
662 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
663 application.ProcessEvent( touchEvent );
664 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
668 touchEvent.points[0].state = TouchPoint::Stationary;
669 touchEvent.points.push_back( TouchPoint( 1, TouchPoint::Down, 50.0f, 50.0f ) );
670 application.ProcessEvent( touchEvent );
671 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
674 // Primary point is up
675 touchEvent.points[0].state = TouchPoint::Up;
676 touchEvent.points[1].state = TouchPoint::Stationary;
677 application.ProcessEvent( touchEvent );
678 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
681 // Remove 1st point now, 2nd point is now in motion
682 touchEvent.points.erase( touchEvent.points.begin() );
683 touchEvent.points[0].state = TouchPoint::Motion;
684 touchEvent.points[0].screen.x = 150.0f;
685 application.ProcessEvent( touchEvent );
686 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
690 touchEvent.points[0].state = TouchPoint::Up;
691 application.ProcessEvent( touchEvent );
692 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
698 int UtcDaliStageSignalWheelEvent(void)
700 TestApplication application;
701 Stage stage = Stage::GetCurrent();
703 WheelEventSignalData data;
704 WheelEventReceivedFunctor functor( data );
705 stage.WheelEventSignal().Connect( &application, functor );
707 Integration::WheelEvent event( Integration::WheelEvent::CUSTOM_WHEEL, 0, 0u, Vector2( 0.0f, 0.0f ), 1, 1000u );
708 application.ProcessEvent( event );
710 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
711 DALI_TEST_CHECK( static_cast< WheelEvent::Type >(event.type) == data.receivedWheelEvent.type );
712 DALI_TEST_CHECK( event.direction == data.receivedWheelEvent.direction );
713 DALI_TEST_CHECK( event.modifiers == data.receivedWheelEvent.modifiers );
714 DALI_TEST_CHECK( event.point == data.receivedWheelEvent.point );
715 DALI_TEST_CHECK( event.z == data.receivedWheelEvent.z );
716 DALI_TEST_CHECK( event.timeStamp == data.receivedWheelEvent.timeStamp );
720 Integration::WheelEvent event2( Integration::WheelEvent::CUSTOM_WHEEL, 0, 0u, Vector2( 0.0f, 0.0f ), -1, 1000u );
721 application.ProcessEvent( event2 );
723 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
724 DALI_TEST_CHECK( static_cast< WheelEvent::Type >(event2.type) == data.receivedWheelEvent.type );
725 DALI_TEST_CHECK( event2.direction == data.receivedWheelEvent.direction );
726 DALI_TEST_CHECK( event2.modifiers == data.receivedWheelEvent.modifiers );
727 DALI_TEST_CHECK( event2.point == data.receivedWheelEvent.point );
728 DALI_TEST_CHECK( event2.z == data.receivedWheelEvent.z );
729 DALI_TEST_CHECK( event2.timeStamp == data.receivedWheelEvent.timeStamp );
733 int UtcDaliStageContextLostRegainedSignals(void)
736 Stage stage = Stage::GetCurrent();
738 bool contextLost = false;
739 bool contextRegained = false;
740 ContextStatusFunctor contextLostFunctor( contextLost );
741 ContextStatusFunctor contextRegainedFunctor( contextRegained );
742 stage.ContextLostSignal().Connect(&app, contextLostFunctor );
743 stage.ContextRegainedSignal().Connect(&app, contextRegainedFunctor );
745 Integration::Core& core = app.GetCore();
746 Integration::ContextNotifierInterface* notifier = core.GetContextNotifier();
747 notifier->NotifyContextLost();
748 DALI_TEST_EQUALS( contextLost, true, TEST_LOCATION );
750 notifier->NotifyContextRegained();
751 DALI_TEST_EQUALS( contextRegained, true, TEST_LOCATION );
756 int UtcDaliStageSceneCreatedSignal(void)
759 Stage stage = Stage::GetCurrent();
761 bool signalCalled = false;
762 SceneCreatedStatusFunctor sceneCreatedFunctor( signalCalled );
763 stage.SceneCreatedSignal().Connect(&app, sceneCreatedFunctor );
765 Integration::Core& core = app.GetCore();
767 DALI_TEST_EQUALS( signalCalled, true, TEST_LOCATION );