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