Moved RotationGesture to the Public API
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-RotationGestureRecognizer.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
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali/integration-api/input-options.h>
23 #include <dali/integration-api/events/touch-event-integ.h>
24 #include <dali-test-suite-utils.h>
25
26
27 using namespace Dali;
28
29 void utc_dali_rotation_gesture_recognizer_startup(void)
30 {
31   test_return_value = TET_UNDEF;
32 }
33
34 void utc_dali_rotation_gesture_recognizer_cleanup(void)
35 {
36   test_return_value = TET_PASS;
37 }
38
39 ///////////////////////////////////////////////////////////////////////////////
40 namespace
41 {
42
43 struct SignalData
44 {
45   SignalData()
46   : functorCalled(false),
47     voidFunctorCalled(false),
48     receivedGesture(Gesture::Started)
49   {}
50
51   void Reset()
52   {
53     functorCalled = false;
54     voidFunctorCalled = false;
55
56     receivedGesture.state = Gesture::Started;
57     receivedGesture.rotation = 0.0f;
58     receivedGesture.screenCenterPoint = Vector2(0.0f, 0.0f);
59     receivedGesture.localCenterPoint = Vector2(0.0f, 0.0f);
60
61     rotatedActor.Reset();
62   }
63
64   bool functorCalled;
65   bool voidFunctorCalled;
66   RotationGesture receivedGesture;
67   Actor rotatedActor;
68 };
69
70 // Functor that sets the data when called
71 struct GestureReceivedFunctor
72 {
73   GestureReceivedFunctor(SignalData& data) : signalData(data) { }
74
75   void operator()(Actor actor, const RotationGesture& rotation)
76   {
77     signalData.functorCalled = true;
78     signalData.receivedGesture = rotation;
79     signalData.rotatedActor = actor;
80   }
81
82   void operator()()
83   {
84     signalData.voidFunctorCalled = true;
85   }
86
87   SignalData& signalData;
88 };
89
90 Integration::TouchEvent GenerateDoubleTouch( PointState::Type stateA, const Vector2& screenPositionA, PointState::Type stateB, const Vector2& screenPositionB, uint32_t time )
91 {
92   Integration::TouchEvent touchEvent;
93   Integration::Point point;
94   point.SetState( stateA );
95   point.SetScreenPosition( screenPositionA );
96   point.SetDeviceClass( Device::Class::TOUCH );
97   point.SetDeviceSubclass( Device::Subclass::NONE );
98   touchEvent.points.push_back( point );
99   point.SetScreenPosition( screenPositionB );
100   point.SetState( stateB);
101   touchEvent.points.push_back( point );
102   touchEvent.time = time;
103   return touchEvent;
104 }
105
106
107 } // anon namespace
108
109 ///////////////////////////////////////////////////////////////////////////////
110
111 int UtcDaliRotationGestureRecognizerMinimumTouchEvents(void)
112 {
113   TestApplication application;
114
115   Actor actor = Actor::New();
116   actor.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
117   actor.SetProperty( Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT );
118   application.GetScene().Add( actor );
119
120   application.SendNotification();
121   application.Render();
122
123   SignalData data;
124   GestureReceivedFunctor functor( data );
125
126   RotationGestureDetector detector = RotationGestureDetector::New();
127   detector.Attach( actor );
128   detector.DetectedSignal().Connect( &application, functor );
129
130   // Case 1
131   // 2 touch events make a gesture begin
132   Integration::SetRotationGestureMinimumTouchEvents( 2 );
133   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), PointState::DOWN, Vector2( 20.0f, 90.0f ), 150 ) );
134   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 90.0f, 90.0f ), 160 ) );
135
136   DALI_TEST_EQUALS( Gesture::Started, data.receivedGesture.state, TEST_LOCATION );
137   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
138   data.Reset();
139
140   // Case 2
141   // 4 touch events make a gesture begin
142   Integration::SetRotationGestureMinimumTouchEvents( 4 );
143   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), PointState::DOWN, Vector2( 20.0f, 90.0f ), 150 ) );
144   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 90.0f, 90.0f ), 160 ) );
145
146   // Check the gesture is not detected unlike previous case
147   DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
148
149   END_TEST;
150 }
151
152 int UtcDaliRotationGestureRecognizerMinimumTouchEventsAfterStart(void)
153 {
154   TestApplication application;
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   application.GetScene().Add( actor );
160
161   application.SendNotification();
162   application.Render();
163
164   SignalData data;
165   GestureReceivedFunctor functor( data );
166
167   RotationGestureDetector detector = RotationGestureDetector::New();
168   detector.Attach( actor );
169   detector.DetectedSignal().Connect( &application, functor );
170
171   // Case 1
172   // > 2 touch events make a gesture begin
173   // > 4 touch events generate gestures after begin
174   Integration::SetRotationGestureMinimumTouchEvents(2);
175   Integration::SetRotationGestureMinimumTouchEventsAfterStart(6);
176
177   application.ProcessEvent( GenerateDoubleTouch( PointState::DOWN, Vector2( 20.0f, 20.0f ), PointState::DOWN, Vector2( 20.0f, 90.0f ), 150 ) );
178   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 90.0f, 90.0f ), 160 ) );
179
180   DALI_TEST_EQUALS(Gesture::Started, data.receivedGesture.state, TEST_LOCATION);
181   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
182
183   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 20.0f, 90.0f ), 170 ) );
184   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 20.0f, 90.0f ), 180 ) );
185   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 20.0f, 90.0f ), 190 ) );
186   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 20.0f, 90.0f ), 200 ) );
187   // > Test : not enough touch events to make the gesture state "Continuing"
188   DALI_TEST_EQUALS(Gesture::Started, data.receivedGesture.state, TEST_LOCATION);
189
190   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 20.0f, 90.0f ), 210 ) );
191   application.ProcessEvent( GenerateDoubleTouch( PointState::MOTION, Vector2( 20.0f, 20.0f ), PointState::MOTION, Vector2( 20.0f, 90.0f ), 220 ) );
192   // > Test : 6 touch events after start make the gesture state "Continuing"
193   DALI_TEST_EQUALS(Gesture::Continuing, data.receivedGesture.state, TEST_LOCATION);
194
195   END_TEST;
196 }