ImageVisual Action::Reload added
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-KeyInputFocusManager.cpp
1 /*
2  * Copyright (c) 2017 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-toolkit-test-suite-utils.h>
21 #include <dali-toolkit/dali-toolkit.h>
22 #include <dali/integration-api/events/key-event-integ.h>
23 #include <dali-toolkit/devel-api/focus-manager/keyinput-focus-manager.h>
24 #include <dali/devel-api/common/stage-devel.h>
25
26 #include "dummy-control.h"
27
28 using namespace Dali;
29 using namespace Toolkit;
30
31 namespace
32 {
33
34 /**
35  * Callback class for KeyInputFocusChanged signal.
36  */
37 class KeyInputFocusChangedCallback : public Dali::ConnectionTracker
38 {
39 public:
40   /**
41    * Constructor
42    * @param[in]  gainActor  Ref to the actor that should be set as the one that gains key input focus.
43    * @param[in]  lostActor  Ref to the actor that should be set as the one that loses key input focus.
44    */
45   KeyInputFocusChangedCallback( Control& gainActor, Control& lostActor )
46   : mActorGain( gainActor ),
47     mActorLost( lostActor )
48   {
49   }
50
51   void Callback( Control gainingActor, Control lostActor )
52   {
53     mActorGain = gainingActor;
54     mActorLost = lostActor;
55   }
56
57   Control& mActorGain;
58   Control& mActorLost;
59 };
60
61 // Stores data that is populated in the callback and will be read by the TET cases
62 struct SignalData
63 {
64   SignalData()
65   : functorCalled(false)
66   {}
67
68   void Reset()
69   {
70     functorCalled = false;
71
72     receivedKeyEvent.keyModifier = 0;
73     receivedKeyEvent.keyPressedName.clear();
74     receivedKeyEvent.keyPressed.clear();
75
76   }
77
78   bool functorCalled;
79   KeyEvent receivedKeyEvent;
80 };
81
82 /**
83  * Callback class for KeyEvent signal of control.
84  */
85 class KeyEventCallback : public Dali::ConnectionTracker
86 {
87 public:
88   /**
89    * Constructor
90    * @param[in]  returnValue  Set return value of KeyEvent callback.
91    * */
92   KeyEventCallback( bool consumed )
93   : mConsumed( consumed ),
94     mIsCalled( false )
95   {
96   }
97
98   bool Callback( Control control, const KeyEvent& keyEvent )
99   {
100     mIsCalled = true;
101     return mConsumed;
102   }
103
104   void Callback( const KeyEvent& keyEvent )
105   {
106     mIsCalled = true;
107   }
108
109   bool mConsumed;
110   bool mIsCalled;
111 };
112
113 } // namespace
114
115 void key_input_focus_manager_startup(void)
116 {
117   test_return_value = TET_UNDEF;
118 }
119
120 void key_input_focus_manager_cleanup(void)
121 {
122   test_return_value = TET_PASS;
123 }
124
125 int UtcDaliKeyInputFocusManagerGet(void)
126 {
127   ToolkitTestApplication application;
128
129   tet_infoline(" UtcDaliKeyInputFocusManagerGet");
130
131   KeyInputFocusManager manager;
132   {
133     manager = KeyInputFocusManager::Get();
134     DALI_TEST_CHECK(manager);
135   }
136
137   KeyInputFocusManager newManager = KeyInputFocusManager::Get();
138   DALI_TEST_CHECK(newManager);
139
140   // Check that focus manager is a singleton
141   DALI_TEST_CHECK(manager == newManager);
142   END_TEST;
143 }
144
145 int UtcDaliKeyInputFocusManagerSetFocus01(void)
146 {
147   ToolkitTestApplication application;
148   Stage stage = Stage::GetCurrent();
149
150   tet_infoline(" Check that there is no focused control. Set focus to control. Check it is now the focused actor and receives KeyInputFocusGained signal");
151
152   KeyInputFocusManager manager = KeyInputFocusManager::Get();
153   DALI_TEST_CHECK(manager);
154
155   Control focusedControl = manager.GetCurrentFocusControl();
156   DALI_TEST_CHECK( ! focusedControl );
157
158   DummyControl dummy = DummyControl::New(true);
159   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummy.GetImplementation());
160   dummy.SetSize(100.0f, 100.0f);
161   stage.Add( dummy );
162   DALI_TEST_CHECK( ! dummyImpl.keyInputFocusGained );
163
164   manager.SetFocus( dummy );
165   DALI_TEST_CHECK( dummy.HasKeyInputFocus()); // Also tests IsKeyboardListener() API
166   DALI_TEST_CHECK( dummyImpl.keyInputFocusGained );
167
168   focusedControl = manager.GetCurrentFocusControl();
169   DALI_TEST_CHECK( focusedControl );
170   DALI_TEST_CHECK( focusedControl == dummy );
171
172   END_TEST;
173 }
174
175 int UtcDaliKeyInputFocusManagerSetFocus02(void)
176 {
177   ToolkitTestApplication application;
178   Stage stage = Stage::GetCurrent();
179
180   tet_infoline(" Set focus to control. Check it is now the focused actor and receives KeyInputFocusGained signal. Set focuse to another control - check that the first control receives KeyInputFocusLost");
181
182   KeyInputFocusManager manager = KeyInputFocusManager::Get();
183   DALI_TEST_CHECK(manager);
184
185   DummyControl dummy1 = DummyControl::New(true);
186   Impl::DummyControl& dummy1Impl = static_cast<Impl::DummyControl&>(dummy1.GetImplementation());
187   dummy1.SetSize(100.0f, 100.0f);
188   stage.Add( dummy1 );
189   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusGained );
190   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusLost );
191
192   manager.SetFocus( dummy1 );
193   DALI_TEST_CHECK( dummy1.HasKeyInputFocus()); // Also tests IsKeyboardListener() API
194   DALI_TEST_CHECK( dummy1Impl.keyInputFocusGained );
195   dummy1Impl.keyInputFocusGained = false;
196
197   DummyControl dummy2 = DummyControl::New(true);
198   Impl::DummyControl& dummy2Impl = static_cast<Impl::DummyControl&>(dummy2.GetImplementation());
199   dummy2.SetSize(100.0f, 100.0f);
200   stage.Add( dummy2 );
201   DALI_TEST_CHECK( ! dummy2Impl.keyInputFocusGained );
202   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusLost );
203
204   manager.SetFocus( dummy2 );
205   DALI_TEST_CHECK( dummy2.HasKeyInputFocus()); // Also tests IsKeyboardListener() API
206   DALI_TEST_CHECK( dummy2Impl.keyInputFocusGained );
207   dummy1Impl.keyInputFocusGained = false;
208
209   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusGained );
210   DALI_TEST_CHECK( dummy1Impl.keyInputFocusLost );
211
212   END_TEST;
213 }
214
215 int UtcDaliKeyInputFocusManagerKeyEventPropagation01(void)
216 {
217
218   ToolkitTestApplication application;
219   Stage stage = Stage::GetCurrent();
220
221   tet_infoline("Test KeyEvent propagation. If focused control doesn't consume KeyEvent, KeyEvent will be recursively delivered to the control and its parents, until the event is consumed or the stage is reached. In this case, KeyEvent is delivered to KeyboardFocusManager via Stage's KeyEventSignal");
222
223   KeyInputFocusManager manager = KeyInputFocusManager::Get();
224   DALI_TEST_CHECK(manager);
225
226   KeyEventCallback stageCallback( false );
227   stage.KeyEventSignal().Connect( &stageCallback, &KeyEventCallback::Callback );
228
229   DummyControl dummy1 = DummyControl::New(true);
230   dummy1.SetSize(100.0f, 100.0f);
231   KeyEventCallback callback1( false );
232   dummy1.KeyEventSignal().Connect( &callback1, &KeyEventCallback::Callback );
233   stage.Add( dummy1 );
234
235   DummyControl dummy2 = DummyControl::New(true);
236   dummy2.SetSize(100.0f, 100.0f);
237   KeyEventCallback callback2( false );
238   dummy2.KeyEventSignal().Connect( &callback2, &KeyEventCallback::Callback );
239   dummy1.Add( dummy2 );
240
241   DummyControl dummy3 = DummyControl::New(true);
242   Impl::DummyControl& dummy3Impl = static_cast<Impl::DummyControl&>(dummy3.GetImplementation());
243   dummy3.SetSize(100.0f, 100.0f);
244   KeyEventCallback callback3( false );
245   dummy3.KeyEventSignal().Connect( &callback3, &KeyEventCallback::Callback );
246   dummy2.Add( dummy3 );
247   DALI_TEST_CHECK( ! dummy3Impl.keyInputFocusGained );
248   DALI_TEST_CHECK( ! dummy3Impl.keyInputFocusLost );
249
250   manager.SetFocus( dummy3 );
251   DALI_TEST_CHECK( dummy3Impl.keyInputFocusGained );
252
253   Integration::KeyEvent event( "a", "a", 0, 0, 0, Integration::KeyEvent::Up, "", Device::Class::TOUCH, Device::Subclass::NONE );
254   application.ProcessEvent(event);
255
256   DALI_TEST_CHECK( callback1.mIsCalled );
257   DALI_TEST_CHECK( callback2.mIsCalled );
258   DALI_TEST_CHECK( callback3.mIsCalled );
259   DALI_TEST_CHECK( stageCallback.mIsCalled );
260
261   END_TEST;
262 }
263
264 int UtcDaliKeyInputFocusManagerKeyEventPropagation02(void)
265 {
266
267   ToolkitTestApplication application;
268   Stage stage = Stage::GetCurrent();
269
270   tet_infoline("Test KeyEvent propagation. If focused control doesn't consume KeyEvent, KeyEvent will be recursively delivered to the control and its parents, until the event is consumed or the stage is reached. In this case, KeyEvent is delivered from dummy3 to dummy2");
271
272   KeyInputFocusManager manager = KeyInputFocusManager::Get();
273   DALI_TEST_CHECK(manager);
274
275   DummyControl dummy1 = DummyControl::New(true);
276   dummy1.SetSize(100.0f, 100.0f);
277   KeyEventCallback callback1( false );
278   dummy1.KeyEventSignal().Connect( &callback1, &KeyEventCallback::Callback );
279   stage.Add( dummy1 );
280
281   DummyControl dummy2 = DummyControl::New(true);
282   dummy2.SetSize(100.0f, 100.0f);
283   KeyEventCallback callback2( true );
284   dummy2.KeyEventSignal().Connect( &callback2, &KeyEventCallback::Callback );
285   dummy1.Add( dummy2 );
286
287   DummyControl dummy3 = DummyControl::New(true);
288   Impl::DummyControl& dummy3Impl = static_cast<Impl::DummyControl&>(dummy3.GetImplementation());
289   dummy3.SetSize(100.0f, 100.0f);
290   KeyEventCallback callback3( false );
291   dummy3.KeyEventSignal().Connect( &callback3, &KeyEventCallback::Callback );
292   dummy2.Add( dummy3 );
293   DALI_TEST_CHECK( ! dummy3Impl.keyInputFocusGained );
294   DALI_TEST_CHECK( ! dummy3Impl.keyInputFocusLost );
295
296   manager.SetFocus( dummy3 );
297   DALI_TEST_CHECK( dummy3Impl.keyInputFocusGained );
298
299   Integration::KeyEvent event( "a", "a", 0, 0, 0, Integration::KeyEvent::Up, "", Device::Class::TOUCH, Device::Subclass::NONE );
300   application.ProcessEvent(event);
301
302   DALI_TEST_CHECK( !callback1.mIsCalled );
303   DALI_TEST_CHECK( callback2.mIsCalled );
304   DALI_TEST_CHECK( callback3.mIsCalled );
305
306   END_TEST;
307 }
308
309 int UtcDaliKeyInputFocusManagerGetCurrentFocusControl(void)
310 {
311   ToolkitTestApplication application;
312   Stage stage = Stage::GetCurrent();
313
314   tet_infoline(" Add 2 controls, check they each get focused. Re-focus the first control - ensure it's now got focus (check signals)");
315
316   KeyInputFocusManager manager = KeyInputFocusManager::Get();
317   DALI_TEST_CHECK(manager);
318
319   DummyControl dummy1 = DummyControl::New(true);
320   Impl::DummyControl& dummy1Impl = static_cast<Impl::DummyControl&>(dummy1.GetImplementation());
321   dummy1.SetSize(100.0f, 100.0f);
322   stage.Add( dummy1 );
323   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusGained );
324   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusLost );
325
326   DummyControl dummy2 = DummyControl::New(true);
327   Impl::DummyControl& dummy2Impl = static_cast<Impl::DummyControl&>(dummy2.GetImplementation());
328   dummy2.SetSize(100.0f, 100.0f);
329   stage.Add( dummy2 );
330   DALI_TEST_CHECK( ! dummy2Impl.keyInputFocusGained );
331   DALI_TEST_CHECK( ! dummy2Impl.keyInputFocusLost );
332
333   manager.SetFocus(dummy1);
334   DALI_TEST_CHECK( dummy1 == manager.GetCurrentFocusControl() );
335   DALI_TEST_CHECK( dummy1Impl.keyInputFocusGained );
336   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusLost );
337   dummy1Impl.keyInputFocusGained = false;
338   dummy1Impl.keyInputFocusLost = false;
339
340   manager.SetFocus(dummy2);
341   DALI_TEST_CHECK( dummy2 == manager.GetCurrentFocusControl() );
342   DALI_TEST_CHECK( dummy1Impl.keyInputFocusLost );
343   DALI_TEST_CHECK( dummy2Impl.keyInputFocusGained );
344   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusGained );
345   DALI_TEST_CHECK( ! dummy2Impl.keyInputFocusLost );
346   // Reset signal received
347   dummy1Impl.keyInputFocusGained = false;
348   dummy1Impl.keyInputFocusLost = false;
349   dummy2Impl.keyInputFocusGained = false;
350   dummy2Impl.keyInputFocusLost = false;
351
352   manager.SetFocus(dummy1);
353   DALI_TEST_CHECK( dummy1 == manager.GetCurrentFocusControl());
354   DALI_TEST_CHECK( dummy1Impl.keyInputFocusGained );
355   DALI_TEST_CHECK( dummy2Impl.keyInputFocusLost );
356   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusLost );
357   DALI_TEST_CHECK( ! dummy2Impl.keyInputFocusGained );
358   END_TEST;
359 }
360
361 int UtcDaliKeyInputFocusManagerRemoveFocus(void)
362 {
363   ToolkitTestApplication application;
364   Stage stage = Stage::GetCurrent();
365
366   tet_infoline(" Add focus controls. Test that removing focus from control which has focus. ");
367
368   KeyInputFocusManager manager = KeyInputFocusManager::Get();
369   DALI_TEST_CHECK(manager);
370
371   DummyControl dummy1 = DummyControl::New(true);
372   Impl::DummyControl& dummy1Impl = static_cast<Impl::DummyControl&>(dummy1.GetImplementation());
373   dummy1.SetSize(100.0f, 100.0f);
374   stage.Add( dummy1 );
375   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusGained );
376   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusLost );
377
378   manager.SetFocus(dummy1);
379   DALI_TEST_CHECK(dummy1 == manager.GetCurrentFocusControl());
380   dummy1Impl.keyInputFocusGained = false;
381   dummy1Impl.keyInputFocusLost = false;
382
383   manager.RemoveFocus(dummy1);
384   DALI_TEST_CHECK(Control() == manager.GetCurrentFocusControl());
385   DALI_TEST_CHECK( dummy1Impl.keyInputFocusLost );
386   DALI_TEST_CHECK( ! dummy1Impl.keyInputFocusGained );
387
388   END_TEST;
389 }
390
391 int UtcDaliKeyInputFocusManagerSignalKeyInputFocusChanged(void)
392 {
393   ToolkitTestApplication application;
394   KeyInputFocusManager manager = KeyInputFocusManager::Get();
395   Stage stage = Stage::GetCurrent();
396
397   tet_infoline(" UtcDaliKeyInputFocusManagerSignalKeyInputFocusChanged");
398
399   PushButton pushButton1 = PushButton::New();
400   PushButton pushButton2 = PushButton::New();
401
402   stage.Add( pushButton1 );
403   stage.Add( pushButton2 );
404
405   PushButton gainActor, lostActor;
406   KeyInputFocusChangedCallback callback( gainActor, lostActor );
407   manager.KeyInputFocusChangedSignal().Connect( &callback, &KeyInputFocusChangedCallback::Callback );
408
409   manager.SetFocus(pushButton1);
410
411   DALI_TEST_CHECK( gainActor == pushButton1 );
412   DALI_TEST_CHECK( lostActor == Control() );
413
414   gainActor.Reset();
415   lostActor.Reset();
416
417   manager.SetFocus(pushButton2);
418
419   DALI_TEST_CHECK( gainActor == pushButton2 );
420   DALI_TEST_CHECK( lostActor == pushButton1 );
421
422   gainActor.Reset();
423   lostActor.Reset();
424
425   // Removing the focus actor from the stage would also result in signal emission.
426   stage.Remove( pushButton1 );
427   stage.Remove( pushButton2 );
428
429   DALI_TEST_CHECK( gainActor == Control() );
430   DALI_TEST_CHECK( lostActor == Control() );
431   END_TEST;
432 }