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/events/key-event-integ.h>
24 #include <dali/integration-api/events/touch-event-integ.h>
26 #include <dali-test-suite-utils.h>
31 void stage_test_startup(void)
33 test_return_value = TET_UNDEF;
36 void stage_test_cleanup(void)
38 test_return_value = TET_PASS;
45 * Functor for EventProcessingFinished signal
47 struct EventProcessingFinishedFunctor
50 * @param[in] eventProcessingFinished reference to a boolean variable used to check if signal has been called.
52 EventProcessingFinishedFunctor( bool& eventProcessingFinished )
53 : mEventProcessingFinished( eventProcessingFinished )
58 mEventProcessingFinished = true;
61 bool& mEventProcessingFinished;
64 // Stores data that is populated in the key-event callback and will be read by the TET cases
65 struct KeyEventSignalData
68 : functorCalled(false)
73 functorCalled = false;
75 receivedKeyEvent.keyModifier = 0;
76 receivedKeyEvent.keyPressedName.clear();
77 receivedKeyEvent.keyPressed.clear();
81 KeyEvent receivedKeyEvent;
84 // Functor that sets the data when called
85 struct KeyEventReceivedFunctor
87 KeyEventReceivedFunctor( KeyEventSignalData& data ) : signalData( data ) { }
89 bool operator()( const KeyEvent& keyEvent )
91 signalData.functorCalled = true;
92 signalData.receivedKeyEvent = keyEvent;
97 KeyEventSignalData& signalData;
100 // Stores data that is populated in the touched signal callback and will be read by the TET cases
101 struct TouchedSignalData
104 : functorCalled(false)
109 functorCalled = false;
111 receivedTouchEvent.points.clear();
112 receivedTouchEvent.time = 0;
116 TouchEvent receivedTouchEvent;
119 // Functor that sets the data when touched signal is received
120 struct TouchedFunctor
122 TouchedFunctor( TouchedSignalData& data ) : signalData( data ) { }
124 void operator()( const TouchEvent& touch )
126 signalData.functorCalled = true;
127 signalData.receivedTouchEvent = touch;
130 TouchedSignalData& signalData;
133 bool DummyTouchCallback( Actor actor, const TouchEvent& touch )
138 } // unnamed namespace
141 int UtcDaliStageDefaultConstructor(void)
143 TestApplication application;
146 DALI_TEST_CHECK(!stage);
150 int UtcDaliStageDestructor(void)
152 TestApplication application;
153 Stage* stage = new Stage();
157 DALI_TEST_CHECK( true );
161 int UtcDaliStageGetCurrent(void)
163 TestApplication application;
164 Stage stage = Stage::GetCurrent();
166 DALI_TEST_CHECK(stage);
170 int UtcDaliStageIsInstalled(void)
172 DALI_TEST_CHECK(!Stage::IsInstalled());
174 TestApplication application;
178 DALI_TEST_CHECK(Stage::IsInstalled());
182 int UtcDaliStageAdd(void)
184 TestApplication application;
186 Stage stage = Stage::GetCurrent();
188 Actor actor = Actor::New();
189 DALI_TEST_CHECK(!actor.OnStage());
192 DALI_TEST_CHECK(actor.OnStage());
196 int UtcDaliStageRemove(void)
198 TestApplication application;
200 Stage stage = Stage::GetCurrent();
202 Actor actor = Actor::New();
203 DALI_TEST_CHECK(!actor.OnStage());
206 DALI_TEST_CHECK(actor.OnStage());
209 DALI_TEST_CHECK(!actor.OnStage());
213 int UtcDaliStageGetSize(void)
215 TestApplication application;
217 Stage stage = Stage::GetCurrent();
219 Vector2 size = stage.GetSize();
221 DALI_TEST_EQUALS(size.width, static_cast<float>(TestApplication::DEFAULT_SURFACE_WIDTH), TEST_LOCATION);
222 DALI_TEST_EQUALS(size.height, static_cast<float>(TestApplication::DEFAULT_SURFACE_HEIGHT), TEST_LOCATION);
226 int UtcDaliStageGetDpi01(void)
228 TestApplication application; // Initializes core DPI to default values
230 Stage stage = Stage::GetCurrent();
232 // Test the default DPI.
233 Vector2 dpi = stage.GetDpi();
234 DALI_TEST_EQUALS(dpi.x, static_cast<float>(TestApplication::DEFAULT_HORIZONTAL_DPI), TEST_LOCATION);
235 DALI_TEST_EQUALS(dpi.y, static_cast<float>(TestApplication::DEFAULT_VERTICAL_DPI), TEST_LOCATION);
239 int UtcDaliStageGetDpi02(void)
241 TestApplication application; // Initializes core DPI to default values
243 // Test that setting core DPI explicitly also sets up the Stage's DPI.
244 application.GetCore().SetDpi(200, 180);
246 Stage stage = Stage::GetCurrent();
247 Vector2 dpi = stage.GetDpi();
248 DALI_TEST_EQUALS(dpi.x, 200.0f, TEST_LOCATION);
249 DALI_TEST_EQUALS(dpi.y, 180.0f, TEST_LOCATION);
254 int UtcDaliStageGetDpi03(void)
256 TestApplication application(480, 800, 72.0f, 120.0f); // Initializes core DPI with specific values
258 Stage stage = Stage::GetCurrent();
260 // Test that setting core DPI explicitly also sets up the Stage's DPI.
261 Vector2 dpi = stage.GetDpi();
262 DALI_TEST_EQUALS(dpi.x, 72.0f, TEST_LOCATION);
263 DALI_TEST_EQUALS(dpi.y, 120.0f, TEST_LOCATION);
267 int UtcDaliStageGetLayerCount(void)
269 TestApplication application;
271 Stage stage = Stage::GetCurrent();
273 // Initially we have a default layer
274 DALI_TEST_EQUALS(stage.GetLayerCount(), 1u, TEST_LOCATION);
276 Layer layer = Layer::New();
279 DALI_TEST_EQUALS(stage.GetLayerCount(), 2u, TEST_LOCATION);
283 int UtcDaliStageGetLayer(void)
285 TestApplication application;
287 Stage stage = Stage::GetCurrent();
289 Layer rootLayer = stage.GetLayer(0);
290 DALI_TEST_CHECK(rootLayer);
292 Layer layer = Layer::New();
295 Layer sameLayer = stage.GetLayer(1);
296 DALI_TEST_CHECK(layer == sameLayer);
301 int UtcDaliStageGetRootLayer(void)
303 TestApplication application;
305 Stage stage = Stage::GetCurrent();
307 Layer rootLayer = stage.GetLayer(0);
308 DALI_TEST_CHECK( rootLayer );
310 Layer layer = Layer::New();
312 layer.LowerToBottom();
314 DALI_TEST_CHECK( stage.GetRootLayer() == rootLayer );
318 int UtcDaliStageSetBackgroundColor(void)
320 TestApplication application;
322 Stage stage = Stage::GetCurrent();
324 Vector4 testColor(0.1f, 0.2f, 0.3f, 1.0f);
325 stage.SetBackgroundColor(testColor);
327 DALI_TEST_EQUALS(testColor, stage.GetBackgroundColor(), TEST_LOCATION);
331 int UtcDaliStageGetBackgroundColor(void)
333 TestApplication application;
335 Stage stage = Stage::GetCurrent();
337 DALI_TEST_EQUALS(Stage::DEFAULT_BACKGROUND_COLOR, stage.GetBackgroundColor(), TEST_LOCATION);
341 int UtcDaliStageKeepRendering(void)
343 TestApplication application;
345 Stage stage = Stage::GetCurrent();
347 // Run core until it wants to sleep
348 bool keepUpdating( true );
349 while ( keepUpdating )
351 application.SendNotification();
352 keepUpdating = application.Render(1000.0f /*1 second*/);
355 // Force rendering for the next 5 seconds
356 stage.KeepRendering( 5.0f );
358 application.SendNotification();
360 // Test that core wants to sleep after 10 seconds
361 keepUpdating = application.Render(1000.0f /*1 second*/);
362 DALI_TEST_CHECK( keepUpdating );
363 keepUpdating = application.Render(1000.0f /*2 seconds*/);
364 DALI_TEST_CHECK( keepUpdating );
365 keepUpdating = application.Render(1000.0f /*3 seconds*/);
366 DALI_TEST_CHECK( keepUpdating );
367 keepUpdating = application.Render(1000.0f /*4 seconds*/);
368 DALI_TEST_CHECK( keepUpdating );
369 keepUpdating = application.Render(1000.0f /*5 seconds*/);
370 DALI_TEST_CHECK( !keepUpdating );
374 int UtcDaliStageEventProcessingFinished(void)
376 TestApplication application;
377 Stage stage = Stage::GetCurrent();
379 bool eventProcessingFinished = false;
380 EventProcessingFinishedFunctor functor( eventProcessingFinished );
381 stage.EventProcessingFinishedSignal().Connect( &application, functor );
383 Actor actor( Actor::New() );
386 application.SendNotification();
387 application.Render();
389 DALI_TEST_CHECK( eventProcessingFinished );
394 int UtcDaliStageSignalKeyEvent(void)
396 TestApplication application;
397 Stage stage = Stage::GetCurrent();
399 KeyEventSignalData data;
400 KeyEventReceivedFunctor functor( data );
401 stage.KeyEventSignal().Connect( &application, functor );
403 Integration::KeyEvent event( "i","i", 0, 0, 0, Integration::KeyEvent::Down );
404 application.ProcessEvent( event );
406 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
407 DALI_TEST_CHECK( event.keyModifier == data.receivedKeyEvent.keyModifier );
408 DALI_TEST_CHECK( event.keyName == data.receivedKeyEvent.keyPressedName );
409 DALI_TEST_CHECK( event.keyString == data.receivedKeyEvent.keyPressed );
410 DALI_TEST_CHECK( event.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
414 Integration::KeyEvent event2( "i","i", 0, 0, 0, Integration::KeyEvent::Up );
415 application.ProcessEvent( event2 );
417 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
418 DALI_TEST_CHECK( event2.keyModifier == data.receivedKeyEvent.keyModifier );
419 DALI_TEST_CHECK( event2.keyName == data.receivedKeyEvent.keyPressedName );
420 DALI_TEST_CHECK( event2.keyString == data.receivedKeyEvent.keyPressed );
421 DALI_TEST_CHECK( event2.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
425 Integration::KeyEvent event3( "a","a", 0, 0, 0, Integration::KeyEvent::Down );
426 application.ProcessEvent( event3 );
428 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
429 DALI_TEST_CHECK( event3.keyModifier == data.receivedKeyEvent.keyModifier );
430 DALI_TEST_CHECK( event3.keyName == data.receivedKeyEvent.keyPressedName );
431 DALI_TEST_CHECK( event3.keyString == data.receivedKeyEvent.keyPressed );
432 DALI_TEST_CHECK( event3.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
436 Integration::KeyEvent event4( "a","a", 0, 0, 0, Integration::KeyEvent::Up );
437 application.ProcessEvent( event4 );
439 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
440 DALI_TEST_CHECK( event4.keyModifier == data.receivedKeyEvent.keyModifier );
441 DALI_TEST_CHECK( event4.keyName == data.receivedKeyEvent.keyPressedName );
442 DALI_TEST_CHECK( event4.keyString == data.receivedKeyEvent.keyPressed );
443 DALI_TEST_CHECK( event4.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
447 int UtcDaliStageTouchedSignal(void)
449 TestApplication application;
450 Stage stage = Stage::GetCurrent();
452 TouchedSignalData data;
453 TouchedFunctor functor( data );
454 stage.TouchedSignal().Connect( &application, functor );
457 application.SendNotification();
458 application.Render();
460 // NO ACTORS, SINGLE TOUCH, DOWN, MOTION THEN UP
462 Integration::TouchEvent touchEvent;
463 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
464 application.ProcessEvent( touchEvent );
466 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
467 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
468 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
471 touchEvent.points[0].state = TouchPoint::Motion;
472 touchEvent.points[0].screen.x = 12.0f; // Some motion
473 application.ProcessEvent( touchEvent );
475 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
478 touchEvent.points[0].state = TouchPoint::Up;
479 application.ProcessEvent( touchEvent );
481 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
482 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
483 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
487 // Add an actor to the scene
489 Actor actor = Actor::New();
490 actor.SetSize( 100.0f, 100.0f );
491 actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
492 actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
493 actor.TouchedSignal().Connect( &DummyTouchCallback );
497 application.SendNotification();
498 application.Render();
500 // ACTOR ON SCENE, SINGLE TOUCH, DOWN IN ACTOR, MOTION THEN UP OUTSIDE ACTOR
502 Integration::TouchEvent touchEvent;
503 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
504 application.ProcessEvent( touchEvent );
506 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
507 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
508 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].hitActor == actor );
511 touchEvent.points[0].state = TouchPoint::Motion;
512 touchEvent.points[0].screen.x = 150.0f; // Some motion
513 application.ProcessEvent( touchEvent );
515 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
518 touchEvent.points[0].state = TouchPoint::Up;
519 application.ProcessEvent( touchEvent );
521 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
522 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
523 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
527 // INTERRUPTED BEFORE DOWN AND INTERRUPTED AFTER DOWN
529 Integration::TouchEvent touchEvent;
530 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Interrupted, 10.0f, 10.0f ) );
531 application.ProcessEvent( touchEvent );
533 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
534 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
535 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
536 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Interrupted );
539 touchEvent.points[0].state = TouchPoint::Down;
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 == actor );
545 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Down );
548 touchEvent.points[0].state = TouchPoint::Interrupted;
549 application.ProcessEvent( touchEvent );
551 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
552 DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
553 DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
554 DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Interrupted );
558 // MULTIPLE TOUCH, SHOULD ONLY RECEIVE TOUCH ON FIRST DOWN AND LAST UP
560 Integration::TouchEvent touchEvent;
563 touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
564 application.ProcessEvent( touchEvent );
565 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
569 touchEvent.points[0].state = TouchPoint::Stationary;
570 touchEvent.points.push_back( TouchPoint( 1, TouchPoint::Down, 50.0f, 50.0f ) );
571 application.ProcessEvent( touchEvent );
572 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
575 // Primary point is up
576 touchEvent.points[0].state = TouchPoint::Up;
577 touchEvent.points[1].state = TouchPoint::Stationary;
578 application.ProcessEvent( touchEvent );
579 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
582 // Remove 1st point now, 2nd point is now in motion
583 touchEvent.points.erase( touchEvent.points.begin() );
584 touchEvent.points[0].state = TouchPoint::Motion;
585 touchEvent.points[0].screen.x = 150.0f;
586 application.ProcessEvent( touchEvent );
587 DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
591 touchEvent.points[0].state = TouchPoint::Up;
592 application.ProcessEvent( touchEvent );
593 DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );