OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "components/view_manager/focus_controller.h" | |
6 | |
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" | |
11 | |
12 namespace view_manager { | |
13 namespace { | |
14 | |
15 class TestFocusControllerDelegate : public FocusControllerDelegate { | |
16 public: | |
17 TestFocusControllerDelegate() | |
18 : change_count_(0u), | |
19 old_focused_view_(nullptr), | |
20 new_focused_view_(nullptr) {} | |
21 | |
22 void ClearAll() { | |
23 change_count_ = 0u; | |
24 old_focused_view_ = nullptr; | |
25 new_focused_view_ = nullptr; | |
26 } | |
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_; } | |
30 | |
31 private: | |
32 // FocusControllerDelegate: | |
33 void OnFocusChanged(ServerView* old_focused_view, | |
34 ServerView* new_focused_view) override { | |
35 change_count_++; | |
36 old_focused_view_ = old_focused_view; | |
37 new_focused_view_ = new_focused_view; | |
38 } | |
39 | |
40 size_t change_count_; | |
41 ServerView* old_focused_view_; | |
42 ServerView* new_focused_view_; | |
43 | |
44 DISALLOW_COPY_AND_ASSIGN(TestFocusControllerDelegate); | |
45 }; | |
46 | |
47 } // namespace | |
48 | |
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); | |
56 root.Add(&child); | |
57 ServerView child_child(&server_view_delegate, ViewId()); | |
58 child_child.SetVisible(true); | |
59 child.Add(&child_child); | |
60 | |
61 TestFocusControllerDelegate focus_delegate; | |
62 FocusController focus_controller(&focus_delegate); | |
63 | |
64 focus_controller.SetFocusedView(&child_child); | |
65 EXPECT_EQ(0u, focus_delegate.change_count()); | |
66 | |
67 // Remove the ancestor of the focused view, focus should go to the |root|. | |
68 root.Remove(&child); | |
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(); | |
73 | |
74 // Make the focused view invisible. Focus is lost in this case (as no one | |
75 // to give focus to). | |
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(); | |
81 | |
82 // Go back to initial state and focus |child_child|. | |
83 root.SetVisible(true); | |
84 root.Add(&child); | |
85 focus_controller.SetFocusedView(&child_child); | |
86 EXPECT_EQ(0u, focus_delegate.change_count()); | |
87 | |
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(); | |
94 | |
95 child_child.SetVisible(true); | |
96 focus_controller.SetFocusedView(&child_child); | |
97 EXPECT_EQ(0u, focus_delegate.change_count()); | |
98 | |
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(); | |
105 } | |
106 | |
107 } // namespace view_manager | |
OLD | NEW |