License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-FocusManager.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <iostream>
19 #include <stdlib.h>
20
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/dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27
28 using namespace Dali;
29 using namespace Toolkit;
30
31
32 void utc_dali_toolkit_focus_manager_startup(void)
33 {
34   test_return_value = TET_UNDEF;
35 }
36
37 void utc_dali_toolkit_focus_manager_cleanup(void)
38 {
39   test_return_value = TET_PASS;
40 }
41
42
43 namespace
44 {
45
46 static bool gObjectCreatedCallBackCalled;
47
48 static void TestCallback(BaseHandle handle)
49 {
50   gObjectCreatedCallBackCalled = true;
51 }
52
53 // Functors to test whether focus changed signal is emitted when the focus is changed
54 class FocusChangedCallback : public Dali::ConnectionTracker
55 {
56 public:
57   FocusChangedCallback(bool& signalReceived)
58   : mSignalVerified(signalReceived),
59     mOriginalFocusedActor(),
60     mCurrentFocusedActor()
61   {
62   }
63
64   void Callback(Actor originalFocusedActor, Actor currentFocusedActor)
65   {
66     tet_infoline("Verifying FocusChangedCallback()");
67
68     if(originalFocusedActor == mCurrentFocusedActor)
69     {
70       mSignalVerified = true;
71     }
72
73     mOriginalFocusedActor = originalFocusedActor;
74     mCurrentFocusedActor = currentFocusedActor;
75   }
76
77   void Reset()
78   {
79     mSignalVerified = false;
80   }
81
82   bool& mSignalVerified;
83   Actor mOriginalFocusedActor;
84   Actor mCurrentFocusedActor;
85 };
86
87 // Functors to test whether focus overshot signal is emitted when there is no way to move focus further.
88 class FocusOvershotCallback : public Dali::ConnectionTracker
89 {
90 public:
91   FocusOvershotCallback(bool& signalReceived)
92   : mSignalVerified(signalReceived),
93     mCurrentFocusedActor(),
94     mFocusOvershotDirection(Toolkit::FocusManager::OVERSHOT_NEXT)
95   {
96   }
97
98   void Callback(Actor currentFocusedActor, Toolkit::FocusManager::FocusOvershotDirection direction)
99   {
100     tet_infoline("Verifying FocusOvershotCallback()");
101
102     if(currentFocusedActor == mCurrentFocusedActor && direction == mFocusOvershotDirection)
103     {
104       mSignalVerified = true;
105     }
106   }
107
108   void Reset()
109   {
110     mSignalVerified = false;
111   }
112
113   bool& mSignalVerified;
114   Actor mCurrentFocusedActor;
115   Toolkit::FocusManager::FocusOvershotDirection mFocusOvershotDirection;
116 };
117
118 } // namespace
119
120
121 int UtcDaliFocusManagerGet(void)
122 {
123   ToolkitTestApplication application;
124
125   tet_infoline(" UtcDaliFocusManagerGet");
126
127   FocusManager manager;
128
129   //Ensure object is created by checking if it's registered
130   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
131   DALI_TEST_CHECK(registry);
132
133   gObjectCreatedCallBackCalled = false;
134   registry.ObjectCreatedSignal().Connect( &TestCallback );
135   {
136     manager = FocusManager::Get();
137     DALI_TEST_CHECK(manager);
138   }
139   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
140
141   FocusManager newManager = FocusManager::Get();
142   DALI_TEST_CHECK(newManager);
143
144   // Check that focus manager is a singleton
145   DALI_TEST_CHECK(manager == newManager);
146   END_TEST;
147 }
148
149 int UtcDaliFocusManagerSetAndGetAccessibilityAttribute(void)
150 {
151   ToolkitTestApplication application;
152
153   tet_infoline(" UtcDaliFocusManagerSetAndGetAccessibilityAttribute");
154
155   FocusManager manager = FocusManager::Get();
156   DALI_TEST_CHECK(manager);
157
158   Actor actor = Actor::New();
159   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(actor, FocusManager::ACCESSIBILITY_LABEL) == "");
160
161   manager.SetAccessibilityAttribute(actor, FocusManager::ACCESSIBILITY_LABEL, "Description");
162   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(actor, FocusManager::ACCESSIBILITY_LABEL) == "Description");
163
164   manager.SetAccessibilityAttribute(actor, FocusManager::ACCESSIBILITY_LABEL, "New description");
165   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(actor, FocusManager::ACCESSIBILITY_LABEL) == "New description");
166   END_TEST;
167 }
168
169 int UtcDaliFocusManagerSetAndGetFocusOrder(void)
170 {
171   ToolkitTestApplication application;
172
173   tet_infoline(" UtcDaliFocusManagerSetAndGetFocusOrder");
174
175   FocusManager manager = FocusManager::Get();
176   DALI_TEST_CHECK(manager);
177
178   Actor first = Actor::New();
179   Actor second = Actor::New();
180   DALI_TEST_CHECK(manager.GetFocusOrder(first) == 0);
181   DALI_TEST_CHECK(manager.GetFocusOrder(second) == 0);
182
183   // Set the focus order and description for the first actor
184   manager.SetFocusOrder(first, 1);
185   manager.SetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL, "first");
186   DALI_TEST_CHECK(manager.GetFocusOrder(first) == 1);
187   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL) == "first");
188
189   // Set the focus order and description for the second actor
190   manager.SetFocusOrder(second, 2);
191   manager.SetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL, "second");
192   DALI_TEST_CHECK(manager.GetFocusOrder(second) == 2);
193   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL) == "second");
194
195   // check that the focus order of the first actor is changed
196   manager.SetFocusOrder(first, 2);
197   DALI_TEST_CHECK(manager.GetFocusOrder(first) == 2);
198   // make sure the change of focus order doesn't affect the actor's description
199   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL) == "first");
200
201   // check that the focus order of the second actor is increased to 3
202   DALI_TEST_CHECK(manager.GetFocusOrder(second) == 3);
203   // make sure the change of focus order doesn't affect the actor's description
204   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL) == "second");
205
206   // check that the focus order of the second actor is changed to 1
207   manager.SetFocusOrder(second, 1);
208   DALI_TEST_CHECK(manager.GetFocusOrder(second) == 1);
209   // make sure the change of focus order doesn't affect the actor's description
210   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL) == "second");
211
212   // Set the focus order and description for the third actor
213   Actor third = Actor::New();
214   manager.SetFocusOrder(third, 1);
215   manager.SetAccessibilityAttribute(third, FocusManager::ACCESSIBILITY_LABEL, "third");
216   DALI_TEST_CHECK(manager.GetFocusOrder(third) == 1);
217   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(third, FocusManager::ACCESSIBILITY_LABEL) == "third");
218
219   // check that the focus order of the second actor is increased to 2.
220   DALI_TEST_CHECK(manager.GetFocusOrder(second) == 2);
221   // make sure the change of focus order doesn't affect the actor's description
222   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL) == "second");
223
224   // check that the focus order of the first actor is increased to 3.
225   DALI_TEST_CHECK(manager.GetFocusOrder(first) == 3);
226   // make sure the change of focus order doesn't affect the actor's description
227   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL) == "first");
228   END_TEST;
229 }
230
231 int UtcDaliFocusManagerGenerateNewFocusOrder(void)
232 {
233   ToolkitTestApplication application;
234
235   tet_infoline(" UtcDaliFocusManagerGenerateNewFocusOrder");
236
237   FocusManager manager = FocusManager::Get();
238   DALI_TEST_CHECK(manager);
239
240   DALI_TEST_CHECK(1 == manager.GenerateNewFocusOrder());
241   DALI_TEST_CHECK(1 == manager.GenerateNewFocusOrder());
242
243   Actor first = Actor::New();
244   Actor second = Actor::New();
245
246   // Set the focus order for the first actor
247   manager.SetFocusOrder(first, 1);
248   manager.SetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL, "first");
249   DALI_TEST_CHECK(manager.GetFocusOrder(first) == 1);
250
251   //Test for new focus order
252   DALI_TEST_CHECK(2 == manager.GenerateNewFocusOrder());
253
254   // Set the focus order for the first actor
255   manager.SetFocusOrder(second, 2);
256   manager.SetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL, "first");
257   DALI_TEST_CHECK(manager.GetFocusOrder(second) == 2);
258   END_TEST;
259 }
260
261 int UtcDaliFocusManagerGetActorByFocusOrder(void)
262 {
263   ToolkitTestApplication application;
264
265   tet_infoline(" UtcDaliFocusManagerGetActorByFocusOrder");
266
267   FocusManager manager = FocusManager::Get();
268   DALI_TEST_CHECK(manager);
269
270   // Create the actors and set their focus orders
271   Actor first = Actor::New();
272   manager.SetFocusOrder(first, 1);
273
274   Actor second = Actor::New();
275   manager.SetFocusOrder(second, 2);
276
277   Actor third = Actor::New();
278   manager.SetFocusOrder(third, 3);
279
280   // Check that we get an empty handle as no actor is added to the stage yet.
281   DALI_TEST_CHECK(manager.GetActorByFocusOrder(1) == Actor());
282   DALI_TEST_CHECK(manager.GetActorByFocusOrder(2) == Actor());
283   DALI_TEST_CHECK(manager.GetActorByFocusOrder(3) == Actor());
284
285   // Add the actors to the stage
286   Stage::GetCurrent().Add(first);
287   Stage::GetCurrent().Add(second);
288   Stage::GetCurrent().Add(third);
289
290   // Check that we get an empty handle because focus order 0 means undefined.
291   DALI_TEST_CHECK(manager.GetActorByFocusOrder(0) == Actor());
292
293   // Check that we get correct actors for the specified focus orders
294   DALI_TEST_CHECK(manager.GetActorByFocusOrder(1) == first);
295   DALI_TEST_CHECK(manager.GetActorByFocusOrder(2) == second);
296   DALI_TEST_CHECK(manager.GetActorByFocusOrder(3) == third);
297
298   // Change the focus order of the third actor to 1
299   manager.SetFocusOrder(third, 1);
300
301   // Check that we still get correct actors after changing their focus orders
302   DALI_TEST_CHECK(manager.GetActorByFocusOrder(1) == third);
303   DALI_TEST_CHECK(manager.GetActorByFocusOrder(2) == first);
304   DALI_TEST_CHECK(manager.GetActorByFocusOrder(3) == second);
305
306   // Check that we get an empty handle because no actor has a focus order of 4
307   DALI_TEST_CHECK(manager.GetActorByFocusOrder(4) == Actor());
308   END_TEST;
309 }
310
311 int UtcDaliFocusManagerSetAndGetCurrentFocusActor(void)
312 {
313   ToolkitTestApplication application;
314
315   tet_infoline(" UtcDaliFocusManagerSetAndGetCurrentFocusActor");
316
317   FocusManager manager = FocusManager::Get();
318   DALI_TEST_CHECK(manager);
319
320   // Create the first actor and add it to the stage
321   Actor first = Actor::New();
322   manager.SetFocusOrder(first, 1);
323   Stage::GetCurrent().Add(first);
324
325   // Create the second actor and add it to the stage
326   Actor second = Actor::New();
327   manager.SetFocusOrder(second, 2);
328   Stage::GetCurrent().Add(second);
329
330   // Create the third actor but don't add it to the stage
331   Actor third = Actor::New();
332   manager.SetFocusOrder(third, 3);
333
334   // Check that no actor is being focused yet.
335   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
336
337   // Check that it will fail to set focus on an invalid actor
338   DALI_TEST_CHECK(manager.SetCurrentFocusActor(Actor()) == false);
339
340   // Check that the focus is set on the first actor
341   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
342   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
343
344   // Check that the focus is set on the second actor
345   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
346   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
347
348   // Check that it will fail to set focus on the third actor as it's not in the stage
349   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == false);
350
351   // Add the third actor to the stage
352   Stage::GetCurrent().Add(third);
353
354   // make the third actor invisible
355   third.SetVisible(false);
356   // flush the queue and render once
357   application.SendNotification();
358   application.Render();
359
360   // Check that it will fail to set focus on the third actor as it's invisible
361   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == false);
362
363   // Make the third actor visible
364   third.SetVisible(true);
365   // flush the queue and render once
366   application.SendNotification();
367   application.Render();
368
369   // Make the third actor not focusable
370   Property::Index propertyActorFocusable = third.GetPropertyIndex("focusable");
371   third.SetProperty(propertyActorFocusable, false);
372   // flush the queue and render once
373   application.SendNotification();
374   application.Render();
375
376   // Check that it will fail to set focus on the third actor as it's not focusable
377   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == false);
378
379   // Make the third actor focusable
380   third.SetProperty(propertyActorFocusable, true);
381   // flush the queue and render once
382   application.SendNotification();
383   application.Render();
384
385   // Check that the focus is successfully moved to the third actor
386   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == true);
387
388   // Make the current focused actor to be not focusable by setting its focus order to be 0
389   manager.SetFocusOrder(third, 0);
390
391   // Check that the focus is automatically cleared
392   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
393
394   // Set the focus order of the third actor again
395   manager.SetFocusOrder(third, 3);
396
397   // Check that the third actor can be focused successfully now
398   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == true);
399   END_TEST;
400 }
401
402 int UtcDaliFocusManagerGetCurrentFocusGroup(void)
403 {
404   ToolkitTestApplication application;
405
406   tet_infoline(" UtcDaliFocusManagerGetCurrentFocusGroup");
407
408   FocusManager manager = FocusManager::Get();
409   DALI_TEST_CHECK(manager);
410
411   // Create an actor with two child actors and add it to the stage
412   Actor parent = Actor::New();
413   Actor firstChild = Actor::New();
414   Actor secondChild = Actor::New();
415   parent.Add(firstChild);
416   parent.Add(secondChild);
417   Stage::GetCurrent().Add(parent);
418
419   // Create three actors and add them as the children of the first child actor
420   Actor firstGrandChild = Actor::New();
421   Actor secondGrandChild = Actor::New();
422   Actor thirdGrandChild = Actor::New();
423   firstChild.Add(firstGrandChild);
424   firstChild.Add(secondGrandChild);
425   firstChild.Add(thirdGrandChild);
426
427   // Set focus order to the actors
428   manager.SetFocusOrder(parent, 1);
429   manager.SetFocusOrder(firstChild, 2);
430   manager.SetFocusOrder(firstGrandChild, 3);
431   manager.SetFocusOrder(secondGrandChild, 4);
432   manager.SetFocusOrder(thirdGrandChild, 5);
433   manager.SetFocusOrder(secondChild, 6);
434
435   // Set the parent and the first child actor as focus groups
436   manager.SetFocusGroup(parent, true);
437   DALI_TEST_CHECK(manager.IsFocusGroup(parent) == true);
438
439   // Set focus to the first grand child actor
440   DALI_TEST_CHECK(manager.SetCurrentFocusActor(firstGrandChild) == true);
441   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == firstGrandChild);
442
443   // The current focus group should be the parent, As it is the immediate parent which is also a focus group.
444   DALI_TEST_CHECK(manager.GetCurrentFocusGroup() == parent);
445
446   manager.SetFocusGroup(firstChild, true);
447   DALI_TEST_CHECK(manager.IsFocusGroup(firstChild) == true);
448
449   // The current focus group should be the firstChild, As it is the immediate parent which is also a focus group.
450   DALI_TEST_CHECK(manager.GetCurrentFocusGroup() == firstChild);
451
452   manager.SetFocusGroup(firstGrandChild, true);
453   DALI_TEST_CHECK(manager.IsFocusGroup(firstGrandChild) == true);
454
455   // The current focus group should be itself, As it is also a focus group.
456   DALI_TEST_CHECK(manager.GetCurrentFocusGroup() == firstGrandChild);
457
458   // Set focus to the second grand child actor
459   DALI_TEST_CHECK(manager.SetCurrentFocusActor(secondGrandChild) == true);
460   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == secondGrandChild);
461
462   // The current focus group should be the firstChild, As it is the immediate parent which is also a
463   // focus group for the current focus actor.
464   DALI_TEST_CHECK(manager.GetCurrentFocusGroup() == firstChild);
465
466   END_TEST;
467 }
468
469 int UtcDaliFocusManagerGetCurrentFocusOrder(void)
470 {
471   ToolkitTestApplication application;
472
473   tet_infoline(" UtcDaliFocusManagerGetCurrentFocusOrder");
474
475   FocusManager manager = FocusManager::Get();
476   DALI_TEST_CHECK(manager);
477
478   Actor first = Actor::New();
479   Stage::GetCurrent().Add(first);
480
481   Actor second = Actor::New();
482   Stage::GetCurrent().Add(second);
483
484   Actor third = Actor::New();
485   Stage::GetCurrent().Add(third);
486
487   // Set the focus order and description for the first actor
488   manager.SetFocusOrder(first, 1);
489   manager.SetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL, "first");
490   DALI_TEST_CHECK(manager.GetFocusOrder(first) == 1);
491   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL) == "first");
492
493   // Set the focus order and description for the second actor
494   manager.SetFocusOrder(second, 2);
495   manager.SetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL, "second");
496   DALI_TEST_CHECK(manager.GetFocusOrder(second) == 2);
497   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL) == "second");
498
499   // Set the focus order and description for the second actor
500   manager.SetFocusOrder(third, 3);
501   manager.SetAccessibilityAttribute(third, FocusManager::ACCESSIBILITY_LABEL, "third");
502   DALI_TEST_CHECK(manager.GetFocusOrder(third) == 3);
503   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(third, FocusManager::ACCESSIBILITY_LABEL) == "third");
504
505   // Check that no actor is being focused yet.
506   DALI_TEST_CHECK(manager.GetCurrentFocusOrder() == 0);
507
508   // Set the focus on the first actor and test
509   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
510   DALI_TEST_CHECK(manager.GetCurrentFocusOrder() == 1);
511
512   // Move the focus forward to the second actor and test
513   manager.MoveFocusForward();
514   DALI_TEST_CHECK(manager.GetCurrentFocusOrder() == 2);
515
516   // Move the focus forward to the third actor and test
517   manager.MoveFocusForward();
518   DALI_TEST_CHECK(manager.GetCurrentFocusOrder() == 3);
519
520   // Clear focus and test
521   manager.ClearFocus();
522   DALI_TEST_CHECK(manager.GetCurrentFocusOrder() == 0);
523   END_TEST;
524 }
525
526 int UtcDaliFocusManagerMoveFocusForward(void)
527 {
528   ToolkitTestApplication application;
529
530   tet_infoline(" UtcDaliFocusManagerMoveFocusForward");
531
532   FocusManager manager = FocusManager::Get();
533   DALI_TEST_CHECK(manager);
534
535   Actor first = Actor::New();
536   Stage::GetCurrent().Add(first);
537
538   Actor second = Actor::New();
539   Stage::GetCurrent().Add(second);
540
541   Actor third = Actor::New();
542   Stage::GetCurrent().Add(third);
543
544   // Set the focus order and description for the first actor
545   manager.SetFocusOrder(first, 1);
546   manager.SetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL, "first");
547   DALI_TEST_CHECK(manager.GetFocusOrder(first) == 1);
548   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL) == "first");
549
550   // Set the focus order and description for the second actor
551   manager.SetFocusOrder(second, 2);
552   manager.SetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL, "second");
553   DALI_TEST_CHECK(manager.GetFocusOrder(second) == 2);
554   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL) == "second");
555
556   // Set the focus order and description for the second actor
557   manager.SetFocusOrder(third, 3);
558   manager.SetAccessibilityAttribute(third, FocusManager::ACCESSIBILITY_LABEL, "third");
559   DALI_TEST_CHECK(manager.GetFocusOrder(third) == 3);
560   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(third, FocusManager::ACCESSIBILITY_LABEL) == "third");
561
562   // Check that no actor is being focused yet.
563   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
564
565   // Set the focus on the first actor
566   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
567   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
568   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "first");
569
570   // Test the non-wrapped move first
571   manager.SetWrapMode(false);
572   DALI_TEST_CHECK(manager.GetWrapMode() == false);
573
574   // Move the focus forward to the second actor
575   manager.MoveFocusForward();
576   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
577   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "second");
578
579   // Move the focus forward to the third actor
580   manager.MoveFocusForward();
581   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
582   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "third");
583
584   // Check that it will fail to move the focus forward again as the third actor is the last
585   // focusable actor in the focus chain
586   manager.MoveFocusForward();
587   // The focus should still be set on the third actor
588   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
589   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "third");
590
591   // Now test the wrapped move
592   manager.SetWrapMode(true);
593   DALI_TEST_CHECK(manager.GetWrapMode() == true);
594
595   // Move the focus forward recursively and this time the first actor should be focused
596   manager.MoveFocusForward();
597   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
598   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "first");
599
600   // Make the second actor not focusable
601   Property::Index propertyActorFocusable = second.GetPropertyIndex("focusable");
602   second.SetProperty(propertyActorFocusable, false);
603   // flush the queue and render once
604   application.SendNotification();
605   application.Render();
606
607   // Move the focus forward and check that the second actor should be skipped and
608   // the third actor should be focused now.
609   manager.MoveFocusForward();
610   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
611   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "third");
612
613   // Make the first actor invisible
614   first.SetVisible(false);
615   // flush the queue and render once
616   application.SendNotification();
617   application.Render();
618
619   // Move the focus forward and check that the first actor should be skipped as it's
620   // invisible and the second actor should also be skipped as it's not focusable,
621   // so the focus will still be on the third actor
622   manager.MoveFocusForward();
623   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
624   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "third");
625
626   // Make the third actor invisible so that no actor can be focused.
627   third.SetVisible(false);
628   // flush the queue and render once
629   application.SendNotification();
630   application.Render();
631
632   // Check that the focus move is failed as all the three actors can not be focused
633   manager.MoveFocusForward();
634   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
635   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "third");
636   END_TEST;
637 }
638
639 int UtcDaliFocusManagerMoveFocusBackward(void)
640 {
641   ToolkitTestApplication application;
642
643   tet_infoline(" UtcDaliFocusManagerMoveFocusBackward");
644
645   FocusManager manager = FocusManager::Get();
646   DALI_TEST_CHECK(manager);
647
648   Actor first = Actor::New();
649   Stage::GetCurrent().Add(first);
650
651   Actor second = Actor::New();
652   Stage::GetCurrent().Add(second);
653
654   Actor third = Actor::New();
655   Stage::GetCurrent().Add(third);
656
657   // Set the focus order and description for the first actor
658   manager.SetFocusOrder(first, 1);
659   manager.SetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL, "first");
660   DALI_TEST_CHECK(manager.GetFocusOrder(first) == 1);
661   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(first, FocusManager::ACCESSIBILITY_LABEL) == "first");
662
663   // Set the focus order and description for the second actor
664   manager.SetFocusOrder(second, 2);
665   manager.SetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL, "second");
666   DALI_TEST_CHECK(manager.GetFocusOrder(second) == 2);
667   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(second, FocusManager::ACCESSIBILITY_LABEL) == "second");
668
669   // Set the focus order and description for the second actor
670   manager.SetFocusOrder(third, 3);
671   manager.SetAccessibilityAttribute(third, FocusManager::ACCESSIBILITY_LABEL, "third");
672   DALI_TEST_CHECK(manager.GetFocusOrder(third) == 3);
673   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(third, FocusManager::ACCESSIBILITY_LABEL) == "third");
674
675   // Check that no actor is being focused yet.
676   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
677
678   // Set the focus on the third actor
679   DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == true);
680   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
681   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "third");
682
683   // Test the non-wrapped move first
684   manager.SetWrapMode(false);
685   DALI_TEST_CHECK(manager.GetWrapMode() == false);
686
687   // Move the focus backward to the second actor
688   manager.MoveFocusBackward();
689   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
690   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "second");
691
692   // Move the focus backward to the first actor
693   manager.MoveFocusBackward();
694   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
695   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "first");
696
697   // Check that it will fail to move the focus backward again as the first actor is the first
698   // focusable actor in the focus chain
699   manager.MoveFocusBackward();
700   // The focus should still be set on the first actor
701   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
702   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "first");
703
704   // Now test the wrapped move
705   manager.SetWrapMode(true);
706   DALI_TEST_CHECK(manager.GetWrapMode() == true);
707
708   // Move the focus backward recursively and this time the third actor should be focused
709   manager.MoveFocusBackward();
710   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
711   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "third");
712
713   // Make the second actor not focusable
714   Property::Index propertyActorFocusable = second.GetPropertyIndex("focusable");
715   second.SetProperty(propertyActorFocusable, false);
716   // flush the queue and render once
717   application.SendNotification();
718   application.Render();
719
720   // Move the focus backward and check that the second actor should be skipped and
721   // the first actor should be focused now.
722   manager.MoveFocusBackward();
723   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
724   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "first");
725
726   // Make the third actor invisible
727   third.SetVisible(false);
728   // flush the queue and render once
729   application.SendNotification();
730   application.Render();
731
732   // Move the focus backward and check that the third actor should be skipped as it's
733   // invisible and the second actor should also be skipped as it's not focusable,
734   // so the focus will still be on the first actor
735   manager.MoveFocusBackward();
736   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
737   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "first");
738
739   // Make the first actor invisible so that no actor can be focused.
740   first.SetVisible(false);
741   // flush the queue and render once
742   application.SendNotification();
743   application.Render();
744
745   // Check that the focus move is failed as all the three actors can not be focused
746   manager.MoveFocusBackward();
747   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
748   DALI_TEST_CHECK(manager.GetAccessibilityAttribute(manager.GetCurrentFocusActor(), FocusManager::ACCESSIBILITY_LABEL) == "first");
749   END_TEST;
750 }
751
752 int UtcDaliFocusManagerClearFocus(void)
753 {
754   ToolkitTestApplication application;
755
756   tet_infoline(" UtcDaliFocusManagerClearFocus");
757
758   FocusManager manager = FocusManager::Get();
759   DALI_TEST_CHECK(manager);
760
761   // Create the first actor and add it to the stage
762   Actor first = Actor::New();
763   manager.SetFocusOrder(first, 1);
764   Stage::GetCurrent().Add(first);
765
766   // Create the second actor and add it to the stage
767   Actor second = Actor::New();
768   manager.SetFocusOrder(second, 2);
769   Stage::GetCurrent().Add(second);
770
771   // Check that no actor is being focused yet.
772   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
773
774   // Check that the focus is set on the first actor
775   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
776   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
777
778   // Check that the focus is set on the second actor
779   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
780   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
781
782   // Clear the focus
783   manager.ClearFocus();
784
785   // Check that no actor is being focused now.
786   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
787   END_TEST;
788 }
789
790 int UtcDaliFocusManagerReset(void)
791 {
792   ToolkitTestApplication application;
793
794   tet_infoline(" UtcDaliFocusManagerReset");
795
796   FocusManager manager = FocusManager::Get();
797   DALI_TEST_CHECK(manager);
798
799   // Create the first actor and add it to the stage
800   Actor first = Actor::New();
801   manager.SetFocusOrder(first, 1);
802   Stage::GetCurrent().Add(first);
803
804   // Create the second actor and add it to the stage
805   Actor second = Actor::New();
806   manager.SetFocusOrder(second, 2);
807   Stage::GetCurrent().Add(second);
808
809   // Check that no actor is being focused yet.
810   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
811
812   // Check that the focus is set on the first actor
813   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
814   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
815
816   // Check that the focus is set on the second actor
817   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
818   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
819
820   // Clear the focus
821   manager.Reset();
822
823   // Check that no actor is being focused now and the focus order of actors have been cleared
824   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
825   DALI_TEST_CHECK(manager.GetFocusOrder(first) == 0);
826   DALI_TEST_CHECK(manager.GetFocusOrder(first) == 0);
827   END_TEST;
828 }
829
830 int UtcDaliFocusManagerFocusGroup(void)
831 {
832   ToolkitTestApplication application;
833
834   tet_infoline(" UtcDaliFocusManagerFocusGroup");
835
836   FocusManager manager = FocusManager::Get();
837   DALI_TEST_CHECK(manager);
838
839   // Create an actor with two child actors and add it to the stage
840   Actor parent = Actor::New();
841   Actor firstChild = Actor::New();
842   Actor secondChild = Actor::New();
843   parent.Add(firstChild);
844   parent.Add(secondChild);
845   Stage::GetCurrent().Add(parent);
846
847   // Create three actors and add them as the children of the first child actor
848   Actor firstGrandChild = Actor::New();
849   Actor secondGrandChild = Actor::New();
850   Actor thirdGrandChild = Actor::New();
851   firstChild.Add(firstGrandChild);
852   firstChild.Add(secondGrandChild);
853   firstChild.Add(thirdGrandChild);
854
855   // Set focus order to the actors
856   manager.SetFocusOrder(parent, 1);
857   manager.SetFocusOrder(firstChild, 2);
858   manager.SetFocusOrder(firstGrandChild, 3);
859   manager.SetFocusOrder(secondGrandChild, 4);
860   manager.SetFocusOrder(thirdGrandChild, 5);
861   manager.SetFocusOrder(secondChild, 6);
862
863   // Set the parent and the first child actor as focus groups
864   manager.SetFocusGroup(parent, true);
865   DALI_TEST_CHECK(manager.IsFocusGroup(parent) == true);
866
867   // The focus group of the parent should be itself, as it is set to be a focus group.
868   DALI_TEST_CHECK(manager.GetFocusGroup(parent) == parent);
869
870   // The focus group of the firstChild should be its parent, as it is the immediate parent which is also a group.
871   DALI_TEST_CHECK(manager.GetFocusGroup(firstChild) == parent);
872
873   manager.SetFocusGroup(firstChild, true);
874   DALI_TEST_CHECK(manager.IsFocusGroup(firstChild) == true);
875
876   // The focus group of the firstChild should be itself, as it is set to be a focus group now.
877   DALI_TEST_CHECK(manager.GetFocusGroup(firstChild) == firstChild);
878
879   // Enable wrap mode for focus movement.
880   manager.SetWrapMode(true);
881   DALI_TEST_CHECK(manager.GetWrapMode() == true);
882
883   // Check that no actor is being focused yet.
884   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
885
886   // Check that the focus is set on the parent actor.
887   DALI_TEST_CHECK(manager.SetCurrentFocusActor(parent) == true);
888   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == parent);
889
890   // Check that group mode is disabled.
891   DALI_TEST_CHECK(manager.GetGroupMode() == false);
892
893   // Check that the focus movement is wrapped as normal.
894   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
895   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == firstChild);
896   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
897   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == firstGrandChild);
898   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
899   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == secondGrandChild);
900   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
901   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == thirdGrandChild);
902   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
903   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == secondChild);
904   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
905   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == parent);
906   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
907   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == firstChild);
908   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
909   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == firstGrandChild);
910
911   // Enable the group mode.
912   manager.SetGroupMode(true);
913   DALI_TEST_CHECK(manager.GetGroupMode() == true);
914
915   // Check that the focus movement is now limited to the current focus group.
916   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
917   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == secondGrandChild);
918   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
919   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == thirdGrandChild);
920   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
921   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == firstChild);
922   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
923   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == firstGrandChild);
924   END_TEST;
925 }
926
927 int UtcDaliFocusManagerSetAndGetFocusIndicator(void)
928 {
929   ToolkitTestApplication application;
930
931   tet_infoline(" UtcDaliFocusManagerSetAndGetFocusIndicator");
932
933   FocusManager manager = FocusManager::Get();
934   DALI_TEST_CHECK(manager);
935
936   Actor defaultFocusIndicatorActor = manager.GetFocusIndicatorActor();
937   DALI_TEST_CHECK(defaultFocusIndicatorActor);
938
939   Actor newFocusIndicatorActor = Actor::New();
940   manager.SetFocusIndicatorActor(newFocusIndicatorActor);
941   DALI_TEST_CHECK(manager.GetFocusIndicatorActor() == newFocusIndicatorActor);
942   END_TEST;
943 }
944
945 int UtcDaliFocusManagerSignalFocusChanged(void)
946 {
947   ToolkitTestApplication application;
948
949   tet_infoline(" UtcDaliFocusManagerSignalFocusChanged");
950
951   FocusManager manager = FocusManager::Get();
952   DALI_TEST_CHECK(manager);
953
954   bool signalVerified = false;
955   FocusChangedCallback callback(signalVerified);
956   manager.FocusChangedSignal().Connect( &callback, &FocusChangedCallback::Callback );
957
958   // Create the first actor and add it to the stage
959   Actor first = Actor::New();
960   manager.SetFocusOrder(first, 1);
961   Stage::GetCurrent().Add(first);
962
963   // Create the second actor and add it to the stage
964   Actor second = Actor::New();
965   manager.SetFocusOrder(second, 2);
966   Stage::GetCurrent().Add(second);
967
968   // Check that no actor is being focused yet.
969   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
970
971   // Check that the focus is set on the first actor
972   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
973   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
974   DALI_TEST_CHECK(callback.mSignalVerified);
975   callback.Reset();
976
977   // Check that the focus is set on the second actor
978   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
979   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
980   DALI_TEST_CHECK(callback.mSignalVerified);
981   callback.Reset();
982
983   // Clear the focus
984   manager.ClearFocus();
985
986   // Check that no actor is being focused now.
987   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
988   DALI_TEST_CHECK(callback.mSignalVerified);
989   END_TEST;
990 }
991
992 int UtcDaliFocusManagerSignalFocusOvershot(void)
993 {
994   ToolkitTestApplication application;
995
996   tet_infoline(" UtcDaliFocusManagerSignalFocusOvershot");
997
998   FocusManager manager = FocusManager::Get();
999   DALI_TEST_CHECK(manager);
1000
1001   bool signalVerified = false;
1002   FocusOvershotCallback callback(signalVerified);
1003   manager.FocusOvershotSignal().Connect(&callback, &FocusOvershotCallback::Callback);
1004
1005   // Create the first actor and add it to the stage
1006   Actor first = Actor::New();
1007   manager.SetFocusOrder(first, 1);
1008   Stage::GetCurrent().Add(first);
1009
1010   // Create the second actor and add it to the stage
1011   Actor second = Actor::New();
1012   manager.SetFocusOrder(second, 2);
1013   Stage::GetCurrent().Add(second);
1014
1015   // Check that the wrap mode is disabled
1016   DALI_TEST_CHECK(manager.GetWrapMode() == false);
1017
1018   // Check that the focus is set on the first actor
1019   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
1020   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1021
1022   // Check that the focus is moved to the second actor successfully.
1023   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
1024   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
1025
1026   // Check that the forward focus movement is overshot.
1027   callback.mCurrentFocusedActor = second;
1028   callback.mFocusOvershotDirection = Toolkit::FocusManager::OVERSHOT_NEXT;
1029   DALI_TEST_CHECK(manager.MoveFocusForward() == false);
1030   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
1031   DALI_TEST_CHECK(signalVerified);
1032   callback.Reset();
1033
1034   // Enable the wrap mode
1035   manager.SetWrapMode(true);
1036   DALI_TEST_CHECK(manager.GetWrapMode() == true);
1037
1038   // Check that the forward focus movement is wrapped and no overshot happens.
1039   DALI_TEST_CHECK(manager.MoveFocusForward() == true);
1040   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1041   DALI_TEST_CHECK(signalVerified == false);
1042
1043   // Disable the wrap mode
1044   manager.SetWrapMode(false);
1045   DALI_TEST_CHECK(manager.GetWrapMode() == false);
1046
1047   // Check that the backward focus movement is overshot.
1048   callback.mCurrentFocusedActor = first;
1049   callback.mFocusOvershotDirection = Toolkit::FocusManager::OVERSHOT_PREVIOUS;
1050   DALI_TEST_CHECK(manager.MoveFocusBackward() == false);
1051   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
1052   DALI_TEST_CHECK(signalVerified);
1053   END_TEST;
1054 }