License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-unmanaged / utc-Dali-MouseWheelEvent.cpp
1 /*
2  * Copyright (c) 2014 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/dali.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali/integration-api/events/mouse-wheel-event-integ.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     receivedMouseWheelEvent.direction = 0;
49     receivedMouseWheelEvent.modifiers = 0;
50     receivedMouseWheelEvent.point = Vector2::ZERO;
51     receivedMouseWheelEvent.z = 0;
52     receivedMouseWheelEvent.timeStamp = 0;
53
54     mouseWheeledActor = NULL;
55   }
56
57   bool functorCalled;
58   MouseWheelEvent receivedMouseWheelEvent;
59   Actor mouseWheeledActor;
60 };
61
62 // Functor that sets the data when called
63 struct MouseWheelEventReceivedFunctor
64 {
65   MouseWheelEventReceivedFunctor( SignalData& data ) : signalData( data ) { }
66
67   bool operator()( Actor actor, const MouseWheelEvent& mouseWheelEvent )
68   {
69     signalData.functorCalled = true;
70     signalData.receivedMouseWheelEvent = mouseWheelEvent;
71     signalData.mouseWheeledActor = actor;
72
73     return true;
74   }
75
76   SignalData& signalData;
77 };
78
79 } // anonymous namespace
80
81 int UtcDaliMouseWheelEventConstructor(void)
82 {
83   TestApplication application; // Reset all test adapter return codes
84
85   MouseWheelEvent event(1, SHIFT_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);  // coustruct a mouse wheel event
86
87   DALI_TEST_EQUALS(1, event.direction, TEST_LOCATION); // check direction
88   DALI_TEST_EQUALS(SHIFT_MODIFIER, event.modifiers, TEST_LOCATION); // check modifier
89   DALI_TEST_EQUALS(Vector2(1.0f, 1.0f), event.point, TEST_LOCATION); // check modifier
90   DALI_TEST_EQUALS(1, event.z, TEST_LOCATION); // check modifier
91   DALI_TEST_EQUALS(1000u, event.timeStamp, TEST_LOCATION); // check modifier
92   END_TEST;
93 }
94
95 // Positive test case for a method
96 int UtcDaliMouseWheelEventIsShiftModifier(void)
97 {
98   TestApplication application; // Reset all test adapter return codes
99
100   MouseWheelEvent event;
101   DALI_TEST_EQUALS(0u, event.modifiers, TEST_LOCATION);
102
103   event.modifiers = SHIFT_MODIFIER; // Set to Shift Modifier
104
105   DALI_TEST_EQUALS(SHIFT_MODIFIER, event.modifiers, TEST_LOCATION); // check able to set
106
107   DALI_TEST_EQUALS(true, event.IsShiftModifier(), TEST_LOCATION); // check IsShiftModifier
108
109   END_TEST;
110 }
111
112 // Positive test case for a method
113 int UtcDaliMouseWheelEventIsCtrlModifier(void)
114 {
115   TestApplication application; // Reset all test adapter return codes
116
117   MouseWheelEvent event;
118   DALI_TEST_EQUALS(0u, event.modifiers, TEST_LOCATION);
119
120   event.modifiers = CTRL_MODIFIER; // Set to Ctrl Modifier
121
122   DALI_TEST_EQUALS(CTRL_MODIFIER, event.modifiers, TEST_LOCATION); // check able to set
123
124   DALI_TEST_EQUALS(true, event.IsCtrlModifier(), TEST_LOCATION); // check IsCtrlModifier
125   END_TEST;
126 }
127
128 // Positive test case for a method
129 int UtcDaliMouseWheelEventIsAltModifier(void)
130 {
131   TestApplication application; // Reset all test adapter return codes
132
133   MouseWheelEvent event;
134   DALI_TEST_EQUALS(0u, event.modifiers, TEST_LOCATION);
135
136   event.modifiers = ALT_MODIFIER; // Set to Alt Modifier
137
138   DALI_TEST_EQUALS(ALT_MODIFIER, event.modifiers, TEST_LOCATION); // check able to set
139
140   DALI_TEST_EQUALS(true, event.IsAltModifier(), TEST_LOCATION);  // IsAltModifier
141   END_TEST;
142 }
143
144 // Positive fail test case for a method
145 int UtcDaliMouseWheelEventIsNotShiftModifier(void)
146 {
147   TestApplication application; // Reset all test adapter return codes
148
149   MouseWheelEvent event(1, CTRL_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
150
151   DALI_TEST_EQUALS(CTRL_MODIFIER, event.modifiers, TEST_LOCATION);  // check different modifier used
152
153   DALI_TEST_EQUALS(false, event.IsShiftModifier(), TEST_LOCATION);
154   END_TEST;
155 }
156
157 // Positive fail test case for a method
158 int UtcDaliMouseWheelEventIsNotCtrlModifier(void)
159 {
160   TestApplication application; // Reset all test adapter return codes
161
162   MouseWheelEvent event(1, ALT_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
163
164   DALI_TEST_EQUALS(ALT_MODIFIER, event.modifiers, TEST_LOCATION);  // check different modifier used
165
166   DALI_TEST_EQUALS(false, event.IsCtrlModifier(), TEST_LOCATION);
167   END_TEST;
168 }
169
170 // Positive fail test case for a method
171 int UtcDaliMouseWheelEventIsNotAltModifier(void)
172 {
173   TestApplication application; // Reset all test adapter return codes
174
175   MouseWheelEvent event(1, SHIFT_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
176
177   DALI_TEST_EQUALS(SHIFT_MODIFIER, event.modifiers, 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 UtcDaliMouseWheelEventANDModifer(void)
185 {
186   TestApplication application; // Reset all test adapter return codes
187
188   MouseWheelEvent event(1, SHIFT_AND_CTRL_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
189   DALI_TEST_EQUALS(true, event.IsCtrlModifier() && event.IsShiftModifier(), TEST_LOCATION);
190
191   event.modifiers = SHIFT_MODIFIER;
192
193   DALI_TEST_EQUALS(false, event.IsCtrlModifier() && event.IsShiftModifier(), TEST_LOCATION);
194   END_TEST;
195 }
196
197 // Positive test case for a method
198 int UtcDaliMouseWheelEventORModifer(void)
199 {
200   TestApplication application; // Reset all test adapter return codes
201
202   MouseWheelEvent event(1, SHIFT_AND_CTRL_MODIFIER, Vector2(1.0f, 1.0f), 1, 1000u);
203   DALI_TEST_EQUALS(true, event.IsCtrlModifier() || event.IsAltModifier(), TEST_LOCATION);
204
205   event.modifiers = SHIFT_MODIFIER;
206
207   DALI_TEST_EQUALS(false, event.IsCtrlModifier() && event.IsAltModifier(), TEST_LOCATION);
208   END_TEST;
209 }
210
211 int UtcDaliMouseWheelEventSignalling(void)
212 {
213   TestApplication application; // Reset all test adapter return codes
214
215   Actor actor = Actor::New();
216   actor.SetSize(100.0f, 100.0f);
217   actor.SetAnchorPoint(AnchorPoint::TOP_LEFT);
218   Stage::GetCurrent().Add(actor);
219
220   // Render and notify
221   application.SendNotification();
222   application.Render();
223
224   // Connect to actor's mouse wheel event signal
225   SignalData data;
226   MouseWheelEventReceivedFunctor functor( data );
227   actor.MouseWheelEventSignal().Connect( &application, functor );
228
229   Vector2 screenCoordinates( 10.0f, 10.0f );
230   Integration::MouseWheelEvent event(0, SHIFT_MODIFIER, screenCoordinates, 1, 1000u);
231
232   // Emit a mouse wheel signal
233   application.ProcessEvent( event );
234   DALI_TEST_EQUALS( true, data.functorCalled, TEST_LOCATION );
235   DALI_TEST_CHECK( actor == data.mouseWheeledActor );
236   DALI_TEST_EQUALS(0, data.receivedMouseWheelEvent.direction, TEST_LOCATION); // check direction
237   DALI_TEST_EQUALS(SHIFT_MODIFIER, data.receivedMouseWheelEvent.modifiers, TEST_LOCATION); // check modifier
238   DALI_TEST_EQUALS(screenCoordinates, data.receivedMouseWheelEvent.point, TEST_LOCATION); // check modifier
239   DALI_TEST_EQUALS(1, data.receivedMouseWheelEvent.z, TEST_LOCATION); // check modifier
240   DALI_TEST_EQUALS(1000u, data.receivedMouseWheelEvent.timeStamp, TEST_LOCATION); // check modifier
241   data.Reset();
242
243   // Emit a mouse wheel signal where the actor is not present, will hit the root actor though
244   Actor rootActor( Stage::GetCurrent().GetRootLayer() );
245
246   // Connect to root actor's mouse wheel event signal
247   SignalData rootData;
248   MouseWheelEventReceivedFunctor rootFunctor( rootData ); // Consumes signal
249   rootActor.MouseWheelEventSignal().Connect( &application, rootFunctor );
250
251   screenCoordinates.x = screenCoordinates.y = 300.0f;
252   Integration::MouseWheelEvent newEvent(0, SHIFT_MODIFIER, screenCoordinates, 1, 1000u);
253   application.ProcessEvent( newEvent );
254   DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
255   DALI_TEST_EQUALS( true, rootData.functorCalled, TEST_LOCATION );
256   DALI_TEST_CHECK( rootActor == rootData.mouseWheeledActor );
257   DALI_TEST_EQUALS(0, rootData.receivedMouseWheelEvent.direction, TEST_LOCATION); // check direction
258   DALI_TEST_EQUALS(SHIFT_MODIFIER, rootData.receivedMouseWheelEvent.modifiers, TEST_LOCATION); // check modifier
259   DALI_TEST_EQUALS(screenCoordinates, rootData.receivedMouseWheelEvent.point, TEST_LOCATION); // check modifier
260   DALI_TEST_EQUALS(1, rootData.receivedMouseWheelEvent.z, TEST_LOCATION); // check modifier
261   DALI_TEST_EQUALS(1000u, rootData.receivedMouseWheelEvent.timeStamp, TEST_LOCATION); // check modifier
262
263   // Remove actor from stage
264   Stage::GetCurrent().Remove( actor );
265
266   // Render and notify
267   application.SendNotification();
268   application.Render();
269
270   // Emit a move at the same point, we should not be signalled.
271   application.ProcessEvent( event );
272   DALI_TEST_EQUALS( false, data.functorCalled, TEST_LOCATION );
273   data.Reset();
274   END_TEST;
275 }