1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/views/focus/focus_manager.h"
10 #include "base/command_line.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "ui/aura/client/focus_client.h"
13 #include "ui/aura/window.h"
14 #include "ui/base/accelerators/accelerator.h"
15 #include "ui/base/ime/dummy_text_input_client.h"
16 #include "ui/base/ime/text_input_focus_manager.h"
17 #include "ui/base/ui_base_switches.h"
18 #include "ui/events/keycodes/keyboard_codes.h"
19 #include "ui/views/accessible_pane_view.h"
20 #include "ui/views/controls/button/label_button.h"
21 #include "ui/views/focus/focus_manager_factory.h"
22 #include "ui/views/focus/widget_focus_manager.h"
23 #include "ui/views/test/focus_manager_test.h"
24 #include "ui/views/widget/widget.h"
28 enum FocusTestEventType
{
33 struct FocusTestEvent
{
34 FocusTestEvent(FocusTestEventType type
, int view_id
)
39 FocusTestEventType type
;
43 class SimpleTestView
: public View
{
45 SimpleTestView(std::vector
<FocusTestEvent
>* event_list
, int view_id
)
46 : event_list_(event_list
) {
51 void OnFocus() override
{
52 event_list_
->push_back(FocusTestEvent(ON_FOCUS
, id()));
55 void OnBlur() override
{
56 event_list_
->push_back(FocusTestEvent(ON_BLUR
, id()));
60 std::vector
<FocusTestEvent
>* event_list_
;
62 DISALLOW_COPY_AND_ASSIGN(SimpleTestView
);
65 // Tests that the appropriate Focus related methods are called when a View
67 TEST_F(FocusManagerTest
, ViewFocusCallbacks
) {
68 std::vector
<FocusTestEvent
> event_list
;
69 const int kView1ID
= 1;
70 const int kView2ID
= 2;
72 SimpleTestView
* view1
= new SimpleTestView(&event_list
, kView1ID
);
73 SimpleTestView
* view2
= new SimpleTestView(&event_list
, kView2ID
);
74 GetContentsView()->AddChildView(view1
);
75 GetContentsView()->AddChildView(view2
);
77 view1
->RequestFocus();
78 ASSERT_EQ(1, static_cast<int>(event_list
.size()));
79 EXPECT_EQ(ON_FOCUS
, event_list
[0].type
);
80 EXPECT_EQ(kView1ID
, event_list
[0].view_id
);
83 view2
->RequestFocus();
84 ASSERT_EQ(2, static_cast<int>(event_list
.size()));
85 EXPECT_EQ(ON_BLUR
, event_list
[0].type
);
86 EXPECT_EQ(kView1ID
, event_list
[0].view_id
);
87 EXPECT_EQ(ON_FOCUS
, event_list
[1].type
);
88 EXPECT_EQ(kView2ID
, event_list
[1].view_id
);
91 GetFocusManager()->ClearFocus();
92 ASSERT_EQ(1, static_cast<int>(event_list
.size()));
93 EXPECT_EQ(ON_BLUR
, event_list
[0].type
);
94 EXPECT_EQ(kView2ID
, event_list
[0].view_id
);
97 TEST_F(FocusManagerTest
, FocusChangeListener
) {
98 View
* view1
= new View();
99 view1
->SetFocusable(true);
100 View
* view2
= new View();
101 view2
->SetFocusable(true);
102 GetContentsView()->AddChildView(view1
);
103 GetContentsView()->AddChildView(view2
);
105 TestFocusChangeListener listener
;
106 AddFocusChangeListener(&listener
);
108 // Required for VS2010: http://connect.microsoft.com/VisualStudio/feedback/details/520043/error-converting-from-null-to-a-pointer-type-in-std-pair
109 views::View
* null_view
= NULL
;
111 view1
->RequestFocus();
112 ASSERT_EQ(1, static_cast<int>(listener
.focus_changes().size()));
113 EXPECT_TRUE(listener
.focus_changes()[0] == ViewPair(null_view
, view1
));
114 listener
.ClearFocusChanges();
116 view2
->RequestFocus();
117 ASSERT_EQ(1, static_cast<int>(listener
.focus_changes().size()));
118 EXPECT_TRUE(listener
.focus_changes()[0] == ViewPair(view1
, view2
));
119 listener
.ClearFocusChanges();
121 GetFocusManager()->ClearFocus();
122 ASSERT_EQ(1, static_cast<int>(listener
.focus_changes().size()));
123 EXPECT_TRUE(listener
.focus_changes()[0] == ViewPair(view2
, null_view
));
126 TEST_F(FocusManagerTest
, WidgetFocusChangeListener
) {
127 TestWidgetFocusChangeListener widget_listener
;
128 AddWidgetFocusChangeListener(&widget_listener
);
130 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
131 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
132 params
.bounds
= gfx::Rect(10, 10, 100, 100);
133 params
.parent
= GetWidget()->GetNativeView();
135 scoped_ptr
<Widget
> widget1(new Widget
);
136 widget1
->Init(params
);
139 scoped_ptr
<Widget
> widget2(new Widget
);
140 widget2
->Init(params
);
143 widget_listener
.ClearFocusChanges();
144 gfx::NativeView native_view1
= widget1
->GetNativeView();
145 aura::client::GetFocusClient(native_view1
)->FocusWindow(native_view1
);
146 ASSERT_EQ(2, static_cast<int>(widget_listener
.focus_changes().size()));
147 EXPECT_EQ(native_view1
, widget_listener
.focus_changes()[0].second
);
148 EXPECT_EQ(native_view1
, widget_listener
.focus_changes()[1].second
);
150 widget_listener
.ClearFocusChanges();
151 gfx::NativeView native_view2
= widget2
->GetNativeView();
152 aura::client::GetFocusClient(native_view2
)->FocusWindow(native_view2
);
153 ASSERT_EQ(2, static_cast<int>(widget_listener
.focus_changes().size()));
154 EXPECT_EQ(NativeViewPair(native_view1
, native_view2
),
155 widget_listener
.focus_changes()[0]);
156 EXPECT_EQ(NativeViewPair(native_view1
, native_view2
),
157 widget_listener
.focus_changes()[1]);
160 // Counts accelerator calls.
161 class TestAcceleratorTarget
: public ui::AcceleratorTarget
{
163 explicit TestAcceleratorTarget(bool process_accelerator
)
164 : accelerator_count_(0),
165 process_accelerator_(process_accelerator
),
166 can_handle_accelerators_(true) {}
168 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
{
169 ++accelerator_count_
;
170 return process_accelerator_
;
173 bool CanHandleAccelerators() const override
{
174 return can_handle_accelerators_
;
177 int accelerator_count() const { return accelerator_count_
; }
179 void set_can_handle_accelerators(bool can_handle_accelerators
) {
180 can_handle_accelerators_
= can_handle_accelerators
;
184 int accelerator_count_
; // number of times that the accelerator is activated
185 bool process_accelerator_
; // return value of AcceleratorPressed
186 bool can_handle_accelerators_
; // return value of CanHandleAccelerators
188 DISALLOW_COPY_AND_ASSIGN(TestAcceleratorTarget
);
191 TEST_F(FocusManagerTest
, CallsNormalAcceleratorTarget
) {
192 FocusManager
* focus_manager
= GetFocusManager();
193 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
194 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
196 TestAcceleratorTarget
return_target(true);
197 TestAcceleratorTarget
escape_target(true);
198 EXPECT_EQ(return_target
.accelerator_count(), 0);
199 EXPECT_EQ(escape_target
.accelerator_count(), 0);
201 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
203 focus_manager
->GetCurrentTargetForAccelerator(escape_accelerator
));
206 focus_manager
->RegisterAccelerator(return_accelerator
,
207 ui::AcceleratorManager::kNormalPriority
,
209 focus_manager
->RegisterAccelerator(escape_accelerator
,
210 ui::AcceleratorManager::kNormalPriority
,
213 // Checks if the correct target is registered.
214 EXPECT_EQ(&return_target
,
215 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
216 EXPECT_EQ(&escape_target
,
217 focus_manager
->GetCurrentTargetForAccelerator(escape_accelerator
));
219 // Hitting the return key.
220 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
221 EXPECT_EQ(return_target
.accelerator_count(), 1);
222 EXPECT_EQ(escape_target
.accelerator_count(), 0);
224 // Hitting the escape key.
225 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
226 EXPECT_EQ(return_target
.accelerator_count(), 1);
227 EXPECT_EQ(escape_target
.accelerator_count(), 1);
229 // Register another target for the return key.
230 TestAcceleratorTarget
return_target2(true);
231 EXPECT_EQ(return_target2
.accelerator_count(), 0);
232 focus_manager
->RegisterAccelerator(return_accelerator
,
233 ui::AcceleratorManager::kNormalPriority
,
235 EXPECT_EQ(&return_target2
,
236 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
238 // Hitting the return key; return_target2 has the priority.
239 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
240 EXPECT_EQ(return_target
.accelerator_count(), 1);
241 EXPECT_EQ(return_target2
.accelerator_count(), 1);
243 // Register a target that does not process the accelerator event.
244 TestAcceleratorTarget
return_target3(false);
245 EXPECT_EQ(return_target3
.accelerator_count(), 0);
246 focus_manager
->RegisterAccelerator(return_accelerator
,
247 ui::AcceleratorManager::kNormalPriority
,
249 EXPECT_EQ(&return_target3
,
250 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
252 // Hitting the return key.
253 // Since the event handler of return_target3 returns false, return_target2
254 // should be called too.
255 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
256 EXPECT_EQ(return_target
.accelerator_count(), 1);
257 EXPECT_EQ(return_target2
.accelerator_count(), 2);
258 EXPECT_EQ(return_target3
.accelerator_count(), 1);
260 // Unregister return_target2.
261 focus_manager
->UnregisterAccelerator(return_accelerator
, &return_target2
);
262 EXPECT_EQ(&return_target3
,
263 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
265 // Hitting the return key. return_target3 and return_target should be called.
266 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
267 EXPECT_EQ(return_target
.accelerator_count(), 2);
268 EXPECT_EQ(return_target2
.accelerator_count(), 2);
269 EXPECT_EQ(return_target3
.accelerator_count(), 2);
271 // Unregister targets.
272 focus_manager
->UnregisterAccelerator(return_accelerator
, &return_target
);
273 focus_manager
->UnregisterAccelerator(return_accelerator
, &return_target3
);
274 focus_manager
->UnregisterAccelerator(escape_accelerator
, &escape_target
);
276 // Now there is no target registered.
278 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
280 focus_manager
->GetCurrentTargetForAccelerator(escape_accelerator
));
282 // Hitting the return key and the escape key. Nothing should happen.
283 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
284 EXPECT_EQ(return_target
.accelerator_count(), 2);
285 EXPECT_EQ(return_target2
.accelerator_count(), 2);
286 EXPECT_EQ(return_target3
.accelerator_count(), 2);
287 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
288 EXPECT_EQ(escape_target
.accelerator_count(), 1);
291 TEST_F(FocusManagerTest
, HighPriorityHandlers
) {
292 FocusManager
* focus_manager
= GetFocusManager();
293 ui::Accelerator
escape_accelerator(ui::VKEY_ESCAPE
, ui::EF_NONE
);
295 TestAcceleratorTarget
escape_target_high(true);
296 TestAcceleratorTarget
escape_target_normal(true);
297 EXPECT_EQ(escape_target_high
.accelerator_count(), 0);
298 EXPECT_EQ(escape_target_normal
.accelerator_count(), 0);
300 focus_manager
->GetCurrentTargetForAccelerator(escape_accelerator
));
301 EXPECT_FALSE(focus_manager
->HasPriorityHandler(escape_accelerator
));
303 // Register high priority target.
304 focus_manager
->RegisterAccelerator(escape_accelerator
,
305 ui::AcceleratorManager::kHighPriority
,
306 &escape_target_high
);
307 EXPECT_EQ(&escape_target_high
,
308 focus_manager
->GetCurrentTargetForAccelerator(escape_accelerator
));
309 EXPECT_TRUE(focus_manager
->HasPriorityHandler(escape_accelerator
));
311 // Hit the escape key.
312 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
313 EXPECT_EQ(escape_target_high
.accelerator_count(), 1);
314 EXPECT_EQ(escape_target_normal
.accelerator_count(), 0);
316 // Add a normal priority target and make sure it doesn't see the key.
317 focus_manager
->RegisterAccelerator(escape_accelerator
,
318 ui::AcceleratorManager::kNormalPriority
,
319 &escape_target_normal
);
321 // Checks if the correct target is registered (same as before, the high
323 EXPECT_EQ(&escape_target_high
,
324 focus_manager
->GetCurrentTargetForAccelerator(escape_accelerator
));
325 EXPECT_TRUE(focus_manager
->HasPriorityHandler(escape_accelerator
));
327 // Hit the escape key.
328 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
329 EXPECT_EQ(escape_target_high
.accelerator_count(), 2);
330 EXPECT_EQ(escape_target_normal
.accelerator_count(), 0);
332 // Unregister the high priority accelerator.
333 focus_manager
->UnregisterAccelerator(escape_accelerator
, &escape_target_high
);
334 EXPECT_EQ(&escape_target_normal
,
335 focus_manager
->GetCurrentTargetForAccelerator(escape_accelerator
));
336 EXPECT_FALSE(focus_manager
->HasPriorityHandler(escape_accelerator
));
338 // Hit the escape key.
339 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
340 EXPECT_EQ(escape_target_high
.accelerator_count(), 2);
341 EXPECT_EQ(escape_target_normal
.accelerator_count(), 1);
343 // Add the high priority target back and make sure it starts seeing the key.
344 focus_manager
->RegisterAccelerator(escape_accelerator
,
345 ui::AcceleratorManager::kHighPriority
,
346 &escape_target_high
);
347 EXPECT_EQ(&escape_target_high
,
348 focus_manager
->GetCurrentTargetForAccelerator(escape_accelerator
));
349 EXPECT_TRUE(focus_manager
->HasPriorityHandler(escape_accelerator
));
351 // Hit the escape key.
352 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
353 EXPECT_EQ(escape_target_high
.accelerator_count(), 3);
354 EXPECT_EQ(escape_target_normal
.accelerator_count(), 1);
356 // Unregister the normal priority accelerator.
357 focus_manager
->UnregisterAccelerator(
358 escape_accelerator
, &escape_target_normal
);
359 EXPECT_EQ(&escape_target_high
,
360 focus_manager
->GetCurrentTargetForAccelerator(escape_accelerator
));
361 EXPECT_TRUE(focus_manager
->HasPriorityHandler(escape_accelerator
));
363 // Hit the escape key.
364 EXPECT_TRUE(focus_manager
->ProcessAccelerator(escape_accelerator
));
365 EXPECT_EQ(escape_target_high
.accelerator_count(), 4);
366 EXPECT_EQ(escape_target_normal
.accelerator_count(), 1);
368 // Unregister the high priority accelerator.
369 focus_manager
->UnregisterAccelerator(escape_accelerator
, &escape_target_high
);
371 focus_manager
->GetCurrentTargetForAccelerator(escape_accelerator
));
372 EXPECT_FALSE(focus_manager
->HasPriorityHandler(escape_accelerator
));
374 // Hit the escape key (no change, no targets registered).
375 EXPECT_FALSE(focus_manager
->ProcessAccelerator(escape_accelerator
));
376 EXPECT_EQ(escape_target_high
.accelerator_count(), 4);
377 EXPECT_EQ(escape_target_normal
.accelerator_count(), 1);
380 TEST_F(FocusManagerTest
, CallsEnabledAcceleratorTargetsOnly
) {
381 FocusManager
* focus_manager
= GetFocusManager();
382 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
384 TestAcceleratorTarget
return_target1(true);
385 TestAcceleratorTarget
return_target2(true);
387 focus_manager
->RegisterAccelerator(return_accelerator
,
388 ui::AcceleratorManager::kNormalPriority
,
390 focus_manager
->RegisterAccelerator(return_accelerator
,
391 ui::AcceleratorManager::kNormalPriority
,
393 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
394 EXPECT_EQ(0, return_target1
.accelerator_count());
395 EXPECT_EQ(1, return_target2
.accelerator_count());
397 // If CanHandleAccelerators() return false, FocusManager shouldn't call
398 // AcceleratorPressed().
399 return_target2
.set_can_handle_accelerators(false);
400 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
401 EXPECT_EQ(1, return_target1
.accelerator_count());
402 EXPECT_EQ(1, return_target2
.accelerator_count());
404 // If no accelerator targets are enabled, ProcessAccelerator() should fail.
405 return_target1
.set_can_handle_accelerators(false);
406 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
407 EXPECT_EQ(1, return_target1
.accelerator_count());
408 EXPECT_EQ(1, return_target2
.accelerator_count());
410 // Enabling the target again causes the accelerators to be processed again.
411 return_target1
.set_can_handle_accelerators(true);
412 return_target2
.set_can_handle_accelerators(true);
413 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
414 EXPECT_EQ(1, return_target1
.accelerator_count());
415 EXPECT_EQ(2, return_target2
.accelerator_count());
418 // Unregisters itself when its accelerator is invoked.
419 class SelfUnregisteringAcceleratorTarget
: public ui::AcceleratorTarget
{
421 SelfUnregisteringAcceleratorTarget(ui::Accelerator accelerator
,
422 FocusManager
* focus_manager
)
423 : accelerator_(accelerator
),
424 focus_manager_(focus_manager
),
425 accelerator_count_(0) {
428 bool AcceleratorPressed(const ui::Accelerator
& accelerator
) override
{
429 ++accelerator_count_
;
430 focus_manager_
->UnregisterAccelerator(accelerator
, this);
434 bool CanHandleAccelerators() const override
{ return true; }
436 int accelerator_count() const { return accelerator_count_
; }
439 ui::Accelerator accelerator_
;
440 FocusManager
* focus_manager_
;
441 int accelerator_count_
;
443 DISALLOW_COPY_AND_ASSIGN(SelfUnregisteringAcceleratorTarget
);
446 TEST_F(FocusManagerTest
, CallsSelfDeletingAcceleratorTarget
) {
447 FocusManager
* focus_manager
= GetFocusManager();
448 ui::Accelerator
return_accelerator(ui::VKEY_RETURN
, ui::EF_NONE
);
449 SelfUnregisteringAcceleratorTarget
target(return_accelerator
, focus_manager
);
450 EXPECT_EQ(target
.accelerator_count(), 0);
452 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
454 // Register the target.
455 focus_manager
->RegisterAccelerator(return_accelerator
,
456 ui::AcceleratorManager::kNormalPriority
,
459 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
461 // Hitting the return key. The target will be unregistered.
462 EXPECT_TRUE(focus_manager
->ProcessAccelerator(return_accelerator
));
463 EXPECT_EQ(target
.accelerator_count(), 1);
465 focus_manager
->GetCurrentTargetForAccelerator(return_accelerator
));
467 // Hitting the return key again; nothing should happen.
468 EXPECT_FALSE(focus_manager
->ProcessAccelerator(return_accelerator
));
469 EXPECT_EQ(target
.accelerator_count(), 1);
472 TEST_F(FocusManagerTest
, SuspendAccelerators
) {
473 const ui::KeyEvent
event(ui::ET_KEY_PRESSED
, ui::VKEY_RETURN
, ui::EF_NONE
);
474 ui::Accelerator
accelerator(event
.key_code(), event
.flags());
475 TestAcceleratorTarget
target(true);
476 FocusManager
* focus_manager
= GetFocusManager();
477 focus_manager
->RegisterAccelerator(accelerator
,
478 ui::AcceleratorManager::kNormalPriority
,
481 focus_manager
->set_shortcut_handling_suspended(true);
482 EXPECT_TRUE(focus_manager
->OnKeyEvent(event
));
483 EXPECT_EQ(0, target
.accelerator_count());
485 focus_manager
->set_shortcut_handling_suspended(false);
486 EXPECT_FALSE(focus_manager
->OnKeyEvent(event
));
487 EXPECT_EQ(1, target
.accelerator_count());
490 class FocusManagerDtorTest
: public FocusManagerTest
{
492 typedef std::vector
<std::string
> DtorTrackVector
;
494 class FocusManagerDtorTracked
: public FocusManager
{
496 FocusManagerDtorTracked(Widget
* widget
, DtorTrackVector
* dtor_tracker
)
497 : FocusManager(widget
, NULL
/* delegate */),
498 dtor_tracker_(dtor_tracker
) {
501 ~FocusManagerDtorTracked() override
{
502 dtor_tracker_
->push_back("FocusManagerDtorTracked");
505 DtorTrackVector
* dtor_tracker_
;
508 DISALLOW_COPY_AND_ASSIGN(FocusManagerDtorTracked
);
511 class TestFocusManagerFactory
: public FocusManagerFactory
{
513 explicit TestFocusManagerFactory(DtorTrackVector
* dtor_tracker
)
514 : dtor_tracker_(dtor_tracker
) {
517 FocusManager
* CreateFocusManager(Widget
* widget
,
518 bool desktop_widget
) override
{
519 return new FocusManagerDtorTracked(widget
, dtor_tracker_
);
523 DtorTrackVector
* dtor_tracker_
;
524 DISALLOW_COPY_AND_ASSIGN(TestFocusManagerFactory
);
527 class LabelButtonDtorTracked
: public LabelButton
{
529 LabelButtonDtorTracked(const base::string16
& text
,
530 DtorTrackVector
* dtor_tracker
)
531 : LabelButton(NULL
, text
),
532 dtor_tracker_(dtor_tracker
) {
533 SetStyle(STYLE_BUTTON
);
535 ~LabelButtonDtorTracked() override
{
536 dtor_tracker_
->push_back("LabelButtonDtorTracked");
539 DtorTrackVector
* dtor_tracker_
;
542 class WindowDtorTracked
: public Widget
{
544 explicit WindowDtorTracked(DtorTrackVector
* dtor_tracker
)
545 : dtor_tracker_(dtor_tracker
) {
548 ~WindowDtorTracked() override
{
549 dtor_tracker_
->push_back("WindowDtorTracked");
552 DtorTrackVector
* dtor_tracker_
;
555 void SetUp() override
{
556 ViewsTestBase::SetUp();
557 FocusManagerFactory::Install(new TestFocusManagerFactory(&dtor_tracker_
));
558 // Create WindowDtorTracked that uses FocusManagerDtorTracked.
559 Widget
* widget
= new WindowDtorTracked(&dtor_tracker_
);
560 Widget::InitParams params
;
561 params
.delegate
= this;
562 params
.bounds
= gfx::Rect(0, 0, 100, 100);
563 widget
->Init(params
);
565 tracked_focus_manager_
=
566 static_cast<FocusManagerDtorTracked
*>(GetFocusManager());
570 void TearDown() override
{
571 FocusManagerFactory::Install(NULL
);
572 ViewsTestBase::TearDown();
575 FocusManager
* tracked_focus_manager_
;
576 DtorTrackVector dtor_tracker_
;
581 class FocusInAboutToRequestFocusFromTabTraversalView
: public View
{
583 FocusInAboutToRequestFocusFromTabTraversalView() : view_to_focus_(NULL
) {}
585 void set_view_to_focus(View
* view
) { view_to_focus_
= view
; }
587 void AboutToRequestFocusFromTabTraversal(bool reverse
) override
{
588 view_to_focus_
->RequestFocus();
592 views::View
* view_to_focus_
;
594 DISALLOW_COPY_AND_ASSIGN(FocusInAboutToRequestFocusFromTabTraversalView
);
598 // Verifies a focus change done during a call to
599 // AboutToRequestFocusFromTabTraversal() is honored.
600 TEST_F(FocusManagerTest
, FocusInAboutToRequestFocusFromTabTraversal
) {
601 // Create 3 views focuses the 3 and advances to the second. The 2nd views
602 // implementation of AboutToRequestFocusFromTabTraversal() focuses the first.
603 views::View
* v1
= new View
;
604 v1
->SetFocusable(true);
605 GetContentsView()->AddChildView(v1
);
607 FocusInAboutToRequestFocusFromTabTraversalView
* v2
=
608 new FocusInAboutToRequestFocusFromTabTraversalView
;
609 v2
->SetFocusable(true);
610 v2
->set_view_to_focus(v1
);
611 GetContentsView()->AddChildView(v2
);
613 views::View
* v3
= new View
;
614 v3
->SetFocusable(true);
615 GetContentsView()->AddChildView(v3
);
618 GetWidget()->GetFocusManager()->AdvanceFocus(true);
619 EXPECT_TRUE(v1
->HasFocus());
622 TEST_F(FocusManagerTest
, RotatePaneFocus
) {
623 views::AccessiblePaneView
* pane1
= new AccessiblePaneView();
624 GetContentsView()->AddChildView(pane1
);
626 views::View
* v1
= new View
;
627 v1
->SetFocusable(true);
628 pane1
->AddChildView(v1
);
630 views::View
* v2
= new View
;
631 v2
->SetFocusable(true);
632 pane1
->AddChildView(v2
);
634 views::AccessiblePaneView
* pane2
= new AccessiblePaneView();
635 GetContentsView()->AddChildView(pane2
);
637 views::View
* v3
= new View
;
638 v3
->SetFocusable(true);
639 pane2
->AddChildView(v3
);
641 views::View
* v4
= new View
;
642 v4
->SetFocusable(true);
643 pane2
->AddChildView(v4
);
645 std::vector
<views::View
*> panes
;
646 panes
.push_back(pane1
);
647 panes
.push_back(pane2
);
648 SetAccessiblePanes(panes
);
650 FocusManager
* focus_manager
= GetWidget()->GetFocusManager();
652 // Advance forwards. Focus should stay trapped within each pane.
653 EXPECT_TRUE(focus_manager
->RotatePaneFocus(
654 FocusManager::kForward
, FocusManager::kWrap
));
655 EXPECT_EQ(v1
, focus_manager
->GetFocusedView());
656 focus_manager
->AdvanceFocus(false);
657 EXPECT_EQ(v2
, focus_manager
->GetFocusedView());
658 focus_manager
->AdvanceFocus(false);
659 EXPECT_EQ(v1
, focus_manager
->GetFocusedView());
661 EXPECT_TRUE(focus_manager
->RotatePaneFocus(
662 FocusManager::kForward
, FocusManager::kWrap
));
663 EXPECT_EQ(v3
, focus_manager
->GetFocusedView());
664 focus_manager
->AdvanceFocus(false);
665 EXPECT_EQ(v4
, focus_manager
->GetFocusedView());
666 focus_manager
->AdvanceFocus(false);
667 EXPECT_EQ(v3
, focus_manager
->GetFocusedView());
669 EXPECT_TRUE(focus_manager
->RotatePaneFocus(
670 FocusManager::kForward
, FocusManager::kWrap
));
671 EXPECT_EQ(v1
, focus_manager
->GetFocusedView());
673 // Advance backwards.
674 EXPECT_TRUE(focus_manager
->RotatePaneFocus(
675 FocusManager::kBackward
, FocusManager::kWrap
));
676 EXPECT_EQ(v3
, focus_manager
->GetFocusedView());
678 EXPECT_TRUE(focus_manager
->RotatePaneFocus(
679 FocusManager::kBackward
, FocusManager::kWrap
));
680 EXPECT_EQ(v1
, focus_manager
->GetFocusedView());
682 // Advance without wrap. When it gets to the end of the list of
683 // panes, RotatePaneFocus should return false but the current
684 // focused view shouldn't change.
685 EXPECT_TRUE(focus_manager
->RotatePaneFocus(
686 FocusManager::kForward
, FocusManager::kNoWrap
));
687 EXPECT_EQ(v3
, focus_manager
->GetFocusedView());
689 EXPECT_FALSE(focus_manager
->RotatePaneFocus(
690 FocusManager::kForward
, FocusManager::kNoWrap
));
691 EXPECT_EQ(v3
, focus_manager
->GetFocusedView());
694 // Verifies the stored focus view tracks the focused view.
695 TEST_F(FocusManagerTest
, ImplicitlyStoresFocus
) {
696 views::View
* v1
= new View
;
697 v1
->SetFocusable(true);
698 GetContentsView()->AddChildView(v1
);
700 views::View
* v2
= new View
;
701 v2
->SetFocusable(true);
702 GetContentsView()->AddChildView(v2
);
704 // Verify a focus request on |v1| implicitly updates the stored focus view.
706 EXPECT_TRUE(v1
->HasFocus());
707 EXPECT_EQ(v1
, GetWidget()->GetFocusManager()->GetStoredFocusView());
709 // Verify a focus request on |v2| implicitly updates the stored focus view.
711 EXPECT_TRUE(v2
->HasFocus());
712 EXPECT_EQ(v2
, GetWidget()->GetFocusManager()->GetStoredFocusView());
717 class FocusManagerArrowKeyTraversalTest
: public FocusManagerTest
{
719 FocusManagerArrowKeyTraversalTest()
720 : previous_arrow_key_traversal_enabled_(false) {
722 ~FocusManagerArrowKeyTraversalTest() override
{}
724 // FocusManagerTest overrides:
725 void SetUp() override
{
726 FocusManagerTest::SetUp();
728 previous_arrow_key_traversal_enabled_
=
729 FocusManager::arrow_key_traversal_enabled();
731 void TearDown() override
{
732 FocusManager::set_arrow_key_traversal_enabled(
733 previous_arrow_key_traversal_enabled_
);
734 FocusManagerTest::TearDown();
738 bool previous_arrow_key_traversal_enabled_
;
740 DISALLOW_COPY_AND_ASSIGN(FocusManagerArrowKeyTraversalTest
);
745 TEST_F(FocusManagerArrowKeyTraversalTest
, ArrowKeyTraversal
) {
746 FocusManager
* focus_manager
= GetFocusManager();
747 const ui::KeyEvent
left_key(ui::ET_KEY_PRESSED
, ui::VKEY_LEFT
, ui::EF_NONE
);
748 const ui::KeyEvent
right_key(ui::ET_KEY_PRESSED
, ui::VKEY_RIGHT
, ui::EF_NONE
);
749 const ui::KeyEvent
up_key(ui::ET_KEY_PRESSED
, ui::VKEY_UP
, ui::EF_NONE
);
750 const ui::KeyEvent
down_key(ui::ET_KEY_PRESSED
, ui::VKEY_DOWN
, ui::EF_NONE
);
752 std::vector
<views::View
*> v
;
753 for (size_t i
= 0; i
< 2; ++i
) {
754 views::View
* view
= new View
;
755 view
->SetFocusable(true);
756 GetContentsView()->AddChildView(view
);
760 // Arrow key traversal is off and arrow key does not change focus.
761 FocusManager::set_arrow_key_traversal_enabled(false);
762 v
[0]->RequestFocus();
763 focus_manager
->OnKeyEvent(right_key
);
764 EXPECT_EQ(v
[0], focus_manager
->GetFocusedView());
765 focus_manager
->OnKeyEvent(left_key
);
766 EXPECT_EQ(v
[0], focus_manager
->GetFocusedView());
767 focus_manager
->OnKeyEvent(down_key
);
768 EXPECT_EQ(v
[0], focus_manager
->GetFocusedView());
769 focus_manager
->OnKeyEvent(up_key
);
770 EXPECT_EQ(v
[0], focus_manager
->GetFocusedView());
772 // Turn on arrow key traversal.
773 FocusManager::set_arrow_key_traversal_enabled(true);
774 v
[0]->RequestFocus();
775 focus_manager
->OnKeyEvent(right_key
);
776 EXPECT_EQ(v
[1], focus_manager
->GetFocusedView());
777 focus_manager
->OnKeyEvent(left_key
);
778 EXPECT_EQ(v
[0], focus_manager
->GetFocusedView());
779 focus_manager
->OnKeyEvent(down_key
);
780 EXPECT_EQ(v
[1], focus_manager
->GetFocusedView());
781 focus_manager
->OnKeyEvent(up_key
);
782 EXPECT_EQ(v
[0], focus_manager
->GetFocusedView());
785 TEST_F(FocusManagerTest
, StoreFocusedView
) {
787 GetFocusManager()->SetFocusedView(&view
);
788 GetFocusManager()->StoreFocusedView(false);
789 EXPECT_EQ(NULL
, GetFocusManager()->GetFocusedView());
790 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView());
791 EXPECT_EQ(&view
, GetFocusManager()->GetStoredFocusView());
793 // Repeat with |true|.
794 GetFocusManager()->SetFocusedView(&view
);
795 GetFocusManager()->StoreFocusedView(true);
796 EXPECT_EQ(NULL
, GetFocusManager()->GetFocusedView());
797 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView());
798 EXPECT_EQ(&view
, GetFocusManager()->GetStoredFocusView());
801 class TextInputTestView
: public View
{
803 TextInputTestView() {}
805 ui::TextInputClient
* GetTextInputClient() override
{
806 return &text_input_client_
;
810 ui::DummyTextInputClient text_input_client_
;
812 DISALLOW_COPY_AND_ASSIGN(TextInputTestView
);
815 TEST_F(FocusManagerTest
, TextInputClient
) {
816 base::CommandLine
* cmd_line
= base::CommandLine::ForCurrentProcess();
817 cmd_line
->AppendSwitch(switches::kEnableTextInputFocusManager
);
819 View
* view
= new TextInputTestView
;
820 ui::TextInputClient
* text_input_client
= view
->GetTextInputClient();
821 view
->SetFocusable(true);
822 GetContentsView()->AddChildView(view
);
823 ui::TextInputFocusManager
* text_input_focus_manager
=
824 ui::TextInputFocusManager::GetInstance();
826 GetFocusManager()->SetFocusedView(view
);
827 EXPECT_EQ(view
, GetFocusManager()->GetFocusedView());
828 EXPECT_EQ(text_input_client
,
829 text_input_focus_manager
->GetFocusedTextInputClient());
830 GetFocusManager()->StoreFocusedView(false);
831 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView());
832 EXPECT_EQ(text_input_client
,
833 text_input_focus_manager
->GetFocusedTextInputClient());
835 // Repeat with |true|.
836 GetFocusManager()->SetFocusedView(view
);
837 EXPECT_EQ(view
, GetFocusManager()->GetFocusedView());
838 EXPECT_EQ(text_input_client
,
839 text_input_focus_manager
->GetFocusedTextInputClient());
840 GetFocusManager()->StoreFocusedView(true);
841 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView());
842 EXPECT_EQ(text_input_client
,
843 text_input_focus_manager
->GetFocusedTextInputClient());
845 // Focus the view twice in a row.
846 GetFocusManager()->SetFocusedView(view
);
847 EXPECT_EQ(text_input_client
,
848 text_input_focus_manager
->GetFocusedTextInputClient());
849 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(NULL
);
850 GetFocusManager()->SetFocusedView(view
);
851 EXPECT_EQ(text_input_client
,
852 text_input_focus_manager
->GetFocusedTextInputClient());
857 // Trivial WidgetDelegate implementation that allows setting return value of
858 // ShouldAdvanceFocusToTopLevelWidget().
859 class AdvanceFocusWidgetDelegate
: public WidgetDelegate
{
861 explicit AdvanceFocusWidgetDelegate(Widget
* widget
)
863 should_advance_focus_to_parent_(false) {}
864 ~AdvanceFocusWidgetDelegate() override
{}
866 void set_should_advance_focus_to_parent(bool value
) {
867 should_advance_focus_to_parent_
= value
;
870 // WidgetDelegate overrides:
871 bool ShouldAdvanceFocusToTopLevelWidget() const override
{
872 return should_advance_focus_to_parent_
;
874 Widget
* GetWidget() override
{ return widget_
; }
875 const Widget
* GetWidget() const override
{ return widget_
; }
879 bool should_advance_focus_to_parent_
;
881 DISALLOW_COPY_AND_ASSIGN(AdvanceFocusWidgetDelegate
);
886 // Verifies focus wrapping happens in the same widget.
887 TEST_F(FocusManagerTest
, AdvanceFocusStaysInWidget
) {
888 // Add |widget_view| as a child of the Widget.
889 View
* widget_view
= new View
;
890 widget_view
->SetFocusable(true);
891 widget_view
->SetBounds(20, 0, 20, 20);
892 GetContentsView()->AddChildView(widget_view
);
894 // Create a widget with two views, focus the second.
895 scoped_ptr
<AdvanceFocusWidgetDelegate
> delegate
;
896 Widget::InitParams params
= CreateParams(Widget::InitParams::TYPE_WINDOW
);
897 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
899 params
.bounds
= gfx::Rect(10, 10, 100, 100);
900 params
.parent
= GetWidget()->GetNativeView();
902 delegate
.reset(new AdvanceFocusWidgetDelegate(&child_widget
));
903 params
.delegate
= delegate
.get();
904 child_widget
.Init(params
);
905 View
* view1
= new View
;
906 view1
->SetFocusable(true);
907 view1
->SetBounds(0, 0, 20, 20);
908 View
* view2
= new View
;
909 view2
->SetFocusable(true);
910 view2
->SetBounds(20, 0, 20, 20);
911 child_widget
.client_view()->AddChildView(view1
);
912 child_widget
.client_view()->AddChildView(view2
);
914 view2
->RequestFocus();
915 EXPECT_EQ(view2
, GetFocusManager()->GetFocusedView());
917 // Advance focus backwards, which should focus the first.
918 GetFocusManager()->AdvanceFocus(false);
919 EXPECT_EQ(view1
, GetFocusManager()->GetFocusedView());
921 // Focus forward to |view2|.
922 GetFocusManager()->AdvanceFocus(true);
923 EXPECT_EQ(view2
, GetFocusManager()->GetFocusedView());
925 // And forward again, wrapping back to |view1|.
926 GetFocusManager()->AdvanceFocus(true);
927 EXPECT_EQ(view1
, GetFocusManager()->GetFocusedView());
929 // Allow focus to go to the parent, and focus backwards which should now move
930 // up |widget_view| (in the parent).
931 delegate
->set_should_advance_focus_to_parent(true);
932 GetFocusManager()->AdvanceFocus(true);
933 EXPECT_EQ(widget_view
, GetFocusManager()->GetFocusedView());