Protecting test cases from memory scribbling
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-DragAndDropDetector.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 #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
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 }
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
167 int UtcDaliDragAndDropDetectorDetachP(void)
168 {
169   ToolkitTestApplication application;
170
171   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
172   Control control1 = Control::New();
173   Control control2 = Control::New();
174   Control control3;
175
176   detector.Attach(control1);
177   detector.Attach(control2);
178   detector.Attach(control3);
179
180   DALI_TEST_EQUALS(2, detector.GetAttachedControlCount(), TEST_LOCATION);
181
182   detector.Detach(control3);
183   DALI_TEST_EQUALS(2, detector.GetAttachedControlCount(), TEST_LOCATION);
184   detector.Detach(control2);
185   DALI_TEST_EQUALS(1, detector.GetAttachedControlCount(), TEST_LOCATION);
186
187   detector.Detach(control1);
188   DALI_TEST_EQUALS(0, detector.GetAttachedControlCount(), TEST_LOCATION);
189
190   END_TEST;
191 }
192
193 int UtcDaliDragAndDropDetectorDetachAllN(void)
194 {
195   ToolkitTestApplication application;
196
197   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
198   detector.DetachAll();
199   DALI_TEST_EQUALS(0, detector.GetAttachedControlCount(), TEST_LOCATION);
200
201   END_TEST;
202 }
203 int UtcDaliDragAndDropDetectorDetachAllP(void)
204 {
205   ToolkitTestApplication application;
206
207   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
208   Control control1 = Control::New();
209   Control control2 = Control::New();
210
211   detector.Attach(control1);
212   detector.Attach(control2);
213   detector.DetachAll();
214   DALI_TEST_EQUALS(0, detector.GetAttachedControlCount(), TEST_LOCATION);
215
216   END_TEST;
217 }
218
219 int UtcDaliDragAndDropDetectorGetAttachedControlCountP(void)
220 {
221   ToolkitTestApplication application;
222
223   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
224   Control control1 = Control::New();
225   Control control2 = Control::New();
226
227   detector.Attach(control1);
228   detector.Attach(control2);
229   DALI_TEST_EQUALS(2, detector.GetAttachedControlCount(), TEST_LOCATION);
230
231   END_TEST;
232 }
233
234 int UtcDaliDragAndDropDetectorGetAttachedControlN(void)
235 {
236   ToolkitTestApplication application;
237
238   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
239   Control control1 = Control::New();
240   Control control2 = Control::New();
241
242   detector.Attach(control1);
243
244   Control control = detector.GetAttachedControl(1);
245   DALI_TEST_CHECK(!control);
246
247   END_TEST;
248 }
249
250 int UtcDaliDragAndDropDetectorGetAttachedControlP(void)
251 {
252   ToolkitTestApplication application;
253
254   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
255   Control control1 = Control::New();
256   Control control2 = Control::New();
257
258   detector.Attach(control1);
259   detector.Attach(control2);
260   Control control = detector.GetAttachedControl(1);
261   DALI_TEST_CHECK(control);
262   DALI_TEST_EQUALS(control2, control, TEST_LOCATION);
263
264   END_TEST;
265 }
266
267 int UtcDaliDragAndDropDetectorStartSignal(void)
268 {
269   ToolkitTestApplication application;
270
271   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
272   Control control = Control::New();
273   control.SetProperty( Actor::Property::SIZE, Vector2(100.0f, 100.0f) );
274   control.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
275   application.GetScene().Add(control);
276   detector.Attach(control);
277
278   application.SendNotification();
279   application.Render(RENDER_FRAME_INTERVAL);
280
281   DALI_TEST_CHECK(detector);
282   DALI_TEST_CHECK(control);
283
284   SignalData data;
285   DragSignalFunctor functor(data);
286   detector.StartedSignal().Connect(&application, functor);
287
288   TestGenerateMiniPan(application);
289
290   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
291   DALI_TEST_EQUALS(control, data.control, TEST_LOCATION);
292   DALI_TEST_EQUALS(Vector2(20.0f, 40.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
293   data.Reset();
294
295   END_TEST;
296 }
297
298 int UtcDaliDragAndDropDetectorEnteredSignal(void)
299 {
300   ToolkitTestApplication application;
301
302   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
303   Control control1 = Control::New();
304   Control control2 = Control::New();
305   control1.SetProperty( Actor::Property::SIZE, Vector2(100.0f,100.0f) );
306   control2.SetProperty( Actor::Property::SIZE, Vector2(100.0f, 100.0f) );
307   control1.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
308   control2.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
309   control1.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
310   control2.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
311   control1.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 0.0f));
312   control2.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 100.0f));
313
314   application.GetScene().Add(control1);
315   application.GetScene().Add(control2);
316
317   detector.Attach(control1);
318   detector.Attach(control2);
319
320   application.SendNotification();
321   application.Render(RENDER_FRAME_INTERVAL);
322
323   SignalData data;
324   DragSignalFunctor functor(data);
325   detector.EnteredSignal().Connect(&application, functor);
326
327   TestGenerateMiniPan(application);
328
329   Vector2 screenCoordinates(10.0f, 110.0f);
330   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
331
332   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
333   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
334
335   data.Reset();
336
337   END_TEST;
338
339 }
340
341 int UtcDaliDragAndDropDetectorMovedSignal(void)
342 {
343   ToolkitTestApplication application;
344
345   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
346   Control control1 = Control::New();
347   Control control2 = Control::New();
348   control1.SetProperty( Actor::Property::SIZE, Vector2(100.0f,100.0f) );
349   control2.SetProperty( Actor::Property::SIZE, Vector2(100.0f, 100.0f) );
350   control1.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
351   control2.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
352   control1.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
353   control2.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
354   control1.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 0.0f));
355   control2.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 100.0f));
356
357   application.GetScene().Add(control1);
358   application.GetScene().Add(control2);
359
360   detector.Attach(control1);
361   detector.Attach(control2);
362
363   application.SendNotification();
364   application.Render(RENDER_FRAME_INTERVAL);
365
366   SignalData data;
367   DragSignalFunctor functor(data);
368   detector.MovedSignal().Connect(&application, functor);
369
370   TestGenerateMiniPan(application);
371
372   Vector2 screenCoordinates(10.0f, 110.0f);
373   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
374
375   screenCoordinates.x = 10.0f;
376   screenCoordinates.y = 120.0f;
377   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
378
379   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
380   DALI_TEST_EQUALS(Vector2(10.0f, 120.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
381   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
382
383   data.Reset();
384
385   END_TEST;
386 }
387
388 int UtcDaliDragAndDropDetectorExitedSignal(void)
389 {
390   ToolkitTestApplication application;
391
392   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
393   Control control1 = Control::New();
394   Control control2 = Control::New();
395   control1.SetProperty( Actor::Property::SIZE, Vector2(100.0f,100.0f) );
396   control2.SetProperty( Actor::Property::SIZE, Vector2(100.0f, 100.0f) );
397   control1.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
398   control2.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
399   control1.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
400   control2.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
401   control1.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 0.0f));
402   control2.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 100.0f));
403
404   control1.SetProperty( Actor::Property::LEAVE_REQUIRED,true);
405   control2.SetProperty( Actor::Property::LEAVE_REQUIRED,true);
406
407   application.GetScene().Add(control1);
408   application.GetScene().Add(control2);
409
410   detector.Attach(control1);
411   detector.Attach(control2);
412
413   application.SendNotification();
414   application.Render(RENDER_FRAME_INTERVAL);
415
416   SignalData data;
417   DragSignalFunctor functor(data);
418   detector.ExitedSignal().Connect(&application, functor);
419
420   TestGenerateMiniPan(application);
421
422   Vector2 screenCoordinates(10.0f, 110.0f);
423   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
424
425   screenCoordinates.x = 20.0f;
426   screenCoordinates.y = 20.0f;
427   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
428   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
429   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
430
431   data.Reset();
432
433   END_TEST;
434 }
435
436 int UtcDaliDragAndDropDetectorDroppedSignal(void)
437 {
438   ToolkitTestApplication application;
439
440   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
441   Control control1 = Control::New();
442   Control control2 = Control::New();
443   control1.SetProperty( Actor::Property::SIZE, Vector2(100.0f,100.0f) );
444   control2.SetProperty( Actor::Property::SIZE, Vector2(100.0f, 100.0f) );
445   control1.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
446   control2.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
447   control1.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
448   control2.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
449   control1.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 0.0f));
450   control2.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 100.0f));
451
452   application.GetScene().Add(control1);
453   application.GetScene().Add(control2);
454
455   detector.Attach(control1);
456   detector.Attach(control2);
457
458   application.SendNotification();
459   application.Render(RENDER_FRAME_INTERVAL);
460
461   SignalData data;
462   DragSignalFunctor functor(data);
463   detector.DroppedSignal().Connect(&application, functor);
464
465   TestGenerateMiniPan(application);
466
467   Vector2 screenCoordinates(10.0f, 110.0f);
468   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
469
470   screenCoordinates.x = 10.0f;
471   screenCoordinates.y = 112.0f;
472   application.ProcessEvent(GenerateSingleTouch(PointState::UP, screenCoordinates));
473
474   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
475   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
476   DALI_TEST_EQUALS(Vector2(10.0f, 112.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
477   DALI_TEST_EQUALS(true, detector.GetContent().empty(), TEST_LOCATION);
478
479   data.Reset();
480
481   END_TEST;
482 }
483
484 int UtcDaliDragAndDropDetectorEndedSignal(void)
485 {
486   ToolkitTestApplication application;
487
488   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
489   Control control1 = Control::New();
490   Control control2 = Control::New();
491   control1.SetProperty( Actor::Property::SIZE, Vector2(100.0f,100.0f) );
492   control2.SetProperty( Actor::Property::SIZE, Vector2(100.0f, 100.0f) );
493   control1.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
494   control2.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
495   control1.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
496   control2.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
497   control1.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 0.0f));
498   control2.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 100.0f));
499
500   application.GetScene().Add(control1);
501   application.GetScene().Add(control2);
502
503   application.SendNotification();
504   application.Render(RENDER_FRAME_INTERVAL);
505
506   detector.Attach(control1);
507   detector.Attach(control2);
508
509   SignalData data;
510   DragSignalFunctor functor(data);
511   detector.EndedSignal().Connect(&application, functor);
512
513   TestGenerateMiniPan(application);
514
515   application.ProcessEvent(GenerateSingleTouch(PointState::DOWN, Vector2(10.0f, 10.0f)));
516
517   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
518   DALI_TEST_EQUALS(control1, data.control, TEST_LOCATION);
519   data.Reset();
520
521   END_TEST;
522 }
523
524 int UtcDaliDragAndDropDetectorGetContent(void)
525 {
526   ToolkitTestApplication application;
527
528   Dali::Toolkit::DragAndDropDetector detector = Dali::Toolkit::DragAndDropDetector::New();
529   Control control1 = Control::New();
530   Control control2 = Control::New();
531   control1.SetProperty( Dali::Actor::Property::NAME,"control1");
532   control2.SetProperty( Dali::Actor::Property::NAME,"control2");
533   control1.SetProperty( Actor::Property::SIZE, Vector2(100.0f, 100.0f) );
534   control2.SetProperty( Actor::Property::SIZE, Vector2(100.0f, 100.0f) );
535   control1.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
536   control2.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
537   control1.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
538   control2.SetProperty( Actor::Property::PARENT_ORIGIN,ParentOrigin::TOP_LEFT);
539   control1.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 0.0f));
540   control2.SetProperty( Actor::Property::POSITION, Vector2(0.0f, 100.0f));
541
542   application.GetScene().Add(control1);
543   application.GetScene().Add(control2);
544
545   detector.Attach(control1);
546   detector.Attach(control2);
547
548   application.SendNotification();
549   application.Render(RENDER_FRAME_INTERVAL);
550
551   SignalData data;
552   DragSignalFunctor functor(data);
553   detector.DroppedSignal().Connect(&application, functor);
554
555   TestGenerateMiniPan(application);
556
557   Vector2 screenCoordinates(10.0f, 110.0f);
558   application.ProcessEvent(GenerateSingleTouch(PointState::MOTION, screenCoordinates));
559
560   screenCoordinates.x = 10.0f;
561   screenCoordinates.y = 112.0f;
562   application.ProcessEvent(GenerateSingleTouch(PointState::UP, screenCoordinates));
563
564   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
565   DALI_TEST_EQUALS(control2, data.control, TEST_LOCATION);
566   DALI_TEST_EQUALS(Vector2(10.0f, 112.0f), data.detector.GetCurrentScreenPosition(), TEST_LOCATION);
567   DALI_TEST_EQUALS("control1", detector.GetContent(), TEST_LOCATION);
568
569   data.Reset();
570
571   END_TEST;
572 }