Revert "Remove dali-core dependency of GLES version."
[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   application.ProcessEvent(GeneratePan(Gesture::Possible, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
309   application.ProcessEvent(GeneratePan(Gesture::Started, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
310
311   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
312   DALI_TEST_EQUALS(control, data.control, TEST_LOCATION);
313   DALI_TEST_EQUALS(Vector2(12.0f, 12.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   application.ProcessEvent(GeneratePan(Gesture::Possible, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
349   application.ProcessEvent(GeneratePan(Gesture::Started, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
350
351   Vector2 screenCoordinates(10.0f, 110.0f);
352   application.ProcessEvent(GenerateSingleTouch(TouchPoint::Motion, screenCoordinates));
353
354   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
355   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
356
357   data.Reset();
358
359   END_TEST;
360
361 }
362
363 int UtcDaliDragAndDropDetectorMovedSignal(void)
364 {
365   TestApplication application;
366
367   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
368   Control control1 = Control::New();
369   Control control2 = Control::New();
370   control1.SetSize(100.0f,100.0f);
371   control2.SetSize(100.0f, 100.0f);
372   control1.SetAnchorPoint(AnchorPoint::TOP_LEFT);
373   control2.SetAnchorPoint(AnchorPoint::TOP_LEFT);
374   control1.SetParentOrigin(ParentOrigin::TOP_LEFT);
375   control2.SetParentOrigin(ParentOrigin::TOP_LEFT);
376   control1.SetPosition(0.0f, 0.0f);
377   control2.SetPosition(0.0f, 100.0f);
378
379   Stage::GetCurrent().Add(control1);
380   Stage::GetCurrent().Add(control2);
381
382   detector.Attach(control1);
383   detector.Attach(control2);
384
385   application.SendNotification();
386   application.Render(RENDER_FRAME_INTERVAL);
387
388   SignalData data;
389   DragSignalFunctor functor(data);
390   detector.MovedSignal().Connect(&application, functor);
391
392   application.ProcessEvent(GeneratePan(Gesture::Possible, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
393   application.ProcessEvent(GeneratePan(Gesture::Started, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
394
395   Vector2 screenCoordinates(10.0f, 110.0f);
396   application.ProcessEvent(GenerateSingleTouch(TouchPoint::Motion, screenCoordinates));
397
398   screenCoordinates.x = 10.0f;
399   screenCoordinates.y = 120.0f;
400   application.ProcessEvent(GenerateSingleTouch(TouchPoint::Motion, screenCoordinates));
401
402   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
403   DALI_TEST_EQUALS(Vector2(10.0f, 120.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
404   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
405
406   data.Reset();
407
408   END_TEST;
409 }
410
411 int UtcDaliDragAndDropDetectorExitedSignal(void)
412 {
413   TestApplication application;
414
415   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
416   Control control1 = Control::New();
417   Control control2 = Control::New();
418   control1.SetSize(100.0f,100.0f);
419   control2.SetSize(100.0f, 100.0f);
420   control1.SetAnchorPoint(AnchorPoint::TOP_LEFT);
421   control2.SetAnchorPoint(AnchorPoint::TOP_LEFT);
422   control1.SetParentOrigin(ParentOrigin::TOP_LEFT);
423   control2.SetParentOrigin(ParentOrigin::TOP_LEFT);
424   control1.SetPosition(0.0f, 0.0f);
425   control2.SetPosition(0.0f, 100.0f);
426
427   control1.SetLeaveRequired(true);
428   control2.SetLeaveRequired(true);
429
430   Stage::GetCurrent().Add(control1);
431   Stage::GetCurrent().Add(control2);
432
433   detector.Attach(control1);
434   detector.Attach(control2);
435
436   application.SendNotification();
437   application.Render(RENDER_FRAME_INTERVAL);
438
439   SignalData data;
440   DragSignalFunctor functor(data);
441   detector.ExitedSignal().Connect(&application, functor);
442
443   application.ProcessEvent(GeneratePan(Gesture::Possible, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
444   application.ProcessEvent(GeneratePan(Gesture::Started, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
445
446   Vector2 screenCoordinates(10.0f, 110.0f);
447   application.ProcessEvent(GenerateSingleTouch(TouchPoint::Motion, screenCoordinates));
448
449   screenCoordinates.x = 20.0f;
450   screenCoordinates.y = 20.0f;
451   application.ProcessEvent(GenerateSingleTouch(TouchPoint::Motion, screenCoordinates));
452   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
453   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
454
455   data.Reset();
456
457   END_TEST;
458 }
459
460 int UtcDaliDragAndDropDetectorDroppedSignal(void)
461 {
462   TestApplication application;
463
464   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
465   Control control1 = Control::New();
466   Control control2 = Control::New();
467   control1.SetSize(100.0f,100.0f);
468   control2.SetSize(100.0f, 100.0f);
469   control1.SetAnchorPoint(AnchorPoint::TOP_LEFT);
470   control2.SetAnchorPoint(AnchorPoint::TOP_LEFT);
471   control1.SetParentOrigin(ParentOrigin::TOP_LEFT);
472   control2.SetParentOrigin(ParentOrigin::TOP_LEFT);
473   control1.SetPosition(0.0f, 0.0f);
474   control2.SetPosition(0.0f, 100.0f);
475
476   Stage::GetCurrent().Add(control1);
477   Stage::GetCurrent().Add(control2);
478
479   detector.Attach(control1);
480   detector.Attach(control2);
481
482   application.SendNotification();
483   application.Render(RENDER_FRAME_INTERVAL);
484
485   SignalData data;
486   DragSignalFunctor functor(data);
487   detector.DroppedSignal().Connect(&application, functor);
488
489   application.ProcessEvent(GeneratePan(Gesture::Possible, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
490   application.ProcessEvent(GeneratePan(Gesture::Started, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
491
492   Vector2 screenCoordinates(10.0f, 110.0f);
493   application.ProcessEvent(GenerateSingleTouch(TouchPoint::Motion, screenCoordinates));
494
495   screenCoordinates.x = 10.0f;
496   screenCoordinates.y = 112.0f;
497   application.ProcessEvent(GenerateSingleTouch(TouchPoint::Up, screenCoordinates));
498
499   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
500   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
501   DALI_TEST_EQUALS(Vector2(10.0f, 112.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
502   DALI_TEST_EQUALS(true, detector.GetContent().empty(), TEST_LOCATION);
503
504   data.Reset();
505
506   END_TEST;
507 }
508
509 int UtcDaliDragAndDropDetectorEndedSignal(void)
510 {
511   TestApplication application;
512
513   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
514   Control control1 = Control::New();
515   Control control2 = Control::New();
516   control1.SetSize(100.0f,100.0f);
517   control2.SetSize(100.0f, 100.0f);
518   control1.SetAnchorPoint(AnchorPoint::TOP_LEFT);
519   control2.SetAnchorPoint(AnchorPoint::TOP_LEFT);
520   control1.SetParentOrigin(ParentOrigin::TOP_LEFT);
521   control2.SetParentOrigin(ParentOrigin::TOP_LEFT);
522   control1.SetPosition(0.0f, 0.0f);
523   control2.SetPosition(0.0f, 100.0f);
524
525   Stage::GetCurrent().Add(control1);
526   Stage::GetCurrent().Add(control2);
527
528   application.SendNotification();
529   application.Render(RENDER_FRAME_INTERVAL);
530
531   detector.Attach(control1);
532   detector.Attach(control2);
533
534   SignalData data;
535   DragSignalFunctor functor(data);
536   detector.EndedSignal().Connect(&application, functor);
537
538   application.ProcessEvent(GeneratePan(Gesture::Possible, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
539   application.ProcessEvent(GeneratePan(Gesture::Started, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
540
541   application.ProcessEvent(GenerateSingleTouch(TouchPoint::Down, Vector2(10.0f, 10.0f)));
542
543   application.ProcessEvent(GeneratePan(Gesture::Continuing, Vector2(10.0f, 10.0f), Vector2(120.0f, 12.0f), 10));
544   application.ProcessEvent(GeneratePan(Gesture::Finished, Vector2(120.0f, 12.0f), Vector2(120.0f, 20.0f), 10));
545
546   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
547   DALI_TEST_EQUALS(control1, data.control, TEST_LOCATION);
548   data.Reset();
549
550   END_TEST;
551 }
552
553 int UtcDaliDragAndDropDetectorGetContent(void)
554 {
555   TestApplication application;
556
557   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
558   Control control1 = Control::New();
559   Control control2 = Control::New();
560   control1.SetName("control1");
561   control2.SetName("control2");
562   control1.SetSize(100.0f,100.0f);
563   control2.SetSize(100.0f, 100.0f);
564   control1.SetAnchorPoint(AnchorPoint::TOP_LEFT);
565   control2.SetAnchorPoint(AnchorPoint::TOP_LEFT);
566   control1.SetParentOrigin(ParentOrigin::TOP_LEFT);
567   control2.SetParentOrigin(ParentOrigin::TOP_LEFT);
568   control1.SetPosition(0.0f, 0.0f);
569   control2.SetPosition(0.0f, 100.0f);
570
571   Stage::GetCurrent().Add(control1);
572   Stage::GetCurrent().Add(control2);
573
574   detector.Attach(control1);
575   detector.Attach(control2);
576
577   application.SendNotification();
578   application.Render(RENDER_FRAME_INTERVAL);
579
580   SignalData data;
581   DragSignalFunctor functor(data);
582   detector.DroppedSignal().Connect(&application, functor);
583
584   application.ProcessEvent(GeneratePan(Gesture::Possible, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
585   application.ProcessEvent(GeneratePan(Gesture::Started, Vector2(10.0f, 10.0f), Vector2(12.0f, 12.0f), 10));
586
587   Vector2 screenCoordinates(10.0f, 110.0f);
588   application.ProcessEvent(GenerateSingleTouch(TouchPoint::Motion, screenCoordinates));
589
590   screenCoordinates.x = 10.0f;
591   screenCoordinates.y = 112.0f;
592   application.ProcessEvent(GenerateSingleTouch(TouchPoint::Up, screenCoordinates));
593
594   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
595   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
596   DALI_TEST_EQUALS(Vector2(10.0f, 112.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
597   DALI_TEST_EQUALS("control1", detector.GetContent(), TEST_LOCATION);
598
599   data.Reset();
600
601   END_TEST;
602 }