[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-DragAndDropDetector.cpp
1 /*
2  * Copyright (c) 2022 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 <stdlib.h>
19 #include <iostream>
20
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
24
25 #include <dali-toolkit/devel-api/drag-drop-detector/drag-and-drop-detector.h>
26 #include <dali/integration-api/events/touch-event-integ.h>
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 void utc_dali_toolkit_drag_drop_detector_startup(void)
32 {
33   test_return_value = TET_UNDEF;
34 }
35
36 void utc_dali_toolkit_drag_drop_detector_cleanup(void)
37 {
38   test_return_value = TET_PASS;
39 }
40
41 namespace
42 {
43 const int RENDER_FRAME_INTERVAL = 16;
44 struct SignalData
45 {
46   SignalData()
47   : functorCalled(false),
48     control(),
49     detector()
50   {
51   }
52
53   void Reset()
54   {
55     functorCalled = false;
56     control.Reset();
57   }
58
59   bool                               functorCalled;
60   Control                            control;
61   Dali::Toolkit::DragAndDropDetector detector;
62 };
63
64 struct DragSignalFunctor
65 {
66   DragSignalFunctor(SignalData& data, bool returnValue = true)
67   : signalData(data),
68     returnValue(returnValue)
69   {
70   }
71
72   bool operator()(Control control, Dali::Toolkit::DragAndDropDetector detector)
73   {
74     signalData.functorCalled = true;
75     signalData.control       = control;
76     signalData.detector      = detector;
77     return returnValue;
78   }
79
80   SignalData& signalData;
81   bool        returnValue;
82 };
83
84 Integration::TouchEvent GenerateSingleTouch(PointState::Type state, const Vector2& screenPosition)
85 {
86   Integration::TouchEvent touchEvent;
87   Integration::Point      point;
88   point.SetState(static_cast<PointState::Type>(state));
89   point.SetScreenPosition(screenPosition);
90   touchEvent.points.push_back(point);
91   return touchEvent;
92 }
93 } // namespace
94
95 int UtcDaliDragAndDropDetectorConstructorN(void)
96 {
97   ToolkitTestApplication application;
98
99   Dali::Toolkit::DragAndDropDetector detector;
100   DALI_TEST_CHECK(!detector);
101
102   END_TEST;
103 }
104
105 int UtcDaliDragAndDropDetectorConstructorP(void)
106 {
107   ToolkitTestApplication application;
108
109   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
110   DALI_TEST_CHECK(detector);
111
112   END_TEST;
113 }
114
115 int UtcDaliDragAndDropDetectorAttachN(void)
116 {
117   ToolkitTestApplication application;
118
119   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
120   Control                            control;
121   detector.Attach(control);
122
123   DALI_TEST_EQUALS(0, detector.GetAttachedControlCount(), TEST_LOCATION);
124   Control control1 = Control::New();
125   detector.Attach(control1);
126   DALI_TEST_EQUALS(1, detector.GetAttachedControlCount(), TEST_LOCATION);
127   detector.Attach(control1);
128   DALI_TEST_EQUALS(1, detector.GetAttachedControlCount(), TEST_LOCATION);
129   END_TEST;
130 }
131
132 int UtcDaliDragAndDropDetectorAttachP(void)
133 {
134   ToolkitTestApplication application;
135
136   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
137   Control                            control  = Control::New();
138
139   detector.Attach(control);
140   DALI_TEST_EQUALS(1, detector.GetAttachedControlCount(), TEST_LOCATION);
141
142   END_TEST;
143 }
144
145 int UtcDaliDragAndDropDetectorDetachN(void)
146 {
147   ToolkitTestApplication application;
148
149   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
150   Control                            control1;
151   Control                            control2 = Control::New();
152
153   detector.Attach(control1);
154   detector.Attach(control2);
155   DALI_TEST_EQUALS(1, detector.GetAttachedControlCount(), TEST_LOCATION);
156
157   detector.Detach(control2);
158   DALI_TEST_EQUALS(0, detector.GetAttachedControlCount(), TEST_LOCATION);
159
160   detector.Detach(control1);
161   DALI_TEST_EQUALS(0, detector.GetAttachedControlCount(), TEST_LOCATION);
162
163   END_TEST;
164 }
165
166 int UtcDaliDragAndDropDetectorDetachP(void)
167 {
168   ToolkitTestApplication application;
169
170   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
171   Control                            control1 = Control::New();
172   Control                            control2 = Control::New();
173   Control                            control3;
174
175   detector.Attach(control1);
176   detector.Attach(control2);
177   detector.Attach(control3);
178
179   DALI_TEST_EQUALS(2, detector.GetAttachedControlCount(), TEST_LOCATION);
180
181   detector.Detach(control3);
182   DALI_TEST_EQUALS(2, detector.GetAttachedControlCount(), TEST_LOCATION);
183   detector.Detach(control2);
184   DALI_TEST_EQUALS(1, detector.GetAttachedControlCount(), TEST_LOCATION);
185
186   detector.Detach(control1);
187   DALI_TEST_EQUALS(0, detector.GetAttachedControlCount(), TEST_LOCATION);
188
189   END_TEST;
190 }
191
192 int UtcDaliDragAndDropDetectorDetachAllN(void)
193 {
194   ToolkitTestApplication application;
195
196   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
197   detector.DetachAll();
198   DALI_TEST_EQUALS(0, detector.GetAttachedControlCount(), TEST_LOCATION);
199
200   END_TEST;
201 }
202 int UtcDaliDragAndDropDetectorDetachAllP(void)
203 {
204   ToolkitTestApplication application;
205
206   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
207   Control                            control1 = Control::New();
208   Control                            control2 = Control::New();
209
210   detector.Attach(control1);
211   detector.Attach(control2);
212   detector.DetachAll();
213   DALI_TEST_EQUALS(0, detector.GetAttachedControlCount(), TEST_LOCATION);
214
215   END_TEST;
216 }
217
218 int UtcDaliDragAndDropDetectorGetAttachedControlCountP(void)
219 {
220   ToolkitTestApplication application;
221
222   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
223   Control                            control1 = Control::New();
224   Control                            control2 = Control::New();
225
226   detector.Attach(control1);
227   detector.Attach(control2);
228   DALI_TEST_EQUALS(2, detector.GetAttachedControlCount(), TEST_LOCATION);
229
230   END_TEST;
231 }
232
233 int UtcDaliDragAndDropDetectorGetAttachedControlN(void)
234 {
235   ToolkitTestApplication application;
236
237   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
238   Control                            control1 = Control::New();
239   Control                            control2 = Control::New();
240
241   detector.Attach(control1);
242
243   Control control = detector.GetAttachedControl(1);
244   DALI_TEST_CHECK(!control);
245
246   END_TEST;
247 }
248
249 int UtcDaliDragAndDropDetectorGetAttachedControlP(void)
250 {
251   ToolkitTestApplication application;
252
253   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
254   Control                            control1 = Control::New();
255   Control                            control2 = Control::New();
256
257   detector.Attach(control1);
258   detector.Attach(control2);
259   Control control = detector.GetAttachedControl(1);
260   DALI_TEST_CHECK(control);
261   DALI_TEST_EQUALS(control2, control, TEST_LOCATION);
262
263   END_TEST;
264 }
265
266 int UtcDaliDragAndDropDetectorStartSignal(void)
267 {
268   ToolkitTestApplication application;
269
270   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
271   Control                            control  = Control::New();
272   control.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
273   control.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
274   application.GetScene().Add(control);
275   detector.Attach(control);
276
277   application.SendNotification();
278   application.Render(RENDER_FRAME_INTERVAL);
279
280   DALI_TEST_CHECK(detector);
281   DALI_TEST_CHECK(control);
282
283   SignalData        data;
284   DragSignalFunctor functor(data);
285   detector.StartedSignal().Connect(&application, functor);
286
287   TestGenerateMiniPan(application);
288
289   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
290   DALI_TEST_EQUALS(control, data.control, TEST_LOCATION);
291   DALI_TEST_EQUALS(Vector2(20.0f, 40.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
292   data.Reset();
293
294   END_TEST;
295 }
296
297 int UtcDaliDragAndDropDetectorEnteredSignal(void)
298 {
299   ToolkitTestApplication application;
300
301   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
302   Control                            control1 = Control::New();
303   Control                            control2 = Control::New();
304   control1.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
305   control2.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
306   control1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
307   control2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
308   control1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
309   control2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
310   control1.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
311   control2.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 100.0f));
312
313   application.GetScene().Add(control1);
314   application.GetScene().Add(control2);
315
316   detector.Attach(control1);
317   detector.Attach(control2);
318
319   application.SendNotification();
320   application.Render(RENDER_FRAME_INTERVAL);
321
322   SignalData        data;
323   DragSignalFunctor functor(data);
324   detector.EnteredSignal().Connect(&application, functor);
325
326   TestGenerateMiniPan(application);
327
328   Vector2 screenCoordinates(10.0f, 110.0f);
329   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
330
331   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
332   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
333
334   data.Reset();
335
336   END_TEST;
337 }
338
339 int UtcDaliDragAndDropDetectorMovedSignal(void)
340 {
341   ToolkitTestApplication application;
342
343   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
344   Control                            control1 = Control::New();
345   Control                            control2 = Control::New();
346   control1.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
347   control2.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
348   control1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
349   control2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
350   control1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
351   control2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
352   control1.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
353   control2.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 100.0f));
354
355   application.GetScene().Add(control1);
356   application.GetScene().Add(control2);
357
358   detector.Attach(control1);
359   detector.Attach(control2);
360
361   application.SendNotification();
362   application.Render(RENDER_FRAME_INTERVAL);
363
364   SignalData        data;
365   DragSignalFunctor functor(data);
366   detector.MovedSignal().Connect(&application, functor);
367
368   TestGenerateMiniPan(application);
369
370   Vector2 screenCoordinates(10.0f, 110.0f);
371   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
372
373   screenCoordinates.x = 10.0f;
374   screenCoordinates.y = 120.0f;
375   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
376
377   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
378   DALI_TEST_EQUALS(Vector2(10.0f, 120.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
379   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
380
381   data.Reset();
382
383   END_TEST;
384 }
385
386 int UtcDaliDragAndDropDetectorExitedSignal(void)
387 {
388   ToolkitTestApplication application;
389
390   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
391   Control                            control1 = Control::New();
392   Control                            control2 = Control::New();
393   control1.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
394   control2.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
395   control1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
396   control2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
397   control1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
398   control2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
399   control1.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
400   control2.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 100.0f));
401
402   control1.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
403   control2.SetProperty(Actor::Property::LEAVE_REQUIRED, true);
404
405   application.GetScene().Add(control1);
406   application.GetScene().Add(control2);
407
408   detector.Attach(control1);
409   detector.Attach(control2);
410
411   application.SendNotification();
412   application.Render(RENDER_FRAME_INTERVAL);
413
414   SignalData        data;
415   DragSignalFunctor functor(data);
416   detector.ExitedSignal().Connect(&application, functor);
417
418   TestGenerateMiniPan(application);
419
420   Vector2 screenCoordinates(10.0f, 110.0f);
421   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
422
423   screenCoordinates.x = 20.0f;
424   screenCoordinates.y = 20.0f;
425   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
426   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
427   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
428
429   data.Reset();
430
431   END_TEST;
432 }
433
434 int UtcDaliDragAndDropDetectorDroppedSignal(void)
435 {
436   ToolkitTestApplication application;
437
438   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
439   Control                            control1 = Control::New();
440   Control                            control2 = Control::New();
441   control1.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
442   control2.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
443   control1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
444   control2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
445   control1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
446   control2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
447   control1.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
448   control2.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 100.0f));
449
450   application.GetScene().Add(control1);
451   application.GetScene().Add(control2);
452
453   detector.Attach(control1);
454   detector.Attach(control2);
455
456   application.SendNotification();
457   application.Render(RENDER_FRAME_INTERVAL);
458
459   SignalData        data;
460   DragSignalFunctor functor(data);
461   detector.DroppedSignal().Connect(&application, functor);
462
463   TestGenerateMiniPan(application);
464
465   Vector2 screenCoordinates(10.0f, 110.0f);
466   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
467
468   screenCoordinates.x = 10.0f;
469   screenCoordinates.y = 112.0f;
470   application.ProcessEvent(GenerateSingleTouch(PointState::UP, screenCoordinates));
471
472   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
473   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
474   DALI_TEST_EQUALS(Vector2(10.0f, 112.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
475   DALI_TEST_EQUALS(true, detector.GetContent().empty(), TEST_LOCATION);
476
477   data.Reset();
478
479   END_TEST;
480 }
481
482 int UtcDaliDragAndDropDetectorEndedSignal(void)
483 {
484   ToolkitTestApplication application;
485
486   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
487   Control                            control1 = Control::New();
488   Control                            control2 = Control::New();
489   control1.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
490   control2.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
491   control1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
492   control2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
493   control1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
494   control2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
495   control1.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
496   control2.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 100.0f));
497
498   application.GetScene().Add(control1);
499   application.GetScene().Add(control2);
500
501   application.SendNotification();
502   application.Render(RENDER_FRAME_INTERVAL);
503
504   detector.Attach(control1);
505   detector.Attach(control2);
506
507   SignalData        data;
508   DragSignalFunctor functor(data);
509   detector.EndedSignal().Connect(&application, functor);
510
511   TestGenerateMiniPan(application);
512
513   application.ProcessEvent(GenerateSingleTouch(PointState::DOWN, Vector2(10.0f, 10.0f)));
514
515   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
516   DALI_TEST_EQUALS(control1, data.control, TEST_LOCATION);
517   data.Reset();
518
519   END_TEST;
520 }
521
522 int UtcDaliDragAndDropDetectorGetContent(void)
523 {
524   ToolkitTestApplication application;
525
526   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
527   Control                            control1 = Control::New();
528   Control                            control2 = Control::New();
529   control1.SetProperty(Dali::Actor::Property::NAME, "control1");
530   control2.SetProperty(Dali::Actor::Property::NAME, "control2");
531   control1.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
532   control2.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
533   control1.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
534   control2.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
535   control1.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
536   control2.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::TOP_LEFT);
537   control1.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 0.0f));
538   control2.SetProperty(Actor::Property::POSITION, Vector2(0.0f, 100.0f));
539
540   application.GetScene().Add(control1);
541   application.GetScene().Add(control2);
542
543   detector.Attach(control1);
544   detector.Attach(control2);
545
546   application.SendNotification();
547   application.Render(RENDER_FRAME_INTERVAL);
548
549   SignalData        data;
550   DragSignalFunctor functor(data);
551   detector.DroppedSignal().Connect(&application, functor);
552
553   TestGenerateMiniPan(application);
554
555   Vector2 screenCoordinates(10.0f, 110.0f);
556   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
557
558   screenCoordinates.x = 10.0f;
559   screenCoordinates.y = 112.0f;
560   application.ProcessEvent(GenerateSingleTouch(PointState::UP, screenCoordinates));
561
562   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
563   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
564   DALI_TEST_EQUALS(Vector2(10.0f, 112.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
565   DALI_TEST_EQUALS("control1", detector.GetContent(), TEST_LOCATION);
566
567   data.Reset();
568
569   END_TEST;
570 }