Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / ui / views / focus / focus_manager_unittest.cc
blob32b8665925646f3f949002e19ba1fa159a7c6548
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"
7 #include <utility>
8 #include <vector>
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"
26 namespace views {
28 enum FocusTestEventType {
29 ON_FOCUS = 0,
30 ON_BLUR
33 struct FocusTestEvent {
34 FocusTestEvent(FocusTestEventType type, int view_id)
35 : type(type),
36 view_id(view_id) {
39 FocusTestEventType type;
40 int view_id;
43 class SimpleTestView : public View {
44 public:
45 SimpleTestView(std::vector<FocusTestEvent>* event_list, int view_id)
46 : event_list_(event_list) {
47 SetFocusable(true);
48 set_id(view_id);
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()));
59 private:
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
66 // gets/loses focus.
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);
82 event_list.clear();
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);
90 event_list.clear();
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);
137 widget1->Show();
139 scoped_ptr<Widget> widget2(new Widget);
140 widget2->Init(params);
141 widget2->Show();
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 {
162 public:
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;
183 private:
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);
200 EXPECT_EQ(NULL,
201 focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
202 EXPECT_EQ(NULL,
203 focus_manager->GetCurrentTargetForAccelerator(escape_accelerator));
205 // Register targets.
206 focus_manager->RegisterAccelerator(return_accelerator,
207 ui::AcceleratorManager::kNormalPriority,
208 &return_target);
209 focus_manager->RegisterAccelerator(escape_accelerator,
210 ui::AcceleratorManager::kNormalPriority,
211 &escape_target);
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,
234 &return_target2);
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,
248 &return_target3);
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.
277 EXPECT_EQ(NULL,
278 focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
279 EXPECT_EQ(NULL,
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);
299 EXPECT_EQ(NULL,
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
322 // priority one).
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);
370 EXPECT_EQ(NULL,
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,
389 &return_target1);
390 focus_manager->RegisterAccelerator(return_accelerator,
391 ui::AcceleratorManager::kNormalPriority,
392 &return_target2);
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 {
420 public:
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);
431 return true;
434 bool CanHandleAccelerators() const override { return true; }
436 int accelerator_count() const { return accelerator_count_; }
438 private:
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);
451 EXPECT_EQ(NULL,
452 focus_manager->GetCurrentTargetForAccelerator(return_accelerator));
454 // Register the target.
455 focus_manager->RegisterAccelerator(return_accelerator,
456 ui::AcceleratorManager::kNormalPriority,
457 &target);
458 EXPECT_EQ(&target,
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);
464 EXPECT_EQ(NULL,
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 class FocusManagerDtorTest : public FocusManagerTest {
473 protected:
474 typedef std::vector<std::string> DtorTrackVector;
476 class FocusManagerDtorTracked : public FocusManager {
477 public:
478 FocusManagerDtorTracked(Widget* widget, DtorTrackVector* dtor_tracker)
479 : FocusManager(widget, NULL /* delegate */),
480 dtor_tracker_(dtor_tracker) {
483 ~FocusManagerDtorTracked() override {
484 dtor_tracker_->push_back("FocusManagerDtorTracked");
487 DtorTrackVector* dtor_tracker_;
489 private:
490 DISALLOW_COPY_AND_ASSIGN(FocusManagerDtorTracked);
493 class TestFocusManagerFactory : public FocusManagerFactory {
494 public:
495 explicit TestFocusManagerFactory(DtorTrackVector* dtor_tracker)
496 : dtor_tracker_(dtor_tracker) {
499 FocusManager* CreateFocusManager(Widget* widget,
500 bool desktop_widget) override {
501 return new FocusManagerDtorTracked(widget, dtor_tracker_);
504 private:
505 DtorTrackVector* dtor_tracker_;
506 DISALLOW_COPY_AND_ASSIGN(TestFocusManagerFactory);
509 class LabelButtonDtorTracked : public LabelButton {
510 public:
511 LabelButtonDtorTracked(const base::string16& text,
512 DtorTrackVector* dtor_tracker)
513 : LabelButton(NULL, text),
514 dtor_tracker_(dtor_tracker) {
515 SetStyle(STYLE_BUTTON);
517 ~LabelButtonDtorTracked() override {
518 dtor_tracker_->push_back("LabelButtonDtorTracked");
521 DtorTrackVector* dtor_tracker_;
524 class WindowDtorTracked : public Widget {
525 public:
526 explicit WindowDtorTracked(DtorTrackVector* dtor_tracker)
527 : dtor_tracker_(dtor_tracker) {
530 ~WindowDtorTracked() override {
531 dtor_tracker_->push_back("WindowDtorTracked");
534 DtorTrackVector* dtor_tracker_;
537 void SetUp() override {
538 ViewsTestBase::SetUp();
539 FocusManagerFactory::Install(new TestFocusManagerFactory(&dtor_tracker_));
540 // Create WindowDtorTracked that uses FocusManagerDtorTracked.
541 Widget* widget = new WindowDtorTracked(&dtor_tracker_);
542 Widget::InitParams params;
543 params.delegate = this;
544 params.bounds = gfx::Rect(0, 0, 100, 100);
545 widget->Init(params);
547 tracked_focus_manager_ =
548 static_cast<FocusManagerDtorTracked*>(GetFocusManager());
549 widget->Show();
552 void TearDown() override {
553 FocusManagerFactory::Install(NULL);
554 ViewsTestBase::TearDown();
557 FocusManager* tracked_focus_manager_;
558 DtorTrackVector dtor_tracker_;
561 namespace {
563 class FocusInAboutToRequestFocusFromTabTraversalView : public View {
564 public:
565 FocusInAboutToRequestFocusFromTabTraversalView() : view_to_focus_(NULL) {}
567 void set_view_to_focus(View* view) { view_to_focus_ = view; }
569 void AboutToRequestFocusFromTabTraversal(bool reverse) override {
570 view_to_focus_->RequestFocus();
573 private:
574 views::View* view_to_focus_;
576 DISALLOW_COPY_AND_ASSIGN(FocusInAboutToRequestFocusFromTabTraversalView);
578 } // namespace
580 // Verifies a focus change done during a call to
581 // AboutToRequestFocusFromTabTraversal() is honored.
582 TEST_F(FocusManagerTest, FocusInAboutToRequestFocusFromTabTraversal) {
583 // Create 3 views focuses the 3 and advances to the second. The 2nd views
584 // implementation of AboutToRequestFocusFromTabTraversal() focuses the first.
585 views::View* v1 = new View;
586 v1->SetFocusable(true);
587 GetContentsView()->AddChildView(v1);
589 FocusInAboutToRequestFocusFromTabTraversalView* v2 =
590 new FocusInAboutToRequestFocusFromTabTraversalView;
591 v2->SetFocusable(true);
592 v2->set_view_to_focus(v1);
593 GetContentsView()->AddChildView(v2);
595 views::View* v3 = new View;
596 v3->SetFocusable(true);
597 GetContentsView()->AddChildView(v3);
599 v3->RequestFocus();
600 GetWidget()->GetFocusManager()->AdvanceFocus(true);
601 EXPECT_TRUE(v1->HasFocus());
604 TEST_F(FocusManagerTest, RotatePaneFocus) {
605 views::AccessiblePaneView* pane1 = new AccessiblePaneView();
606 GetContentsView()->AddChildView(pane1);
608 views::View* v1 = new View;
609 v1->SetFocusable(true);
610 pane1->AddChildView(v1);
612 views::View* v2 = new View;
613 v2->SetFocusable(true);
614 pane1->AddChildView(v2);
616 views::AccessiblePaneView* pane2 = new AccessiblePaneView();
617 GetContentsView()->AddChildView(pane2);
619 views::View* v3 = new View;
620 v3->SetFocusable(true);
621 pane2->AddChildView(v3);
623 views::View* v4 = new View;
624 v4->SetFocusable(true);
625 pane2->AddChildView(v4);
627 std::vector<views::View*> panes;
628 panes.push_back(pane1);
629 panes.push_back(pane2);
630 SetAccessiblePanes(panes);
632 FocusManager* focus_manager = GetWidget()->GetFocusManager();
634 // Advance forwards. Focus should stay trapped within each pane.
635 EXPECT_TRUE(focus_manager->RotatePaneFocus(
636 FocusManager::kForward, FocusManager::kWrap));
637 EXPECT_EQ(v1, focus_manager->GetFocusedView());
638 focus_manager->AdvanceFocus(false);
639 EXPECT_EQ(v2, focus_manager->GetFocusedView());
640 focus_manager->AdvanceFocus(false);
641 EXPECT_EQ(v1, focus_manager->GetFocusedView());
643 EXPECT_TRUE(focus_manager->RotatePaneFocus(
644 FocusManager::kForward, FocusManager::kWrap));
645 EXPECT_EQ(v3, focus_manager->GetFocusedView());
646 focus_manager->AdvanceFocus(false);
647 EXPECT_EQ(v4, focus_manager->GetFocusedView());
648 focus_manager->AdvanceFocus(false);
649 EXPECT_EQ(v3, focus_manager->GetFocusedView());
651 EXPECT_TRUE(focus_manager->RotatePaneFocus(
652 FocusManager::kForward, FocusManager::kWrap));
653 EXPECT_EQ(v1, focus_manager->GetFocusedView());
655 // Advance backwards.
656 EXPECT_TRUE(focus_manager->RotatePaneFocus(
657 FocusManager::kBackward, FocusManager::kWrap));
658 EXPECT_EQ(v3, focus_manager->GetFocusedView());
660 EXPECT_TRUE(focus_manager->RotatePaneFocus(
661 FocusManager::kBackward, FocusManager::kWrap));
662 EXPECT_EQ(v1, focus_manager->GetFocusedView());
664 // Advance without wrap. When it gets to the end of the list of
665 // panes, RotatePaneFocus should return false but the current
666 // focused view shouldn't change.
667 EXPECT_TRUE(focus_manager->RotatePaneFocus(
668 FocusManager::kForward, FocusManager::kNoWrap));
669 EXPECT_EQ(v3, focus_manager->GetFocusedView());
671 EXPECT_FALSE(focus_manager->RotatePaneFocus(
672 FocusManager::kForward, FocusManager::kNoWrap));
673 EXPECT_EQ(v3, focus_manager->GetFocusedView());
676 // Verifies the stored focus view tracks the focused view.
677 TEST_F(FocusManagerTest, ImplicitlyStoresFocus) {
678 views::View* v1 = new View;
679 v1->SetFocusable(true);
680 GetContentsView()->AddChildView(v1);
682 views::View* v2 = new View;
683 v2->SetFocusable(true);
684 GetContentsView()->AddChildView(v2);
686 // Verify a focus request on |v1| implicitly updates the stored focus view.
687 v1->RequestFocus();
688 EXPECT_TRUE(v1->HasFocus());
689 EXPECT_EQ(v1, GetWidget()->GetFocusManager()->GetStoredFocusView());
691 // Verify a focus request on |v2| implicitly updates the stored focus view.
692 v2->RequestFocus();
693 EXPECT_TRUE(v2->HasFocus());
694 EXPECT_EQ(v2, GetWidget()->GetFocusManager()->GetStoredFocusView());
697 namespace {
699 class FocusManagerArrowKeyTraversalTest : public FocusManagerTest {
700 public:
701 FocusManagerArrowKeyTraversalTest()
702 : previous_arrow_key_traversal_enabled_(false) {
704 ~FocusManagerArrowKeyTraversalTest() override {}
706 // FocusManagerTest overrides:
707 void SetUp() override {
708 FocusManagerTest::SetUp();
710 previous_arrow_key_traversal_enabled_ =
711 FocusManager::arrow_key_traversal_enabled();
713 void TearDown() override {
714 FocusManager::set_arrow_key_traversal_enabled(
715 previous_arrow_key_traversal_enabled_);
716 FocusManagerTest::TearDown();
719 private:
720 bool previous_arrow_key_traversal_enabled_;
722 DISALLOW_COPY_AND_ASSIGN(FocusManagerArrowKeyTraversalTest);
725 } // namespace
727 TEST_F(FocusManagerArrowKeyTraversalTest, ArrowKeyTraversal) {
728 FocusManager* focus_manager = GetFocusManager();
729 const ui::KeyEvent left_key(ui::ET_KEY_PRESSED, ui::VKEY_LEFT, ui::EF_NONE);
730 const ui::KeyEvent right_key(ui::ET_KEY_PRESSED, ui::VKEY_RIGHT, ui::EF_NONE);
731 const ui::KeyEvent up_key(ui::ET_KEY_PRESSED, ui::VKEY_UP, ui::EF_NONE);
732 const ui::KeyEvent down_key(ui::ET_KEY_PRESSED, ui::VKEY_DOWN, ui::EF_NONE);
734 std::vector<views::View*> v;
735 for (size_t i = 0; i < 2; ++i) {
736 views::View* view = new View;
737 view->SetFocusable(true);
738 GetContentsView()->AddChildView(view);
739 v.push_back(view);
742 // Arrow key traversal is off and arrow key does not change focus.
743 FocusManager::set_arrow_key_traversal_enabled(false);
744 v[0]->RequestFocus();
745 focus_manager->OnKeyEvent(right_key);
746 EXPECT_EQ(v[0], focus_manager->GetFocusedView());
747 focus_manager->OnKeyEvent(left_key);
748 EXPECT_EQ(v[0], focus_manager->GetFocusedView());
749 focus_manager->OnKeyEvent(down_key);
750 EXPECT_EQ(v[0], focus_manager->GetFocusedView());
751 focus_manager->OnKeyEvent(up_key);
752 EXPECT_EQ(v[0], focus_manager->GetFocusedView());
754 // Turn on arrow key traversal.
755 FocusManager::set_arrow_key_traversal_enabled(true);
756 v[0]->RequestFocus();
757 focus_manager->OnKeyEvent(right_key);
758 EXPECT_EQ(v[1], focus_manager->GetFocusedView());
759 focus_manager->OnKeyEvent(left_key);
760 EXPECT_EQ(v[0], focus_manager->GetFocusedView());
761 focus_manager->OnKeyEvent(down_key);
762 EXPECT_EQ(v[1], focus_manager->GetFocusedView());
763 focus_manager->OnKeyEvent(up_key);
764 EXPECT_EQ(v[0], focus_manager->GetFocusedView());
767 TEST_F(FocusManagerTest, StoreFocusedView) {
768 View view;
769 GetFocusManager()->SetFocusedView(&view);
770 GetFocusManager()->StoreFocusedView(false);
771 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView());
772 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView());
773 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView());
775 // Repeat with |true|.
776 GetFocusManager()->SetFocusedView(&view);
777 GetFocusManager()->StoreFocusedView(true);
778 EXPECT_EQ(NULL, GetFocusManager()->GetFocusedView());
779 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView());
780 EXPECT_EQ(&view, GetFocusManager()->GetStoredFocusView());
783 class TextInputTestView : public View {
784 public:
785 TextInputTestView() {}
787 ui::TextInputClient* GetTextInputClient() override {
788 return &text_input_client_;
791 private:
792 ui::DummyTextInputClient text_input_client_;
794 DISALLOW_COPY_AND_ASSIGN(TextInputTestView);
797 TEST_F(FocusManagerTest, TextInputClient) {
798 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
799 cmd_line->AppendSwitch(switches::kEnableTextInputFocusManager);
801 View* view = new TextInputTestView;
802 ui::TextInputClient* text_input_client = view->GetTextInputClient();
803 view->SetFocusable(true);
804 GetContentsView()->AddChildView(view);
805 ui::TextInputFocusManager* text_input_focus_manager =
806 ui::TextInputFocusManager::GetInstance();
808 GetFocusManager()->SetFocusedView(view);
809 EXPECT_EQ(view, GetFocusManager()->GetFocusedView());
810 EXPECT_EQ(text_input_client,
811 text_input_focus_manager->GetFocusedTextInputClient());
812 GetFocusManager()->StoreFocusedView(false);
813 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView());
814 EXPECT_EQ(text_input_client,
815 text_input_focus_manager->GetFocusedTextInputClient());
817 // Repeat with |true|.
818 GetFocusManager()->SetFocusedView(view);
819 EXPECT_EQ(view, GetFocusManager()->GetFocusedView());
820 EXPECT_EQ(text_input_client,
821 text_input_focus_manager->GetFocusedTextInputClient());
822 GetFocusManager()->StoreFocusedView(true);
823 EXPECT_TRUE(GetFocusManager()->RestoreFocusedView());
824 EXPECT_EQ(text_input_client,
825 text_input_focus_manager->GetFocusedTextInputClient());
827 // Focus the view twice in a row.
828 GetFocusManager()->SetFocusedView(view);
829 EXPECT_EQ(text_input_client,
830 text_input_focus_manager->GetFocusedTextInputClient());
831 ui::TextInputFocusManager::GetInstance()->FocusTextInputClient(NULL);
832 GetFocusManager()->SetFocusedView(view);
833 EXPECT_EQ(text_input_client,
834 text_input_focus_manager->GetFocusedTextInputClient());
837 namespace {
839 // Trivial WidgetDelegate implementation that allows setting return value of
840 // ShouldAdvanceFocusToTopLevelWidget().
841 class AdvanceFocusWidgetDelegate : public WidgetDelegate {
842 public:
843 explicit AdvanceFocusWidgetDelegate(Widget* widget)
844 : widget_(widget),
845 should_advance_focus_to_parent_(false) {}
846 ~AdvanceFocusWidgetDelegate() override {}
848 void set_should_advance_focus_to_parent(bool value) {
849 should_advance_focus_to_parent_ = value;
852 // WidgetDelegate overrides:
853 bool ShouldAdvanceFocusToTopLevelWidget() const override {
854 return should_advance_focus_to_parent_;
856 Widget* GetWidget() override { return widget_; }
857 const Widget* GetWidget() const override { return widget_; }
859 private:
860 Widget* widget_;
861 bool should_advance_focus_to_parent_;
863 DISALLOW_COPY_AND_ASSIGN(AdvanceFocusWidgetDelegate);
866 } // namespace
868 // Verifies focus wrapping happens in the same widget.
869 TEST_F(FocusManagerTest, AdvanceFocusStaysInWidget) {
870 // Add |widget_view| as a child of the Widget.
871 View* widget_view = new View;
872 widget_view->SetFocusable(true);
873 widget_view->SetBounds(20, 0, 20, 20);
874 GetContentsView()->AddChildView(widget_view);
876 // Create a widget with two views, focus the second.
877 scoped_ptr<AdvanceFocusWidgetDelegate> delegate;
878 Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
879 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
880 params.child = true;
881 params.bounds = gfx::Rect(10, 10, 100, 100);
882 params.parent = GetWidget()->GetNativeView();
883 Widget child_widget;
884 delegate.reset(new AdvanceFocusWidgetDelegate(&child_widget));
885 params.delegate = delegate.get();
886 child_widget.Init(params);
887 View* view1 = new View;
888 view1->SetFocusable(true);
889 view1->SetBounds(0, 0, 20, 20);
890 View* view2 = new View;
891 view2->SetFocusable(true);
892 view2->SetBounds(20, 0, 20, 20);
893 child_widget.client_view()->AddChildView(view1);
894 child_widget.client_view()->AddChildView(view2);
895 child_widget.Show();
896 view2->RequestFocus();
897 EXPECT_EQ(view2, GetFocusManager()->GetFocusedView());
899 // Advance focus backwards, which should focus the first.
900 GetFocusManager()->AdvanceFocus(false);
901 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView());
903 // Focus forward to |view2|.
904 GetFocusManager()->AdvanceFocus(true);
905 EXPECT_EQ(view2, GetFocusManager()->GetFocusedView());
907 // And forward again, wrapping back to |view1|.
908 GetFocusManager()->AdvanceFocus(true);
909 EXPECT_EQ(view1, GetFocusManager()->GetFocusedView());
911 // Allow focus to go to the parent, and focus backwards which should now move
912 // up |widget_view| (in the parent).
913 delegate->set_should_advance_focus_to_parent(true);
914 GetFocusManager()->AdvanceFocus(true);
915 EXPECT_EQ(widget_view, GetFocusManager()->GetFocusedView());
918 } // namespace views