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>
27 #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 } // unnamed namespace
162 int UtcDaliStageDefaultConstructor(void)
164 TestApplication application;
167 DALI_TEST_CHECK(!stage);
171 int UtcDaliStageDestructor(void)
173 TestApplication application;
174 Stage* stage = new Stage();
178 DALI_TEST_CHECK( true );
182 int UtcDaliStageGetCurrent(void)
184 TestApplication application;
185 Stage stage = Stage::GetCurrent();
187 DALI_TEST_CHECK(stage);
191 int UtcDaliStageIsInstalled(void)
193 DALI_TEST_CHECK(!Stage::IsInstalled());
195 TestApplication application;
199 DALI_TEST_CHECK(Stage::IsInstalled());
203 int UtcDaliStageAdd(void)
205 TestApplication application;
207 Stage stage = Stage::GetCurrent();
209 Actor actor = Actor::New();
210 DALI_TEST_CHECK(!actor.OnStage());
213 DALI_TEST_CHECK(actor.OnStage());
217 int UtcDaliStageRemove(void)
219 TestApplication application;
221 Stage stage = Stage::GetCurrent();
223 Actor actor = Actor::New();
224 DALI_TEST_CHECK(!actor.OnStage());
227 DALI_TEST_CHECK(actor.OnStage());
230 DALI_TEST_CHECK(!actor.OnStage());
234 int UtcDaliStageGetSize(void)
236 TestApplication application;
238 Stage stage = Stage::GetCurrent();
240 Vector2 size = stage.GetSize();
242 DALI_TEST_EQUALS(size.width, static_cast<float>(TestApplication::DEFAULT_SURFACE_WIDTH), TEST_LOCATION);
243 DALI_TEST_EQUALS(size.height, static_cast<float>(TestApplication::DEFAULT_SURFACE_HEIGHT), TEST_LOCATION);
247 int UtcDaliStageGetDpi01(void)
249 TestApplication application; // Initializes core DPI to default values
251 Stage stage = Stage::GetCurrent();
253 // Test the default DPI.
254 Vector2 dpi = stage.GetDpi();
255 DALI_TEST_EQUALS(dpi.x, static_cast<float>(TestApplication::DEFAULT_HORIZONTAL_DPI), TEST_LOCATION);
256 DALI_TEST_EQUALS(dpi.y, static_cast<float>(TestApplication::DEFAULT_VERTICAL_DPI), TEST_LOCATION);
260 int UtcDaliStageGetDpi02(void)
262 TestApplication application; // Initializes core DPI to default values
264 // Test that setting core DPI explicitly also sets up the Stage's DPI.
265 application.GetCore().SetDpi(200, 180);
267 Stage stage = Stage::GetCurrent();
268 Vector2 dpi = stage.GetDpi();
269 DALI_TEST_EQUALS(dpi.x, 200.0f, TEST_LOCATION);
270 DALI_TEST_EQUALS(dpi.y, 180.0f, TEST_LOCATION);
275 int UtcDaliStageGetDpi03(void)
277 TestApplication application(480, 800, 72.0f, 120.0f); // Initializes core DPI with specific values
279 Stage stage = Stage::GetCurrent();
281 // Test that setting core DPI explicitly also sets up the Stage's DPI.
282 Vector2 dpi = stage.GetDpi();
283 DALI_TEST_EQUALS(dpi.x, 72.0f, TEST_LOCATION);
284 DALI_TEST_EQUALS(dpi.y, 120.0f, TEST_LOCATION);
288 int UtcDaliStageGetLayerCount(void)
290 TestApplication application;
292 Stage stage = Stage::GetCurrent();
294 // Initially we have a default layer
295 DALI_TEST_EQUALS(stage.GetLayerCount(), 1u, TEST_LOCATION);
297 Layer layer = Layer::New();
300 DALI_TEST_EQUALS(stage.GetLayerCount(), 2u, TEST_LOCATION);
304 int UtcDaliStageGetLayer(void)
306 TestApplication application;
308 Stage stage = Stage::GetCurrent();
310 Layer rootLayer = stage.GetLayer(0);
311 DALI_TEST_CHECK(rootLayer);
313 Layer layer = Layer::New();
316 Layer sameLayer = stage.GetLayer(1);
317 DALI_TEST_CHECK(layer == sameLayer);
322 int UtcDaliStageGetRootLayer(void)
324 TestApplication application;
326 Stage stage = Stage::GetCurrent();
328 Layer rootLayer = stage.GetLayer(0);
329 DALI_TEST_CHECK( rootLayer );
331 Layer layer = Layer::New();
333 layer.LowerToBottom();
335 DALI_TEST_CHECK( stage.GetRootLayer() == rootLayer );
339 int UtcDaliStageSetBackgroundColor(void)
341 TestApplication application;
343 Stage stage = Stage::GetCurrent();
345 Vector4 testColor(0.1f, 0.2f, 0.3f, 1.0f);
346 stage.SetBackgroundColor(testColor);
348 DALI_TEST_EQUALS(testColor, stage.GetBackgroundColor(), TEST_LOCATION);
352 int UtcDaliStageGetBackgroundColor(void)
354 TestApplication application;
356 Stage stage = Stage::GetCurrent();
358 DALI_TEST_EQUALS(Stage::DEFAULT_BACKGROUND_COLOR, stage.GetBackgroundColor(), TEST_LOCATION);
362 int UtcDaliStageKeepRendering(void)
364 TestApplication application;
366 Stage stage = Stage::GetCurrent();
368 // Run core until it wants to sleep
369 bool keepUpdating( true );
370 while ( keepUpdating )
372 application.SendNotification();
373 keepUpdating = application.Render(1000.0f /*1 second*/);
376 // Force rendering for the next 5 seconds
377 stage.KeepRendering( 5.0f );
379 application.SendNotification();
381 // Test that core wants to sleep after 10 seconds
382 keepUpdating = application.Render(1000.0f /*1 second*/);
383 DALI_TEST_CHECK( keepUpdating );
384 keepUpdating = application.Render(1000.0f /*2 seconds*/);
385 DALI_TEST_CHECK( keepUpdating );
386 keepUpdating = application.Render(1000.0f /*3 seconds*/);
387 DALI_TEST_CHECK( keepUpdating );
388 keepUpdating = application.Render(1000.0f /*4 seconds*/);
389 DALI_TEST_CHECK( keepUpdating );
390 keepUpdating = application.Render(1000.0f /*5 seconds*/);
391 DALI_TEST_CHECK( !keepUpdating );
395 int UtcDaliStageEventProcessingFinished(void)
397 TestApplication application;
398 Stage stage = Stage::GetCurrent();
400 bool eventProcessingFinished = false;
401 EventProcessingFinishedFunctor functor( eventProcessingFinished );
402 stage.EventProcessingFinishedSignal().Connect( &application, functor );
404 Actor actor( Actor::New() );
407 application.SendNotification();
408 application.Render();
410 DALI_TEST_CHECK( eventProcessingFinished );
415 int UtcDaliStageSignalKeyEvent(void)
417 TestApplication application;
418 Stage stage = Stage::GetCurrent();
420 KeyEventSignalData data;
421 KeyEventReceivedFunctor functor( data );
422 stage.KeyEventSignal().Connect( &application, functor );
424 Integration::KeyEvent event( "i","i", 0, 0, 0, Integration::KeyEvent::Down );
425 application.ProcessEvent( event );
427 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
428 DALI_TEST_CHECK( event.keyModifier == data.receivedKeyEvent.keyModifier );
429 DALI_TEST_CHECK( event.keyName == data.receivedKeyEvent.keyPressedName );
430 DALI_TEST_CHECK( event.keyString == data.receivedKeyEvent.keyPressed );
431 DALI_TEST_CHECK( event.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
435 Integration::KeyEvent event2( "i","i", 0, 0, 0, Integration::KeyEvent::Up );
436 application.ProcessEvent( event2 );
438 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
439 DALI_TEST_CHECK( event2.keyModifier == data.receivedKeyEvent.keyModifier );
440 DALI_TEST_CHECK( event2.keyName == data.receivedKeyEvent.keyPressedName );
441 DALI_TEST_CHECK( event2.keyString == data.receivedKeyEvent.keyPressed );
442 DALI_TEST_CHECK( event2.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
446 Integration::KeyEvent event3( "a","a", 0, 0, 0, Integration::KeyEvent::Down );
447 application.ProcessEvent( event3 );
449 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
450 DALI_TEST_CHECK( event3.keyModifier == data.receivedKeyEvent.keyModifier );
451 DALI_TEST_CHECK( event3.keyName == data.receivedKeyEvent.keyPressedName );
452 DALI_TEST_CHECK( event3.keyString == data.receivedKeyEvent.keyPressed );
453 DALI_TEST_CHECK( event3.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
457 Integration::KeyEvent event4( "a","a", 0, 0, 0, Integration::KeyEvent::Up );
458 application.ProcessEvent( event4 );
460 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
461 DALI_TEST_CHECK( event4.keyModifier == data.receivedKeyEvent.keyModifier );
462 DALI_TEST_CHECK( event4.keyName == data.receivedKeyEvent.keyPressedName );
463 DALI_TEST_CHECK( event4.keyString == data.receivedKeyEvent.keyPressed );
464 DALI_TEST_CHECK( event4.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
468 int UtcDaliStageTouchedSignal(void)
470 TestApplication application;
471 Stage stage = Stage::GetCurrent();
473 TouchedSignalData data;
474 TouchedFunctor functor( data );
475 stage.TouchedSignal().Connect( &application, functor );
478 application.SendNotification();
479 application.Render();
481 // NO ACTORS, SINGLE TOUCH, DOWN, MOTION THEN UP
483 Integration::TouchEvent touchEvent;
484 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
485 application.ProcessEvent( touchEvent );
487 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
488 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
489 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
492 touchEvent.points[0].state = TouchPoint::Motion;
493 touchEvent.points[0].screen.x = 12.0f; // Some motion
494 application.ProcessEvent( touchEvent );
496 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
499 touchEvent.points[0].state = TouchPoint::Up;
500 application.ProcessEvent( touchEvent );
502 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
503 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
504 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
508 // Add an actor to the scene
510 Actor actor = Actor::New();
511 actor.SetSize( 100.0f, 100.0f );
512 actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
513 actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
514 actor.TouchedSignal().Connect( &DummyTouchCallback );
518 application.SendNotification();
519 application.Render();
521 // ACTOR ON SCENE, SINGLE TOUCH, DOWN IN ACTOR, MOTION THEN UP OUTSIDE ACTOR
523 Integration::TouchEvent touchEvent;
524 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
525 application.ProcessEvent( touchEvent );
527 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
528 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
529 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].hitActor == actor );
532 touchEvent.points[0].state = TouchPoint::Motion;
533 touchEvent.points[0].screen.x = 150.0f; // Some motion
534 application.ProcessEvent( touchEvent );
536 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
539 touchEvent.points[0].state = TouchPoint::Up;
540 application.ProcessEvent( touchEvent );
542 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
543 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
544 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
548 // INTERRUPTED BEFORE DOWN AND INTERRUPTED AFTER DOWN
550 Integration::TouchEvent touchEvent;
551 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Interrupted, 10.0f, 10.0f ) );
552 application.ProcessEvent( touchEvent );
554 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
555 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
556 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
557 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Interrupted );
560 touchEvent.points[0].state = TouchPoint::Down;
561 application.ProcessEvent( touchEvent );
563 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
564 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
565 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].hitActor == actor );
566 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Down );
569 touchEvent.points[0].state = TouchPoint::Interrupted;
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 );
575 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Interrupted );
579 // MULTIPLE TOUCH, SHOULD ONLY RECEIVE TOUCH ON FIRST DOWN AND LAST UP
581 Integration::TouchEvent touchEvent;
584 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
585 application.ProcessEvent( touchEvent );
586 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
590 touchEvent.points[0].state = TouchPoint::Stationary;
591 touchEvent.points.push_back( TouchPoint( 1, TouchPoint::Down, 50.0f, 50.0f ) );
592 application.ProcessEvent( touchEvent );
593 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
596 // Primary point is up
597 touchEvent.points[0].state = TouchPoint::Up;
598 touchEvent.points[1].state = TouchPoint::Stationary;
599 application.ProcessEvent( touchEvent );
600 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
603 // Remove 1st point now, 2nd point is now in motion
604 touchEvent.points.erase( touchEvent.points.begin() );
605 touchEvent.points[0].state = TouchPoint::Motion;
606 touchEvent.points[0].screen.x = 150.0f;
607 application.ProcessEvent( touchEvent );
608 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
612 touchEvent.points[0].state = TouchPoint::Up;
613 application.ProcessEvent( touchEvent );
614 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
620 int UtcDaliStageContextLostRegainedSignals(void)
623 Stage stage = Stage::GetCurrent();
625 bool contextLost = false;
626 bool contextRegained = false;
627 ContextStatusFunctor contextLostFunctor( contextLost );
628 ContextStatusFunctor contextRegainedFunctor( contextRegained );
629 stage.ContextLostSignal().Connect(&app, contextLostFunctor );
630 stage.ContextRegainedSignal().Connect(&app, contextRegainedFunctor );
632 Integration::Core& core = app.GetCore();
633 Integration::ContextNotifierInterface* notifier = core.GetContextNotifier();
634 notifier->NotifyContextLost();
635 DALI_TEST_EQUALS( contextLost, true, TEST_LOCATION );
637 notifier->NotifyContextRegained();
638 DALI_TEST_EQUALS( contextRegained, true, TEST_LOCATION );