[dali_2.3.32] Merge branch 'devel/master'
[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 <dali-test-suite-utils.h>
19 #include <dali/devel-api/actors/actor-devel.h>
20 #include <dali/devel-api/events/wheel-event-devel.h>
21 #include <dali/integration-api/events/wheel-event-integ.h>
22 #include <dali/public-api/dali-core.h>
23 #include <stdlib.h>
24
25 #include <iostream>
26
27 using namespace Dali;
28
29 namespace
30 {
31 // Key Event Test references
32 const static unsigned int SHIFT_MODIFIER          = 0x1;
33 const static unsigned int CTRL_MODIFIER           = 0x2;
34 const static unsigned int ALT_MODIFIER            = 0x4;
35 const static unsigned int SHIFT_AND_CTRL_MODIFIER = SHIFT_MODIFIER | CTRL_MODIFIER;
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
45   void Reset()
46   {
47     functorCalled = false;
48
49     receivedWheelEvent.Reset();
50     wheeledActor.Reset();
51   }
52
53   bool       functorCalled;
54   WheelEvent receivedWheelEvent;
55   Actor      wheeledActor;
56 };
57
58 // Functor that sets the data when called
59 struct WheelEventReceivedFunctor
60 {
61   WheelEventReceivedFunctor(SignalData& data, bool returnValue = true)
62   : signalData(data),
63     returnValue(returnValue)
64   {
65   }
66
67   bool operator()(Actor actor, const WheelEvent& wheelEvent)
68   {
69     signalData.functorCalled      = true;
70     signalData.receivedWheelEvent = wheelEvent;
71     signalData.wheeledActor       = actor;
72
73     return returnValue;
74   }
75
76   SignalData& signalData;
77   bool        returnValue;
78 };
79
80 } // anonymous namespace
81
82 int UtcDaliWheelEventConstructor(void)
83 {
84   TestApplication application; // Reset all test adapter return codes
85
86   Dali::WheelEvent event = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
87   DALI_TEST_CHECK(event);
88
89   DALI_TEST_EQUALS(Dali::WheelEvent::MOUSE_WHEEL, event.GetType(), TEST_LOCATION); // check type
90   DALI_TEST_EQUALS(1, event.GetDirection(), TEST_LOCATION);                        // check direction
91   DALI_TEST_EQUALS(SHIFT_MODIFIER, event.GetModifiers(), TEST_LOCATION);           // check modifier
92   DALI_TEST_EQUALS(Vector2(1.0f, 1.0f), event.GetPoint(), TEST_LOCATION);          // check modifier
93   DALI_TEST_EQUALS(1, event.GetDelta(), TEST_LOCATION);                            // check modifier
94   DALI_TEST_EQUALS(1000u, event.GetTime(), TEST_LOCATION);                         // check modifier
95   END_TEST;
96 }
97
98 // Positive test case for a method
99 int UtcDaliWheelEventIsShiftModifier(void)
100 {
101   TestApplication application; // Reset all test adapter return codes
102
103   Dali::WheelEvent event = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
104   DALI_TEST_CHECK(event);
105
106   DALI_TEST_EQUALS(SHIFT_MODIFIER, event.GetModifiers(), TEST_LOCATION);
107
108   DALI_TEST_EQUALS(true, event.IsShiftModifier(), TEST_LOCATION); // check IsShiftModifier
109
110   END_TEST;
111 }
112
113 // Positive test case for a method
114 int UtcDaliWheelEventIsCtrlModifier(void)
115 {
116   TestApplication application; // Reset all test adapter return codes
117
118   Dali::WheelEvent event = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, CTRL_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
119   DALI_TEST_CHECK(event);
120
121   DALI_TEST_EQUALS(CTRL_MODIFIER, event.GetModifiers(), TEST_LOCATION);
122
123   DALI_TEST_EQUALS(true, event.IsCtrlModifier(), TEST_LOCATION); // check IsCtrlModifier
124   END_TEST;
125 }
126
127 // Positive test case for a method
128 int UtcDaliWheelEventIsAltModifier(void)
129 {
130   TestApplication application; // Reset all test adapter return codes
131
132   Dali::WheelEvent event = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, ALT_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
133   DALI_TEST_CHECK(event);
134
135   DALI_TEST_EQUALS(ALT_MODIFIER, event.GetModifiers(), TEST_LOCATION);
136
137   DALI_TEST_EQUALS(true, event.IsAltModifier(), TEST_LOCATION); // IsAltModifier
138   END_TEST;
139 }
140
141 // Positive fail test case for a method
142 int UtcDaliWheelEventIsNotShiftModifier(void)
143 {
144   TestApplication application; // Reset all test adapter return codes
145
146   Dali::WheelEvent event = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, CTRL_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
147   DALI_TEST_CHECK(event);
148
149   DALI_TEST_EQUALS(CTRL_MODIFIER, event.GetModifiers(), TEST_LOCATION); // check different modifier used
150
151   DALI_TEST_EQUALS(false, event.IsShiftModifier(), TEST_LOCATION);
152   END_TEST;
153 }
154
155 // Positive fail test case for a method
156 int UtcDaliWheelEventIsNotCtrlModifier(void)
157 {
158   TestApplication application; // Reset all test adapter return codes
159
160   Dali::WheelEvent event = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, ALT_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
161   DALI_TEST_CHECK(event);
162
163   DALI_TEST_EQUALS(ALT_MODIFIER, event.GetModifiers(), TEST_LOCATION); // check different modifier used
164
165   DALI_TEST_EQUALS(false, event.IsCtrlModifier(), TEST_LOCATION);
166   END_TEST;
167 }
168
169 // Positive fail test case for a method
170 int UtcDaliWheelEventIsNotAltModifier(void)
171 {
172   TestApplication application; // Reset all test adapter return codes
173
174   Dali::WheelEvent event = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
175   DALI_TEST_CHECK(event);
176
177   DALI_TEST_EQUALS(SHIFT_MODIFIER, event.GetModifiers(), TEST_LOCATION); // check different modifier used
178
179   DALI_TEST_EQUALS(false, event.IsAltModifier(), TEST_LOCATION);
180   END_TEST;
181 }
182
183 // Positive test case for a method
184 int UtcDaliWheelEventANDModifer(void)
185 {
186   TestApplication application; // Reset all test adapter return codes
187
188   Dali::WheelEvent event = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_AND_CTRL_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
189   DALI_TEST_CHECK(event);
190   DALI_TEST_EQUALS(true, event.IsCtrlModifier() && event.IsShiftModifier(), TEST_LOCATION);
191
192   Dali::WheelEvent newEvent = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
193   DALI_TEST_EQUALS(false, newEvent.IsCtrlModifier() && newEvent.IsShiftModifier(), TEST_LOCATION);
194   END_TEST;
195 }
196
197 // Positive test case for a method
198 int UtcDaliWheelEventORModifer(void)
199 {
200   TestApplication application; // Reset all test adapter return codes
201
202   Dali::WheelEvent event = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_AND_CTRL_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
203   DALI_TEST_CHECK(event);
204   DALI_TEST_EQUALS(true, event.IsCtrlModifier() || event.IsAltModifier(), TEST_LOCATION);
205
206   Dali::WheelEvent newEvent = DevelWheelEvent::New(Dali::WheelEvent::MOUSE_WHEEL, 1, SHIFT_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
207   DALI_TEST_EQUALS(false, newEvent.IsCtrlModifier() && newEvent.IsAltModifier(), TEST_LOCATION);
208   END_TEST;
209 }
210
211 int UtcDaliWheelEventSignalling(void)
212 {
213   TestApplication application; // Reset all test adapter return codes
214
215   Actor actor = Actor::New();
216   actor.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
217   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
218   application.GetScene().Add(actor);
219
220   // Render and notify
221   application.SendNotification();
222   application.Render();
223
224   // Connect to actor's wheel event signal
225   SignalData                data;
226   WheelEventReceivedFunctor functor(data);
227   actor.WheelEventSignal().Connect(&application, functor);
228
229   Vector2                 screenCoordinates(10.0f, 10.0f);
230   Integration::WheelEvent event(Integration::WheelEvent::MOUSE_WHEEL, 0, SHIFT_MODIFIER, screenCoordinates, 1, 1000u);
231
232   // Emit a wheel signal
233   application.ProcessEvent(event);
234   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
235   DALI_TEST_CHECK(actor == data.wheeledActor);
236   DALI_TEST_EQUALS(WheelEvent::MOUSE_WHEEL, data.receivedWheelEvent.GetType(), TEST_LOCATION); // check type
237   DALI_TEST_EQUALS(0, data.receivedWheelEvent.GetDirection(), TEST_LOCATION);                  // check direction
238   DALI_TEST_EQUALS(SHIFT_MODIFIER, data.receivedWheelEvent.GetModifiers(), TEST_LOCATION);     // check modifier
239   DALI_TEST_EQUALS(screenCoordinates, data.receivedWheelEvent.GetPoint(), TEST_LOCATION);      // check modifier
240   DALI_TEST_EQUALS(1, data.receivedWheelEvent.GetDelta(), TEST_LOCATION);                      // check modifier
241   DALI_TEST_EQUALS(1000u, data.receivedWheelEvent.GetTime(), TEST_LOCATION);                   // check modifier
242   data.Reset();
243
244   // Emit a wheel signal where the actor is not present, will hit the root actor though
245   Actor rootActor(application.GetScene().GetRootLayer());
246
247   // Connect to root actor's wheel event signal
248   SignalData                rootData;
249   WheelEventReceivedFunctor rootFunctor(rootData); // Consumes signal
250   rootActor.WheelEventSignal().Connect(&application, rootFunctor);
251
252   screenCoordinates.x = screenCoordinates.y = 300.0f;
253   Integration::WheelEvent newEvent(Integration::WheelEvent::MOUSE_WHEEL, 0, SHIFT_MODIFIER, screenCoordinates, 1, 1000u);
254   application.ProcessEvent(newEvent);
255   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
256   DALI_TEST_EQUALS(true, rootData.functorCalled, TEST_LOCATION);
257   DALI_TEST_CHECK(rootActor == rootData.wheeledActor);
258   DALI_TEST_EQUALS(WheelEvent::MOUSE_WHEEL, rootData.receivedWheelEvent.GetType(), TEST_LOCATION); // check type
259   DALI_TEST_EQUALS(0, rootData.receivedWheelEvent.GetDirection(), TEST_LOCATION);                  // check direction
260   DALI_TEST_EQUALS(SHIFT_MODIFIER, rootData.receivedWheelEvent.GetModifiers(), TEST_LOCATION);     // check modifier
261   DALI_TEST_EQUALS(screenCoordinates, rootData.receivedWheelEvent.GetPoint(), TEST_LOCATION);      // check modifier
262   DALI_TEST_EQUALS(1, rootData.receivedWheelEvent.GetDelta(), TEST_LOCATION);                      // check modifier
263   DALI_TEST_EQUALS(1000u, rootData.receivedWheelEvent.GetTime(), TEST_LOCATION);                   // check modifier
264
265   // Remove actor from the scene
266   application.GetScene().Remove(actor);
267
268   // Render and notify
269   application.SendNotification();
270   application.Render();
271
272   // Emit a move at the same point, we should not be signalled.
273   application.ProcessEvent(event);
274   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
275   data.Reset();
276   END_TEST;
277 }
278
279 int UtcDaliWheelEventIntercept(void)
280 {
281   TestApplication application; // Reset all test adapter return codes
282
283   Actor actor = Actor::New();
284   actor.SetProperty(Actor::Property::SIZE, Vector2(100.0f, 100.0f));
285   actor.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::TOP_LEFT);
286   application.GetScene().Add(actor);
287
288   // Render and notify
289   application.SendNotification();
290   application.Render();
291
292   // Connect to actor's wheel event signal
293   SignalData                data;
294   WheelEventReceivedFunctor functor(data);
295   actor.WheelEventSignal().Connect(&application, functor);
296
297   Vector2                 screenCoordinates(10.0f, 10.0f);
298   Integration::WheelEvent event(Integration::WheelEvent::MOUSE_WHEEL, 0, SHIFT_MODIFIER, screenCoordinates, 1, 1000u);
299
300   // Emit a wheel signal
301   application.ProcessEvent(event);
302   DALI_TEST_EQUALS(true, data.functorCalled, TEST_LOCATION);
303   DALI_TEST_CHECK(actor == data.wheeledActor);
304   DALI_TEST_EQUALS(WheelEvent::MOUSE_WHEEL, data.receivedWheelEvent.GetType(), TEST_LOCATION); // check type
305   DALI_TEST_EQUALS(0, data.receivedWheelEvent.GetDirection(), TEST_LOCATION);                  // check direction
306   DALI_TEST_EQUALS(SHIFT_MODIFIER, data.receivedWheelEvent.GetModifiers(), TEST_LOCATION);     // check modifier
307   DALI_TEST_EQUALS(screenCoordinates, data.receivedWheelEvent.GetPoint(), TEST_LOCATION);      // check modifier
308   DALI_TEST_EQUALS(1, data.receivedWheelEvent.GetDelta(), TEST_LOCATION);                      // check modifier
309   DALI_TEST_EQUALS(1000u, data.receivedWheelEvent.GetTime(), TEST_LOCATION);                   // check modifier
310   data.Reset();
311
312   // Intercept wheel events on the root actor.
313   Actor rootActor(application.GetScene().GetRootLayer());
314
315   // Connect to root actor's intercept wheel event signal
316   SignalData                rootData;
317   WheelEventReceivedFunctor rootFunctor(rootData); // Consumes signal
318   Dali::DevelActor::InterceptWheelSignal(rootActor).Connect(&application, rootFunctor);
319
320   Integration::WheelEvent newEvent(Integration::WheelEvent::MOUSE_WHEEL, 0, SHIFT_MODIFIER, screenCoordinates, 1, 1000u);
321   application.ProcessEvent(newEvent);
322
323   // It should be able to receive wheel events to root actor by registering only InterceptWheelEvent.
324   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
325   DALI_TEST_EQUALS(true, rootData.functorCalled, TEST_LOCATION);
326   DALI_TEST_CHECK(rootActor == rootData.wheeledActor);
327   DALI_TEST_EQUALS(WheelEvent::MOUSE_WHEEL, rootData.receivedWheelEvent.GetType(), TEST_LOCATION); // check type
328   DALI_TEST_EQUALS(0, rootData.receivedWheelEvent.GetDirection(), TEST_LOCATION);                  // check direction
329   DALI_TEST_EQUALS(SHIFT_MODIFIER, rootData.receivedWheelEvent.GetModifiers(), TEST_LOCATION);     // check modifier
330   DALI_TEST_EQUALS(screenCoordinates, rootData.receivedWheelEvent.GetPoint(), TEST_LOCATION);      // check modifier
331   DALI_TEST_EQUALS(1, rootData.receivedWheelEvent.GetDelta(), TEST_LOCATION);                      // check modifier
332   DALI_TEST_EQUALS(1000u, rootData.receivedWheelEvent.GetTime(), TEST_LOCATION);                   // check modifier
333
334   // Remove actor from the scene
335   application.GetScene().Remove(actor);
336
337   // Render and notify
338   application.SendNotification();
339   application.Render();
340
341   // Emit a move at the same point, we should not be signalled.
342   application.ProcessEvent(event);
343   DALI_TEST_EQUALS(false, data.functorCalled, TEST_LOCATION);
344   data.Reset();
345   END_TEST;
346 }