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