[Tizen] Add screen and client rotation itself function
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Scene.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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
18 #include <iostream>
19
20 #include <stdlib.h>
21 #include <dali/integration-api/scene.h>
22 #include <dali/integration-api/events/key-event-integ.h>
23 #include <dali/public-api/events/key-event.h>
24 #include <dali/integration-api/events/touch-event-integ.h>
25 #include <dali/integration-api/events/wheel-event-integ.h>
26
27 #include <dali-test-suite-utils.h>
28 #include <mesh-builder.h>
29
30 // Internal headers are allowed here
31
32 namespace
33 {
34
35 const std::string DEFAULT_DEVICE_NAME("hwKeyboard");
36
37 // Functor for EventProcessingFinished signal
38 struct EventProcessingFinishedFunctor
39 {
40   /**
41    * @param[in] eventProcessingFinished reference to a boolean variable used to check if signal has been called.
42    */
43   EventProcessingFinishedFunctor( bool& eventProcessingFinished )
44   : mEventProcessingFinished( eventProcessingFinished )
45   {}
46
47   void operator()()
48   {
49     mEventProcessingFinished = true;
50   }
51
52   bool& mEventProcessingFinished;
53 };
54
55 // Stores data that is populated in the key-event callback and will be read by the TET cases
56 struct KeyEventSignalData
57 {
58   KeyEventSignalData()
59   : functorCalled(false)
60   {}
61
62   void Reset()
63   {
64     functorCalled = false;
65
66     receivedKeyEvent.keyModifier = 0;
67     receivedKeyEvent.keyPressedName.clear();
68     receivedKeyEvent.keyPressed.clear();
69   }
70
71   bool functorCalled;
72   KeyEvent receivedKeyEvent;
73 };
74
75 // Functor that sets the data when called
76 struct KeyEventReceivedFunctor
77 {
78   KeyEventReceivedFunctor( KeyEventSignalData& data ) : signalData( data ) { }
79
80   bool operator()( const KeyEvent& keyEvent )
81   {
82     signalData.functorCalled = true;
83     signalData.receivedKeyEvent = keyEvent;
84
85     return true;
86   }
87
88   KeyEventSignalData& signalData;
89 };
90
91 // Stores data that is populated in the touched signal callback and will be read by the TET cases
92 struct TouchedSignalData
93 {
94   TouchedSignalData()
95   : functorCalled(false),
96     createNewScene(false),
97     newSceneCreated(false)
98   {}
99
100   void Reset()
101   {
102     functorCalled = false;
103     createNewScene = false;
104     newSceneCreated = false;
105
106     receivedTouchEvent.points.clear();
107     receivedTouchEvent.time = 0;
108
109     receivedTouchData.Reset();
110   }
111
112   bool functorCalled;
113   bool createNewScene;
114   bool newSceneCreated;
115   TouchEvent receivedTouchEvent;
116   TouchData receivedTouchData;
117 };
118
119 // Functor that sets the data when touched signal is received
120 struct TouchedFunctor
121 {
122   TouchedFunctor( TouchedSignalData& data ) : signalData( data ) { }
123
124   void operator()( const TouchEvent& touch )
125   {
126     signalData.functorCalled = true;
127     signalData.receivedTouchEvent = touch;
128   }
129
130   TouchedSignalData& signalData;
131 };
132
133 // Functor that sets the data when touched signal is received
134 struct TouchFunctor
135 {
136   TouchFunctor( TouchedSignalData& data ) : signalData( data ) { }
137
138   void operator()( const TouchData& touch )
139   {
140     signalData.functorCalled = true;
141     signalData.receivedTouchData = touch;
142
143     if ( signalData.createNewScene )
144     {
145       Dali::Integration::Scene scene = Dali::Integration::Scene::New( Size( 480.0f, 800.0f  ) );
146       DALI_TEST_CHECK( scene );
147
148       signalData.newSceneCreated = true;
149     }
150   }
151
152   void operator()()
153   {
154     signalData.functorCalled = true;
155   }
156
157   TouchedSignalData& signalData;
158 };
159
160 // Stores data that is populated in the wheel-event callback and will be read by the TET cases
161 struct WheelEventSignalData
162 {
163   WheelEventSignalData()
164   : functorCalled(false)
165   {}
166
167   void Reset()
168   {
169     functorCalled = false;
170   }
171
172   bool functorCalled;
173   WheelEvent receivedWheelEvent;
174 };
175
176 // Functor that sets the data when wheel-event signal is received
177 struct WheelEventReceivedFunctor
178 {
179   WheelEventReceivedFunctor( WheelEventSignalData& data ) : signalData( data ) { }
180
181   bool operator()( const WheelEvent& wheelEvent )
182   {
183     signalData.functorCalled = true;
184     signalData.receivedWheelEvent = wheelEvent;
185
186     return true;
187   }
188
189   WheelEventSignalData& signalData;
190 };
191
192 // Stores data that is populated in the KeyEventGeneratedSignal callback and will be read by the TET cases
193 struct KeyEventGeneratedSignalData
194 {
195   KeyEventGeneratedSignalData()
196   : functorCalled(false)
197   {}
198
199   void Reset()
200   {
201     functorCalled = false;
202
203     receivedKeyEvent.keyModifier = 0;
204     receivedKeyEvent.keyPressedName.clear();
205     receivedKeyEvent.keyPressed.clear();
206   }
207
208   bool functorCalled;
209   KeyEvent receivedKeyEvent;
210 };
211
212 // Functor that sets the data when called
213 struct KeyEventGeneratedReceivedFunctor
214 {
215   KeyEventGeneratedReceivedFunctor( KeyEventGeneratedSignalData& data )
216   : signalData( data )
217   {}
218
219   bool operator()( const KeyEvent& keyEvent )
220   {
221     signalData.functorCalled = true;
222     signalData.receivedKeyEvent = keyEvent;
223
224     return true;
225   }
226
227   bool operator()()
228   {
229     signalData.functorCalled = true;
230     return true;
231   }
232
233   KeyEventGeneratedSignalData& signalData;
234 };
235
236 void GenerateTouch( TestApplication& application, PointState::Type state, const Vector2& screenPosition )
237 {
238   Integration::TouchEvent touchEvent;
239   Integration::Point point;
240   point.SetState( state );
241   point.SetScreenPosition( screenPosition );
242   touchEvent.points.push_back( point );
243   application.ProcessEvent( touchEvent );
244 }
245
246 bool DummyTouchCallback( Actor actor, const TouchEvent& touch )
247 {
248   return true;
249 }
250
251 } // unnamed namespace
252
253 int UtcDaliSceneAdd(void)
254 {
255   TestApplication application;
256   tet_infoline("Testing Dali::Integration::Scene::Add");
257
258   Dali::Integration::Scene scene = application.GetScene();
259
260   Actor actor = Actor::New();
261   DALI_TEST_CHECK( !actor.OnStage() );
262
263   scene.Add( actor );
264   DALI_TEST_CHECK( actor.OnStage() );
265
266   END_TEST;
267 }
268
269 int UtcDaliSceneRemove(void)
270 {
271   TestApplication application;
272   tet_infoline("Testing Dali::Integration::Scene::Remove");
273
274   Dali::Integration::Scene scene = application.GetScene();
275
276   Actor actor = Actor::New();
277   DALI_TEST_CHECK( !actor.OnStage() );
278
279   scene.Add( actor );
280   DALI_TEST_CHECK( actor.OnStage() );
281
282   scene.Remove(actor);
283   DALI_TEST_CHECK( !actor.OnStage() );
284
285   END_TEST;
286 }
287
288 int UtcDaliSceneGetSize(void)
289 {
290   TestApplication application;
291   tet_infoline("Testing Dali::Integration::Scene::GetSize");
292
293   Dali::Integration::Scene scene = application.GetScene();
294   Size size = scene.GetSize();
295   DALI_TEST_EQUALS( TestApplication::DEFAULT_SURFACE_WIDTH, size.width, TEST_LOCATION );
296   DALI_TEST_EQUALS( TestApplication::DEFAULT_SURFACE_HEIGHT, size.height, TEST_LOCATION );
297
298   END_TEST;
299 }
300
301 int UtcDaliSceneGetDpi(void)
302 {
303   TestApplication application; // Initializes core DPI to default values
304
305   // Test that setting core DPI explicitly also sets up the scene's DPI.
306   Dali::Integration::Scene scene = application.GetScene();
307   scene.SetDpi( Vector2(200.0f, 180.0f) );
308   Vector2 dpi = scene.GetDpi();
309   DALI_TEST_EQUALS( dpi.x, 200.0f, TEST_LOCATION );
310   DALI_TEST_EQUALS( dpi.y, 180.0f, TEST_LOCATION );
311   END_TEST;
312 }
313
314 int UtcDaliSceneGetRenderTaskList(void)
315 {
316   TestApplication application;
317   tet_infoline("Testing Dali::Integration::Scene::GetRenderTaskList");
318
319   Dali::Integration::Scene scene = application.GetScene();
320
321   // Check we get a valid instance.
322   const RenderTaskList& tasks = scene.GetRenderTaskList();
323
324   // There should be 1 task by default.
325   DALI_TEST_EQUALS( tasks.GetTaskCount(), 1u, TEST_LOCATION );
326
327   // RenderTaskList has it's own UTC tests.
328   // But we can confirm that GetRenderTaskList in Stage retrieves the same RenderTaskList each time.
329   RenderTask newTask = scene.GetRenderTaskList().CreateTask();
330
331   DALI_TEST_EQUALS( scene.GetRenderTaskList().GetTask( 1 ), newTask, TEST_LOCATION );
332
333   END_TEST;
334 }
335
336 int UtcDaliSceneGetRootLayer(void)
337 {
338   TestApplication application;
339   tet_infoline("Testing Dali::Integration::Scene::GetRootLayer");
340
341   Dali::Integration::Scene scene = application.GetScene();
342   Layer layer = scene.GetLayer( 0 );
343   DALI_TEST_CHECK( layer );
344
345   // Check that GetRootLayer() correctly retreived layer 0.
346   DALI_TEST_CHECK( scene.GetRootLayer() == layer );
347
348   END_TEST;
349 }
350
351 int UtcDaliSceneGetLayerCount(void)
352 {
353   TestApplication application;
354   tet_infoline("Testing Dali::Integration::Scene::GetLayerCount");
355
356   Dali::Integration::Scene scene = application.GetScene();
357   // Initially we have a default layer
358   DALI_TEST_EQUALS( scene.GetLayerCount(), 1u, TEST_LOCATION );
359
360   Layer layer = Layer::New();
361   scene.Add( layer );
362
363   DALI_TEST_EQUALS( scene.GetLayerCount(), 2u, TEST_LOCATION );
364   END_TEST;
365 }
366
367 int UtcDaliSceneGetLayer(void)
368 {
369   TestApplication application;
370   tet_infoline("Testing Dali::Integration::Scene::GetLayer");
371
372   Dali::Integration::Scene scene = application.GetScene();
373
374   Layer rootLayer = scene.GetLayer( 0 );
375   DALI_TEST_CHECK( rootLayer );
376
377   Layer layer = Layer::New();
378   scene.Add( layer );
379
380   Layer sameLayer = scene.GetLayer( 1 );
381   DALI_TEST_CHECK( layer == sameLayer );
382
383   END_TEST;
384 }
385
386 int UtcDaliSceneGet(void)
387 {
388   TestApplication application;
389   tet_infoline("Testing Dali::Integration::Scene::Get");
390
391   Dali::Integration::Scene scene = application.GetScene();
392
393   Actor actor = Actor::New();
394   DALI_TEST_CHECK( Dali::Integration::Scene() == Dali::Integration::Scene::Get( actor ) );
395
396   scene.Add( actor );
397
398   DALI_TEST_CHECK( scene == Dali::Integration::Scene::Get( actor ) );
399
400   END_TEST;
401 }
402
403 int UtcDaliSceneDiscard(void)
404 {
405   TestApplication application;
406   tet_infoline("Testing Dali::Scene::Discard");
407
408   // Create a new Scene
409   Dali::Integration::Scene scene = Dali::Integration::Scene::New( Size( 480.0f, 800.0f ) );
410   DALI_TEST_CHECK( scene );
411
412   // One reference of scene kept here and the other one kept in the Core
413   DALI_TEST_CHECK( scene.GetBaseObject().ReferenceCount() == 2 );
414
415   // Render and notify.
416   application.SendNotification();
417   application.Render(0);
418
419   // Keep the reference of the root layer handle so it will still be alive after the scene is deleted
420   Layer rootLayer = scene.GetRootLayer();
421   DALI_TEST_CHECK( rootLayer );
422   DALI_TEST_CHECK( rootLayer.GetBaseObject().ReferenceCount() == 2 );
423
424   // Request to discard the scene from the Core
425   scene.Discard();
426   DALI_TEST_CHECK( scene.GetBaseObject().ReferenceCount() == 1 );
427
428   // Reset the scene handle
429   scene.Reset();
430
431   // Render and notify.
432   application.SendNotification();
433   application.Render(0);
434
435   // At this point, the scene should have been automatically deleted
436   // To prove this, the ref count of the root layer handle should be decremented to 1
437   DALI_TEST_CHECK( rootLayer.GetBaseObject().ReferenceCount() == 1 );
438
439   // Delete the root layer handle
440   rootLayer.Reset();
441
442   // Render and notify.
443   application.SendNotification();
444   application.Render(0);
445
446   END_TEST;
447 }
448
449 int UtcDaliSceneCreateNewSceneDuringCoreEventProcessing(void)
450 {
451   TestApplication application;
452
453   Dali::Integration::Scene scene = application.GetScene();
454
455   TouchedSignalData data;
456   data.createNewScene = true;
457   TouchFunctor functor( data );
458   scene.TouchSignal().Connect( &application, functor );
459
460   // Render and notify.
461   application.SendNotification();
462   application.Render();
463
464   GenerateTouch( application, PointState::DOWN, Vector2( 10.0f, 10.0f ) );
465
466   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
467   DALI_TEST_EQUALS( true, data.createNewScene, TEST_LOCATION );
468   DALI_TEST_EQUALS( true, data.newSceneCreated, TEST_LOCATION );
469   data.Reset();
470
471   END_TEST;
472 }
473
474 int UtcDaliSceneRootLayerAndSceneAlignment(void)
475 {
476   TestApplication application;
477
478   // Create a Scene
479   Dali::Integration::Scene scene = Dali::Integration::Scene::New( Size( 480.0f, 800.0f ) );
480   DALI_TEST_CHECK( scene );
481
482   // One reference of scene kept here and the other one kept in the Core
483   DALI_TEST_CHECK( scene.GetBaseObject().ReferenceCount() == 2 );
484
485   // Add a renderable actor to the scene
486   auto actor = CreateRenderableActor();
487   scene.Add( actor );
488
489   // Render and notify.
490   application.SendNotification();
491   application.Render(0);
492
493   // Keep the reference of the root layer handle so it will still be alive after the scene is deleted
494   Layer rootLayer = scene.GetRootLayer();
495   DALI_TEST_CHECK( rootLayer );
496   DALI_TEST_CHECK( rootLayer.GetBaseObject().ReferenceCount() == 2 );
497
498   // Request to discard the scene from the Core
499   scene.Discard();
500   DALI_TEST_CHECK( scene.GetBaseObject().ReferenceCount() == 1 );
501
502   // Reset the scene handle
503   scene.Reset();
504
505   // Render and notify.
506   application.SendNotification();
507   application.Render(0);
508
509   // At this point, the scene should have been automatically deleted
510   // To prove this, the ref count of the root layer handle should be decremented to 1
511   DALI_TEST_CHECK( rootLayer.GetBaseObject().ReferenceCount() == 1 );
512
513   // Create a new Scene while the root layer of the deleted scene is still alive
514   Dali::Integration::Scene newScene = Dali::Integration::Scene::New( Size( 480.0f, 800.0f ) );
515   DALI_TEST_CHECK( newScene );
516
517   // Render and notify.
518   application.SendNotification();
519   application.Render(0);
520
521   // At this point, we have only one scene but two root layers
522   // The root layer of the deleted scene is still alive
523   DALI_TEST_CHECK( rootLayer.GetBaseObject().ReferenceCount() == 1 );
524
525   // Delete the root layer of the deleted scene
526   rootLayer.Reset();
527
528   // Render and notify.
529   application.SendNotification();
530   application.Render(0);
531
532   END_TEST;
533 }
534
535 int UtcDaliSceneEventProcessingFinishedP(void)
536 {
537   TestApplication application;
538   Dali::Integration::Scene scene = application.GetScene();
539
540   bool eventProcessingFinished = false;
541   EventProcessingFinishedFunctor functor( eventProcessingFinished );
542   scene.EventProcessingFinishedSignal().Connect( &application, functor );
543
544   Actor actor( Actor::New() );
545   scene.Add( actor );
546
547   application.SendNotification();
548   application.Render();
549
550   DALI_TEST_CHECK( eventProcessingFinished );
551
552   END_TEST;
553 }
554
555 int UtcDaliSceneEventProcessingFinishedN(void)
556 {
557   TestApplication application;
558   Dali::Integration::Scene scene = application.GetScene();
559
560   bool eventProcessingFinished = false;
561   EventProcessingFinishedFunctor functor( eventProcessingFinished );
562   scene.EventProcessingFinishedSignal().Connect( &application, functor );
563
564   Actor actor( Actor::New() );
565   scene.Add( actor );
566
567   // Do not complete event processing and confirm the signal has not been emitted.
568   DALI_TEST_CHECK( !eventProcessingFinished );
569
570   END_TEST;
571 }
572
573 int UtcDaliSceneSignalKeyEventP(void)
574 {
575   TestApplication application;
576   Dali::Integration::Scene scene = application.GetScene();
577
578   KeyEventSignalData data;
579   KeyEventReceivedFunctor functor( data );
580   scene.KeyEventSignal().Connect( &application, functor );
581
582   Integration::KeyEvent event( "i", "", "i", 0, 0, 0, Integration::KeyEvent::Down, "i", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE );
583   application.ProcessEvent( event );
584
585   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
586   DALI_TEST_CHECK( event.keyModifier == data.receivedKeyEvent.keyModifier );
587   DALI_TEST_CHECK( event.keyName == data.receivedKeyEvent.keyPressedName );
588   DALI_TEST_CHECK( event.keyString == data.receivedKeyEvent.keyPressed );
589   DALI_TEST_CHECK( event.state == static_cast<Integration::KeyEvent::State>( data.receivedKeyEvent.state ) );
590
591   data.Reset();
592
593   Integration::KeyEvent event2( "i", "", "i", 0, 0, 0, Integration::KeyEvent::Up, "i", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE );
594   application.ProcessEvent( event2 );
595
596   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
597   DALI_TEST_CHECK( event2.keyModifier == data.receivedKeyEvent.keyModifier );
598   DALI_TEST_CHECK( event2.keyName == data.receivedKeyEvent.keyPressedName );
599   DALI_TEST_CHECK( event2.keyString == data.receivedKeyEvent.keyPressed );
600   DALI_TEST_CHECK( event2.state == static_cast<Integration::KeyEvent::State>( data.receivedKeyEvent.state ) );
601
602   data.Reset();
603
604   Integration::KeyEvent event3( "a", "", "a", 0, 0, 0, Integration::KeyEvent::Down, "a", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE );
605   application.ProcessEvent( event3 );
606
607   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
608   DALI_TEST_CHECK( event3.keyModifier == data.receivedKeyEvent.keyModifier );
609   DALI_TEST_CHECK( event3.keyName == data.receivedKeyEvent.keyPressedName );
610   DALI_TEST_CHECK( event3.keyString == data.receivedKeyEvent.keyPressed );
611   DALI_TEST_CHECK( event3.state == static_cast<Integration::KeyEvent::State>( data.receivedKeyEvent.state ) );
612
613   data.Reset();
614
615   Integration::KeyEvent event4( "a", "", "a", 0, 0, 0, Integration::KeyEvent::Up, "a", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE );
616   application.ProcessEvent( event4 );
617
618   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
619   DALI_TEST_CHECK( event4.keyModifier == data.receivedKeyEvent.keyModifier );
620   DALI_TEST_CHECK( event4.keyName == data.receivedKeyEvent.keyPressedName );
621   DALI_TEST_CHECK( event4.keyString == data.receivedKeyEvent.keyPressed );
622   DALI_TEST_CHECK( event4.state == static_cast<Integration::KeyEvent::State>( data.receivedKeyEvent.state ) );
623   END_TEST;
624 }
625
626 int UtcDaliSceneSignalKeyEventN(void)
627 {
628   TestApplication application;
629   Dali::Integration::Scene scene = application.GetScene();
630
631   KeyEventSignalData data;
632   KeyEventReceivedFunctor functor( data );
633   scene.KeyEventSignal().Connect( &application, functor );
634
635   // Check that a non-pressed key events data is not modified.
636   DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
637
638   END_TEST;
639 }
640
641 int UtcDaliSceneTouchSignalP(void)
642 {
643   TestApplication application;
644   Dali::Integration::Scene scene = application.GetScene();
645
646   TouchedSignalData data;
647   TouchFunctor functor( data );
648   scene.TouchSignal().Connect( &application, functor );
649
650   // Render and notify.
651   application.SendNotification();
652   application.Render();
653
654   // Basic test: No actors, single touch (down then up).
655   {
656     GenerateTouch( application, PointState::DOWN, Vector2( 10.0f, 10.0f ) );
657
658     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
659     DALI_TEST_CHECK( data.receivedTouchData.GetPointCount() != 0u );
660     DALI_TEST_CHECK( !data.receivedTouchData.GetHitActor(0) );
661     data.Reset();
662
663     GenerateTouch( application, PointState::UP, Vector2( 10.0f, 10.0f ) );
664
665     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
666     DALI_TEST_CHECK( data.receivedTouchData.GetPointCount() != 0u );
667     DALI_TEST_CHECK( !data.receivedTouchData.GetHitActor(0) );
668     data.Reset();
669   }
670
671   // Add an actor to the scene.
672   Actor actor = Actor::New();
673   actor.SetSize( 100.0f, 100.0f );
674   actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
675   actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
676   actor.TouchedSignal().Connect( &DummyTouchCallback );
677   scene.Add( actor );
678
679   // Render and notify.
680   application.SendNotification();
681   application.Render();
682
683   // Actor on scene, single touch, down in actor, motion, then up outside actor.
684   {
685     GenerateTouch( application, PointState::DOWN, Vector2( 10.0f, 10.0f ) );
686
687     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
688     DALI_TEST_CHECK( data.receivedTouchData.GetPointCount() != 0u );
689     DALI_TEST_CHECK( data.receivedTouchData.GetHitActor(0) == actor );
690     data.Reset();
691
692     GenerateTouch( application, PointState::MOTION, Vector2( 150.0f, 10.0f ) ); // Some motion
693
694     DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
695     data.Reset();
696
697     GenerateTouch( application, PointState::UP, Vector2( 150.0f, 10.0f ) ); // Some motion
698
699     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
700     DALI_TEST_CHECK( data.receivedTouchData.GetPointCount() != 0u );
701     DALI_TEST_CHECK( !data.receivedTouchData.GetHitActor(0) );
702     data.Reset();
703   }
704
705   // Multiple touch. Should only receive a touch on first down and last up.
706   {
707     Integration::TouchEvent touchEvent;
708     Integration::Point point;
709
710     // 1st point
711     point.SetState( PointState::DOWN );
712     point.SetScreenPosition( Vector2( 10.0f, 10.0f ) );
713     touchEvent.points.push_back( point );
714     application.ProcessEvent( touchEvent );
715     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
716     DALI_TEST_EQUALS( data.receivedTouchData.GetPointCount(), 1u, TEST_LOCATION );
717     data.Reset();
718
719     // 2nd point
720     touchEvent.points[0].SetState( PointState::STATIONARY );
721     point.SetDeviceId( 1 );
722     point.SetScreenPosition( Vector2( 50.0f, 50.0f ) );
723     touchEvent.points.push_back( point );
724     application.ProcessEvent( touchEvent );
725     DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
726     data.Reset();
727
728     // Primary point is up
729     touchEvent.points[0].SetState( PointState::UP );
730     touchEvent.points[1].SetState( PointState::STATIONARY );
731     application.ProcessEvent( touchEvent );
732     DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
733     data.Reset();
734
735     // Remove 1st point now, 2nd point is now in motion
736     touchEvent.points.erase( touchEvent.points.begin() );
737     touchEvent.points[0].SetState( PointState::MOTION );
738     touchEvent.points[0].SetScreenPosition( Vector2( 150.0f, 50.0f ) );
739     application.ProcessEvent( touchEvent );
740     DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
741     data.Reset();
742
743     // Final point Up
744     touchEvent.points[0].SetState( PointState::UP );
745     application.ProcessEvent( touchEvent );
746     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
747     DALI_TEST_EQUALS( data.receivedTouchData.GetPointCount(), 1u, TEST_LOCATION );
748     data.Reset();
749   }
750   END_TEST;
751 }
752
753 int UtcDaliSceneTouchSignalN(void)
754 {
755   TestApplication application;
756   Dali::Integration::Scene scene = application.GetScene();
757
758   TouchedSignalData data;
759   TouchFunctor functor( data );
760   scene.TouchSignal().Connect( &application, functor );
761
762   // Render and notify.
763   application.SendNotification();
764   application.Render();
765
766   // Confirm functor not called before there has been any touch event.
767   DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
768
769   // No actors, single touch, down, motion then up.
770   {
771     GenerateTouch( application, PointState::DOWN, Vector2( 10.0f, 10.0f ) );
772
773     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
774     DALI_TEST_CHECK( data.receivedTouchData.GetPointCount() != 0u );
775     DALI_TEST_CHECK( !data.receivedTouchData.GetHitActor(0));
776
777     data.Reset();
778
779     // Confirm there is no signal when the touchpoint is only moved.
780     GenerateTouch( application, PointState::MOTION, Vector2( 1200.0f, 10.0f ) ); // Some motion
781
782     DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
783     data.Reset();
784
785     // Confirm a following up event generates a signal.
786     GenerateTouch( application, PointState::UP, Vector2( 1200.0f, 10.0f ) );
787
788     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
789     DALI_TEST_CHECK( data.receivedTouchData.GetPointCount() != 0u );
790     DALI_TEST_CHECK( !data.receivedTouchData.GetHitActor(0));
791     data.Reset();
792   }
793
794   // Add an actor to the scene.
795   Actor actor = Actor::New();
796   actor.SetSize( 100.0f, 100.0f );
797   actor.SetAnchorPoint( AnchorPoint::TOP_LEFT );
798   actor.SetParentOrigin( ParentOrigin::TOP_LEFT );
799   actor.TouchedSignal().Connect( &DummyTouchCallback );
800   scene.Add( actor );
801
802   // Render and notify.
803   application.SendNotification();
804   application.Render();
805
806   // Actor on scene. Interrupted before down and interrupted after down.
807   {
808     GenerateTouch( application, PointState::INTERRUPTED, Vector2( 10.0f, 10.0f ) );
809
810     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
811     DALI_TEST_CHECK( data.receivedTouchData.GetPointCount() != 0u );
812     DALI_TEST_CHECK( !data.receivedTouchData.GetHitActor(0) );
813     DALI_TEST_CHECK( data.receivedTouchData.GetState(0) == PointState::INTERRUPTED );
814     data.Reset();
815
816     GenerateTouch( application, PointState::DOWN, Vector2( 10.0f, 10.0f ) );
817
818     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
819     DALI_TEST_CHECK( data.receivedTouchData.GetPointCount() != 0u );
820     DALI_TEST_CHECK( data.receivedTouchData.GetHitActor(0) == actor );
821     DALI_TEST_CHECK( data.receivedTouchData.GetState(0) == PointState::DOWN );
822     data.Reset();
823
824     GenerateTouch( application, PointState::INTERRUPTED, Vector2( 10.0f, 10.0f ) );
825
826     DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
827     DALI_TEST_CHECK( data.receivedTouchData.GetPointCount() != 0u );
828     DALI_TEST_CHECK( !data.receivedTouchData.GetHitActor(0) );
829     DALI_TEST_CHECK( data.receivedTouchData.GetState(0) == PointState::INTERRUPTED );
830
831     DALI_TEST_EQUALS( data.receivedTouchData.GetPointCount(), 1u, TEST_LOCATION );
832
833     // Check that getting info about a non-existent point returns an empty handle
834     Actor actor = data.receivedTouchData.GetHitActor( 1 );
835     DALI_TEST_CHECK( !actor );
836
837     data.Reset();
838   }
839
840   END_TEST;
841 }
842
843 int UtcDaliSceneSignalWheelEventP(void)
844 {
845   TestApplication application;
846   Dali::Integration::Scene scene = application.GetScene();
847
848   WheelEventSignalData data;
849   WheelEventReceivedFunctor functor( data );
850   scene.WheelEventSignal().Connect( &application, functor );
851
852   Integration::WheelEvent event( Integration::WheelEvent::CUSTOM_WHEEL, 0, 0u, Vector2( 0.0f, 0.0f ), 1, 1000u );
853   application.ProcessEvent( event );
854
855   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
856   DALI_TEST_CHECK( static_cast< WheelEvent::Type >(event.type) == data.receivedWheelEvent.type );
857   DALI_TEST_CHECK( event.direction == data.receivedWheelEvent.direction );
858   DALI_TEST_CHECK( event.modifiers == data.receivedWheelEvent.modifiers );
859   DALI_TEST_CHECK( event.point == data.receivedWheelEvent.point );
860   DALI_TEST_CHECK( event.z == data.receivedWheelEvent.z );
861   DALI_TEST_CHECK( event.timeStamp == data.receivedWheelEvent.timeStamp );
862
863   data.Reset();
864
865   Integration::WheelEvent event2( Integration::WheelEvent::CUSTOM_WHEEL, 0, 0u, Vector2( 0.0f, 0.0f ), -1, 1000u );
866   application.ProcessEvent( event2 );
867
868   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
869   DALI_TEST_CHECK( static_cast< WheelEvent::Type >(event2.type) == data.receivedWheelEvent.type );
870   DALI_TEST_CHECK( event2.direction == data.receivedWheelEvent.direction );
871   DALI_TEST_CHECK( event2.modifiers == data.receivedWheelEvent.modifiers );
872   DALI_TEST_CHECK( event2.point == data.receivedWheelEvent.point );
873   DALI_TEST_CHECK( event2.z == data.receivedWheelEvent.z );
874   DALI_TEST_CHECK( event2.timeStamp == data.receivedWheelEvent.timeStamp );
875   END_TEST;
876 }
877
878 int UtcDaliSceneSurfaceResizedDefaultScene(void)
879 {
880   tet_infoline( "Ensure resizing of the surface is handled properly" );
881
882   TestApplication application;
883
884   auto defaultScene = application.GetScene();
885   DALI_TEST_CHECK( defaultScene );
886
887   // Ensure stage size matches the scene size
888   auto stage = Stage::GetCurrent();
889   DALI_TEST_EQUALS( stage.GetSize(), defaultScene.GetSize(), TEST_LOCATION );
890
891   // Resize the scene
892   Vector2 newSize( 1000.0f, 2000.0f );
893   DALI_TEST_CHECK( stage.GetSize() != newSize );
894
895   defaultScene.SurfaceResized( newSize.width, newSize.height, 0, false );
896
897   DALI_TEST_EQUALS( stage.GetSize(), newSize, TEST_LOCATION );
898   DALI_TEST_EQUALS( defaultScene.GetSize(), newSize, TEST_LOCATION );
899
900   END_TEST;
901 }
902
903 int UtcDaliSceneSurfaceResizedDefaultSceneViewport(void)
904 {
905   tet_infoline( "Ensure resizing of the surface & viewport is handled properly" );
906
907   TestApplication application;
908   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
909   TraceCallStack& callStack = glAbstraction.GetViewportTrace();
910   glAbstraction.EnableViewportCallTrace( true );
911
912   // Initial scene setup
913   Geometry geometry = CreateQuadGeometry();
914   Shader shader = CreateShader();
915   Renderer renderer = Renderer::New( geometry, shader );
916
917   Actor actor = Actor::New();
918   actor.AddRenderer(renderer);
919   actor.SetSize(400, 400);
920   Stage::GetCurrent().Add(actor);
921
922   // Render before resizing surface
923   application.SendNotification();
924   application.Render(0);
925   glAbstraction.ResetViewportCallStack();
926
927   auto defaultScene = application.GetScene();
928   DALI_TEST_CHECK( defaultScene );
929
930   // Ensure stage size matches the scene size
931   auto stage = Stage::GetCurrent();
932   DALI_TEST_EQUALS( stage.GetSize(), defaultScene.GetSize(), TEST_LOCATION );
933
934   // Resize the scene
935   Vector2 newSize( 1000.0f, 2000.0f );
936   std::string viewportParams( "0, 0, 1000, 2000" ); // to match newSize
937   DALI_TEST_CHECK( stage.GetSize() != newSize );
938   defaultScene.SurfaceResized( newSize.width, newSize.height, 0, false );
939
940   DALI_TEST_EQUALS( stage.GetSize(), newSize, TEST_LOCATION );
941   DALI_TEST_EQUALS( defaultScene.GetSize(), newSize, TEST_LOCATION );
942
943   // Render after resizing surface
944   application.SendNotification();
945   application.Render(0);
946
947   // Check that the viewport is handled properly
948   DALI_TEST_CHECK( callStack.FindMethodAndGetParameters("Viewport", viewportParams ) );
949
950   END_TEST;
951 }
952
953 int UtcDaliSceneSurfaceResizedMultipleRenderTasks(void)
954 {
955   tet_infoline( "Ensure resizing of the surface & viewport is handled properly" );
956
957   TestApplication application;
958   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
959   TraceCallStack& callStack = glAbstraction.GetViewportTrace();
960   glAbstraction.EnableViewportCallTrace( true );
961
962   // Initial scene setup
963   auto stage = Stage::GetCurrent();
964
965   Geometry geometry = CreateQuadGeometry();
966   Shader shader = CreateShader();
967   Renderer renderer = Renderer::New( geometry, shader );
968
969   Actor actor = Actor::New();
970   actor.AddRenderer(renderer);
971   int testWidth = 400;
972   int testHeight = 400;
973   actor.SetSize(testWidth, testHeight);
974   stage.Add(actor);
975
976   CameraActor offscreenCameraActor = CameraActor::New( Size( testWidth, testHeight ) );
977   Stage::GetCurrent().Add( offscreenCameraActor );
978
979   FrameBuffer newFrameBuffer = FrameBuffer::New( testWidth, testHeight, FrameBuffer::Attachment::NONE );
980
981   RenderTask newTask = stage.GetRenderTaskList().CreateTask();
982   newTask.SetCameraActor( offscreenCameraActor );
983   newTask.SetSourceActor( actor );
984   newTask.SetFrameBuffer( newFrameBuffer );
985   newTask.SetViewportPosition( Vector2(0, 0) );
986   newTask.SetViewportSize( Vector2(testWidth, testHeight) );
987
988   // Render before resizing surface
989   application.SendNotification();
990   application.Render(0);
991   glAbstraction.ResetViewportCallStack();
992
993   Rect<int32_t> initialViewport = newTask.GetViewport();
994   int initialWidth = initialViewport.width;
995   int initialHeight = initialViewport.height;
996   DALI_TEST_EQUALS( initialWidth, testWidth, TEST_LOCATION );
997   DALI_TEST_EQUALS( initialHeight, testHeight, TEST_LOCATION );
998
999   auto defaultScene = application.GetScene();
1000   DALI_TEST_CHECK( defaultScene );
1001
1002   // Ensure stage size matches the scene size
1003   DALI_TEST_EQUALS( stage.GetSize(), defaultScene.GetSize(), TEST_LOCATION );
1004
1005   // Resize the scene
1006   Vector2 newSize( 1000.0f, 2000.0f );
1007   std::string viewportParams( "0, 0, 1000, 2000" ); // to match newSize
1008   DALI_TEST_CHECK( stage.GetSize() != newSize );
1009   defaultScene.SurfaceResized( newSize.width, newSize.height, 0, false );
1010
1011   DALI_TEST_EQUALS( stage.GetSize(), newSize, TEST_LOCATION );
1012   DALI_TEST_EQUALS( defaultScene.GetSize(), newSize, TEST_LOCATION );
1013
1014   // Render after resizing surface
1015   application.SendNotification();
1016   application.Render(0);
1017
1018   // Check that the viewport is handled properly
1019   DALI_TEST_CHECK( callStack.FindMethodAndGetParameters("Viewport", viewportParams ) );
1020
1021   // Second render-task should not be affected
1022   Rect<int32_t> viewport = newTask.GetViewport();
1023   int width = viewport.width;
1024   int height = viewport.height;
1025   DALI_TEST_EQUALS( width, testWidth, TEST_LOCATION );
1026   DALI_TEST_EQUALS( height, testHeight, TEST_LOCATION );
1027
1028   END_TEST;
1029 }
1030
1031 int UtcDaliSceneSurfaceResizedAdditionalScene(void)
1032 {
1033   tet_infoline( "Ensure resizing of the surface is handled properly on additional scenes" );
1034
1035   TestApplication application;
1036   Vector2 originalSurfaceSize( 500.0f, 1000.0f );
1037
1038   auto scene = Integration::Scene::New( Size( originalSurfaceSize.width, originalSurfaceSize.height ) );
1039
1040   // Ensure stage size does NOT match the surface size
1041   auto stage = Stage::GetCurrent();
1042   const auto stageSize = stage.GetSize();
1043   DALI_TEST_CHECK( stageSize != originalSurfaceSize );
1044   DALI_TEST_EQUALS( originalSurfaceSize, scene.GetSize(), TEST_LOCATION );
1045
1046   // Resize the surface and inform the scene accordingly
1047   Vector2 newSize( 1000.0f, 2000.0f );
1048   DALI_TEST_CHECK( stage.GetSize() != newSize );
1049
1050   scene.SurfaceResized( newSize.width, newSize.height, 0, false );
1051
1052   // Ensure the stage hasn't been resized
1053   DALI_TEST_EQUALS( stage.GetSize(), stageSize, TEST_LOCATION );
1054   DALI_TEST_EQUALS( scene.GetSize(), newSize, TEST_LOCATION );
1055
1056   END_TEST;
1057 }
1058
1059 int UtcDaliSceneKeyEventGeneratedSignalP(void)
1060 {
1061   TestApplication application;
1062   Dali::Integration::Scene scene = application.GetScene();
1063
1064   KeyEventGeneratedSignalData data;
1065   KeyEventGeneratedReceivedFunctor functor( data );
1066   scene.KeyEventGeneratedSignal().Connect( &application, functor );
1067
1068   Integration::KeyEvent event( "a", "", "a", 0, 0, 0, Integration::KeyEvent::Up, "a", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE );
1069   application.ProcessEvent( event );
1070
1071   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
1072   DALI_TEST_CHECK( event.keyModifier == data.receivedKeyEvent.keyModifier );
1073   DALI_TEST_CHECK( event.keyName == data.receivedKeyEvent.keyPressedName );
1074   DALI_TEST_CHECK( event.keyString == data.receivedKeyEvent.keyPressed );
1075   DALI_TEST_CHECK( event.state == static_cast<Integration::KeyEvent::State>( data.receivedKeyEvent.state ) );
1076
1077   data.Reset();
1078
1079   Integration::KeyEvent event2( "i", "", "i", 0, 0, 0, Integration::KeyEvent::Up, "i", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE );
1080   application.ProcessEvent( event2 );
1081
1082   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
1083   DALI_TEST_CHECK( event2.keyModifier == data.receivedKeyEvent.keyModifier );
1084   DALI_TEST_CHECK( event2.keyName == data.receivedKeyEvent.keyPressedName );
1085   DALI_TEST_CHECK( event2.keyString == data.receivedKeyEvent.keyPressed );
1086   DALI_TEST_CHECK( event2.state == static_cast<Integration::KeyEvent::State>( data.receivedKeyEvent.state ) );
1087
1088   data.Reset();
1089
1090   Integration::KeyEvent event3( "a", "", "a", 0, 0, 0, Integration::KeyEvent::Down, "a", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE );
1091   application.ProcessEvent( event3 );
1092
1093   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
1094   DALI_TEST_CHECK( event3.keyModifier == data.receivedKeyEvent.keyModifier );
1095   DALI_TEST_CHECK( event3.keyName == data.receivedKeyEvent.keyPressedName );
1096   DALI_TEST_CHECK( event3.keyString == data.receivedKeyEvent.keyPressed );
1097   DALI_TEST_CHECK( event3.state == static_cast<Integration::KeyEvent::State>( data.receivedKeyEvent.state ) );
1098
1099   data.Reset();
1100
1101   Integration::KeyEvent event4( "a", "", "a", 0, 0, 0, Integration::KeyEvent::Up, "a", DEFAULT_DEVICE_NAME, Device::Class::NONE, Device::Subclass::NONE );
1102   application.ProcessEvent( event4 );
1103
1104   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
1105   DALI_TEST_CHECK( event4.keyModifier == data.receivedKeyEvent.keyModifier );
1106   DALI_TEST_CHECK( event4.keyName == data.receivedKeyEvent.keyPressedName );
1107   DALI_TEST_CHECK( event4.keyString == data.receivedKeyEvent.keyPressed );
1108   DALI_TEST_CHECK( event4.state == static_cast<Integration::KeyEvent::State>( data.receivedKeyEvent.state ) );
1109   END_TEST;
1110 }
1111
1112 int UtcDaliSceneEnsureReplacedSurfaceKeepsClearColor(void)
1113 {
1114   tet_infoline( "Ensure we keep background color when the scene surface is replaced " );
1115
1116   TestApplication application;
1117
1118   // Create a new scene and set the background color of the main scene
1119   auto defaultScene = application.GetScene();
1120   defaultScene.SetBackgroundColor( Color::BLUE );
1121
1122   // Need to create a renderable as we don't start rendering until we have at least one
1123   // We don't need to add this to any scene
1124   auto actor = CreateRenderableActor();
1125
1126   auto& glAbstraction = application.GetGlAbstraction();
1127   auto clearCountBefore = glAbstraction.GetClearCountCalled();
1128
1129   application.SendNotification();
1130   application.Render();
1131
1132   DALI_TEST_EQUALS( glAbstraction.GetClearCountCalled(), clearCountBefore + 1, TEST_LOCATION );
1133   DALI_TEST_EQUALS( glAbstraction.GetLastClearColor(), Color::BLUE, TEST_LOCATION );
1134
1135   defaultScene.SurfaceReplaced();
1136
1137   application.SendNotification();
1138   application.Render();
1139
1140   DALI_TEST_EQUALS( glAbstraction.GetClearCountCalled(), clearCountBefore + 2, TEST_LOCATION );
1141   DALI_TEST_EQUALS( glAbstraction.GetLastClearColor(), Color::BLUE, TEST_LOCATION );
1142
1143   // Check when the main render task viewport is set the clear color is clipped using scissors
1144   TraceCallStack& scissorTrace = glAbstraction.GetScissorTrace();
1145   TraceCallStack& enabledDisableTrace = glAbstraction.GetEnableDisableTrace();
1146   scissorTrace.Enable( true );
1147   enabledDisableTrace.Enable( true );
1148
1149   defaultScene.GetRenderTaskList().GetTask( 0 ).SetViewport( Viewport( 0.0f, 0.0f, 100.0f, 100.0f ) );
1150
1151   application.SendNotification();
1152   application.Render();
1153
1154   // Check scissor test was enabled.
1155   DALI_TEST_CHECK( enabledDisableTrace.FindMethodAndParams( "Enable", "3089" ) ); // 3089 = 0xC11 (GL_SCISSOR_TEST)
1156
1157   // Check the scissor was set, and the coordinates are correct.
1158   DALI_TEST_CHECK( scissorTrace.FindMethodAndParams( "Scissor", "0, 700, 100, 100" ) );
1159
1160   DALI_TEST_EQUALS( glAbstraction.GetClearCountCalled(), clearCountBefore + 3, TEST_LOCATION );
1161   DALI_TEST_EQUALS( glAbstraction.GetLastClearColor(), Color::BLUE, TEST_LOCATION );
1162
1163   scissorTrace.Enable( false );
1164   scissorTrace.Reset();
1165
1166   enabledDisableTrace.Enable( false );
1167   enabledDisableTrace.Reset();
1168
1169   END_TEST;
1170 }