Further Setter/Getter public API removal from Dali::Actor
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-LongPressGestureRecognizer.cpp
1 /*
2  * Copyright (c) 2019 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 <thread>
20 #include <chrono>
21
22 #include <stdlib.h>
23 #include <dali/public-api/dali-core.h>
24 #include <dali/integration-api/events/touch-event-integ.h>
25 #include <dali/integration-api/render-task-list-integ.h>
26 #include <dali-test-suite-utils.h>
27 #include <test-touch-utils.h>
28
29 using namespace Dali;
30
31 ///////////////////////////////////////////////////////////////////////////////
32 namespace
33 {
34
35 struct SignalData
36 {
37   SignalData()
38   : functorCalled(false),
39     voidFunctorCalled(false),
40     receivedGesture(Gesture::Started)
41   {}
42
43   void Reset()
44   {
45     functorCalled = false;
46     voidFunctorCalled = false;
47
48     receivedGesture.state = Gesture::Started;
49
50     pressedActor.Reset();
51   }
52
53   bool functorCalled;
54   bool voidFunctorCalled;
55   LongPressGesture receivedGesture;
56   Actor pressedActor;
57 };
58
59 // Functor that sets the data when called
60 struct GestureReceivedFunctor
61 {
62   GestureReceivedFunctor(SignalData& data) : signalData(data) { }
63
64   void operator()(Actor actor, const LongPressGesture& pan)
65   {
66     signalData.functorCalled = true;
67     signalData.receivedGesture = pan;
68     signalData.pressedActor = actor;
69   }
70
71   void operator()()
72   {
73     signalData.voidFunctorCalled = true;
74   }
75
76   SignalData& signalData;
77 };
78
79
80 Integration::TouchEvent GenerateSingleTouch( PointState::Type state, const Vector2& screenPosition, uint32_t time )
81 {
82   Integration::TouchEvent touchEvent;
83   Integration::Point point;
84   point.SetState( state );
85   point.SetDeviceId(4);
86   point.SetScreenPosition( screenPosition );
87   point.SetDeviceClass( Device::Class::TOUCH );
88   point.SetDeviceSubclass( Device::Subclass::NONE );
89   touchEvent.points.push_back( point );
90   touchEvent.time = time;
91   return touchEvent;
92 }
93
94 Integration::TouchEvent GenerateDoubleTouch( PointState::Type stateA, const Vector2& screenPositionA, PointState::Type stateB, const Vector2& screenPositionB, uint32_t time )
95 {
96   Integration::TouchEvent touchEvent;
97   Integration::Point point;
98   point.SetState( stateA );
99   point.SetDeviceId(4);
100   point.SetScreenPosition( screenPositionA );
101   point.SetDeviceClass( Device::Class::TOUCH );
102   point.SetDeviceSubclass( Device::Subclass::NONE );
103   touchEvent.points.push_back( point );
104   point.SetScreenPosition( screenPositionB );
105   point.SetState( stateB);
106   point.SetDeviceId(7);
107   touchEvent.points.push_back( point );
108   touchEvent.time = time;
109   return touchEvent;
110 }
111
112
113 } // anon namespace
114
115 ///////////////////////////////////////////////////////////////////////////////
116
117
118 int UtcDaliLongPressGestureRecognizerBasicNoAction(void)
119 {
120   TestApplication application;
121
122   LongPressGestureDetector detector = LongPressGestureDetector::New();
123
124   Actor actor = Actor::New();
125   actor.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
126   actor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
127   Stage::GetCurrent().Add(actor);
128
129   // Render and notify
130   application.SendNotification();
131   application.Render();
132
133   detector.Attach(actor);
134
135   SignalData data;
136   GestureReceivedFunctor functor(data);
137   detector.DetectedSignal().Connect(&application, functor);
138
139   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
140
141   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 200 ) );
142
143   application.SendNotification();
144
145   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
146
147   END_TEST;
148 }
149
150 int UtcDaliLongPressGestureRecognizerBasic(void)
151 {
152   TestApplication application;
153
154   LongPressGestureDetector detector = LongPressGestureDetector::New();
155
156   Actor actor = Actor::New();
157   actor.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
158   actor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
159   Stage::GetCurrent().Add(actor);
160
161   // Render and notify
162   application.SendNotification();
163   application.Render();
164
165   detector.Attach(actor);
166
167   SignalData data;
168   GestureReceivedFunctor functor(data);
169   detector.DetectedSignal().Connect(&application, functor);
170
171   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
172
173   application.GetPlatform().TriggerTimer();
174
175   application.SendNotification();
176
177   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
178
179   END_TEST;
180 }
181
182 int UtcDaliLongPressGestureRecognizerTooShortWait(void)
183 {
184   TestApplication application;
185
186   LongPressGestureDetector detector = LongPressGestureDetector::New();
187
188   Actor actor = Actor::New();
189   actor.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
190   actor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
191   Stage::GetCurrent().Add(actor);
192
193   // Render and notify
194   application.SendNotification();
195   application.Render();
196
197   detector.Attach(actor);
198
199   SignalData data;
200   GestureReceivedFunctor functor(data);
201   detector.DetectedSignal().Connect(&application, functor);
202
203   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
204
205   application.SendNotification();
206
207   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
208
209   END_TEST;
210 }
211
212 int UtcDaliLongPressGestureRecognizerTooFewPoints(void)
213 {
214   TestApplication application;
215
216   LongPressGestureDetector detector = LongPressGestureDetector::New();
217
218   detector.SetTouchesRequired(2,2);
219
220   Actor actor = Actor::New();
221   actor.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
222   actor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
223   Stage::GetCurrent().Add(actor);
224
225   // Render and notify
226   application.SendNotification();
227   application.Render();
228
229   detector.Attach(actor);
230
231   SignalData data;
232   GestureReceivedFunctor functor(data);
233   detector.DetectedSignal().Connect(&application, functor);
234
235   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
236
237   // There should be no function to call
238   application.GetPlatform().TriggerTimer();
239
240   application.SendNotification();
241
242   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
243
244   END_TEST;
245 }
246
247 int UtcDaliLongPressGestureRecognizerTooManyPoints(void)
248 {
249   TestApplication application;
250
251   LongPressGestureDetector detector = LongPressGestureDetector::New();
252
253   Actor actor = Actor::New();
254   actor.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
255   actor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
256   Stage::GetCurrent().Add(actor);
257
258   // Render and notify
259   application.SendNotification();
260   application.Render();
261
262   detector.Attach(actor);
263
264   SignalData data;
265   GestureReceivedFunctor functor(data);
266   detector.DetectedSignal().Connect(&application, functor);
267
268   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
269   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), PointState::DOWN, Vector2( 20.0f, 90.0f ), 151 ) );
270
271   // There should be no function to call as the double touch should have cancelled it
272   application.GetPlatform().TriggerTimer();
273
274   application.SendNotification();
275
276   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
277
278   END_TEST;
279 }
280
281 int UtcDaliLongPressGestureRecognizerMultiplePointsMoving(void)
282 {
283   TestApplication application;
284
285   LongPressGestureDetector detector = LongPressGestureDetector::New();
286
287   detector.SetTouchesRequired(2,2);
288
289   Actor actor = Actor::New();
290   actor.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
291   actor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
292   Stage::GetCurrent().Add(actor);
293
294   // Render and notify
295   application.SendNotification();
296   application.Render();
297
298   detector.Attach(actor);
299
300   SignalData data;
301   GestureReceivedFunctor functor(data);
302   detector.DetectedSignal().Connect(&application, functor);
303
304   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 0.0f ), PointState::DOWN, Vector2( 20.0f, 90.0f ), 151 ) );
305   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 10.0f ), PointState::MOTION, Vector2( 20.0f, 80.0f ), 153 ) );
306   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 20.0f, 70.0f ), 155 ) );
307   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 30.0f ), PointState::MOTION, Vector2( 20.0f, 60.0f ), 157 ) );
308   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 40.0f ), PointState::MOTION, Vector2( 20.0f, 50.0f ), 159 ) );
309   application.ProcessEvent( GenerateDoubleTouch( PointState::STATIONARY, Vector2( 20.0f, 40.0f ), PointState::UP, Vector2( 20.0f, 50.0f ), 160 ) );
310   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 40.0f ), 161 ) );
311
312   application.GetPlatform().TriggerTimer();
313
314   application.SendNotification();
315
316   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
317
318   END_TEST;
319 }
320
321 int UtcDaliLongPressGestureRecognizerMultiplePointsLongPress(void)
322 {
323   TestApplication application;
324
325   LongPressGestureDetector detector = LongPressGestureDetector::New();
326
327   detector.SetTouchesRequired(2,2);
328
329   Actor actor = Actor::New();
330   actor.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
331   actor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
332   Stage::GetCurrent().Add(actor);
333
334   // Render and notify
335   application.SendNotification();
336   application.Render();
337
338   detector.Attach(actor);
339
340   SignalData data;
341   GestureReceivedFunctor functor(data);
342   detector.DetectedSignal().Connect(&application, functor);
343
344   application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 40.0f ), 140 ) );
345   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 40.0f ), PointState::DOWN, Vector2( 20.0f, 90.0f ), 150 ) );
346
347   application.GetPlatform().TriggerTimer();
348
349   application.ProcessEvent( GenerateDoubleTouch( PointState::STATIONARY, Vector2( 20.0f, 20.0f ), PointState::UP, Vector2( 20.0f, 90.0f ), 760 ) );
350   application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 761 ) );
351
352   application.SendNotification();
353
354   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
355
356   END_TEST;
357 }
358
359 int UtcDaliLongPressGestureRecognizerMultipleDetectors(void)
360 {
361   TestApplication application;
362
363     Actor actor = Actor::New();
364     actor.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
365     actor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
366     Stage::GetCurrent().Add(actor);
367
368     Actor actor2 = Actor::New();
369     actor2.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
370     actor2.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
371     actor2.SetProperty( Actor::Property::POSITION_X, 100.0f);
372     Stage::GetCurrent().Add(actor2);
373
374     // Render and notify
375     application.SendNotification();
376     application.Render();
377
378     LongPressGestureDetector detector = LongPressGestureDetector::New();
379     detector.Attach(actor);
380
381     LongPressGestureDetector detector2 = LongPressGestureDetector::New(2);
382     detector2.Attach(actor2);
383
384     SignalData data;
385     GestureReceivedFunctor functor(data);
386     detector.DetectedSignal().Connect(&application, functor);
387
388     SignalData data2;
389     GestureReceivedFunctor functor2(data2);
390     detector2.DetectedSignal().Connect(&application, functor2);
391
392     application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), 150 ) );
393
394     application.GetPlatform().TriggerTimer();
395
396     application.ProcessEvent( GenerateSingleTouch( PointState::UP, Vector2( 20.0f, 20.0f ), 700 ) );
397
398     application.SendNotification();
399
400     DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
401     DALI_TEST_EQUALS(true, actor == data.pressedActor, TEST_LOCATION);
402     data.Reset();
403     DALI_TEST_EQUALS(false, data2.functorCalled, TEST_LOCATION);
404
405     application.ProcessEvent( GenerateSingleTouch( PointState::DOWN, Vector2( 120.0f, 40.0f ), 800 ) );
406     application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 120.0f, 40.0f ), PointState::DOWN, Vector2( 120.0f, 90.0f ), 805 ) );
407
408     application.GetPlatform().TriggerTimer();
409
410     application.SendNotification();
411
412     DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
413     DALI_TEST_EQUALS(true, data2.functorCalled, TEST_LOCATION);
414
415     END_TEST;
416 }