Updated tests using Integration KeyEvent
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-KeyboardFocusManager.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
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
24
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali/integration-api/events/key-event-integ.h>
27 #include <dali-toolkit/devel-api/focus-manager/keyboard-focus-manager-devel.h>
28 #include <dali-toolkit/devel-api/controls/control-devel.h>
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 void utc_dali_toolkit_keyboard_focus_manager_startup(void)
34 {
35   test_return_value = TET_UNDEF;
36 }
37
38 void utc_dali_toolkit_keyboard_focus_manager_cleanup(void)
39 {
40   test_return_value = TET_PASS;
41 }
42
43
44 namespace
45 {
46
47 const std::string DEFAULT_DEVICE_NAME("hwKeyboard");
48
49
50 // Functors to test whether GetNextFocusableActor() method of CustomAlgorithmInterface is called when the keyboard focus is about to change
51 class CustomAlgorithm : public Dali::Toolkit::DevelKeyboardFocusManager::CustomAlgorithmInterface
52 {
53 public:
54   CustomAlgorithm(bool& interfaceVerified)
55   : mInterfaceVerified(interfaceVerified),
56     mCurrentFocusedActor(),
57     mProposedActorToFocus(),
58     mDirection(Control::KeyboardFocus::LEFT)
59   {
60   }
61
62   Actor GetNextFocusableActor(Actor currentFocusedActor, Actor proposedActorToFocus, Control::KeyboardFocus::Direction direction)
63   {
64     tet_infoline("Verifying CustomAlgorithm()");
65
66     mInterfaceVerified = true;
67
68     mCurrentFocusedActor = currentFocusedActor;
69     mProposedActorToFocus = proposedActorToFocus;
70     mDirection = direction;
71
72     return mProposedActorToFocus;
73   }
74
75   void Reset()
76   {
77     mInterfaceVerified = false;
78     mCurrentFocusedActor = Actor();
79     mProposedActorToFocus = Actor();
80     mDirection = Control::KeyboardFocus::LEFT;
81   }
82
83   bool& mInterfaceVerified;
84   Actor mCurrentFocusedActor;
85   Actor mProposedActorToFocus;
86   Control::KeyboardFocus::Direction mDirection;
87 };
88
89 // Functors to test whether PreFocusChange signal is emitted when the keyboard focus is about to change
90 class PreFocusChangeCallback : public Dali::ConnectionTracker
91 {
92 public:
93   PreFocusChangeCallback(bool& signalReceived)
94   : mSignalVerified(signalReceived),
95     mCurrentFocusedActor(),
96     mProposedActorToFocus(),
97     mDirection(Control::KeyboardFocus::LEFT)
98   {
99   }
100
101   Actor Callback(Actor currentFocusedActor, Actor proposedActorToFocus, Control::KeyboardFocus::Direction direction)
102   {
103     tet_infoline("Verifying PreFocusChangeCallback()");
104
105     mSignalVerified = true;
106
107     mCurrentFocusedActor = currentFocusedActor;
108     mProposedActorToFocus = proposedActorToFocus;
109     mDirection = direction;
110
111     return mProposedActorToFocus;
112   }
113
114   void Reset()
115   {
116     mSignalVerified = false;
117     mCurrentFocusedActor = Actor();
118     mProposedActorToFocus = Actor();
119     mDirection = Control::KeyboardFocus::LEFT;
120   }
121
122   bool& mSignalVerified;
123   Actor mCurrentFocusedActor;
124   Actor mProposedActorToFocus;
125   Control::KeyboardFocus::Direction mDirection;
126 };
127
128 // Functors to test whether focus changed signal is emitted when the keyboard focus is changed
129 class FocusChangedCallback : public Dali::ConnectionTracker
130 {
131 public:
132   FocusChangedCallback(bool& signalReceived)
133   : mSignalVerified(signalReceived),
134     mOriginalFocusedActor(),
135     mCurrentFocusedActor()
136   {
137   }
138
139   void Callback(Actor originalFocusedActor, Actor currentFocusedActor)
140   {
141     tet_infoline("Verifying FocusChangedCallback()");
142
143     if(originalFocusedActor == mCurrentFocusedActor)
144     {
145       mSignalVerified = true;
146     }
147
148     mOriginalFocusedActor = originalFocusedActor;
149     mCurrentFocusedActor = currentFocusedActor;
150   }
151
152   void Reset()
153   {
154     mSignalVerified = false;
155   }
156
157   bool& mSignalVerified;
158   Actor mOriginalFocusedActor;
159   Actor mCurrentFocusedActor;
160 };
161
162 // Functors to test whether focus group changed signal is emitted when the keyboard focus group is changed
163 class FocusGroupChangedCallback : public Dali::ConnectionTracker
164 {
165 public:
166   FocusGroupChangedCallback(bool& signalReceived)
167   : mSignalVerified(signalReceived),
168     mCurrentFocusedActor(),
169     mForward(true)
170   {
171   }
172
173   void Callback(Actor currentFocusedActor, bool forward)
174   {
175     tet_infoline("Verifying FocusGroupChangedCallback()");
176
177     mSignalVerified = true;
178
179     mCurrentFocusedActor = currentFocusedActor;
180     mForward = forward;
181   }
182
183   void Reset()
184   {
185     mSignalVerified = false;
186   }
187
188   bool& mSignalVerified;
189   Actor mCurrentFocusedActor;
190   bool mForward;
191 };
192
193 // Functors to test whether focused actor activated signal is emitted when the focused actor is activated
194 class FocusedActorActivatedCallback : public Dali::ConnectionTracker
195 {
196 public:
197   FocusedActorActivatedCallback(bool& signalReceived)
198   : mSignalVerified(signalReceived),
199     mActivatedActor()
200   {
201   }
202
203   void Callback(Actor activatedActor)
204   {
205     tet_infoline("Verifying FocusedActorActivatedCallback()");
206
207     mSignalVerified = true;
208
209     mActivatedActor = activatedActor;
210   }
211
212   void Reset()
213   {
214     mSignalVerified = false;
215   }
216
217   bool& mSignalVerified;
218   Actor mActivatedActor;
219 };
220
221 // Used to connect to signals via the ConnectSignal Handle method
222 struct CallbackFunctor
223 {
224   CallbackFunctor()
225   {
226   }
227
228   void operator()()
229   {
230   }
231 };
232
233 } // namespace
234
235 int UtcDaliKeyboardFocusManagerGet(void)
236 {
237   ToolkitTestApplication application;
238
239   tet_infoline(" UtcDaliKeyboardKeyboardFocusManagerGet");
240
241   // Register Type
242   TypeInfo type;
243   type = TypeRegistry::Get().GetTypeInfo( "KeyboardFocusManager" );
244   DALI_TEST_CHECK( type );
245   BaseHandle handle = type.CreateInstance();
246   DALI_TEST_CHECK( handle );
247
248   KeyboardFocusManager manager;
249
250   manager = KeyboardFocusManager::Get();
251   DALI_TEST_CHECK(manager);
252
253   KeyboardFocusManager newManager = KeyboardFocusManager::Get();
254   DALI_TEST_CHECK(newManager);
255
256   // Check that focus manager is a singleton
257   DALI_TEST_CHECK(manager == newManager);
258   END_TEST;
259 }
260
261 int UtcDaliKeyboardFocusManagerSetAndGetCurrentFocusActor(void)
262 {
263   ToolkitTestApplication application;
264
265   tet_infoline(" UtcDaliKeyboardFocusManagerSetAndGetCurrentFocusActor");
266
267   KeyboardFocusManager manager = KeyboardFocusManager::Get();
268   DALI_TEST_CHECK(manager);
269
270   // Create the first actor and add it to the stage
271   Actor first = Actor::New();
272   first.SetKeyboardFocusable(true);
273   Stage::GetCurrent().Add(first);
274
275   // Create the second actor and add it to the stage
276   Actor second = Actor::New();
277   second.SetKeyboardFocusable(true);
278   Stage::GetCurrent().Add(second);
279
280   // Create the third actor but don't add it to the stage
281   Actor third = Actor::New();
282
283   // Check that no actor is being focused yet.
284   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
285
286   // Check that it will fail to set focus on an invalid actor
287   DALI_TEST_CHECK(manager.SetCurrentFocusActor(Actor()) == false);
288
289   // Check that the focus is set on the first actor
290   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
291   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
292
293   // Check that the focus is set on the second actor
294   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
295   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
296
297   // Check that it will fail to set focus on the third actor as it's not in the stage
298   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == false);
299   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
300
301   // Add the third actor to the stage
302   Stage::GetCurrent().Add(third);
303
304   // Check that it will fail to set focus on the third actor as it's not focusable
305   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == false);
306   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
307
308   // Make the third actor focusable
309   third.SetKeyboardFocusable(true);
310
311   // Check that the focus is successfully moved to the third actor
312   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == true);
313   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
314   END_TEST;
315 }
316
317 int UtcDaliKeyboardFocusManagerMoveFocus(void)
318 {
319   ToolkitTestApplication application;
320
321   tet_infoline(" UtcDaliKeyboardFocusManagerMoveFocus");
322
323   // Register Type
324   TypeInfo type;
325   type = TypeRegistry::Get().GetTypeInfo( "KeyboardFocusManager" );
326   DALI_TEST_CHECK( type );
327   BaseHandle handle = type.CreateInstance();
328   DALI_TEST_CHECK( handle );
329
330   KeyboardFocusManager manager = KeyboardFocusManager::Get();
331   DALI_TEST_CHECK(manager);
332
333   bool preFocusChangeSignalVerified = false;
334   PreFocusChangeCallback preFocusChangeCallback(preFocusChangeSignalVerified);
335   manager.PreFocusChangeSignal().Connect( &preFocusChangeCallback, &PreFocusChangeCallback::Callback );
336
337   bool focusChangedSignalVerified = false;
338   FocusChangedCallback focusChangedCallback(focusChangedSignalVerified);
339   manager.FocusChangedSignal().Connect( &focusChangedCallback, &FocusChangedCallback::Callback );
340
341   // Create the first actor and add it to the stage
342   Actor first = Actor::New();
343   first.SetKeyboardFocusable(true);
344   Stage::GetCurrent().Add(first);
345
346   // Create the second actor and add it to the stage
347   Actor second = Actor::New();
348   second.SetKeyboardFocusable(true);
349   Stage::GetCurrent().Add(second);
350
351   // Move the focus to the right
352   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::RIGHT) == false);
353
354   // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
355   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
356   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == Actor());
357   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
358   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::RIGHT);
359   preFocusChangeCallback.Reset();
360
361   // Check that the focus is set on the first actor
362   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
363   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
364   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
365   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == Actor());
366   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
367   focusChangedCallback.Reset();
368
369   // Move the focus towards right
370   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::RIGHT) == false);
371
372   // Because no layout control in the stage and the first actor is focused, it should emit the PreFocusChange signal
373   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
374   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == first);
375   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
376   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::RIGHT);
377   preFocusChangeCallback.Reset();
378
379   // Check that the focus is set on the second actor
380   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
381   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
382   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
383   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
384   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == second);
385   focusChangedCallback.Reset();
386
387   // Move the focus towards up
388   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::UP) == false);
389
390   // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
391   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
392   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == second);
393   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
394   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::UP);
395   preFocusChangeCallback.Reset();
396   DALI_TEST_CHECK(!focusChangedCallback.mSignalVerified);
397
398   // Create a 2x2 table view and try to move focus inside it
399   TableView tableView = TableView::New( 2, 2 );
400   Stage::GetCurrent().Add(tableView);
401
402   // Create the third actor
403   Actor third = Actor::New();
404   third.SetKeyboardFocusable(true);
405
406   // Create the fourth actor
407   Actor fourth = Actor::New();
408   fourth.SetKeyboardFocusable(true);
409
410   // Add the four children to table view
411   tableView.AddChild(first, TableView::CellPosition(0, 0));
412   tableView.AddChild(second, TableView::CellPosition(0, 1));
413   tableView.AddChild(third, TableView::CellPosition(1, 0));
414   tableView.AddChild(fourth, TableView::CellPosition(1, 1));
415
416   // Set the focus to the first actor
417   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
418   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
419   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
420   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == second);
421   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
422   focusChangedCallback.Reset();
423
424   // Move the focus towards right
425   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::RIGHT) == true);
426   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
427   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
428   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
429   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == second);
430   focusChangedCallback.Reset();
431
432   // Move the focus towards down
433   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::DOWN) == true);
434   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == fourth);
435   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
436   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == second);
437   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == fourth);
438   focusChangedCallback.Reset();
439
440   // Move the focus towards left
441   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::LEFT) == true);
442   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
443   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
444   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == fourth);
445   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == third);
446   focusChangedCallback.Reset();
447
448   // Move the focus towards up
449   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::UP) == true);
450   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
451   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
452   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == third);
453   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
454   focusChangedCallback.Reset();
455
456   // Move the focus towards left. The focus move will fail as no way to move it upwards
457   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::LEFT) == false);
458   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
459   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
460   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == first);
461   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
462   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::LEFT);
463   preFocusChangeCallback.Reset();
464   DALI_TEST_CHECK(!focusChangedCallback.mSignalVerified);
465
466   // Enable the loop
467   manager.SetFocusGroupLoop(true);
468   DALI_TEST_CHECK(manager.GetFocusGroupLoop() == true);
469
470   // Move the focus towards left again. The focus should move to the fourth actor.
471   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::LEFT) == true);
472   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == fourth);
473   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
474   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
475   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == fourth);
476   focusChangedCallback.Reset();
477   END_TEST;
478 }
479
480 int UtcDaliKeyboardFocusManagerCustomAlgorithmMoveFocus(void)
481 {
482   ToolkitTestApplication application;
483
484   tet_infoline(" UtcDaliKeyboardFocusManagerCustomAlgorithmMoveFocus");
485
486   // Register Type
487   TypeInfo type;
488   type = TypeRegistry::Get().GetTypeInfo( "KeyboardFocusManager" );
489   DALI_TEST_CHECK( type );
490   BaseHandle handle = type.CreateInstance();
491   DALI_TEST_CHECK( handle );
492
493   KeyboardFocusManager manager = KeyboardFocusManager::Get();
494   DALI_TEST_CHECK(manager);
495
496   bool preFocusChangeSignalVerified = false;
497   PreFocusChangeCallback preFocusChangeCallback(preFocusChangeSignalVerified);
498   manager.PreFocusChangeSignal().Connect( &preFocusChangeCallback, &PreFocusChangeCallback::Callback );
499
500   bool focusChangedSignalVerified = false;
501   FocusChangedCallback focusChangedCallback(focusChangedSignalVerified);
502   manager.FocusChangedSignal().Connect( &focusChangedCallback, &FocusChangedCallback::Callback );
503
504   // Create the first actor and add it to the stage
505   Actor first = Actor::New();
506   first.SetKeyboardFocusable(true);
507   Stage::GetCurrent().Add(first);
508
509   // Create the second actor and add it to the stage
510   Actor second = Actor::New();
511   second.SetKeyboardFocusable(true);
512   Stage::GetCurrent().Add(second);
513
514   // Move the focus to the right
515   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::RIGHT) == false);
516
517   // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
518   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
519   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == Actor());
520   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
521   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::RIGHT);
522   preFocusChangeCallback.Reset();
523
524   bool customAlgorithmInterfaceVerified = false;
525   CustomAlgorithm customAlgorithm(customAlgorithmInterfaceVerified);
526   Toolkit::DevelKeyboardFocusManager::SetCustomAlgorithm(manager, customAlgorithm);
527
528   // Move the focus towards right
529   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::RIGHT) == false);
530
531   // Because no layout control in the stage and the first actor is focused, it should invoke CustomAlgorithm
532   DALI_TEST_CHECK(customAlgorithm.mInterfaceVerified);
533   DALI_TEST_CHECK(customAlgorithm.mCurrentFocusedActor == Actor());
534   DALI_TEST_CHECK(customAlgorithm.mProposedActorToFocus == Actor());
535   DALI_TEST_CHECK(customAlgorithm.mDirection == Control::KeyboardFocus::RIGHT);
536   customAlgorithm.Reset();
537
538   // Check that the focus is set on the first actor
539   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
540   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
541   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
542   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == Actor());
543   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
544   focusChangedCallback.Reset();
545
546   // Move the focus towards right
547   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::RIGHT) == false);
548
549   // Because no layout control in the stage and the first actor is focused, it should invoke CustomAlgorithm
550   DALI_TEST_CHECK(customAlgorithm.mInterfaceVerified);
551   DALI_TEST_CHECK(customAlgorithm.mCurrentFocusedActor == first);
552   DALI_TEST_CHECK(customAlgorithm.mProposedActorToFocus == Actor());
553   DALI_TEST_CHECK(customAlgorithm.mDirection == Control::KeyboardFocus::RIGHT);
554   customAlgorithm.Reset();
555
556   // Check that the focus is set on the second actor
557   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
558   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
559   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
560   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
561   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == second);
562   focusChangedCallback.Reset();
563
564   // Move the focus towards up
565   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::UP) == false);
566
567   // Because no layout control in the stage and no actor is focused, it should invoke CustomAlgorithm
568   DALI_TEST_CHECK(customAlgorithm.mInterfaceVerified);
569   DALI_TEST_CHECK(customAlgorithm.mCurrentFocusedActor == second);
570   DALI_TEST_CHECK(customAlgorithm.mProposedActorToFocus == Actor());
571   DALI_TEST_CHECK(customAlgorithm.mDirection == Control::KeyboardFocus::UP);
572   customAlgorithm.Reset();
573   DALI_TEST_CHECK(!focusChangedCallback.mSignalVerified);
574
575   END_TEST;
576 }
577 int UtcDaliKeyboardFocusManagerFocusablePropertiesMoveFocus(void)
578 {
579   ToolkitTestApplication application;
580
581   tet_infoline(" UtcDaliKeyboardFocusManagerCustomAlgorithmMoveFocus");
582
583   // Register Type
584   TypeInfo type;
585   type = TypeRegistry::Get().GetTypeInfo( "KeyboardFocusManager" );
586   DALI_TEST_CHECK( type );
587   BaseHandle handle = type.CreateInstance();
588   DALI_TEST_CHECK( handle );
589
590   KeyboardFocusManager manager = KeyboardFocusManager::Get();
591   DALI_TEST_CHECK(manager);
592
593   bool focusChangedSignalVerified = false;
594   FocusChangedCallback focusChangedCallback(focusChangedSignalVerified);
595   manager.FocusChangedSignal().Connect( &focusChangedCallback, &FocusChangedCallback::Callback );
596
597   PushButton button1 = PushButton::New();
598   PushButton button2 = PushButton::New();
599   button1.SetKeyboardFocusable(true);
600   button2.SetKeyboardFocusable(true);
601   Stage::GetCurrent().Add(button1);
602   Stage::GetCurrent().Add(button2);
603
604   // Set the focus to the button1
605   DALI_TEST_CHECK(manager.SetCurrentFocusActor(button1) == true);
606   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == button1);
607   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
608   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == Actor());
609   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == button1);
610   focusChangedCallback.Reset();
611
612   // set the navigation properties of button1
613   button1.SetProperty(Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID, Property::Value((int)button2.GetId()));
614   button1.SetProperty(Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID, Property::Value((int)button2.GetId()));
615   button1.SetProperty(Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID, Property::Value((int)button2.GetId()));
616   button1.SetProperty(Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID, Property::Value((int)button2.GetId()));
617
618   // set the navigation properties of button2
619   button2.SetProperty(Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID, Property::Value((int)button1.GetId()));
620   button2.SetProperty(Toolkit::DevelControl::Property::RIGHT_FOCUSABLE_ACTOR_ID, Property::Value((int)button1.GetId()));
621   button2.SetProperty(Toolkit::DevelControl::Property::UP_FOCUSABLE_ACTOR_ID, Property::Value((int)button1.GetId()));
622   button2.SetProperty(Toolkit::DevelControl::Property::DOWN_FOCUSABLE_ACTOR_ID, Property::Value((int)button1.GetId()));
623
624   // Move the focus towards left
625   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::LEFT) == true);
626
627   // Confirm whether focus is moved to button2
628   DALI_TEST_EQUALS(button2.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
629   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
630   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == button1);
631   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == button2);
632   focusChangedCallback.Reset();
633
634   // Move the focus towards right
635   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::RIGHT) == true);
636
637   // Confirm whether focus is moved to button1
638   DALI_TEST_EQUALS(button1.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
639   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
640   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == button2);
641   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == button1);
642   focusChangedCallback.Reset();
643
644   // Move the focus towards up
645   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::UP) == true);
646
647   // Confirm whether focus is moved to button2
648   DALI_TEST_EQUALS(button2.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
649   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
650   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == button1);
651   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == button2);
652   focusChangedCallback.Reset();
653
654   // Move the focus towards down
655   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::DOWN) == true);
656
657   // Confirm whether focus is moved to button1
658   DALI_TEST_EQUALS(button1.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
659   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
660   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == button2);
661   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == button1);
662   focusChangedCallback.Reset();
663
664   // Create a 1x1 table view and try to move focus inside it
665   TableView tableView = TableView::New( 1, 1 );
666   Stage::GetCurrent().Add(tableView);
667
668   PushButton button = PushButton::New();
669   button.SetKeyboardFocusable(true);
670   tableView.AddChild(button, TableView::CellPosition(0, 0));
671
672   // set the navigation properties of button3
673   button.SetProperty(Toolkit::DevelControl::Property::LEFT_FOCUSABLE_ACTOR_ID, Property::Value((int)button1.GetId()));
674
675   // Set the focus to the button
676   DALI_TEST_CHECK(manager.SetCurrentFocusActor(button) == true);
677   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == button);
678   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
679   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == button1);
680   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == button);
681   focusChangedCallback.Reset();
682
683   // Move the focus towards left
684   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::LEFT) == true);
685
686   // Confirm whether focus is moved to button1
687   DALI_TEST_EQUALS(button1.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
688   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
689   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == button);
690   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == button1);
691   focusChangedCallback.Reset();
692
693   END_TEST;
694 }
695
696 int UtcDaliKeyboardFocusManagerClearFocus(void)
697 {
698   ToolkitTestApplication application;
699
700   tet_infoline(" UtcDaliKeyboardFocusManagerClearFocus");
701
702   KeyboardFocusManager manager = KeyboardFocusManager::Get();
703   DALI_TEST_CHECK(manager);
704
705   // Create the first actor and add it to the stage
706   Actor first = Actor::New();
707   first.SetKeyboardFocusable(true);
708   Stage::GetCurrent().Add(first);
709
710   // Create the second actor and add it to the stage
711   Actor second = Actor::New();
712   second.SetKeyboardFocusable(true);
713   Stage::GetCurrent().Add(second);
714
715   // Check that the focus is set on the first actor
716   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
717   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
718
719   // Check that the focus is set on the second actor
720   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
721   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
722
723   // Clear the focus
724   manager.ClearFocus();
725
726   // Check that no actor is being focused now.
727   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
728   END_TEST;
729 }
730
731 int UtcDaliKeyboardFocusManagerSetAndGetFocusGroupLoop(void)
732 {
733   ToolkitTestApplication application;
734
735   tet_infoline(" UtcDaliKeyboardFocusManagerSetAndGetFocusGroupLoop");
736
737   KeyboardFocusManager manager = KeyboardFocusManager::Get();
738   DALI_TEST_CHECK(manager);
739
740   // Check that the focus movement is not looped within the same focus group by default
741   DALI_TEST_CHECK(manager.GetFocusGroupLoop() == false);
742
743   // Enable the loop
744   manager.SetFocusGroupLoop(true);
745   DALI_TEST_CHECK(manager.GetFocusGroupLoop() == true);
746   END_TEST;
747 }
748
749 int UtcDaliKeyboardFocusManagerSetAsFocusGroup(void)
750 {
751   ToolkitTestApplication application;
752
753   tet_infoline(" UtcDaliKeyboardFocusManagerSetAsFocusGroup");
754
755   KeyboardFocusManager manager = KeyboardFocusManager::Get();
756   DALI_TEST_CHECK(manager);
757
758   // Create an actor and check that it is not a focus group by default
759   Actor actor = Actor::New();
760   DALI_TEST_CHECK(manager.IsFocusGroup(actor) == false);
761
762   // Set the actor as focus group
763   manager.SetAsFocusGroup(actor, true);
764
765   // flush the queue and render once
766   application.SendNotification();
767   application.Render();
768
769   DALI_TEST_CHECK(manager.IsFocusGroup(actor) == true);
770
771   // Set the actor not as focus group
772   manager.SetAsFocusGroup(actor, false);
773
774   // flush the queue and render once
775   application.SendNotification();
776   application.Render();
777
778   DALI_TEST_CHECK(manager.IsFocusGroup(actor) == false);
779   END_TEST;
780 }
781
782 int UtcDaliKeyboardFocusManagerGetFocusGroup(void)
783 {
784   ToolkitTestApplication application;
785
786   tet_infoline(" UtcDaliKeyboardFocusManagerGetFocusGroup");
787
788   KeyboardFocusManager manager = KeyboardFocusManager::Get();
789   DALI_TEST_CHECK(manager);
790
791   // Create an actor with two child actors and add it to the stage
792   Actor parent = Actor::New();
793   Actor child = Actor::New();
794   parent.Add(child);
795   Stage::GetCurrent().Add(parent);
796
797   // Create three actors and add them as the children of the first child actor
798   Actor grandChild = Actor::New();
799   child.Add(grandChild);
800
801   // Set the parent and the first child actor as focus groups
802   manager.SetAsFocusGroup(parent, true);
803
804   // flush the queue and render once
805   application.SendNotification();
806   application.Render();
807
808   DALI_TEST_CHECK(manager.IsFocusGroup(parent) == true);
809
810   // The current focus group should be the parent, As it is the immediate parent which is also a focus group.
811   DALI_TEST_CHECK(manager.GetFocusGroup(grandChild) == parent);
812
813   manager.SetAsFocusGroup(child, true);
814
815   // flush the queue and render once
816   application.SendNotification();
817   application.Render();
818
819   DALI_TEST_CHECK(manager.IsFocusGroup(child) == true);
820
821   // The focus group should be the child, As it is the immediate parent which is also a focus group.
822   DALI_TEST_CHECK(manager.GetFocusGroup(grandChild) == child);
823
824   manager.SetAsFocusGroup(grandChild, true);
825
826   // flush the queue and render once
827   application.SendNotification();
828   application.Render();
829
830   DALI_TEST_CHECK(manager.IsFocusGroup(grandChild) == true);
831
832   // The current focus group should be itself, As it is also a focus group.
833   DALI_TEST_CHECK(manager.GetFocusGroup(grandChild) == grandChild);
834   END_TEST;
835 }
836
837 int UtcDaliKeyboardFocusManagerSetAndGetFocusIndicator(void)
838 {
839   ToolkitTestApplication application;
840
841   tet_infoline(" UtcDaliKeyboardFocusManagerSetAndGetFocusIndicator");
842
843   KeyboardFocusManager manager = KeyboardFocusManager::Get();
844   DALI_TEST_CHECK(manager);
845
846   Actor defaultFocusIndicatorActor = manager.GetFocusIndicatorActor();
847   DALI_TEST_CHECK(defaultFocusIndicatorActor);
848
849   Actor newFocusIndicatorActor = Actor::New();
850   manager.SetFocusIndicatorActor(newFocusIndicatorActor);
851   DALI_TEST_CHECK(manager.GetFocusIndicatorActor() == newFocusIndicatorActor);
852   END_TEST;
853 }
854
855
856 int UtcDaliKeyboardFocusManagerSignalFocusedActorActivated(void)
857 {
858   ToolkitTestApplication application;
859
860   tet_infoline(" UtcDaliKeyboardFocusManagerSignalFocusedActorActivated");
861
862   KeyboardFocusManager manager = KeyboardFocusManager::Get();
863   DALI_TEST_CHECK(manager);
864
865   bool focusedActorActivatedSignalVerified = false;
866   FocusedActorActivatedCallback focusedActorActivatedCallback(focusedActorActivatedSignalVerified);
867   manager.FocusedActorEnterKeySignal().Connect( &focusedActorActivatedCallback, &FocusedActorActivatedCallback::Callback );
868
869   Integration::KeyEvent returnEvent("Return", "", 0, 0, 0, Integration::KeyEvent::Up, DEFAULT_DEVICE_NAME );
870
871   // Press Any key to notice physical keyboard event is comming to KeyboardFocusManager
872   // It makes mIsFocusIndicatorEnabled true
873   application.ProcessEvent(returnEvent);
874
875   // Create the first button and add it to the stage
876   PushButton firstPushButton = PushButton::New();
877   firstPushButton.SetKeyboardFocusable(true);
878   Stage::GetCurrent().Add(firstPushButton);
879
880   // Create the second button and add it to the stage
881   PushButton secondPushButton = PushButton::New();
882   secondPushButton.SetKeyboardFocusable(true);
883   Stage::GetCurrent().Add(secondPushButton);
884
885   // Check that the focus is set on the first button
886   DALI_TEST_CHECK(manager.SetCurrentFocusActor(firstPushButton) == true);
887   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == firstPushButton);
888
889   // Send the return event to activate the first button
890   application.ProcessEvent(returnEvent);
891   DALI_TEST_CHECK(focusedActorActivatedCallback.mSignalVerified);
892   DALI_TEST_CHECK(focusedActorActivatedCallback.mActivatedActor == firstPushButton);
893   focusedActorActivatedCallback.Reset();
894
895   // Check that the focus is set on the second button
896   DALI_TEST_CHECK(manager.SetCurrentFocusActor(secondPushButton) == true);
897   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == secondPushButton);
898
899   // Send the return event again to activate the second button
900   application.ProcessEvent(returnEvent);
901   DALI_TEST_CHECK(focusedActorActivatedCallback.mSignalVerified);
902   DALI_TEST_CHECK(focusedActorActivatedCallback.mActivatedActor == secondPushButton);
903   focusedActorActivatedCallback.Reset();
904   END_TEST;
905 }
906
907 int UtcDaliKeyboardFocusManagerSignalFocusGroupChanged(void)
908 {
909   ToolkitTestApplication application;
910
911   tet_infoline(" UtcDaliKeyboardFocusManagerSignalFocusGroupChanged");
912
913   // Register Type
914   TypeInfo type;
915   type = TypeRegistry::Get().GetTypeInfo( "KeyboardFocusManager" );
916   DALI_TEST_CHECK( type );
917   BaseHandle handle = type.CreateInstance();
918   DALI_TEST_CHECK( handle );
919
920   KeyboardFocusManager manager = KeyboardFocusManager::Get();
921   DALI_TEST_CHECK(manager);
922
923   bool focusGroupChangedSignalVerified = false;
924   FocusGroupChangedCallback focusGroupChangedCallback(focusGroupChangedSignalVerified);
925   manager.FocusGroupChangedSignal().Connect( &focusGroupChangedCallback, &FocusGroupChangedCallback::Callback );
926
927   Integration::KeyEvent tabEvent("Tab", "", 0, 0, 0, Integration::KeyEvent::Down, DEFAULT_DEVICE_NAME );
928   Integration::KeyEvent shiftTabEvent("Tab", "", 0, 1, 0, Integration::KeyEvent::Down, DEFAULT_DEVICE_NAME);
929
930   // Press Any key to notice physical keyboard event is comming to KeyboardFocusManager
931   // It makes mIsFocusIndicatorEnabled true
932   application.ProcessEvent(tabEvent);
933
934   // Send the tab event to change focus group in the forward direction
935   application.ProcessEvent(tabEvent);
936   DALI_TEST_CHECK(focusGroupChangedCallback.mSignalVerified);
937   DALI_TEST_CHECK(focusGroupChangedCallback.mCurrentFocusedActor == Actor());
938   DALI_TEST_CHECK(focusGroupChangedCallback.mForward == true);
939   focusGroupChangedCallback.Reset();
940
941   // Send the shift tab event to change focus group in the backward direction
942   application.ProcessEvent(shiftTabEvent);
943   DALI_TEST_CHECK(focusGroupChangedCallback.mSignalVerified);
944   DALI_TEST_CHECK(focusGroupChangedCallback.mCurrentFocusedActor == Actor());
945   DALI_TEST_CHECK(focusGroupChangedCallback.mForward == false);
946   focusGroupChangedCallback.Reset();
947   END_TEST;
948 }
949
950 int UtcDaliKeyboardFocusManagerSignals(void)
951 {
952   ToolkitTestApplication application;
953
954   KeyboardFocusManager manager = KeyboardFocusManager::Get();
955   DALI_TEST_CHECK( manager );
956
957   ConnectionTracker* testTracker = new ConnectionTracker();
958   DALI_TEST_EQUALS( true, manager.ConnectSignal( testTracker, "keyboardPreFocusChange", CallbackFunctor() ), TEST_LOCATION );
959   DALI_TEST_EQUALS( true, manager.ConnectSignal( testTracker, "keyboardFocusChanged", CallbackFunctor() ), TEST_LOCATION );
960   DALI_TEST_EQUALS( true, manager.ConnectSignal( testTracker, "keyboardFocusGroupChanged", CallbackFunctor() ), TEST_LOCATION );
961   DALI_TEST_EQUALS( true, manager.ConnectSignal( testTracker, "keyboardFocusedActorEnterKey", CallbackFunctor() ), TEST_LOCATION );
962
963   END_TEST;
964 }
965
966 int UtcDaliKeyboardFocusManagerMoveFocusBackward(void)
967 {
968   ToolkitTestApplication application;
969
970   tet_infoline(" UtcDaliKeyboardFocusManagerMoveFocusBackward");
971
972   KeyboardFocusManager manager = KeyboardFocusManager::Get();
973   DALI_TEST_CHECK(manager);
974
975   // Make history stack full
976   for(int i = 0 ; i < 31 ; i ++)
977   {
978     Actor actor = Actor::New();
979     actor.SetKeyboardFocusable(true);
980     Stage::GetCurrent().Add(actor);
981     manager.SetCurrentFocusActor(actor);
982   }
983
984   // Create the first actor and add it to the stage
985   Actor first = Actor::New();
986   first.SetKeyboardFocusable(true);
987   Stage::GetCurrent().Add(first);
988
989   // Create the second actor and add it to the stage
990   Actor second = Actor::New();
991   second.SetKeyboardFocusable(true);
992   Stage::GetCurrent().Add(second);
993
994   // Create the second actor and add it to the stage
995   Actor third = Actor::New();
996   third.SetKeyboardFocusable(true);
997   Stage::GetCurrent().Add(third);
998
999   // Check that the focus is set on the second actor
1000   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
1001   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1002
1003   // Check that the focus is set on the second actor
1004   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
1005   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
1006
1007   // Check that the focus is set on the third  actor
1008   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == true);
1009   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
1010
1011   // Move the focus backward
1012   manager.MoveFocusBackward();
1013
1014   // Check that it current focused actor is second actor
1015   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
1016
1017   // Check that the focus is set on the third actor
1018   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == true);
1019   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
1020
1021   // Remove the second actor on stage
1022   second.Unparent();
1023
1024   // Move the focus backward
1025   manager.MoveFocusBackward();
1026
1027   // Check that it current focused actor is first actor
1028   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1029
1030   END_TEST;
1031 }
1032
1033 int UtcDaliKeyboardFocusManagerChangeFocusDirectionByKeyEvents(void)
1034 {
1035   ToolkitTestApplication application;
1036
1037   tet_infoline(" UtcDaliKeyboardFocusManagerChangeFocusDirectionByKeyEvents");
1038
1039   KeyboardFocusManager manager = KeyboardFocusManager::Get();
1040   DALI_TEST_CHECK(manager);
1041
1042   bool preFocusChangeSignalVerified = false;
1043   PreFocusChangeCallback preFocusChangeCallback(preFocusChangeSignalVerified);
1044   manager.PreFocusChangeSignal().Connect( &preFocusChangeCallback, &PreFocusChangeCallback::Callback );
1045
1046   bool focusChangedSignalVerified = false;
1047   FocusChangedCallback focusChangedCallback(focusChangedSignalVerified);
1048   manager.FocusChangedSignal().Connect( &focusChangedCallback, &FocusChangedCallback::Callback );
1049
1050   Integration::KeyEvent leftEvent("Left", "", 0, 0, 0, Integration::KeyEvent::Down, DEFAULT_DEVICE_NAME);
1051   Integration::KeyEvent rightEvent("Right", "", 0, 0, 0, Integration::KeyEvent::Down, DEFAULT_DEVICE_NAME );
1052   Integration::KeyEvent upEvent("Up", "", 0, 0, 0, Integration::KeyEvent::Down, DEFAULT_DEVICE_NAME );
1053   Integration::KeyEvent downEvent("Down", "", 0, 0, 0, Integration::KeyEvent::Down, DEFAULT_DEVICE_NAME );
1054   Integration::KeyEvent pageUpEvent("Prior", "", 0, 0, 0, Integration::KeyEvent::Down, DEFAULT_DEVICE_NAME );
1055   Integration::KeyEvent pageDownEvent("Next", "", 0, 0, 0, Integration::KeyEvent::Down, DEFAULT_DEVICE_NAME );
1056
1057   // Press Any key to notice physical keyboard event is comming to KeyboardFocusManager
1058   // It makes mIsFocusIndicatorEnabled true
1059   application.ProcessEvent(leftEvent);
1060
1061   // Create a 2x2 table view and try to move focus inside it
1062   TableView tableView = TableView::New( 2, 2 );
1063   Stage::GetCurrent().Add(tableView);
1064
1065   // Create the first actor
1066   Actor first = Actor::New();
1067   first.SetKeyboardFocusable(true);
1068
1069   // Create the second actor
1070   Actor second = Actor::New();
1071   second.SetKeyboardFocusable(true);
1072
1073   // Create the third actor
1074   Actor third = Actor::New();
1075   third.SetKeyboardFocusable(true);
1076
1077   // Create the fourth actor
1078   Actor fourth = Actor::New();
1079   fourth.SetKeyboardFocusable(true);
1080
1081   // Add the four children to table view
1082   tableView.AddChild(first, TableView::CellPosition(0, 0));
1083   tableView.AddChild(second, TableView::CellPosition(0, 1));
1084   tableView.AddChild(third, TableView::CellPosition(1, 0));
1085   tableView.AddChild(fourth, TableView::CellPosition(1, 1));
1086
1087   // Set the focus to the first actor
1088   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
1089   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1090   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1091   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == Actor());
1092   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
1093   focusChangedCallback.Reset();
1094
1095   // Send the right key event to move the focus towards right
1096   application.ProcessEvent(rightEvent);
1097   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
1098   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1099   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
1100   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == second);
1101   focusChangedCallback.Reset();
1102
1103   // Send the down key event to move the focus towards down
1104   application.ProcessEvent(downEvent);
1105   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == fourth);
1106   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1107   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == second);
1108   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == fourth);
1109   focusChangedCallback.Reset();
1110
1111   // Send the down event to move the focus towards left
1112   application.ProcessEvent(leftEvent);
1113   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
1114   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1115   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == fourth);
1116   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == third);
1117   focusChangedCallback.Reset();
1118
1119   // Send the up event to move the focus towards up
1120   application.ProcessEvent(upEvent);
1121   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1122   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1123   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == third);
1124   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
1125   focusChangedCallback.Reset();
1126
1127   // Send the pape up event, but focus should not be moved because page up is not supported by table view
1128   application.ProcessEvent(pageUpEvent);
1129   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1130   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
1131   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == first);
1132   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == first);
1133   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::PAGE_UP);
1134   preFocusChangeCallback.Reset();
1135
1136   // Send the pape down event, but focus should not be moved because page down is not supported by table view
1137   application.ProcessEvent(pageDownEvent);
1138   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1139   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
1140   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == first);
1141   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == first);
1142   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::PAGE_DOWN);
1143   preFocusChangeCallback.Reset();
1144
1145   // Clear the focus
1146   manager.ClearFocus();
1147
1148   // Send the pape up event, but nothing was focued so focus manager will try the initial focus
1149   preFocusChangeCallback.Reset();
1150   application.ProcessEvent(pageUpEvent);
1151   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
1152   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
1153   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == Actor());
1154   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
1155   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::RIGHT);
1156
1157   // Clear the focus again
1158   manager.ClearFocus();
1159
1160   // Send the pape down event, but nothing was focued so focus manager will try the initial focus
1161   preFocusChangeCallback.Reset();
1162   application.ProcessEvent(pageDownEvent);
1163   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
1164   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
1165   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == Actor());
1166   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
1167   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::RIGHT);
1168
1169   END_TEST;
1170 }
1171
1172
1173
1174
1175
1176 int UtcDaliKeyboardFocusManagerMoveFocusTestStateChange(void)
1177 {
1178   ToolkitTestApplication application;
1179
1180   tet_infoline(" UtcDaliKeyboardFocusManagerMoveFocusTestStateChange");
1181
1182   // Register Type
1183   TypeInfo type;
1184   type = TypeRegistry::Get().GetTypeInfo( "KeyboardFocusManager" );
1185   DALI_TEST_CHECK( type );
1186   BaseHandle handle = type.CreateInstance();
1187   DALI_TEST_CHECK( handle );
1188
1189   KeyboardFocusManager manager = KeyboardFocusManager::Get();
1190   DALI_TEST_CHECK(manager);
1191
1192   bool preFocusChangeSignalVerified = false;
1193   PreFocusChangeCallback preFocusChangeCallback(preFocusChangeSignalVerified);
1194   manager.PreFocusChangeSignal().Connect( &preFocusChangeCallback, &PreFocusChangeCallback::Callback );
1195
1196   bool focusChangedSignalVerified = false;
1197   FocusChangedCallback focusChangedCallback(focusChangedSignalVerified);
1198   manager.FocusChangedSignal().Connect( &focusChangedCallback, &FocusChangedCallback::Callback );
1199
1200   // Create the first actor and add it to the stage
1201   Control first = Control::New();
1202   first.SetKeyboardFocusable(true);
1203   Stage::GetCurrent().Add(first);
1204
1205   // Create the second actor and add it to the stage
1206   Control second = Control::New();
1207   second.SetKeyboardFocusable(true);
1208   Stage::GetCurrent().Add(second);
1209
1210   // Move the focus to the right
1211   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::RIGHT) == false);
1212
1213   // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
1214   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
1215   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == Actor());
1216   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
1217   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::RIGHT);
1218   preFocusChangeCallback.Reset();
1219
1220   // Check that the focus is set on the first actor
1221   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
1222   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1223   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1224   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == Actor());
1225   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
1226   DALI_TEST_EQUALS(first.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
1227   focusChangedCallback.Reset();
1228
1229   // Move the focus towards right
1230   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::RIGHT) == false);
1231
1232   // Because no layout control in the stage and the first actor is focused, it should emit the PreFocusChange signal
1233   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
1234   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == first);
1235   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
1236   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::RIGHT);
1237   preFocusChangeCallback.Reset();
1238
1239   // Check that the focus is set on the second actor
1240   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
1241   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
1242   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1243   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
1244   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == second);
1245   DALI_TEST_EQUALS(first.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1246   DALI_TEST_EQUALS(second.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
1247   focusChangedCallback.Reset();
1248
1249   // Move the focus towards up
1250   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::UP) == false);
1251
1252   // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
1253   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
1254   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == second);
1255   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
1256   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::UP);
1257   preFocusChangeCallback.Reset();
1258   DALI_TEST_CHECK(!focusChangedCallback.mSignalVerified);
1259
1260   // Create a 2x2 table view and try to move focus inside it
1261   TableView tableView = TableView::New( 2, 2 );
1262   Stage::GetCurrent().Add(tableView);
1263
1264   // Create the third actor
1265   Control third = Control::New();
1266   third.SetKeyboardFocusable(true);
1267
1268   // Create the fourth actor
1269   Control fourth = Control::New();
1270   fourth.SetKeyboardFocusable(true);
1271
1272   // Add the four children to table view
1273   tableView.AddChild(first, TableView::CellPosition(0, 0));
1274   tableView.AddChild(second, TableView::CellPosition(0, 1));
1275   tableView.AddChild(third, TableView::CellPosition(1, 0));
1276   tableView.AddChild(fourth, TableView::CellPosition(1, 1));
1277
1278   // Set the focus to the first actor
1279   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
1280   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1281   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1282   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == second);
1283   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
1284
1285   DALI_TEST_EQUALS(first.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
1286   DALI_TEST_EQUALS(second.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1287
1288   focusChangedCallback.Reset();
1289
1290   // Move the focus towards right
1291   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::RIGHT) == true);
1292   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
1293   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1294   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
1295   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == second);
1296   DALI_TEST_EQUALS(first.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1297   DALI_TEST_EQUALS(second.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
1298
1299   focusChangedCallback.Reset();
1300
1301   // Move the focus towards down
1302   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::DOWN) == true);
1303   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == fourth);
1304   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1305   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == second);
1306   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == fourth);
1307
1308   DALI_TEST_EQUALS(first.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1309   DALI_TEST_EQUALS(second.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1310   DALI_TEST_EQUALS(third.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1311   DALI_TEST_EQUALS(fourth.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
1312
1313   focusChangedCallback.Reset();
1314
1315   // Move the focus towards left
1316   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::LEFT) == true);
1317   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
1318   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1319   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == fourth);
1320   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == third);
1321
1322   DALI_TEST_EQUALS(first.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1323   DALI_TEST_EQUALS(second.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1324   DALI_TEST_EQUALS(third.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
1325   DALI_TEST_EQUALS(fourth.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1326
1327   focusChangedCallback.Reset();
1328
1329   // Move the focus towards up
1330   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::UP) == true);
1331   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1332   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1333   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == third);
1334   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
1335   DALI_TEST_EQUALS(first.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
1336   DALI_TEST_EQUALS(second.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1337   DALI_TEST_EQUALS(third.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1338   DALI_TEST_EQUALS(fourth.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1339   focusChangedCallback.Reset();
1340
1341   // Move the focus towards left. The focus move will fail as no way to move it upwards
1342   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::LEFT) == false);
1343   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1344   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
1345   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == first);
1346   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
1347   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::KeyboardFocus::LEFT);
1348   DALI_TEST_EQUALS(first.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
1349   DALI_TEST_EQUALS(second.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1350   DALI_TEST_EQUALS(third.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1351   DALI_TEST_EQUALS(fourth.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1352
1353   preFocusChangeCallback.Reset();
1354   DALI_TEST_CHECK(!focusChangedCallback.mSignalVerified);
1355
1356   // Enable the loop
1357   manager.SetFocusGroupLoop(true);
1358   DALI_TEST_CHECK(manager.GetFocusGroupLoop() == true);
1359
1360   // Move the focus towards left again. The focus should move to the fourth actor.
1361   DALI_TEST_CHECK(manager.MoveFocus(Control::KeyboardFocus::LEFT) == true);
1362   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == fourth);
1363   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
1364   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
1365   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == fourth);
1366
1367   DALI_TEST_EQUALS(first.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1368   DALI_TEST_EQUALS(second.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1369   DALI_TEST_EQUALS(third.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1370   DALI_TEST_EQUALS(fourth.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::FOCUSED, TEST_LOCATION );
1371
1372   focusChangedCallback.Reset();
1373
1374   // Clear the focus
1375   manager.ClearFocus();
1376   DALI_TEST_EQUALS(first.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1377   DALI_TEST_EQUALS(second.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1378   DALI_TEST_EQUALS(third.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1379   DALI_TEST_EQUALS(fourth.GetProperty<int>(DevelControl::Property::STATE), (int)DevelControl::NORMAL, TEST_LOCATION );
1380
1381
1382   END_TEST;
1383 }