1 // Copyright 2015 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 "components/view_manager/focus_controller.h"
7 #include "components/view_manager/focus_controller_delegate.h"
8 #include "components/view_manager/server_view.h"
9 #include "components/view_manager/test_server_view_delegate.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace view_manager
{
15 class TestFocusControllerDelegate
: public FocusControllerDelegate
{
17 TestFocusControllerDelegate()
19 old_focused_view_(nullptr),
20 new_focused_view_(nullptr) {}
24 old_focused_view_
= nullptr;
25 new_focused_view_
= nullptr;
27 size_t change_count() const { return change_count_
; }
28 ServerView
* old_focused_view() { return old_focused_view_
; }
29 ServerView
* new_focused_view() { return new_focused_view_
; }
32 // FocusControllerDelegate:
33 void OnFocusChanged(ServerView
* old_focused_view
,
34 ServerView
* new_focused_view
) override
{
36 old_focused_view_
= old_focused_view
;
37 new_focused_view_
= new_focused_view
;
41 ServerView
* old_focused_view_
;
42 ServerView
* new_focused_view_
;
44 DISALLOW_COPY_AND_ASSIGN(TestFocusControllerDelegate
);
49 TEST(FocusControllerTest
, Basic
) {
50 TestServerViewDelegate server_view_delegate
;
51 ServerView
root(&server_view_delegate
, ViewId());
52 server_view_delegate
.set_root_view(&root
);
53 root
.SetVisible(true);
54 ServerView
child(&server_view_delegate
, ViewId());
55 child
.SetVisible(true);
57 ServerView
child_child(&server_view_delegate
, ViewId());
58 child_child
.SetVisible(true);
59 child
.Add(&child_child
);
61 TestFocusControllerDelegate focus_delegate
;
62 FocusController
focus_controller(&focus_delegate
);
64 focus_controller
.SetFocusedView(&child_child
);
65 EXPECT_EQ(0u, focus_delegate
.change_count());
67 // Remove the ancestor of the focused view, focus should go to the |root|.
69 EXPECT_EQ(1u, focus_delegate
.change_count());
70 EXPECT_EQ(&root
, focus_delegate
.new_focused_view());
71 EXPECT_EQ(&child_child
, focus_delegate
.old_focused_view());
72 focus_delegate
.ClearAll();
74 // Make the focused view invisible. Focus is lost in this case (as no one
76 root
.SetVisible(false);
77 EXPECT_EQ(1u, focus_delegate
.change_count());
78 EXPECT_EQ(nullptr, focus_delegate
.new_focused_view());
79 EXPECT_EQ(&root
, focus_delegate
.old_focused_view());
80 focus_delegate
.ClearAll();
82 // Go back to initial state and focus |child_child|.
83 root
.SetVisible(true);
85 focus_controller
.SetFocusedView(&child_child
);
86 EXPECT_EQ(0u, focus_delegate
.change_count());
88 // Hide the focused view, focus should go to parent.
89 child_child
.SetVisible(false);
90 EXPECT_EQ(1u, focus_delegate
.change_count());
91 EXPECT_EQ(&child
, focus_delegate
.new_focused_view());
92 EXPECT_EQ(&child_child
, focus_delegate
.old_focused_view());
93 focus_delegate
.ClearAll();
95 child_child
.SetVisible(true);
96 focus_controller
.SetFocusedView(&child_child
);
97 EXPECT_EQ(0u, focus_delegate
.change_count());
99 // Hide the parent of the focused view.
100 child
.SetVisible(false);
101 EXPECT_EQ(1u, focus_delegate
.change_count());
102 EXPECT_EQ(&root
, focus_delegate
.new_focused_view());
103 EXPECT_EQ(&child_child
, focus_delegate
.old_focused_view());
104 focus_delegate
.ClearAll();
107 } // namespace view_manager