Adding new test harness
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit-unmanaged / utc-Dali-KeyboardFocusManager.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
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 #include <iostream>
18 #include <stdlib.h>
19 #include <dali-toolkit-test-suite-utils.h>
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <dali/integration-api/events/key-event-integ.h>
22
23
24 using namespace Dali;
25 using namespace Dali::Toolkit;
26
27 void dali_keyboard_focus_manager_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void dali_keyboard_focus_manager_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37
38 namespace
39 {
40
41 // Functors to test whether PreFocusChange signal is emitted when the keyboard focus is about to change
42 class PreFocusChangeCallback : public Dali::ConnectionTracker
43 {
44 public:
45   PreFocusChangeCallback(bool& signalReceived)
46   : mSignalVerified(signalReceived),
47     mCurrentFocusedActor(),
48     mProposedActorToFocus(),
49     mDirection(Control::Left)
50   {
51   }
52
53   Actor Callback(Actor currentFocusedActor, Actor proposedActorToFocus, Control::KeyboardFocusNavigationDirection direction)
54   {
55     tet_infoline("Verifying PreFocusChangeCallback()");
56
57     mSignalVerified = true;
58
59     mCurrentFocusedActor = currentFocusedActor;
60     mProposedActorToFocus = proposedActorToFocus;
61     mDirection = direction;
62
63     return mProposedActorToFocus;
64   }
65
66   void Reset()
67   {
68     mSignalVerified = false;
69     mCurrentFocusedActor = Actor();
70     mProposedActorToFocus = Actor();
71     mDirection = Control::Left;
72   }
73
74   bool& mSignalVerified;
75   Actor mCurrentFocusedActor;
76   Actor mProposedActorToFocus;
77   Control::KeyboardFocusNavigationDirection mDirection;
78 };
79
80 // Functors to test whether focus changed signal is emitted when the keyboard focus is changed
81 class FocusChangedCallback : public Dali::ConnectionTracker
82 {
83 public:
84   FocusChangedCallback(bool& signalReceived)
85   : mSignalVerified(signalReceived),
86     mOriginalFocusedActor(),
87     mCurrentFocusedActor()
88   {
89   }
90
91   void Callback(Actor originalFocusedActor, Actor currentFocusedActor)
92   {
93     tet_infoline("Verifying FocusChangedCallback()");
94
95     if(originalFocusedActor == mCurrentFocusedActor)
96     {
97       mSignalVerified = true;
98     }
99
100     mOriginalFocusedActor = originalFocusedActor;
101     mCurrentFocusedActor = currentFocusedActor;
102   }
103
104   void Reset()
105   {
106     mSignalVerified = false;
107   }
108
109   bool& mSignalVerified;
110   Actor mOriginalFocusedActor;
111   Actor mCurrentFocusedActor;
112 };
113
114 // Functors to test whether focus group changed signal is emitted when the keyboard focus group is changed
115 class FocusGroupChangedCallback : public Dali::ConnectionTracker
116 {
117 public:
118   FocusGroupChangedCallback(bool& signalReceived)
119   : mSignalVerified(signalReceived),
120     mCurrentFocusedActor(),
121     mForward(true)
122   {
123   }
124
125   void Callback(Actor currentFocusedActor, bool forward)
126   {
127     tet_infoline("Verifying FocusGroupChangedCallback()");
128
129     mSignalVerified = true;
130
131     mCurrentFocusedActor = currentFocusedActor;
132     mForward = forward;
133   }
134
135   void Reset()
136   {
137     mSignalVerified = false;
138   }
139
140   bool& mSignalVerified;
141   Actor mCurrentFocusedActor;
142   bool mForward;
143 };
144
145 // Functors to test whether focused actor activated signal is emitted when the focused actor is activated
146 class FocusedActorActivatedCallback : public Dali::ConnectionTracker
147 {
148 public:
149   FocusedActorActivatedCallback(bool& signalReceived)
150   : mSignalVerified(signalReceived),
151     mActivatedActor()
152   {
153   }
154
155   void Callback(Actor activatedActor)
156   {
157     tet_infoline("Verifying FocusedActorActivatedCallback()");
158
159     mSignalVerified = true;
160
161     mActivatedActor = activatedActor;
162   }
163
164   void Reset()
165   {
166     mSignalVerified = false;
167   }
168
169   bool& mSignalVerified;
170   Actor mActivatedActor;
171 };
172
173 } // namespace
174
175
176
177 int UtcDaliKeyboardFocusManagerGet(void)
178 {
179   ToolkitTestApplication application;
180
181   tet_infoline(" UtcDaliKeyboardKeyboardFocusManagerGet");
182
183   // Register Type
184   TypeInfo type;
185   type = TypeRegistry::Get().GetTypeInfo( "KeyboardFocusManager" );
186   DALI_TEST_CHECK( type );
187   BaseHandle handle = type.CreateInstance();
188   DALI_TEST_CHECK( handle );
189
190   KeyboardFocusManager manager;
191
192   manager = KeyboardFocusManager::Get();
193   DALI_TEST_CHECK(manager);
194
195   KeyboardFocusManager newManager = KeyboardFocusManager::Get();
196   DALI_TEST_CHECK(newManager);
197
198   // Check that focus manager is a singleton
199   DALI_TEST_CHECK(manager == newManager);
200   END_TEST;
201 }
202
203
204
205 int UtcDaliKeyboardFocusManagerMoveFocus(void)
206 {
207   ToolkitTestApplication application;
208
209   tet_infoline(" UtcDaliKeyboardFocusManagerMoveFocus");
210
211   // Register Type
212   TypeInfo type;
213   type = TypeRegistry::Get().GetTypeInfo( "KeyboardFocusManager" );
214   DALI_TEST_CHECK( type );
215   BaseHandle handle = type.CreateInstance();
216   DALI_TEST_CHECK( handle );
217
218   KeyboardFocusManager manager = KeyboardFocusManager::Get();
219   DALI_TEST_CHECK(manager);
220
221   bool preFocusChangeSignalVerified = false;
222   PreFocusChangeCallback preFocusChangeCallback(preFocusChangeSignalVerified);
223   manager.PreFocusChangeSignal().Connect( &preFocusChangeCallback, &PreFocusChangeCallback::Callback );
224
225   bool focusChangedSignalVerified = false;
226   FocusChangedCallback focusChangedCallback(focusChangedSignalVerified);
227   manager.FocusChangedSignal().Connect( &focusChangedCallback, &FocusChangedCallback::Callback );
228
229   // Create the first actor and add it to the stage
230   Actor first = Actor::New();
231   first.SetKeyboardFocusable(true);
232   Stage::GetCurrent().Add(first);
233
234   // Create the second actor and add it to the stage
235   Actor second = Actor::New();
236   second.SetKeyboardFocusable(true);
237   Stage::GetCurrent().Add(second);
238
239   // Move the focus to the right
240   DALI_TEST_CHECK(manager.MoveFocus(Control::Right) == false);
241
242   // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
243   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
244   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == Actor());
245   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
246   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::Right);
247   preFocusChangeCallback.Reset();
248
249   // Check that the focus is set on the first actor
250   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
251   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
252   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
253   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == Actor());
254   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
255   focusChangedCallback.Reset();
256
257   // Move the focus towards right
258   DALI_TEST_CHECK(manager.MoveFocus(Control::Right) == false);
259
260   // Because no layout control in the stage and the first actor is focused, it should emit the PreFocusChange signal
261   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
262   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == first);
263   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
264   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::Right);
265   preFocusChangeCallback.Reset();
266
267   // Check that the focus is set on the second actor
268   DALI_TEST_CHECK(manager.SetCurrentFocusActor(second) == true);
269   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
270   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
271   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
272   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == second);
273   focusChangedCallback.Reset();
274
275   // Move the focus towards up
276   DALI_TEST_CHECK(manager.MoveFocus(Control::Up) == false);
277
278   // Because no layout control in the stage and no actor is focused, it should emit the PreFocusChange signal
279   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
280   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == second);
281   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
282   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::Up);
283   preFocusChangeCallback.Reset();
284   DALI_TEST_CHECK(!focusChangedCallback.mSignalVerified);
285
286   // Create a 2x2 table view and try to move focus inside it
287   TableView tableView = TableView::New( 2, 2 );
288   Stage::GetCurrent().Add(tableView);
289
290   // Create the third actor
291   Actor third = Actor::New();
292   third.SetKeyboardFocusable(true);
293
294   // Create the fourth actor
295   Actor fourth = Actor::New();
296   fourth.SetKeyboardFocusable(true);
297
298   // Add the four children to table view
299   tableView.AddChild(first, TableView::CellPosition(0, 0));
300   tableView.AddChild(second, TableView::CellPosition(0, 1));
301   tableView.AddChild(third, TableView::CellPosition(1, 0));
302   tableView.AddChild(fourth, TableView::CellPosition(1, 1));
303
304   // Set the focus to the first actor
305   DALI_TEST_CHECK(manager.SetCurrentFocusActor(first) == true);
306   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
307   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
308   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == second);
309   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
310   focusChangedCallback.Reset();
311
312   // Move the focus towards right
313   DALI_TEST_CHECK(manager.MoveFocus(Control::Right) == true);
314   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == second);
315   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
316   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
317   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == second);
318   focusChangedCallback.Reset();
319
320   // Move the focus towards down
321   DALI_TEST_CHECK(manager.MoveFocus(Control::Down) == true);
322   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == fourth);
323   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
324   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == second);
325   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == fourth);
326   focusChangedCallback.Reset();
327
328   // Move the focus towards left
329   DALI_TEST_CHECK(manager.MoveFocus(Control::Left) == true);
330   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == third);
331   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
332   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == fourth);
333   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == third);
334   focusChangedCallback.Reset();
335
336   // Move the focus towards up
337   DALI_TEST_CHECK(manager.MoveFocus(Control::Up) == true);
338   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
339   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
340   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == third);
341   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == first);
342   focusChangedCallback.Reset();
343
344   // Move the focus towards left. The focus move will fail as no way to move it upwards
345   DALI_TEST_CHECK(manager.MoveFocus(Control::Left) == false);
346   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == first);
347   DALI_TEST_CHECK(preFocusChangeCallback.mSignalVerified);
348   DALI_TEST_CHECK(preFocusChangeCallback.mCurrentFocusedActor == first);
349   DALI_TEST_CHECK(preFocusChangeCallback.mProposedActorToFocus == Actor());
350   DALI_TEST_CHECK(preFocusChangeCallback.mDirection == Control::Left);
351   preFocusChangeCallback.Reset();
352   DALI_TEST_CHECK(!focusChangedCallback.mSignalVerified);
353
354   // Enable the loop
355   manager.SetFocusGroupLoop(true);
356   DALI_TEST_CHECK(manager.GetFocusGroupLoop() == true);
357
358   // Move the focus towards left again. The focus should move to the fourth actor.
359   DALI_TEST_CHECK(manager.MoveFocus(Control::Left) == true);
360   DALI_TEST_CHECK(manager.GetCurrentFocusActor() == fourth);
361   DALI_TEST_CHECK(focusChangedCallback.mSignalVerified);
362   DALI_TEST_CHECK(focusChangedCallback.mOriginalFocusedActor == first);
363   DALI_TEST_CHECK(focusChangedCallback.mCurrentFocusedActor == fourth);
364   focusChangedCallback.Reset();
365   END_TEST;
366 }
367
368
369 int UtcDaliKeyboardFocusManagerSignalFocusGroupChanged(void)
370 {
371   ToolkitTestApplication application;
372
373   tet_infoline(" UtcDaliKeyboardFocusManagerSignalFocusGroupChanged");
374
375   // Register Type
376   TypeInfo type;
377   type = TypeRegistry::Get().GetTypeInfo( "KeyboardFocusManager" );
378   DALI_TEST_CHECK( type );
379   BaseHandle handle = type.CreateInstance();
380   DALI_TEST_CHECK( handle );
381
382   KeyboardFocusManager manager = KeyboardFocusManager::Get();
383   DALI_TEST_CHECK(manager);
384
385   bool focusGroupChangedSignalVerified = false;
386   FocusGroupChangedCallback focusGroupChangedCallback(focusGroupChangedSignalVerified);
387   manager.FocusGroupChangedSignal().Connect( &focusGroupChangedCallback, &FocusGroupChangedCallback::Callback );
388
389   Integration::KeyEvent tabEvent("Tab", "", 0, 0, 0, Integration::KeyEvent::Down);
390   Integration::KeyEvent shiftTabEvent("Tab", "", 1, 0, 0, Integration::KeyEvent::Down);
391
392   // Send the tab event to change focus group in the forward direction
393   application.ProcessEvent(tabEvent);
394   DALI_TEST_CHECK(focusGroupChangedCallback.mSignalVerified);
395   DALI_TEST_CHECK(focusGroupChangedCallback.mCurrentFocusedActor == Actor());
396   DALI_TEST_CHECK(focusGroupChangedCallback.mForward == true);
397   focusGroupChangedCallback.Reset();
398
399   // Send the shift tab event to change focus group in the backward direction
400   application.ProcessEvent(shiftTabEvent);
401   DALI_TEST_CHECK(focusGroupChangedCallback.mSignalVerified);
402   DALI_TEST_CHECK(focusGroupChangedCallback.mCurrentFocusedActor == Actor());
403   DALI_TEST_CHECK(focusGroupChangedCallback.mForward == false);
404   focusGroupChangedCallback.Reset();
405   END_TEST;
406 }