2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
20 // Need to override adaptor classes for toolkit test harness, so include
21 // test harness headers before dali headers.
22 #include <dali-toolkit-test-suite-utils.h>
24 #include <dali/dali.h>
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali/integration-api/events/key-event-integ.h>
29 using namespace Dali::Toolkit;
31 void utc_dali_toolkit_keyboard_focus_manager_startup(void)
33 test_return_value = TET_UNDEF;
36 void utc_dali_toolkit_keyboard_focus_manager_cleanup(void)
38 test_return_value = TET_PASS;
45 // Functors to test whether PreFocusChange signal is emitted when the keyboard focus is about to change
46 class PreFocusChangeCallback : public Dali::ConnectionTracker
49 PreFocusChangeCallback(bool& signalReceived)
50 : mSignalVerified(signalReceived),
51 mCurrentFocusedActor(),
52 mProposedActorToFocus(),
53 mDirection(Control::Left)
57 Actor Callback(Actor currentFocusedActor, Actor proposedActorToFocus, Control::KeyboardFocusNavigationDirection direction)
59 tet_infoline("Verifying PreFocusChangeCallback()");
61 mSignalVerified = true;
63 mCurrentFocusedActor = currentFocusedActor;
64 mProposedActorToFocus = proposedActorToFocus;
65 mDirection = direction;
67 return mProposedActorToFocus;
72 mSignalVerified = false;
73 mCurrentFocusedActor = Actor();
74 mProposedActorToFocus = Actor();
75 mDirection = Control::Left;
78 bool& mSignalVerified;
79 Actor mCurrentFocusedActor;
80 Actor mProposedActorToFocus;
81 Control::KeyboardFocusNavigationDirection mDirection;
84 // Functors to test whether focus changed signal is emitted when the keyboard focus is changed
85 class FocusChangedCallback : public Dali::ConnectionTracker
88 FocusChangedCallback(bool& signalReceived)
89 : mSignalVerified(signalReceived),
90 mOriginalFocusedActor(),
91 mCurrentFocusedActor()
95 void Callback(Actor originalFocusedActor, Actor currentFocusedActor)
97 tet_infoline("Verifying FocusChangedCallback()");
99 if(originalFocusedActor == mCurrentFocusedActor)
101 mSignalVerified = true;
104 mOriginalFocusedActor = originalFocusedActor;
105 mCurrentFocusedActor = currentFocusedActor;
110 mSignalVerified = false;
113 bool& mSignalVerified;
114 Actor mOriginalFocusedActor;
115 Actor mCurrentFocusedActor;
118 // Functors to test whether focus group changed signal is emitted when the keyboard focus group is changed
119 class FocusGroupChangedCallback : public Dali::ConnectionTracker
122 FocusGroupChangedCallback(bool& signalReceived)
123 : mSignalVerified(signalReceived),
124 mCurrentFocusedActor(),
129 void Callback(Actor currentFocusedActor, bool forward)
131 tet_infoline("Verifying FocusGroupChangedCallback()");
133 mSignalVerified = true;
135 mCurrentFocusedActor = currentFocusedActor;
141 mSignalVerified = false;
144 bool& mSignalVerified;
145 Actor mCurrentFocusedActor;
149 // Functors to test whether focused actor activated signal is emitted when the focused actor is activated
150 class FocusedActorActivatedCallback : public Dali::ConnectionTracker
153 FocusedActorActivatedCallback(bool& signalReceived)
154 : mSignalVerified(signalReceived),
159 void Callback(Actor activatedActor)
161 tet_infoline("Verifying FocusedActorActivatedCallback()");
163 mSignalVerified = true;
165 mActivatedActor = activatedActor;
170 mSignalVerified = false;
173 bool& mSignalVerified;
174 Actor mActivatedActor;
180 int UtcDaliKeyboardFocusManagerGet(void)
182 ToolkitTestApplication application;
184 tet_infoline(" UtcDaliKeyboardKeyboardFocusManagerGet");
186 KeyboardFocusManager manager;
188 manager = KeyboardFocusManager::Get();
189 DALI_TEST_CHECK(manager);
191 KeyboardFocusManager newManager = KeyboardFocusManager::Get();
192 DALI_TEST_CHECK(newManager);
194 // Check that focus manager is a singleton
195 DALI_TEST_CHECK(manager == newManager);
199 int UtcDaliKeyboardFocusManagerSetAndGetCurrentFocusActor(void)
201 ToolkitTestApplication application;
203 tet_infoline(" UtcDaliKeyboardFocusManagerSetAndGetCurrentFocusActor");
205 KeyboardFocusManager manager = KeyboardFocusManager::Get();
206 DALI_TEST_CHECK(manager);
208 // Create the first actor and add it to the stage
209 Actor first = Actor::New();
210 first.SetKeyboardFocusable(true);
211 Stage::GetCurrent().Add(first);
213 // Create the second actor and add it to the stage
214 Actor second = Actor::New();
215 second.SetKeyboardFocusable(true);
216 Stage::GetCurrent().Add(second);
218 // Create the third actor but don't add it to the stage
219 Actor third = Actor::New();
221 // Check that no actor is being focused yet.
222 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
224 // Check that it will fail to set focus on an invalid actor
225 DALI_TEST_CHECK(manager.SetCurrentFocusActor(Actor()) == false);
227 // Check that the focus is set on the first actor
228 DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
229 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
231 // Check that the focus is set on the second actor
232 DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
233 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
235 // Check that it will fail to set focus on the third actor as it's not in the stage
236 DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == false);
237 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
239 // Add the third actor to the stage
240 Stage::GetCurrent().Add(third);
242 // Check that it will fail to set focus on the third actor as it's not focusable
243 DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == false);
244 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
246 // Make the third actor focusable
247 third.SetKeyboardFocusable(true);
249 // Check that the focus is successfully moved to the third actor
250 DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == true);
251 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
255 int UtcDaliKeyboardFocusManagerMoveFocus(void)
257 ToolkitTestApplication application;
259 tet_infoline(" UtcDaliKeyboardFocusManagerMoveFocus");
261 KeyboardFocusManager manager = KeyboardFocusManager::Get();
262 DALI_TEST_CHECK(manager);
264 bool preFocusChangeSignalVerified = false;
265 PreFocusChangeCallback preFocusChangeCallback(preFocusChangeSignalVerified);
266 manager.PreFocusChangeSignal().Connect( &preFocusChangeCallback, &PreFocusChangeCallback::Callback );
268 bool focusChangedSignalVerified = false;
269 FocusChangedCallback focusChangedCallback(focusChangedSignalVerified);
270 manager.FocusChangedSignal().Connect( &focusChangedCallback, &FocusChangedCallback::Callback );
272 // Create the first actor and add it to the stage
273 Actor first = Actor::New();
274 first.SetKeyboardFocusable(true);
275 Stage::GetCurrent().Add(first);
277 // Create the second actor and add it to the stage
278 Actor second = Actor::New();
279 second.SetKeyboardFocusable(true);
280 Stage::GetCurrent().Add(second);
282 // Move the focus to the right
283 DALI_TEST_CHECK(manager.MoveFocus(Control::Right) == false);
285 // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
286 DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
287 DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == Actor());
288 DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
289 DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::Right);
290 preFocusChangeCallback.Reset();
292 // Check that the focus is set on the first actor
293 DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
294 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
295 DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
296 DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == Actor());
297 DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
298 focusChangedCallback.Reset();
300 // Move the focus towards right
301 DALI_TEST_CHECK(manager.MoveFocus(Control::Right) == false);
303 // Because no layout control in the stage and the first actor is focused, it should emit the PreFocusChange signal
304 DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
305 DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == first);
306 DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
307 DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::Right);
308 preFocusChangeCallback.Reset();
310 // Check that the focus is set on the second actor
311 DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
312 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
313 DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
314 DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
315 DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == second);
316 focusChangedCallback.Reset();
318 // Move the focus towards up
319 DALI_TEST_CHECK(manager.MoveFocus(Control::Up) == false);
321 // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
322 DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
323 DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == second);
324 DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
325 DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::Up);
326 preFocusChangeCallback.Reset();
327 DALI_TEST_CHECK(!focusChangedCallback.mSignalVerified);
332 int UtcDaliKeyboardFocusManagerClearFocus(void)
334 ToolkitTestApplication application;
336 tet_infoline(" UtcDaliKeyboardFocusManagerClearFocus");
338 KeyboardFocusManager manager = KeyboardFocusManager::Get();
339 DALI_TEST_CHECK(manager);
341 // Create the first actor and add it to the stage
342 Actor first = Actor::New();
343 first.SetKeyboardFocusable(true);
344 Stage::GetCurrent().Add(first);
346 // Create the second actor and add it to the stage
347 Actor second = Actor::New();
348 second.SetKeyboardFocusable(true);
349 Stage::GetCurrent().Add(second);
351 // Check that the focus is set on the first actor
352 DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
353 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
355 // Check that the focus is set on the second actor
356 DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
357 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
360 manager.ClearFocus();
362 // Check that no actor is being focused now.
363 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
367 int UtcDaliKeyboardFocusManagerSetAndGetFocusGroupLoop(void)
369 ToolkitTestApplication application;
371 tet_infoline(" UtcDaliKeyboardFocusManagerSetAndGetFocusGroupLoop");
373 KeyboardFocusManager manager = KeyboardFocusManager::Get();
374 DALI_TEST_CHECK(manager);
376 // Check that the focus movement is not looped within the same focus group by default
377 DALI_TEST_CHECK(manager.GetFocusGroupLoop() == false);
380 manager.SetFocusGroupLoop(true);
381 DALI_TEST_CHECK(manager.GetFocusGroupLoop() == true);
385 int UtcDaliKeyboardFocusManagerSetAsFocusGroup(void)
387 ToolkitTestApplication application;
389 tet_infoline(" UtcDaliKeyboardFocusManagerSetAsFocusGroup");
391 KeyboardFocusManager manager = KeyboardFocusManager::Get();
392 DALI_TEST_CHECK(manager);
394 // Create an actor and check that it is not a focus group by default
395 Actor actor = Actor::New();
396 DALI_TEST_CHECK(manager.IsFocusGroup(actor) == false);
398 // Set the actor as focus group
399 manager.SetAsFocusGroup(actor, true);
401 // flush the queue and render once
402 application.SendNotification();
403 application.Render();
405 DALI_TEST_CHECK(manager.IsFocusGroup(actor) == true);
407 // Set the actor not as focus group
408 manager.SetAsFocusGroup(actor, false);
410 // flush the queue and render once
411 application.SendNotification();
412 application.Render();
414 DALI_TEST_CHECK(manager.IsFocusGroup(actor) == false);
418 int UtcDaliKeyboardFocusManagerGetFocusGroup(void)
420 ToolkitTestApplication application;
422 tet_infoline(" UtcDaliKeyboardFocusManagerGetFocusGroup");
424 KeyboardFocusManager manager = KeyboardFocusManager::Get();
425 DALI_TEST_CHECK(manager);
427 // Create an actor with two child actors and add it to the stage
428 Actor parent = Actor::New();
429 Actor child = Actor::New();
431 Stage::GetCurrent().Add(parent);
433 // Create three actors and add them as the children of the first child actor
434 Actor grandChild = Actor::New();
435 child.Add(grandChild);
437 // Set the parent and the first child actor as focus groups
438 manager.SetAsFocusGroup(parent, true);
440 // flush the queue and render once
441 application.SendNotification();
442 application.Render();
444 DALI_TEST_CHECK(manager.IsFocusGroup(parent) == true);
446 // The current focus group should be the parent, As it is the immediate parent which is also a focus group.
447 DALI_TEST_CHECK(manager.GetFocusGroup(grandChild) == parent);
449 manager.SetAsFocusGroup(child, true);
451 // flush the queue and render once
452 application.SendNotification();
453 application.Render();
455 DALI_TEST_CHECK(manager.IsFocusGroup(child) == true);
457 // The focus group should be the child, As it is the immediate parent which is also a focus group.
458 DALI_TEST_CHECK(manager.GetFocusGroup(grandChild) == child);
460 manager.SetAsFocusGroup(grandChild, true);
462 // flush the queue and render once
463 application.SendNotification();
464 application.Render();
466 DALI_TEST_CHECK(manager.IsFocusGroup(grandChild) == true);
468 // The current focus group should be itself, As it is also a focus group.
469 DALI_TEST_CHECK(manager.GetFocusGroup(grandChild) == grandChild);
473 int UtcDaliKeyboardFocusManagerSetAndGetFocusIndicator(void)
475 ToolkitTestApplication application;
477 tet_infoline(" UtcDaliKeyboardFocusManagerSetAndGetFocusIndicator");
479 KeyboardFocusManager manager = KeyboardFocusManager::Get();
480 DALI_TEST_CHECK(manager);
482 Actor defaultFocusIndicatorActor = manager.GetFocusIndicatorActor();
483 DALI_TEST_CHECK(defaultFocusIndicatorActor);
485 Actor newFocusIndicatorActor = Actor::New();
486 manager.SetFocusIndicatorActor(newFocusIndicatorActor);
487 DALI_TEST_CHECK(manager.GetFocusIndicatorActor() == newFocusIndicatorActor);
492 int UtcDaliKeyboardFocusManagerSignalFocusedActorActivated(void)
494 ToolkitTestApplication application;
496 tet_infoline(" UtcDaliKeyboardFocusManagerSignalFocusedActorActivated");
498 KeyboardFocusManager manager = KeyboardFocusManager::Get();
499 DALI_TEST_CHECK(manager);
501 bool focusedActorActivatedSignalVerified = false;
502 FocusedActorActivatedCallback focusedActorActivatedCallback(focusedActorActivatedSignalVerified);
503 manager.FocusedActorActivatedSignal().Connect( &focusedActorActivatedCallback, &FocusedActorActivatedCallback::Callback );
505 Integration::KeyEvent returnEvent("Return", "", 0, 0, 0, Integration::KeyEvent::Up);
507 // Create the first button and add it to the stage
508 PushButton firstPushButton = PushButton::New();
509 firstPushButton.SetKeyboardFocusable(true);
510 Stage::GetCurrent().Add(firstPushButton);
512 // Create the second button and add it to the stage
513 PushButton secondPushButton = PushButton::New();
514 secondPushButton.SetKeyboardFocusable(true);
515 Stage::GetCurrent().Add(secondPushButton);
517 // Check that the focus is set on the first button
518 DALI_TEST_CHECK(manager.SetCurrentFocusActor(firstPushButton) == true);
519 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == firstPushButton);
521 // Send the return event to activate the first button
522 application.ProcessEvent(returnEvent);
523 DALI_TEST_CHECK(focusedActorActivatedCallback.mSignalVerified);
524 DALI_TEST_CHECK(focusedActorActivatedCallback.mActivatedActor == firstPushButton);
525 focusedActorActivatedCallback.Reset();
527 // Check that the focus is set on the second button
528 DALI_TEST_CHECK(manager.SetCurrentFocusActor(secondPushButton) == true);
529 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == secondPushButton);
531 // Send the return event again to activate the second button
532 application.ProcessEvent(returnEvent);
533 DALI_TEST_CHECK(focusedActorActivatedCallback.mSignalVerified);
534 DALI_TEST_CHECK(focusedActorActivatedCallback.mActivatedActor == secondPushButton);
535 focusedActorActivatedCallback.Reset();