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 root
.SetVisible(true);
53 ServerView
child(&server_view_delegate
, ViewId());
54 child
.SetVisible(true);
56 ServerView
child_child(&server_view_delegate
, ViewId());
57 child_child
.SetVisible(true);
58 child
.Add(&child_child
);
60 TestFocusControllerDelegate focus_delegate
;
61 FocusController
focus_controller(&focus_delegate
, &root
);
63 focus_controller
.SetFocusedView(&child_child
);
64 EXPECT_EQ(0u, focus_delegate
.change_count());
66 // Remove the ancestor of the focused view, focus should go to the |root|.
68 EXPECT_EQ(1u, focus_delegate
.change_count());
69 EXPECT_EQ(&root
, focus_delegate
.new_focused_view());
70 EXPECT_EQ(&child_child
, focus_delegate
.old_focused_view());
71 focus_delegate
.ClearAll();
73 // Make the focused view invisible. Focus is lost in this case (as no one
75 root
.SetVisible(false);
76 EXPECT_EQ(1u, focus_delegate
.change_count());
77 EXPECT_EQ(nullptr, focus_delegate
.new_focused_view());
78 EXPECT_EQ(&root
, focus_delegate
.old_focused_view());
79 focus_delegate
.ClearAll();
81 // Go back to initial state and focus |child_child|.
82 root
.SetVisible(true);
84 focus_controller
.SetFocusedView(&child_child
);
85 EXPECT_EQ(0u, focus_delegate
.change_count());
87 // Hide the focused view, focus should go to parent.
88 child_child
.SetVisible(false);
89 EXPECT_EQ(1u, focus_delegate
.change_count());
90 EXPECT_EQ(&child
, focus_delegate
.new_focused_view());
91 EXPECT_EQ(&child_child
, focus_delegate
.old_focused_view());
92 focus_delegate
.ClearAll();
94 child_child
.SetVisible(true);
95 focus_controller
.SetFocusedView(&child_child
);
96 EXPECT_EQ(0u, focus_delegate
.change_count());
98 // Hide the parent of the focused view.
99 child
.SetVisible(false);
100 EXPECT_EQ(1u, focus_delegate
.change_count());
101 EXPECT_EQ(&root
, focus_delegate
.new_focused_view());
102 EXPECT_EQ(&child_child
, focus_delegate
.old_focused_view());
103 focus_delegate
.ClearAll();
106 } // namespace view_manager