2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
25 #include <dali/dali.h>
26 #include <dali-toolkit/dali-toolkit.h>
27 #include <dali/integration-api/events/key-event-integ.h>
30 using namespace Dali::Toolkit;
32 void utc_dali_toolkit_keyboard_focus_manager_startup(void)
34 test_return_value = TET_UNDEF;
37 void utc_dali_toolkit_keyboard_focus_manager_cleanup(void)
39 test_return_value = TET_PASS;
46 // Functors to test whether PreFocusChange signal is emitted when the keyboard focus is about to change
47 class PreFocusChangeCallback : public Dali::ConnectionTracker
50 PreFocusChangeCallback(bool& signalReceived)
51 : mSignalVerified(signalReceived),
52 mCurrentFocusedActor(),
53 mProposedActorToFocus(),
54 mDirection(Control::Left)
58 Actor Callback(Actor currentFocusedActor, Actor proposedActorToFocus, Control::KeyboardFocusNavigationDirection direction)
60 tet_infoline("Verifying PreFocusChangeCallback()");
62 mSignalVerified = true;
64 mCurrentFocusedActor = currentFocusedActor;
65 mProposedActorToFocus = proposedActorToFocus;
66 mDirection = direction;
68 return mProposedActorToFocus;
73 mSignalVerified = false;
74 mCurrentFocusedActor = Actor();
75 mProposedActorToFocus = Actor();
76 mDirection = Control::Left;
79 bool& mSignalVerified;
80 Actor mCurrentFocusedActor;
81 Actor mProposedActorToFocus;
82 Control::KeyboardFocusNavigationDirection mDirection;
85 // Functors to test whether focus changed signal is emitted when the keyboard focus is changed
86 class FocusChangedCallback : public Dali::ConnectionTracker
89 FocusChangedCallback(bool& signalReceived)
90 : mSignalVerified(signalReceived),
91 mOriginalFocusedActor(),
92 mCurrentFocusedActor()
96 void Callback(Actor originalFocusedActor, Actor currentFocusedActor)
98 tet_infoline("Verifying FocusChangedCallback()");
100 if(originalFocusedActor == mCurrentFocusedActor)
102 mSignalVerified = true;
105 mOriginalFocusedActor = originalFocusedActor;
106 mCurrentFocusedActor = currentFocusedActor;
111 mSignalVerified = false;
114 bool& mSignalVerified;
115 Actor mOriginalFocusedActor;
116 Actor mCurrentFocusedActor;
119 // Functors to test whether focus group changed signal is emitted when the keyboard focus group is changed
120 class FocusGroupChangedCallback : public Dali::ConnectionTracker
123 FocusGroupChangedCallback(bool& signalReceived)
124 : mSignalVerified(signalReceived),
125 mCurrentFocusedActor(),
130 void Callback(Actor currentFocusedActor, bool forward)
132 tet_infoline("Verifying FocusGroupChangedCallback()");
134 mSignalVerified = true;
136 mCurrentFocusedActor = currentFocusedActor;
142 mSignalVerified = false;
145 bool& mSignalVerified;
146 Actor mCurrentFocusedActor;
150 // Functors to test whether focused actor activated signal is emitted when the focused actor is activated
151 class FocusedActorActivatedCallback : public Dali::ConnectionTracker
154 FocusedActorActivatedCallback(bool& signalReceived)
155 : mSignalVerified(signalReceived),
160 void Callback(Actor activatedActor)
162 tet_infoline("Verifying FocusedActorActivatedCallback()");
164 mSignalVerified = true;
166 mActivatedActor = activatedActor;
171 mSignalVerified = false;
174 bool& mSignalVerified;
175 Actor mActivatedActor;
181 int UtcDaliKeyboardFocusManagerGet(void)
183 ToolkitTestApplication application;
185 tet_infoline(" UtcDaliKeyboardKeyboardFocusManagerGet");
187 KeyboardFocusManager manager;
189 manager = KeyboardFocusManager::Get();
190 DALI_TEST_CHECK(manager);
192 KeyboardFocusManager newManager = KeyboardFocusManager::Get();
193 DALI_TEST_CHECK(newManager);
195 // Check that focus manager is a singleton
196 DALI_TEST_CHECK(manager == newManager);
200 int UtcDaliKeyboardFocusManagerSetAndGetCurrentFocusActor(void)
202 ToolkitTestApplication application;
204 tet_infoline(" UtcDaliKeyboardFocusManagerSetAndGetCurrentFocusActor");
206 KeyboardFocusManager manager = KeyboardFocusManager::Get();
207 DALI_TEST_CHECK(manager);
209 // Create the first actor and add it to the stage
210 Actor first = Actor::New();
211 first.SetKeyboardFocusable(true);
212 Stage::GetCurrent().Add(first);
214 // Create the second actor and add it to the stage
215 Actor second = Actor::New();
216 second.SetKeyboardFocusable(true);
217 Stage::GetCurrent().Add(second);
219 // Create the third actor but don't add it to the stage
220 Actor third = Actor::New();
222 // Check that no actor is being focused yet.
223 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
225 // Check that it will fail to set focus on an invalid actor
226 DALI_TEST_CHECK(manager.SetCurrentFocusActor(Actor()) == false);
228 // Check that the focus is set on the first actor
229 DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
230 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
232 // Check that the focus is set on the second actor
233 DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
234 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
236 // Check that it will fail to set focus on the third actor as it's not in the stage
237 DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == false);
238 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
240 // Add the third actor to the stage
241 Stage::GetCurrent().Add(third);
243 // Check that it will fail to set focus on the third actor as it's not focusable
244 DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == false);
245 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
247 // Make the third actor focusable
248 third.SetKeyboardFocusable(true);
250 // Check that the focus is successfully moved to the third actor
251 DALI_TEST_CHECK(manager.SetCurrentFocusActor(third) == true);
252 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
256 int UtcDaliKeyboardFocusManagerMoveFocus(void)
258 ToolkitTestApplication application;
260 tet_infoline(" UtcDaliKeyboardFocusManagerMoveFocus");
262 KeyboardFocusManager manager = KeyboardFocusManager::Get();
263 DALI_TEST_CHECK(manager);
265 bool preFocusChangeSignalVerified = false;
266 PreFocusChangeCallback preFocusChangeCallback(preFocusChangeSignalVerified);
267 manager.PreFocusChangeSignal().Connect( &preFocusChangeCallback, &PreFocusChangeCallback::Callback );
269 bool focusChangedSignalVerified = false;
270 FocusChangedCallback focusChangedCallback(focusChangedSignalVerified);
271 manager.FocusChangedSignal().Connect( &focusChangedCallback, &FocusChangedCallback::Callback );
273 // Create the first actor and add it to the stage
274 Actor first = Actor::New();
275 first.SetKeyboardFocusable(true);
276 Stage::GetCurrent().Add(first);
278 // Create the second actor and add it to the stage
279 Actor second = Actor::New();
280 second.SetKeyboardFocusable(true);
281 Stage::GetCurrent().Add(second);
283 // Move the focus to the right
284 DALI_TEST_CHECK(manager.MoveFocus(Control::Right) == false);
286 // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
287 DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
288 DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == Actor());
289 DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
290 DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::Right);
291 preFocusChangeCallback.Reset();
293 // Check that the focus is set on the first actor
294 DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
295 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
296 DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
297 DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == Actor());
298 DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
299 focusChangedCallback.Reset();
301 // Move the focus towards right
302 DALI_TEST_CHECK(manager.MoveFocus(Control::Right) == false);
304 // Because no layout control in the stage and the first actor is focused, it should emit the PreFocusChange signal
305 DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
306 DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == first);
307 DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
308 DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::Right);
309 preFocusChangeCallback.Reset();
311 // Check that the focus is set on the second actor
312 DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
313 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
314 DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
315 DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
316 DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == second);
317 focusChangedCallback.Reset();
319 // Move the focus towards up
320 DALI_TEST_CHECK(manager.MoveFocus(Control::Up) == false);
322 // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
323 DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
324 DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == second);
325 DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
326 DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::Up);
327 preFocusChangeCallback.Reset();
328 DALI_TEST_CHECK(!focusChangedCallback.mSignalVerified);
333 int UtcDaliKeyboardFocusManagerClearFocus(void)
335 ToolkitTestApplication application;
337 tet_infoline(" UtcDaliKeyboardFocusManagerClearFocus");
339 KeyboardFocusManager manager = KeyboardFocusManager::Get();
340 DALI_TEST_CHECK(manager);
342 // Create the first actor and add it to the stage
343 Actor first = Actor::New();
344 first.SetKeyboardFocusable(true);
345 Stage::GetCurrent().Add(first);
347 // Create the second actor and add it to the stage
348 Actor second = Actor::New();
349 second.SetKeyboardFocusable(true);
350 Stage::GetCurrent().Add(second);
352 // Check that the focus is set on the first actor
353 DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
354 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
356 // Check that the focus is set on the second actor
357 DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
358 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
361 manager.ClearFocus();
363 // Check that no actor is being focused now.
364 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == Actor());
368 int UtcDaliKeyboardFocusManagerSetAndGetFocusGroupLoop(void)
370 ToolkitTestApplication application;
372 tet_infoline(" UtcDaliKeyboardFocusManagerSetAndGetFocusGroupLoop");
374 KeyboardFocusManager manager = KeyboardFocusManager::Get();
375 DALI_TEST_CHECK(manager);
377 // Check that the focus movement is not looped within the same focus group by default
378 DALI_TEST_CHECK(manager.GetFocusGroupLoop() == false);
381 manager.SetFocusGroupLoop(true);
382 DALI_TEST_CHECK(manager.GetFocusGroupLoop() == true);
386 int UtcDaliKeyboardFocusManagerSetAsFocusGroup(void)
388 ToolkitTestApplication application;
390 tet_infoline(" UtcDaliKeyboardFocusManagerSetAsFocusGroup");
392 KeyboardFocusManager manager = KeyboardFocusManager::Get();
393 DALI_TEST_CHECK(manager);
395 // Create an actor and check that it is not a focus group by default
396 Actor actor = Actor::New();
397 DALI_TEST_CHECK(manager.IsFocusGroup(actor) == false);
399 // Set the actor as focus group
400 manager.SetAsFocusGroup(actor, true);
402 // flush the queue and render once
403 application.SendNotification();
404 application.Render();
406 DALI_TEST_CHECK(manager.IsFocusGroup(actor) == true);
408 // Set the actor not as focus group
409 manager.SetAsFocusGroup(actor, false);
411 // flush the queue and render once
412 application.SendNotification();
413 application.Render();
415 DALI_TEST_CHECK(manager.IsFocusGroup(actor) == false);
419 int UtcDaliKeyboardFocusManagerGetFocusGroup(void)
421 ToolkitTestApplication application;
423 tet_infoline(" UtcDaliKeyboardFocusManagerGetFocusGroup");
425 KeyboardFocusManager manager = KeyboardFocusManager::Get();
426 DALI_TEST_CHECK(manager);
428 // Create an actor with two child actors and add it to the stage
429 Actor parent = Actor::New();
430 Actor child = Actor::New();
432 Stage::GetCurrent().Add(parent);
434 // Create three actors and add them as the children of the first child actor
435 Actor grandChild = Actor::New();
436 child.Add(grandChild);
438 // Set the parent and the first child actor as focus groups
439 manager.SetAsFocusGroup(parent, true);
441 // flush the queue and render once
442 application.SendNotification();
443 application.Render();
445 DALI_TEST_CHECK(manager.IsFocusGroup(parent) == true);
447 // The current focus group should be the parent, As it is the immediate parent which is also a focus group.
448 DALI_TEST_CHECK(manager.GetFocusGroup(grandChild) == parent);
450 manager.SetAsFocusGroup(child, true);
452 // flush the queue and render once
453 application.SendNotification();
454 application.Render();
456 DALI_TEST_CHECK(manager.IsFocusGroup(child) == true);
458 // The focus group should be the child, As it is the immediate parent which is also a focus group.
459 DALI_TEST_CHECK(manager.GetFocusGroup(grandChild) == child);
461 manager.SetAsFocusGroup(grandChild, true);
463 // flush the queue and render once
464 application.SendNotification();
465 application.Render();
467 DALI_TEST_CHECK(manager.IsFocusGroup(grandChild) == true);
469 // The current focus group should be itself, As it is also a focus group.
470 DALI_TEST_CHECK(manager.GetFocusGroup(grandChild) == grandChild);
474 int UtcDaliKeyboardFocusManagerSetAndGetFocusIndicator(void)
476 ToolkitTestApplication application;
478 tet_infoline(" UtcDaliKeyboardFocusManagerSetAndGetFocusIndicator");
480 KeyboardFocusManager manager = KeyboardFocusManager::Get();
481 DALI_TEST_CHECK(manager);
483 Actor defaultFocusIndicatorActor = manager.GetFocusIndicatorActor();
484 DALI_TEST_CHECK(defaultFocusIndicatorActor);
486 Actor newFocusIndicatorActor = Actor::New();
487 manager.SetFocusIndicatorActor(newFocusIndicatorActor);
488 DALI_TEST_CHECK(manager.GetFocusIndicatorActor() == newFocusIndicatorActor);
493 int UtcDaliKeyboardFocusManagerSignalFocusedActorActivated(void)
495 ToolkitTestApplication application;
497 tet_infoline(" UtcDaliKeyboardFocusManagerSignalFocusedActorActivated");
499 KeyboardFocusManager manager = KeyboardFocusManager::Get();
500 DALI_TEST_CHECK(manager);
502 bool focusedActorActivatedSignalVerified = false;
503 FocusedActorActivatedCallback focusedActorActivatedCallback(focusedActorActivatedSignalVerified);
504 manager.FocusedActorActivatedSignal().Connect( &focusedActorActivatedCallback, &FocusedActorActivatedCallback::Callback );
506 Integration::KeyEvent returnEvent("Return", "", 0, 0, 0, Integration::KeyEvent::Up);
508 // Create the first button and add it to the stage
509 PushButton firstPushButton = PushButton::New();
510 firstPushButton.SetKeyboardFocusable(true);
511 Stage::GetCurrent().Add(firstPushButton);
513 // Create the second button and add it to the stage
514 PushButton secondPushButton = PushButton::New();
515 secondPushButton.SetKeyboardFocusable(true);
516 Stage::GetCurrent().Add(secondPushButton);
518 // Check that the focus is set on the first button
519 DALI_TEST_CHECK(manager.SetCurrentFocusActor(firstPushButton) == true);
520 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == firstPushButton);
522 // Send the return event to activate the first button
523 application.ProcessEvent(returnEvent);
524 DALI_TEST_CHECK(focusedActorActivatedCallback.mSignalVerified);
525 DALI_TEST_CHECK(focusedActorActivatedCallback.mActivatedActor == firstPushButton);
526 focusedActorActivatedCallback.Reset();
528 // Check that the focus is set on the second button
529 DALI_TEST_CHECK(manager.SetCurrentFocusActor(secondPushButton) == true);
530 DALI_TEST_CHECK(manager.GetCurrentFocusActor() == secondPushButton);
532 // Send the return event again to activate the second button
533 application.ProcessEvent(returnEvent);
534 DALI_TEST_CHECK(focusedActorActivatedCallback.mSignalVerified);
535 DALI_TEST_CHECK(focusedActorActivatedCallback.mActivatedActor == secondPushButton);
536 focusedActorActivatedCallback.Reset();