Revert "Revert "WheelEvent class pimpling""
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-WheelEvent.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 #include <dali/public-api/dali-core.h>
21 #include <dali/integration-api/events/wheel-event-integ.h>
22 #include <dali/devel-api/events/wheel-event-devel.h>
23 #include <dali-test-suite-utils.h>
24
25 using namespace Dali;
26
27 namespace
28 {
29
30 // Key Event Test references
31 const static unsigned int SHIFT_MODIFIER  = 0x1;
32 const static unsigned int CTRL_MODIFIER  = 0x2;
33 const static unsigned int ALT_MODIFIER  = 0x4;
34 const static unsigned int SHIFT_AND_CTRL_MODIFIER  = SHIFT_MODIFIER | CTRL_MODIFIER;
35
36
37 // Stores data that is populated in the callback and will be read by the TET cases
38 struct SignalData
39 {
40   SignalData()
41   : functorCalled(false)
42   {}
43
44   void Reset()
45   {
46     functorCalled = false;
47
48     receivedWheelEvent.Reset();
49     wheeledActor.Reset();
50   }
51
52   bool functorCalled;
53   WheelEvent receivedWheelEvent;
54   Actor wheeledActor;
55 };
56
57 // Functor that sets the data when called
58 struct WheelEventReceivedFunctor
59 {
60   WheelEventReceivedFunctor( SignalData& data ) : signalData( data ) { }
61
62   bool operator()( Actor actor, const WheelEvent& wheelEvent )
63   {
64     signalData.functorCalled = true;
65     signalData.receivedWheelEvent = wheelEvent;
66     signalData.wheeledActor = actor;
67
68     return true;
69   }
70
71   SignalData& signalData;
72 };
73
74 } // anonymous namespace
75
76 int UtcDaliWheelEventConstructor(void)
77 {
78   TestApplication application; // Reset all test adapter return codes
79
80   Dali::WheelEvent event = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
81   DALI_TEST_CHECK( event );
82
83   DALI_TEST_EQUALS(Dali::WheelEvent::MOUSE_WHEEL, event.GetType(), TEST_LOCATION); // check type
84   DALI_TEST_EQUALS(1, event.GetDirection(), TEST_LOCATION); // check direction
85   DALI_TEST_EQUALS(SHIFT_MODIFIER, event.GetModifiers(), TEST_LOCATION); // check modifier
86   DALI_TEST_EQUALS(Vector2(1.0f, 1.0f), event.GetPoint(), TEST_LOCATION); // check modifier
87   DALI_TEST_EQUALS(1, event.GetDelta(), TEST_LOCATION); // check modifier
88   DALI_TEST_EQUALS(1000u, event.GetTime(), TEST_LOCATION); // check modifier
89   END_TEST;
90 }
91
92 // Positive test case for a method
93 int UtcDaliWheelEventIsShiftModifier(void)
94 {
95   TestApplication application; // Reset all test adapter return codes
96
97   Dali::WheelEvent event = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
98   DALI_TEST_CHECK( event );
99
100   DALI_TEST_EQUALS(SHIFT_MODIFIER, event.GetModifiers(), TEST_LOCATION);
101
102   DALI_TEST_EQUALS(true, event.IsShiftModifier(), TEST_LOCATION); // check IsShiftModifier
103
104   END_TEST;
105 }
106
107 // Positive test case for a method
108 int UtcDaliWheelEventIsCtrlModifier(void)
109 {
110   TestApplication application; // Reset all test adapter return codes
111
112   Dali::WheelEvent event = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, CTRL_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
113   DALI_TEST_CHECK( event );
114
115   DALI_TEST_EQUALS(CTRL_MODIFIER, event.GetModifiers(), TEST_LOCATION);
116
117   DALI_TEST_EQUALS(true, event.IsCtrlModifier(), TEST_LOCATION); // check IsCtrlModifier
118   END_TEST;
119 }
120
121 // Positive test case for a method
122 int UtcDaliWheelEventIsAltModifier(void)
123 {
124   TestApplication application; // Reset all test adapter return codes
125
126   Dali::WheelEvent event = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, ALT_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
127   DALI_TEST_CHECK( event );
128
129   DALI_TEST_EQUALS(ALT_MODIFIER, event.GetModifiers(), TEST_LOCATION);
130
131   DALI_TEST_EQUALS(true, event.IsAltModifier(), TEST_LOCATION);  // IsAltModifier
132   END_TEST;
133 }
134
135 // Positive fail test case for a method
136 int UtcDaliWheelEventIsNotShiftModifier(void)
137 {
138   TestApplication application; // Reset all test adapter return codes
139
140   Dali::WheelEvent event = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, CTRL_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
141   DALI_TEST_CHECK( event );
142
143   DALI_TEST_EQUALS(CTRL_MODIFIER, event.GetModifiers(), TEST_LOCATION);  // check different modifier used
144
145   DALI_TEST_EQUALS(false, event.IsShiftModifier(), TEST_LOCATION);
146   END_TEST;
147 }
148
149 // Positive fail test case for a method
150 int UtcDaliWheelEventIsNotCtrlModifier(void)
151 {
152   TestApplication application; // Reset all test adapter return codes
153
154   Dali::WheelEvent event = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, ALT_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
155   DALI_TEST_CHECK( event );
156
157   DALI_TEST_EQUALS(ALT_MODIFIER, event.GetModifiers(), TEST_LOCATION);  // check different modifier used
158
159   DALI_TEST_EQUALS(false, event.IsCtrlModifier(), TEST_LOCATION);
160   END_TEST;
161 }
162
163 // Positive fail test case for a method
164 int UtcDaliWheelEventIsNotAltModifier(void)
165 {
166   TestApplication application; // Reset all test adapter return codes
167
168   Dali::WheelEvent event = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
169   DALI_TEST_CHECK( event );
170
171   DALI_TEST_EQUALS(SHIFT_MODIFIER, event.GetModifiers(), TEST_LOCATION);  // check different modifier used
172
173   DALI_TEST_EQUALS(false, event.IsAltModifier(), TEST_LOCATION);
174   END_TEST;
175 }
176
177 // Positive test case for a method
178 int UtcDaliWheelEventANDModifer(void)
179 {
180   TestApplication application; // Reset all test adapter return codes
181
182   Dali::WheelEvent event = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_AND_CTRL_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
183   DALI_TEST_CHECK( event );
184   DALI_TEST_EQUALS(true, event.IsCtrlModifier() && event.IsShiftModifier(), TEST_LOCATION);
185
186   Dali::WheelEvent newEvent = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
187   DALI_TEST_EQUALS(false, newEvent.IsCtrlModifier() && newEvent.IsShiftModifier(), TEST_LOCATION);
188   END_TEST;
189 }
190
191 // Positive test case for a method
192 int UtcDaliWheelEventORModifer(void)
193 {
194   TestApplication application; // Reset all test adapter return codes
195
196   Dali::WheelEvent event = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_AND_CTRL_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
197   DALI_TEST_CHECK( event );
198   DALI_TEST_EQUALS(true, event.IsCtrlModifier() || event.IsAltModifier(), TEST_LOCATION);
199
200   Dali::WheelEvent newEvent = DevelWheelEvent::New( Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_MODIFIER, Vector2( 1.0f, 1.0f ), 1, 1000u );
201   DALI_TEST_EQUALS(false, newEvent.IsCtrlModifier() && newEvent.IsAltModifier(), TEST_LOCATION);
202   END_TEST;
203 }
204
205 int UtcDaliWheelEventSignalling(void)
206 {
207   TestApplication application; // Reset all test adapter return codes
208
209   Actor actor = Actor::New();
210   actor.SetProperty( Actor::Property::SIZE, Vector2( 100.0f, 100.0f ) );
211   actor.SetProperty( Actor::Property::ANCHOR_POINT,AnchorPoint::TOP_LEFT);
212   application.GetScene().Add(actor);
213
214   // Render and notify
215   application.SendNotification();
216   application.Render();
217
218   // Connect to actor's wheel event signal
219   SignalData data;
220   WheelEventReceivedFunctor functor( data );
221   actor.WheelEventSignal().Connect( &application, functor );
222
223   Vector2 screenCoordinates( 10.0f, 10.0f );
224   Integration::WheelEvent event(Integration::WheelEvent::MOUSE_WHEEL, 0, SHIFT_MODIFIER, screenCoordinates, 1, 1000u);
225
226   // Emit a wheel signal
227   application.ProcessEvent( event );
228   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
229   DALI_TEST_CHECK( actor == data.wheeledActor );
230   DALI_TEST_EQUALS(WheelEvent::MOUSE_WHEEL, data.receivedWheelEvent.GetType(), TEST_LOCATION); // check type
231   DALI_TEST_EQUALS(0, data.receivedWheelEvent.GetDirection(), TEST_LOCATION); // check direction
232   DALI_TEST_EQUALS(SHIFT_MODIFIER, data.receivedWheelEvent.GetModifiers(), TEST_LOCATION); // check modifier
233   DALI_TEST_EQUALS(screenCoordinates, data.receivedWheelEvent.GetPoint(), TEST_LOCATION); // check modifier
234   DALI_TEST_EQUALS(1, data.receivedWheelEvent.GetDelta(), TEST_LOCATION); // check modifier
235   DALI_TEST_EQUALS(1000u, data.receivedWheelEvent.GetTime(), TEST_LOCATION); // check modifier
236   data.Reset();
237
238   // Emit a wheel signal where the actor is not present, will hit the root actor though
239   Actor rootActor( application.GetScene().GetRootLayer() );
240
241   // Connect to root actor's wheel event signal
242   SignalData rootData;
243   WheelEventReceivedFunctor rootFunctor( rootData ); // Consumes signal
244   rootActor.WheelEventSignal().Connect( &application, rootFunctor );
245
246   screenCoordinates.x = screenCoordinates.y = 300.0f;
247   Integration::WheelEvent newEvent(Integration::WheelEvent::MOUSE_WHEEL, 0, SHIFT_MODIFIER, screenCoordinates, 1, 1000u);
248   application.ProcessEvent( newEvent );
249   DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
250   DALI_TEST_EQUALS( true, rootData.functorCalled, TEST_LOCATION );
251   DALI_TEST_CHECK( rootActor == rootData.wheeledActor );
252   DALI_TEST_EQUALS(WheelEvent::MOUSE_WHEEL, rootData.receivedWheelEvent.GetType(), TEST_LOCATION); // check type
253   DALI_TEST_EQUALS(0, rootData.receivedWheelEvent.GetDirection(), TEST_LOCATION); // check direction
254   DALI_TEST_EQUALS(SHIFT_MODIFIER, rootData.receivedWheelEvent.GetModifiers(), TEST_LOCATION); // check modifier
255   DALI_TEST_EQUALS(screenCoordinates, rootData.receivedWheelEvent.GetPoint(), TEST_LOCATION); // check modifier
256   DALI_TEST_EQUALS(1, rootData.receivedWheelEvent.GetDelta(), TEST_LOCATION); // check modifier
257   DALI_TEST_EQUALS(1000u, rootData.receivedWheelEvent.GetTime(), TEST_LOCATION); // check modifier
258
259   // Remove actor from the scene
260   application.GetScene().Remove( actor );
261
262   // Render and notify
263   application.SendNotification();
264   application.Render();
265
266   // Emit a move at the same point, we should not be signalled.
267   application.ProcessEvent( event );
268   DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
269   data.Reset();
270   END_TEST;
271 }