7a943127a4ca28a7f4c6be4d61d99ae42bfd7567
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Stage.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 #include <iostream>
18
19 #include <stdlib.h>
20
21 #include <dali/dali.h>
22 #include <dali/integration-api/events/key-event-integ.h>
23 #include <dali/integration-api/events/touch-event-integ.h>
24
25 #include <dali-test-suite-utils.h>
26
27 using namespace Dali;
28 using namespace std;
29
30 void stage_test_startup(void)
31 {
32   test_return_value = TET_UNDEF;
33 }
34
35 void stage_test_cleanup(void)
36 {
37   test_return_value = TET_PASS;
38 }
39
40 namespace
41 {
42
43 /**
44  * Functor for EventProcessingFinished signal
45  */
46 struct EventProcessingFinishedFunctor
47 {
48   /**
49    * @param[in] eventProcessingFinished reference to a boolean variable used to check if signal has been called.
50    */
51   EventProcessingFinishedFunctor( bool& eventProcessingFinished )
52   : mEventProcessingFinished( eventProcessingFinished )
53   {}
54
55   void operator()()
56   {
57     mEventProcessingFinished = true;
58   }
59
60   bool& mEventProcessingFinished;
61 };
62
63 // Stores data that is populated in the key-event callback and will be read by the TET cases
64 struct KeyEventSignalData
65 {
66   KeyEventSignalData()
67   : functorCalled(false)
68   {}
69
70   void Reset()
71   {
72     functorCalled = false;
73
74     receivedKeyEvent.keyModifier = 0;
75     receivedKeyEvent.keyPressedName.clear();
76     receivedKeyEvent.keyPressed.clear();
77   }
78
79   bool functorCalled;
80   KeyEvent receivedKeyEvent;
81 };
82
83 // Functor that sets the data when called
84 struct KeyEventReceivedFunctor
85 {
86   KeyEventReceivedFunctor( KeyEventSignalData& data ) : signalData( data ) { }
87
88   bool operator()( const KeyEvent& keyEvent )
89   {
90     signalData.functorCalled = true;
91     signalData.receivedKeyEvent = keyEvent;
92
93     return true;
94   }
95
96   KeyEventSignalData& signalData;
97 };
98
99 // Stores data that is populated in the touched signal callback and will be read by the TET cases
100 struct TouchedSignalData
101 {
102   TouchedSignalData()
103   : functorCalled(false)
104   {}
105
106   void Reset()
107   {
108     functorCalled = false;
109
110     receivedTouchEvent.points.clear();
111     receivedTouchEvent.time = 0;
112   }
113
114   bool functorCalled;
115   TouchEvent receivedTouchEvent;
116 };
117
118 // Functor that sets the data when touched signal is received
119 struct TouchedFunctor
120 {
121   TouchedFunctor( TouchedSignalData& data ) : signalData( data ) { }
122
123   void operator()( const TouchEvent& touch )
124   {
125     signalData.functorCalled = true;
126     signalData.receivedTouchEvent = touch;
127   }
128
129   TouchedSignalData& signalData;
130 };
131
132 bool DummyTouchCallback( Actor actor, const TouchEvent& touch )
133 {
134   return true;
135 }
136
137 } // unnamed namespace
138
139
140 int UtcDaliStageDefaultConstructor(void)
141 {
142   TestApplication application;
143   Stage stage;
144
145   DALI_TEST_CHECK(!stage);
146   END_TEST;
147 }
148
149 int UtcDaliStageDestructor(void)
150 {
151   TestApplication application;
152   Stage* stage = new Stage();
153   delete stage;
154   stage = NULL;
155
156   DALI_TEST_CHECK( true );
157   END_TEST;
158 }
159
160 int UtcDaliStageGetCurrent(void)
161 {
162   TestApplication application;
163   Stage stage = Stage::GetCurrent();
164
165   DALI_TEST_CHECK(stage);
166   END_TEST;
167 }
168
169 int UtcDaliStageIsInstalled(void)
170 {
171   DALI_TEST_CHECK(!Stage::IsInstalled());
172
173   TestApplication application;
174
175   Stage::GetCurrent();
176
177   DALI_TEST_CHECK(Stage::IsInstalled());
178   END_TEST;
179 }
180
181 int UtcDaliStageAdd(void)
182 {
183   TestApplication application;
184
185   Stage stage = Stage::GetCurrent();
186
187   Actor actor = Actor::New();
188   DALI_TEST_CHECK(!actor.OnStage());
189
190   stage.Add(actor);
191   DALI_TEST_CHECK(actor.OnStage());
192   END_TEST;
193 }
194
195 int UtcDaliStageRemove(void)
196 {
197   TestApplication application;
198
199   Stage stage = Stage::GetCurrent();
200
201   Actor actor = Actor::New();
202   DALI_TEST_CHECK(!actor.OnStage());
203
204   stage.Add(actor);
205   DALI_TEST_CHECK(actor.OnStage());
206
207   stage.Remove(actor);
208   DALI_TEST_CHECK(!actor.OnStage());
209   END_TEST;
210 }
211
212 int UtcDaliStageGetSize(void)
213 {
214   TestApplication application;
215
216   Stage stage = Stage::GetCurrent();
217
218   Vector2 size = stage.GetSize();
219
220   DALI_TEST_EQUALS(size.width,  static_cast<float>(TestApplication::DEFAULT_SURFACE_WIDTH),  TEST_LOCATION);
221   DALI_TEST_EQUALS(size.height, static_cast<float>(TestApplication::DEFAULT_SURFACE_HEIGHT), TEST_LOCATION);
222   END_TEST;
223 }
224
225 int UtcDaliStageGetDpi01(void)
226 {
227   TestApplication application; // Initializes core DPI to default values
228
229   Stage stage = Stage::GetCurrent();
230
231   // Test the default DPI.
232   Vector2 dpi = stage.GetDpi();
233   DALI_TEST_EQUALS(dpi.x, static_cast<float>(TestApplication::DEFAULT_HORIZONTAL_DPI), TEST_LOCATION);
234   DALI_TEST_EQUALS(dpi.y, static_cast<float>(TestApplication::DEFAULT_VERTICAL_DPI),   TEST_LOCATION);
235   END_TEST;
236 }
237
238 int UtcDaliStageGetDpi02(void)
239 {
240   TestApplication application; // Initializes core DPI to default values
241
242   // Test that setting core DPI explicitly also sets up the Stage's DPI.
243   application.GetCore().SetDpi(200, 180);
244
245   Stage stage = Stage::GetCurrent();
246   Vector2 dpi = stage.GetDpi();
247   DALI_TEST_EQUALS(dpi.x, 200.0f, TEST_LOCATION);
248   DALI_TEST_EQUALS(dpi.y, 180.0f, TEST_LOCATION);
249   END_TEST;
250 }
251
252
253 int UtcDaliStageGetDpi03(void)
254 {
255   TestApplication application(480, 800, 72.0f, 120.0f); // Initializes core DPI with specific values
256
257   Stage stage = Stage::GetCurrent();
258
259   // Test that setting core DPI explicitly also sets up the Stage's DPI.
260   Vector2 dpi = stage.GetDpi();
261   DALI_TEST_EQUALS(dpi.x, 72.0f, TEST_LOCATION);
262   DALI_TEST_EQUALS(dpi.y, 120.0f, TEST_LOCATION);
263   END_TEST;
264 }
265
266 int UtcDaliStageGetLayerCount(void)
267 {
268   TestApplication application;
269
270   Stage stage = Stage::GetCurrent();
271
272   // Initially we have a default layer
273   DALI_TEST_EQUALS(stage.GetLayerCount(), 1u, TEST_LOCATION);
274
275   Layer layer = Layer::New();
276   stage.Add(layer);
277
278   DALI_TEST_EQUALS(stage.GetLayerCount(), 2u, TEST_LOCATION);
279   END_TEST;
280 }
281
282 int UtcDaliStageGetLayer(void)
283 {
284   TestApplication application;
285
286   Stage stage = Stage::GetCurrent();
287
288   Layer rootLayer = stage.GetLayer(0);
289   DALI_TEST_CHECK(rootLayer);
290
291   Layer layer = Layer::New();
292   stage.Add(layer);
293
294   Layer sameLayer = stage.GetLayer(1);
295   DALI_TEST_CHECK(layer == sameLayer);
296   END_TEST;
297 }
298
299
300 int UtcDaliStageGetRootLayer(void)
301 {
302   TestApplication application;
303
304   Stage stage = Stage::GetCurrent();
305
306   Layer rootLayer = stage.GetLayer(0);
307   DALI_TEST_CHECK( rootLayer );
308
309   Layer layer = Layer::New();
310   stage.Add( layer );
311   layer.LowerToBottom();
312
313   DALI_TEST_CHECK( stage.GetRootLayer() == rootLayer );
314   END_TEST;
315 }
316
317 int UtcDaliStageSetBackgroundColor(void)
318 {
319   TestApplication application;
320
321   Stage stage = Stage::GetCurrent();
322
323   Vector4 testColor(0.1f, 0.2f, 0.3f, 1.0f);
324   stage.SetBackgroundColor(testColor);
325
326   DALI_TEST_EQUALS(testColor, stage.GetBackgroundColor(), TEST_LOCATION);
327   END_TEST;
328 }
329
330 int UtcDaliStageGetBackgroundColor(void)
331 {
332   TestApplication application;
333
334   Stage stage = Stage::GetCurrent();
335
336   DALI_TEST_EQUALS(Stage::DEFAULT_BACKGROUND_COLOR, stage.GetBackgroundColor(), TEST_LOCATION);
337   END_TEST;
338 }
339
340 int UtcDaliStageKeepRendering(void)
341 {
342   TestApplication application;
343
344   Stage stage = Stage::GetCurrent();
345
346   // Run core until it wants to sleep
347   bool keepUpdating( true );
348   while ( keepUpdating )
349   {
350     application.SendNotification();
351     keepUpdating = application.Render(1000.0f /*1 second*/);
352   }
353
354   // Force rendering for the next 5 seconds
355   stage.KeepRendering( 5.0f );
356
357   application.SendNotification();
358
359   // Test that core wants to sleep after 10 seconds
360   keepUpdating = application.Render(1000.0f /*1 second*/);
361   DALI_TEST_CHECK( keepUpdating );
362   keepUpdating = application.Render(1000.0f /*2 seconds*/);
363   DALI_TEST_CHECK( keepUpdating );
364   keepUpdating = application.Render(1000.0f /*3 seconds*/);
365   DALI_TEST_CHECK( keepUpdating );
366   keepUpdating = application.Render(1000.0f /*4 seconds*/);
367   DALI_TEST_CHECK( keepUpdating );
368   keepUpdating = application.Render(1000.0f /*5 seconds*/);
369   DALI_TEST_CHECK( !keepUpdating );
370   END_TEST;
371 }
372
373 int UtcDaliStageEventProcessingFinished(void)
374 {
375   TestApplication application;
376   Stage stage = Stage::GetCurrent();
377
378   bool eventProcessingFinished = false;
379   EventProcessingFinishedFunctor functor( eventProcessingFinished );
380   stage.EventProcessingFinishedSignal().Connect( &application, functor );
381
382   Actor actor( Actor::New() );
383   stage.Add( actor );
384
385   application.SendNotification();
386   application.Render();
387
388   DALI_TEST_CHECK( eventProcessingFinished );
389
390   END_TEST;
391 }
392
393 int UtcDaliStageSignalKeyEvent(void)
394 {
395   TestApplication application;
396   Stage stage = Stage::GetCurrent();
397
398   KeyEventSignalData data;
399   KeyEventReceivedFunctor functor( data );
400   stage.KeyEventSignal().Connect( &application, functor );
401
402   Integration::KeyEvent event( "i","i", 0, 0, 0, Integration::KeyEvent::Down );
403   application.ProcessEvent( event );
404
405   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
406   DALI_TEST_CHECK( event.keyModifier == data.receivedKeyEvent.keyModifier );
407   DALI_TEST_CHECK( event.keyName == data.receivedKeyEvent.keyPressedName );
408   DALI_TEST_CHECK( event.keyString == data.receivedKeyEvent.keyPressed );
409   DALI_TEST_CHECK( event.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
410
411   data.Reset();
412
413   Integration::KeyEvent event2( "i","i", 0, 0, 0, Integration::KeyEvent::Up );
414   application.ProcessEvent( event2 );
415
416   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
417   DALI_TEST_CHECK( event2.keyModifier == data.receivedKeyEvent.keyModifier );
418   DALI_TEST_CHECK( event2.keyName == data.receivedKeyEvent.keyPressedName );
419   DALI_TEST_CHECK( event2.keyString == data.receivedKeyEvent.keyPressed );
420   DALI_TEST_CHECK( event2.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
421
422   data.Reset();
423
424   Integration::KeyEvent event3( "a","a", 0, 0, 0, Integration::KeyEvent::Down );
425   application.ProcessEvent( event3 );
426
427   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
428   DALI_TEST_CHECK( event3.keyModifier == data.receivedKeyEvent.keyModifier );
429   DALI_TEST_CHECK( event3.keyName == data.receivedKeyEvent.keyPressedName );
430   DALI_TEST_CHECK( event3.keyString == data.receivedKeyEvent.keyPressed );
431   DALI_TEST_CHECK( event3.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
432
433   data.Reset();
434
435   Integration::KeyEvent event4( "a","a", 0, 0, 0, Integration::KeyEvent::Up );
436   application.ProcessEvent( event4 );
437
438   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
439   DALI_TEST_CHECK( event4.keyModifier == data.receivedKeyEvent.keyModifier );
440   DALI_TEST_CHECK( event4.keyName == data.receivedKeyEvent.keyPressedName );
441   DALI_TEST_CHECK( event4.keyString == data.receivedKeyEvent.keyPressed );
442   DALI_TEST_CHECK( event4.state == static_cast<Integration::KeyEvent::State>(data.receivedKeyEvent.state) );
443   END_TEST;
444 }
445
446 int UtcDaliStageTouchedSignal(void)
447 {
448   TestApplication application;
449   Stage stage = Stage::GetCurrent();
450
451   TouchedSignalData data;
452   TouchedFunctor functor( data );
453   stage.TouchedSignal().Connect( &application, functor );
454
455   // Render and notify
456   application.SendNotification();
457   application.Render();
458
459   // NO ACTORS, SINGLE TOUCH, DOWN, MOTION THEN UP
460   {
461     Integration::TouchEvent touchEvent;
462     touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
463     application.ProcessEvent( touchEvent );
464
465     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
466     DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
467     DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
468     data.Reset();
469
470     touchEvent.points[0].state = TouchPoint::Motion;
471     touchEvent.points[0].screen.x = 12.0f; // Some motion
472     application.ProcessEvent( touchEvent );
473
474     DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
475     data.Reset();
476
477     touchEvent.points[0].state = TouchPoint::Up;
478     application.ProcessEvent( touchEvent );
479
480     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
481     DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
482     DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
483     data.Reset();
484   }
485
486   // Add an actor to the scene
487
488   Actor actor = Actor::New();
489   actor.SetSize( 100.0f, 100.0f );
490   actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
491   actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
492   actor.TouchedSignal().Connect( &DummyTouchCallback );
493   stage.Add( actor );
494
495   // Render and notify
496   application.SendNotification();
497   application.Render();
498
499   // ACTOR ON SCENE, SINGLE TOUCH, DOWN IN ACTOR, MOTION THEN UP OUTSIDE ACTOR
500   {
501     Integration::TouchEvent touchEvent;
502     touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
503     application.ProcessEvent( touchEvent );
504
505     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
506     DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
507     DALI_TEST_CHECK( data.receivedTouchEvent.points[0].hitActor == actor );
508     data.Reset();
509
510     touchEvent.points[0].state = TouchPoint::Motion;
511     touchEvent.points[0].screen.x = 150.0f; // Some motion
512     application.ProcessEvent( touchEvent );
513
514     DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
515     data.Reset();
516
517     touchEvent.points[0].state = TouchPoint::Up;
518     application.ProcessEvent( touchEvent );
519
520     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
521     DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
522     DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
523     data.Reset();
524   }
525
526   // INTERRUPTED BEFORE DOWN AND INTERRUPTED AFTER DOWN
527   {
528     Integration::TouchEvent touchEvent;
529     touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Interrupted, 10.0f, 10.0f ) );
530     application.ProcessEvent( touchEvent );
531
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 );
535     DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Interrupted );
536     data.Reset();
537
538     touchEvent.points[0].state = TouchPoint::Down;
539     application.ProcessEvent( touchEvent );
540
541     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
542     DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
543     DALI_TEST_CHECK( data.receivedTouchEvent.points[0].hitActor == actor );
544     DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Down );
545     data.Reset();
546
547     touchEvent.points[0].state = TouchPoint::Interrupted;
548     application.ProcessEvent( touchEvent );
549
550     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
551     DALI_TEST_CHECK( data.receivedTouchEvent.GetPointCount() != 0 );
552     DALI_TEST_CHECK( !data.receivedTouchEvent.points[0].hitActor );
553     DALI_TEST_CHECK( data.receivedTouchEvent.points[0].state == TouchPoint::Interrupted );
554     data.Reset();
555   }
556
557   // MULTIPLE TOUCH, SHOULD ONLY RECEIVE TOUCH ON FIRST DOWN AND LAST UP
558   {
559     Integration::TouchEvent touchEvent;
560
561     // 1st point
562     touchEvent.points.push_back( TouchPoint( 0, TouchPoint::Down, 10.0f, 10.0f ) );
563     application.ProcessEvent( touchEvent );
564     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
565     data.Reset();
566
567     // 2nd point
568     touchEvent.points[0].state = TouchPoint::Stationary;
569     touchEvent.points.push_back( TouchPoint( 1, TouchPoint::Down, 50.0f, 50.0f ) );
570     application.ProcessEvent( touchEvent );
571     DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
572     data.Reset();
573
574     // Primary point is up
575     touchEvent.points[0].state = TouchPoint::Up;
576     touchEvent.points[1].state = TouchPoint::Stationary;
577     application.ProcessEvent( touchEvent );
578     DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
579     data.Reset();
580
581     // Remove 1st point now, 2nd point is now in motion
582     touchEvent.points.erase( touchEvent.points.begin() );
583     touchEvent.points[0].state = TouchPoint::Motion;
584     touchEvent.points[0].screen.x = 150.0f;
585     application.ProcessEvent( touchEvent );
586     DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
587     data.Reset();
588
589     // Final point Up
590     touchEvent.points[0].state = TouchPoint::Up;
591     application.ProcessEvent( touchEvent );
592     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
593     data.Reset();
594   }
595   END_TEST;
596 }