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