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